From 4fcbff9017957914a8decaa388bcec477a714b37 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 10 Jan 2025 16:01:32 +0000 Subject: [PATCH] Add and use collection_year instead of from/to in log validations --- app/services/documentation_generator.rb | 25 +++++----------- .../20250110150609_add_collection_year.rb | 5 ++++ db/schema.rb | 3 +- .../generate_lettings_documentation.rake | 1 + .../set_log_validation_collection_year.rake | 6 ++++ ...set_log_validation_collection_year_spec.rb | 29 +++++++++++++++++++ spec/services/documentation_generator_spec.rb | 12 +++----- 7 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20250110150609_add_collection_year.rb create mode 100644 lib/tasks/set_log_validation_collection_year.rake create mode 100644 spec/lib/tasks/set_log_validation_collection_year_spec.rb diff --git a/app/services/documentation_generator.rb b/app/services/documentation_generator.rb index 5063d95da..43426d8d4 100644 --- a/app/services/documentation_generator.rb +++ b/app/services/documentation_generator.rb @@ -43,7 +43,7 @@ class DocumentationGenerator def describe_bu_validations(client, form, row_parser_class, all_validation_methods, all_helper_methods, field_mapping_for_errors, log_type) all_validation_methods.each do |meth| - if LogValidation.where(validation_name: meth.to_s, bulk_upload_specific: true, from: form.start_date, log_type:).exists? + if LogValidation.where(validation_name: meth.to_s, bulk_upload_specific: true, collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", log_type:).exists? Rails.logger.info("Validation #{meth} already exists for #{form.start_date.year}") next end @@ -168,14 +168,6 @@ private required: %w[error_message field], }, }, - from: { - type: :number, - description: "the year from which the validation starts. If this validation runs for logs with a startdate after a certain year, specify that year here, only if it is not specified in the validation method, leave this field blank", - }, - to: { - type: :number, - description: "the year in which the validation ends. If this validation runs for logs with a startdate before a certain year, specify that year here, only if it is not specified in the validation method, leave this field blank", - }, validation_type: { type: :string, enum: %w[presence format minimum maximum range inclusion length other], @@ -273,8 +265,7 @@ Look at these helper methods where needed to understand what is being checked in error_message: error["error_message"], case: case_info["case_description"], section: form.get_question(error["field"], nil)&.subsection&.id, - from: case_info["from"] || "", - to: case_info["to"] || "", + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: case_info["validation_type"], hard_soft: "hard", other_validated_models: case_info["other_validated_models"]) @@ -297,8 +288,7 @@ Look at these helper methods where needed to understand what is being checked in error_message: error["error_message"], case: case_info["case_description"], section: form.get_question(error_field, nil)&.subsection&.id, - from: form.start_date, - to: form.start_date + 1.year, + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: case_info["validation_type"], hard_soft: "hard", other_validated_models: case_info["other_validated_models"], @@ -335,7 +325,7 @@ Look at these helper methods where needed to understand what is being checked in return end - if LogValidation.where(validation_name: validation_depends_on_hash.keys.first, field: page_the_validation_applied_to.questions.first.id, from: form.start_date, log_type:).exists? + if LogValidation.where(validation_name: validation_depends_on_hash.keys.first, field: page_the_validation_applied_to.questions.first.id, collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", log_type:).exists? Rails.logger.info("Validation #{validation_depends_on_hash.keys.first} already exists for #{page_the_validation_applied_to.questions.first.id} for start year #{form.start_date.year}") return end @@ -362,8 +352,7 @@ Look at these helper methods where needed to understand what is being checked in error_message:, case: case_info, section: form.get_question(page_the_validation_applied_to.questions.first.id, nil)&.subsection&.id, - from: form.start_date, - to: form.start_date + 1.year, + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: result["validation_type"], hard_soft: "soft", other_validated_models: result["other_validated_models"]) @@ -377,7 +366,7 @@ Look at these helper methods where needed to understand what is being checked in def translation_file_path(form, file_path) return "./config/locales/validations/#{form.type}/#{form.start_date.year}/bulk_upload.en.yml" if file_path.include?("bulk_upload") - + file_name = file_path.split("/").last.gsub("_validations.rb", "") translation_file_name = TRANSLATION_FILE_MAPPINGS[file_name] || file_name @@ -386,7 +375,7 @@ Look at these helper methods where needed to understand what is being checked in shared_file_path = "./config/locales/validations/#{translation_file_name}.en.yml" return shared_file_path if File.exist?(shared_file_path) - + "./config/locales/en.yml" end end diff --git a/db/migrate/20250110150609_add_collection_year.rb b/db/migrate/20250110150609_add_collection_year.rb new file mode 100644 index 000000000..b56e347c0 --- /dev/null +++ b/db/migrate/20250110150609_add_collection_year.rb @@ -0,0 +1,5 @@ +class AddCollectionYear < ActiveRecord::Migration[7.2] + def change + add_column :log_validations, :collection_year, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index bd7672c24..08f6ae3b6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_12_06_142944) do +ActiveRecord::Schema[7.2].define(version: 2025_01_10_150609) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -450,6 +450,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_06_142944) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "checked" + t.string "collection_year" end create_table "merge_request_organisations", force: :cascade do |t| diff --git a/lib/tasks/generate_lettings_documentation.rake b/lib/tasks/generate_lettings_documentation.rake index 3dc8e71b4..e87fd8dca 100644 --- a/lib/tasks/generate_lettings_documentation.rake +++ b/lib/tasks/generate_lettings_documentation.rake @@ -83,6 +83,7 @@ namespace :generate_lettings_documentation do error_message:, case: validation_description, section: form.get_question(field, nil)&.subsection&.id, + collection_year: "#{form.start_date.year}/#{form.start_date.year + 1}", validation_type: validation_name, hard_soft: "hard") end diff --git a/lib/tasks/set_log_validation_collection_year.rake b/lib/tasks/set_log_validation_collection_year.rake new file mode 100644 index 000000000..9468d2b0f --- /dev/null +++ b/lib/tasks/set_log_validation_collection_year.rake @@ -0,0 +1,6 @@ +desc "Sets value for collection_year log validations depending on the from value" +task set_log_validation_collection_year: :environment do + LogValidation.all.each do |log_validation| + log_validation.update(collection_year: "#{log_validation.from.year}/#{log_validation.from.year + 1}") + end +end diff --git a/spec/lib/tasks/set_log_validation_collection_year_spec.rb b/spec/lib/tasks/set_log_validation_collection_year_spec.rb new file mode 100644 index 000000000..0e68a3b88 --- /dev/null +++ b/spec/lib/tasks/set_log_validation_collection_year_spec.rb @@ -0,0 +1,29 @@ +require "rails_helper" +require "rake" + +RSpec.describe "set_log_validation_collection_year" do + describe ":set_log_validation_collection_year", type: :task do + subject(:task) { Rake::Task["set_log_validation_collection_year"] } + + before do + Rake.application.rake_require("tasks/set_log_validation_collection_year") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let(:user) { create(:user) } + + context "and version whodunnit exists for create" do + let!(:log_validation_2023) { LogValidation.create(from: Time.zone.local(2023, 4, 1), to: Time.zone.local(2024, 4, 1)) } + let!(:log_validation_2024) { LogValidation.create(from: Time.zone.local(2024, 4, 1), to: Time.zone.local(2025, 4, 1)) } + + it "sets collection_year" do + task.invoke + expect(log_validation_2023.reload.collection_year).to eq("2023/2024") + expect(log_validation_2024.reload.collection_year).to eq("2024/2025") + end + end + end + end +end diff --git a/spec/services/documentation_generator_spec.rb b/spec/services/documentation_generator_spec.rb index d214aaa9e..8c1d82726 100644 --- a/spec/services/documentation_generator_spec.rb +++ b/spec/services/documentation_generator_spec.rb @@ -106,8 +106,7 @@ describe DocumentationGenerator do expect(any_validation.field).not_to be_empty expect(any_validation.error_message).not_to be_empty expect(any_validation.case).to eq("Provided values fulfill the description") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).not_to be_nil expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("soft") expect(any_validation.other_validated_models).to eq("User") @@ -137,8 +136,7 @@ describe DocumentationGenerator do expect(any_validation.field).not_to be_empty expect(any_validation.error_message).not_to be_empty expect(any_validation.case).to eq("Provided values fulfill the description") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).not_to be_nil expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("soft") expect(any_validation.other_validated_models).to eq("User") @@ -165,8 +163,7 @@ describe DocumentationGenerator do expect(any_validation.field).to eq("ppostcode_full") expect(any_validation.error_message).to eq("Enter a valid postcode") expect(any_validation.case).to eq("Previous postcode is known and current postcode is blank") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).to eq("2023/2024") expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("hard") expect(any_validation.other_validated_models).to eq("User") @@ -218,8 +215,7 @@ describe DocumentationGenerator do expect(any_validation.field).to eq("ppostcode_full") expect(any_validation.error_message).to eq("Enter a valid postcode") expect(any_validation.case).to eq("Previous postcode is known and current postcode is blank") - expect(any_validation.from).not_to be_nil - expect(any_validation.to).not_to be_nil + expect(any_validation.collection_year).to eq("2023/2024") expect(any_validation.validation_type).to eq("format") expect(any_validation.hard_soft).to eq("hard") expect(any_validation.other_validated_models).to eq("User")