Browse Source

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
pull/2868/head^2
kosiakkatrina 6 days ago committed by GitHub
parent
commit
54912a066c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      app/models/validations/financial_validations.rb
  2. 20
      lib/tasks/clear_invalid_benefits.rake
  3. 95
      spec/lib/tasks/clear_invalid_benefits_spec.rb
  4. 14
      spec/models/validations/financial_validations_spec.rb
  5. 2
      spec/requests/form_controller_spec.rb
  6. 2
      spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb
  7. 2
      spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb

6
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

20
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

95
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

14
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)

2
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)

2
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",

2
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",

Loading…
Cancel
Save