Skip to content

Commit

Permalink
Merge pull request #7257 from ministryofjustice/crimapp-4616-property…
Browse files Browse the repository at this point in the history
…-value-bug

AP-4616 Property values bug
  • Loading branch information
RoseSAK authored Oct 16, 2024
2 parents da7b50a + cbdc0b4 commit 7ca571e
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 178 deletions.
6 changes: 5 additions & 1 deletion app/controllers/providers/means/own_homes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def form_params
merge_with_model(legal_aid_application) do
next {} unless params[:legal_aid_application]

params.require(:legal_aid_application).permit(:own_home)
params.require(:legal_aid_application).permit(:own_home,
:property_value,
:outstanding_mortgage_amount,
:shared_ownership,
:percentage_home)
end
end
end
Expand Down
22 changes: 21 additions & 1 deletion app/forms/legal_aid_applications/own_home_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ class OwnHomeForm < BaseForm

validates :own_home, presence_partner_optional: { partner_labels: :has_partner_with_no_contrary_interest? }, unless: :draft?

delegate :own_home_no?, :own_home_mortgage?, :own_home_owned_outright?, to: :model
def save!
model.update!(attributes_to_reset)
super
end

def attributes_to_reset
if own_home == "no"
{
property_value: nil,
outstanding_mortgage_amount: nil,
shared_ownership: nil,
percentage_home: nil,
}
elsif own_home == "owned_outright"
{
outstanding_mortgage_amount: nil,
}
else
{}
end
end
end
end
3 changes: 1 addition & 2 deletions app/models/concerns/base_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def allow_ccms_submission?
delegated_functions_used
overriding_dwp_result
],
to: :applicant_details_checked,
after: proc { |legal_aid_application| CleanupCapitalAttributes.call(legal_aid_application) }
to: :applicant_details_checked

transitions from: :provider_entering_merits, to: :applicant_details_checked, guard: proc { non_means_tested? }
end
Expand Down
1 change: 0 additions & 1 deletion app/services/applicant_complete_means.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def initialize(legal_aid_application)
end

def call
CleanupCapitalAttributes.call(legal_aid_application)
legal_aid_application.update!(
provider_step: intended_provider_step,
completed_at: Time.current,
Expand Down
46 changes: 0 additions & 46 deletions app/services/cleanup_capital_attributes.rb

This file was deleted.

4 changes: 0 additions & 4 deletions spec/concerns/base_state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
let(:event) { :applicant_details_checked }

context "without guard" do
before do
allow(CleanupCapitalAttributes).to receive(:call)
end

it { is_expected.to transition_from(:checking_applicant_details).to(:applicant_details_checked).on_event(event) }
it { is_expected.to transition_from(:use_ccms).to(:applicant_details_checked).on_event(event) }
it { is_expected.to transition_from(:delegated_functions_used).to(:applicant_details_checked).on_event(event) }
Expand Down
78 changes: 67 additions & 11 deletions spec/forms/legal_aid_applications/own_home_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,76 @@
end

describe "#save" do
let(:params) { { own_home: "mortgage" } }
context "when there are no previously saved values" do
let(:params) { { own_home: "mortgage" } }

it "updates own home attribute" do
expect(application.own_home).to be_nil
described_form.save!
expect(application.own_home).to eq "mortgage"
it "updates own home attribute" do
expect(application.own_home).to be_nil
described_form.save!
expect(application.own_home).to eq "mortgage"
end

it "leaves other attributes on the record unchanged" do
expected_attributes = application.attributes.symbolize_keys.except(:state, :own_home, :updated_at, :created_at)
described_form.save!
application.reload
expected_attributes.each do |attr, val|
expect(application.send(attr)).to eq(val), "Attr #{attr}: expected #{val}, got #{application.send(attr)}"
end
end
end

it "leaves other attributes on the record unchanged" do
expected_attributes = application.attributes.symbolize_keys.except(:state, :own_home, :updated_at, :created_at)
described_form.save!
application.reload
expected_attributes.each do |attr, val|
expect(application.send(attr)).to eq(val), "Attr #{attr}: expected #{val}, got #{application.send(attr)}"
context "when there are previously saved values" do
let(:application) { create(:legal_aid_application, :with_applicant_and_address, :with_property_values) }

before do
described_form.save!
end

context "when `mortgage` is selected" do
let(:params) { { own_home: "mortgage" } }

it "updates own home attribute" do
expect(application.own_home).to eq "mortgage"
end

it "leaves other attributes on the record unchanged" do
expected_attributes = application.attributes.symbolize_keys.except(:state, :own_home, :updated_at, :created_at)
described_form.save!
application.reload
expected_attributes.each do |attr, val|
expect(application.send(attr)).to eq(val), "Attr #{attr}: expected #{val}, got #{application.send(attr)}"
end
end
end

context "when `owned_outright` is selected" do
let(:params) { { own_home: "owned_outright" } }

it "updates own home attribute" do
expect(application.own_home).to eq "owned_outright"
end

it "resets mortgage amount to nil" do
described_form.save!
expect(application.outstanding_mortgage_amount).to be_nil
end
end

context "when `no` is selected" do
let(:params) { { own_home: "no" } }

it "updates own home attribute" do
expect(application.own_home).to eq "no"
end

it "resets all property values to nil" do
described_form.save!
expect(application.property_value).to be_nil
expect(application.outstanding_mortgage_amount).to be_nil
expect(application.shared_ownership).to be_nil
expect(application.percentage_home).to be_nil
end
end
end
end
Expand Down
29 changes: 0 additions & 29 deletions spec/models/legal_aid_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,41 +670,13 @@
describe "#applicant_details_checked!" do
let(:legal_aid_application) { create(:legal_aid_application, :with_everything, :without_own_home, :checking_applicant_details) }

it "passes application to keep in sync service" do
expect(CleanupCapitalAttributes).to receive(:call).with(legal_aid_application)
legal_aid_application.applicant_details_checked!
end

it "transitions to applicant_details_checked" do
expect { legal_aid_application.applicant_details_checked! }
.to change { legal_aid_application.reload.state }
.from("checking_applicant_details")
.to("applicant_details_checked")
end

context "and attributes changed" do
before do
legal_aid_application.applicant_details_checked!
legal_aid_application.reload
end

it "resets property values" do
expect(legal_aid_application.property_value).to be_blank
end

it "resets outstanding mortgage" do
expect(legal_aid_application.outstanding_mortgage_amount).to be_blank
end

it "resets shared ownership" do
expect(legal_aid_application.shared_ownership).to be_blank
end

it "resets percentage home" do
expect(legal_aid_application.percentage_home).to be_blank
end
end

context "when transitioning from provider_entering_merits" do
let(:legal_aid_application) { create(:legal_aid_application, :at_provider_entering_merits) }

Expand Down Expand Up @@ -1358,7 +1330,6 @@
let(:legal_aid_application) { create(:legal_aid_application, :with_non_passported_state_machine, :checking_citizen_answers) }

it "runs the complete means service and the bank transaction analyser" do
expect(ApplicantCompleteMeans).to receive(:call).with(legal_aid_application)
expect(BankTransactionsAnalyserJob).to receive(:perform_later).with(legal_aid_application)
legal_aid_application.complete_non_passported_means!
end
Expand Down
5 changes: 0 additions & 5 deletions spec/requests/citizens/check_answers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,5 @@
patch_request
expect(legal_aid_application.reload.declaration_accepted_at).to be_between(2.seconds.ago, Time.current)
end

it "syncs the application" do
expect(CleanupCapitalAttributes).to receive(:call).with(legal_aid_application)
patch_request
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@
expect(application.reload.state).to eq "applicant_details_checked"
end

it "syncs the application" do
expect(CleanupCapitalAttributes).to receive(:call).with(application)
patch_request
end

it "redirects to the next page" do
patch_request
expect(response).to have_http_status(:redirect)
Expand Down
73 changes: 0 additions & 73 deletions spec/services/cleanup_capital_attributes_spec.rb

This file was deleted.

0 comments on commit 7ca571e

Please sign in to comment.