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