diff --git a/app/models/merge_request_organisation.rb b/app/models/merge_request_organisation.rb index 6dda8b35e..5bfbe14d7 100644 --- a/app/models/merge_request_organisation.rb +++ b/app/models/merge_request_organisation.rb @@ -29,5 +29,12 @@ private if merging_organisation_id.blank? || !Organisation.where(id: merging_organisation_id).exists? merge_request.errors.add(:merging_organisation, I18n.t("validations.merge_request.organisation_not_selected")) end + + existing_merges = MergeRequestOrganisation.with_merging_organisation(merging_organisation) + if existing_merges.count.positive? + existing_merge_request = existing_merges.first.merge_request + errors.add(:merging_organisation, I18n.t("validations.merge_request.organisation_part_of_another_merge")) + merge_request.errors.add(:merging_organisation, I18n.t("validations.merge_request.organisation_part_of_another_incomplete_merge", organisation: merging_organisation.name, absorbing_organisation: existing_merge_request.absorbing_organisation&.name, merge_date: existing_merge_request.merge_date&.to_fs(:govuk_date))) + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 18fc6d2c4..698618717 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -186,6 +186,8 @@ en: more_than_year_from_today: "The merge date must not be later than a year from today’s date." existing_absorbing_organisation: blank: "You must answer absorbing organisation already active?" + merging_organisation_id: + part_of_another_merge: "Another merge request records %{organisation} as merging into %{absorbing_organisation} on %{merge_date}. Select another organisation or remove this organisation from the other merge request." notification: attributes: title: @@ -371,6 +373,7 @@ en: during_deactivated_period: "The location is already deactivated during this date, please enter a different date." merge_request: organisation_part_of_another_merge: "This organisation is part of another merge - select a different one." + organisation_part_of_another_incomplete_merge: "Another merge request records %{organisation} as merging into %{absorbing_organisation} on %{merge_date}. Select another organisation or remove this organisation from the other merge request." organisation_not_selected: "Select an organisation from the search list." soft_validations: diff --git a/spec/requests/merge_requests_controller_spec.rb b/spec/requests/merge_requests_controller_spec.rb index 869b73568..a73db8067 100644 --- a/spec/requests/merge_requests_controller_spec.rb +++ b/spec/requests/merge_requests_controller_spec.rb @@ -84,6 +84,22 @@ RSpec.describe MergeRequestsController, type: :request do end end + context "when the user updates merge request with organisation that is already part of another merge" do + let(:another_organisation) { create(:organisation) } + let(:other_merge_request) { create(:merge_request, merge_date: Time.zone.local(2022, 5, 4)) } + let(:params) { { merge_request: { merging_organisation: another_organisation.id, new_merging_org_ids: [] } } } + + before do + MergeRequestOrganisation.create!(merge_request_id: other_merge_request.id, merging_organisation_id: another_organisation.id) + patch "/merge-request/#{merge_request.id}/merging-organisations", headers:, params: + end + + it "displays the page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content("Another merge request records #{another_organisation.name} as merging into #{other_merge_request.absorbing_organisation&.name} on 4 May 2022. Select another organisation or remove this organisation from the other merge request.") + end + end + context "when the user selects an organisation that is a part of another merge" do let(:another_organisation) { create(:organisation) } let(:params) { { merge_request: { merging_organisation: another_organisation.id, new_merging_org_ids: [] } } } @@ -400,7 +416,7 @@ RSpec.describe MergeRequestsController, type: :request do context "when merge date set to a date more than 1 year in the future" do let(:merge_request) { MergeRequest.create!(requesting_organisation: organisation) } let(:params) do - { merge_request: { page: "merge_date", "merge_date(3i)": "#{Time.zone.now.day + 1}", "merge_date(2i)": "#{Time.zone.now.month}", "merge_date(1i)": "#{Time.zone.now.year + 1}" } } + { merge_request: { page: "merge_date", "merge_date(3i)": (Time.zone.now.day + 1).to_s, "merge_date(2i)": Time.zone.now.month.to_s, "merge_date(1i)": (Time.zone.now.year + 1).to_s } } end let(:request) do