From db34e950b46cff14c974ca4f0f0af1ee815d0fb9 Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Wed, 23 Apr 2025 13:58:12 +0100 Subject: [PATCH] Add tasks to just count and surface invalid LettingsLog and SalesLog --- lib/helpers/invalid_logs_helper.rb | 26 ++++++++++++++++++++++++++ lib/tasks/invalid_logs.rake | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lib/helpers/invalid_logs_helper.rb create mode 100644 lib/tasks/invalid_logs.rake diff --git a/lib/helpers/invalid_logs_helper.rb b/lib/helpers/invalid_logs_helper.rb new file mode 100644 index 000000000..68f3c695e --- /dev/null +++ b/lib/helpers/invalid_logs_helper.rb @@ -0,0 +1,26 @@ +module InvalidLogsHelper + module_function + + def count_and_display_invalid_logs(model, log_type, year) + invalid_logs = fetch_invalid_logs(model, year) + puts "Number of invalid #{log_type} for year #{year}: #{invalid_logs.size}" + puts "Invalid #{log_type} IDs: #{invalid_logs.map(&:id).join(', ')}" + end + + def surface_invalid_logs(model, log_type, year) + invalid_logs = fetch_invalid_logs(model, year) + if invalid_logs.any? + invalid_logs.each do |log| + puts "#{log_type} ID: #{log.id}" + log.errors.full_messages.each { |message| puts " - #{message}" } + end + else + puts "No invalid #{log_type} found for year #{year}." + end + end + + def fetch_invalid_logs(model, year) + model.filter_by_year(year).reject(&:valid?) + end +end + diff --git a/lib/tasks/invalid_logs.rake b/lib/tasks/invalid_logs.rake new file mode 100644 index 000000000..a5637ef84 --- /dev/null +++ b/lib/tasks/invalid_logs.rake @@ -0,0 +1,21 @@ +require "helpers/invalid_logs_helper" + +namespace :logs do + desc "Count the number of invalid LettingsLog and SalesLog for a given year" + task :count_invalid, [:year] => :environment do |_task, args| + include CollectionTimeHelper + + year = args[:year] || current_collection_year + InvalidLogsHelper.count_and_display_invalid_logs(LettingsLog, "LettingsLog", year) + InvalidLogsHelper.count_and_display_invalid_logs(SalesLog, "SalesLog", year) + end + + desc "Surface all invalid logs and output their error messages for a given year" + task :surface_invalid, [:year] => :environment do |_task, args| + include CollectionTimeHelper + + year = args[:year] || current_collection_year + InvalidLogsHelper.surface_invalid_logs(LettingsLog, "LettingsLog", year) + InvalidLogsHelper.surface_invalid_logs(SalesLog, "SalesLog", year) + end +end