From fbb1b84131d4a44010d05ae0c0536f410dd31417 Mon Sep 17 00:00:00 2001 From: Adam Goldstone <13471320+agoldstone93@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:38:16 +0100 Subject: [PATCH] AP-5220: Delete home address when applicant has no fixed address Delete home address when applicant has said they have no fixed address. This is most important when they have already entered an address, so it will now be removed. Updated wording in spec to be clearer --- .../home_address/statuses_controller.rb | 7 +++++++ .../home_address/statuses_controller_spec.rb | 17 +++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/controllers/providers/home_address/statuses_controller.rb b/app/controllers/providers/home_address/statuses_controller.rb index 60a422c3f01..b2e4d5f06ff 100644 --- a/app/controllers/providers/home_address/statuses_controller.rb +++ b/app/controllers/providers/home_address/statuses_controller.rb @@ -12,11 +12,18 @@ def update @form = ::HomeAddress::StatusForm.new(form_params) @correspondence_address = applicant.address + delete_home_address if form_params[:no_fixed_residence].eql?("true") + render :show unless save_continue_or_draft(@form) end private + def delete_home_address + applicant.home_address = nil + applicant.save! + end + def applicant legal_aid_application.applicant end diff --git a/spec/requests/providers/home_address/statuses_controller_spec.rb b/spec/requests/providers/home_address/statuses_controller_spec.rb index 1ee13cba71c..717ad81d8dc 100644 --- a/spec/requests/providers/home_address/statuses_controller_spec.rb +++ b/spec/requests/providers/home_address/statuses_controller_spec.rb @@ -42,11 +42,11 @@ it_behaves_like "a provider not authenticated" end - context "when yes chosen" do - let(:params) { { applicant: { no_fixed_residence: "true" } } } + context "when applicant does have a home address" do + let(:params) { { applicant: { no_fixed_residence: "false" } } } it "records the answer" do - expect { patch_request }.to change { applicant.reload.no_fixed_residence }.from(nil).to(true) + expect { patch_request }.to change { applicant.reload.no_fixed_residence }.from(nil).to(false) end it "redirects to the next page" do @@ -55,11 +55,16 @@ end end - context "when no chosen" do - let(:params) { { applicant: { no_fixed_residence: "false" } } } + context "when applicant does not have a home address" do + let(:params) { { applicant: { no_fixed_residence: "true" } } } it "records the answer" do - expect { patch_request }.to change { applicant.reload.no_fixed_residence }.from(nil).to(false) + expect { patch_request }.to change { applicant.reload.no_fixed_residence }.from(nil).to(true) + end + + it "deletes the home address" do + patch_request + expect(applicant.reload.home_address).to be_nil end it "redirects to the next page" do