From 54912a066c56ccae6a064f259e2967f8b86379d4 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:44:26 +0000 Subject: [PATCH] CLDC-3744 Validate correct benefits value in the validation (#2769) * Check correct benefits value in the validation * Update some tests * Update validation * clear invalid benefits * Update more tests * Fix world * Clear the benefits * Move benefits assign --- .../validations/financial_validations.rb | 6 +- lib/tasks/clear_invalid_benefits.rake | 20 ++++ spec/lib/tasks/clear_invalid_benefits_spec.rb | 95 +++++++++++++++++++ .../validations/financial_validations_spec.rb | 14 +-- spec/requests/form_controller_spec.rb | 2 +- .../lettings/year2023/row_parser_spec.rb | 2 +- .../lettings/year2024/row_parser_spec.rb | 2 +- 7 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 lib/tasks/clear_invalid_benefits.rake create mode 100644 spec/lib/tasks/clear_invalid_benefits_spec.rb diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 19209f2d8..73b389f4c 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -11,14 +11,14 @@ module Validations::FinancialValidations end end - EMPLOYED_STATUSES = [1, 0].freeze + EMPLOYED_STATUSES = [1, 2].freeze def validate_net_income_uc_proportion(record) (1..8).any? do |n| economic_status = record["ecstat#{n}"] is_employed = EMPLOYED_STATUSES.include?(economic_status) relationship = record["relat#{n}"] - is_partner_or_main = relationship == "P" || (relationship.nil? && economic_status.present?) - if is_employed && is_partner_or_main && record.benefits&.zero? + is_partner_or_main = relationship == "P" || n == 1 + if is_employed && is_partner_or_main && record.benefits == 1 record.errors.add :benefits, I18n.t("validations.lettings.financial.benefits.part_or_full_time") end end diff --git a/lib/tasks/clear_invalid_benefits.rake b/lib/tasks/clear_invalid_benefits.rake new file mode 100644 index 000000000..415559a6b --- /dev/null +++ b/lib/tasks/clear_invalid_benefits.rake @@ -0,0 +1,20 @@ +desc "clear benefit value for logs that would trigger the validation" +task clear_invalid_benefits: :environment do + validation_trigger_condition = "ecstat1 = 1 OR ecstat1 = 2 OR (ecstat2 = 1 AND relat2 = 'P') OR (ecstat2 = 2 AND relat2 = 'P') OR (ecstat3 = 1 AND relat3 = 'P') OR (ecstat3 = 2 AND relat3 = 'P') OR (ecstat4 = 1 AND relat4 = 'P') OR (ecstat4 = 2 AND relat4 = 'P') OR (ecstat5 = 1 AND relat5 = 'P') OR (ecstat5 = 2 AND relat5 = 'P') OR (ecstat6 = 1 AND relat6 = 'P') OR (ecstat6 = 2 AND relat6 = 'P') OR (ecstat7 = 1 AND relat7 = 'P') OR (ecstat7 = 2 AND relat7 = 'P') OR (ecstat8 = 1 AND relat8 = 'P') OR (ecstat8 = 2 AND relat8 = 'P')" + LettingsLog.filter_by_year(2024).where(status: "pending", status_cache: "completed", benefits: 1).where(validation_trigger_condition).find_each do |log| + log.benefits = nil + log.status_cache = log.calculate_status + log.skip_update_status = true + + unless log.save + Rails.logger.info "Could not save changes to pending lettings log #{log.id}" + end + end + + LettingsLog.filter_by_year(2024).visible.where(benefits: 1).where(validation_trigger_condition).find_each do |log| + log.benefits = nil + unless log.save + Rails.logger.info "Could not save changes to lettings log #{log.id}" + end + end +end diff --git a/spec/lib/tasks/clear_invalid_benefits_spec.rb b/spec/lib/tasks/clear_invalid_benefits_spec.rb new file mode 100644 index 000000000..755612f8b --- /dev/null +++ b/spec/lib/tasks/clear_invalid_benefits_spec.rb @@ -0,0 +1,95 @@ +require "rails_helper" +require "rake" + +RSpec.describe "clear_invalid_benefits" do + describe ":clear_invalid_benefits", type: :task do + subject(:task) { Rake::Task["clear_invalid_benefits"] } + + before do + Rake.application.rake_require("tasks/clear_invalid_benefits") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + context "and there is a completed lettings log that trips the validation" do + let(:log) { build(:lettings_log, :completed, ecstat1: 1, benefits: 1, assigned_to: create(:user), period: nil) } + + before do + log.status = "completed" + log.skip_update_status = true + log.save!(validate: false) + end + + it "clear benefits and sets the log to in progress" do + expect(log.reload.benefits).to eq(1) + task.invoke + log.reload + expect(log.benefits).to eq(nil) + expect(log.status).to eq("in_progress") + end + end + + context "and there is a lettings log that trips the validation for person 2" do + let(:log) { build(:lettings_log, :completed, ecstat2: 2, benefits: 1, relat2: "P", assigned_to: create(:user), period: nil) } + + before do + log.status = "completed" + log.skip_update_status = true + log.save!(validate: false) + end + + it "clear benefits and sets the log to in progress" do + expect(log.reload.benefits).to eq(1) + task.invoke + log.reload + expect(log.benefits).to eq(nil) + expect(log.status).to eq("in_progress") + end + end + + context "and there is a lettings log that trips the validation for person 8" do + let(:log) { build(:lettings_log, :completed, ecstat8: 1, benefits: 1, relat8: "P", assigned_to: create(:user), period: nil) } + + before do + log.status = "completed" + log.skip_update_status = true + log.save!(validate: false) + end + + it "clear benefits and sets the log to in progress" do + expect(log.reload.benefits).to eq(1) + task.invoke + log.reload + expect(log.benefits).to eq(nil) + expect(log.status).to eq("in_progress") + end + end + + context "and there is a pending lettings log that trips the validation" do + let(:log) { build(:lettings_log, :completed, ecstat1: 1, benefits: 1, assigned_to: create(:user), period: nil) } + + before do + log.status = "pending" + log.status_cache = "completed" + log.skip_update_status = true + log.save!(validate: false) + end + + it "clears benefits and updates the status cache" do + expect(log.reload.benefits).to eq(1) + task.invoke + log.reload + expect(log.benefits).to eq(nil) + expect(log.status_cache).to eq("in_progress") + end + + it "does not change the log status" do + task.invoke + log.reload + expect(log.status).to eq("pending") + end + end + end + end +end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index a2d51f7f9..282e050a2 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -36,36 +36,36 @@ RSpec.describe Validations::FinancialValidations do describe "benefits proportion validations" do context "when the proportion is all" do it "validates that the lead tenant is not in full time employment" do - record.benefits = 0 + record.benefits = 1 record.ecstat1 = 1 financial_validator.validate_net_income_uc_proportion(record) expect(record.errors["benefits"]).to include(match I18n.t("validations.lettings.financial.benefits.part_or_full_time")) end it "validates that the lead tenant is not in part time employment" do - record.benefits = 0 - record.ecstat1 = 0 + record.benefits = 1 + record.ecstat1 = 2 financial_validator.validate_net_income_uc_proportion(record) expect(record.errors["benefits"]).to include(match I18n.t("validations.lettings.financial.benefits.part_or_full_time")) end it "expects that the lead tenant is not in full-time or part-time employment" do - record.benefits = 0 + record.benefits = 1 record.ecstat1 = 4 financial_validator.validate_net_income_uc_proportion(record) expect(record.errors["benefits"]).to be_empty end it "validates that the tenant’s partner is not in full time employment" do - record.benefits = 0 - record.ecstat2 = 0 + record.benefits = 1 + record.ecstat2 = 2 record.relat2 = "P" financial_validator.validate_net_income_uc_proportion(record) expect(record.errors["benefits"]).to include(match I18n.t("validations.lettings.financial.benefits.part_or_full_time")) end it "expects that the tenant’s partner is not in full-time or part-time employment" do - record.benefits = 0 + record.benefits = 1 record.ecstat2 = 4 record.relat2 = "P" financial_validator.validate_net_income_uc_proportion(record) diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 6ab49e302..f6ecfa2b5 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -1681,7 +1681,7 @@ RSpec.describe FormController, type: :request do end before do - completed_lettings_log.update!(ecstat1: 1, earnings: 130, hhmemb: 1) # we're not routing to that page, so it gets cleared? + completed_lettings_log.update!(ecstat1: 1, earnings: 130, hhmemb: 1, benefits: 4) # we're not routing to that page, so it gets cleared? allow(completed_lettings_log).to receive(:net_income_soft_validation_triggered?).and_return(true) allow(completed_lettings_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day) allow(completed_lettings_log.form).to receive(:edit_end_date).and_return(Time.zone.today + 2.months) diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index acaba83e9..fcf0c5a9d 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -206,7 +206,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do field_122: "2300", field_121: "2", field_123: "1", - field_124: "1", + field_124: "4", field_126: "4", field_128: "1234.56", diff --git a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb index 8ffae6bd2..357d661b7 100644 --- a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb @@ -227,7 +227,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do field_118: "2", field_119: "2300", field_120: "1", - field_121: "1", + field_121: "4", field_123: "4", field_125: "1234.56",