diff --git a/app/models/organisation.rb b/app/models/organisation.rb
index c7447cfe6..eb4226614 100644
--- a/app/models/organisation.rb
+++ b/app/models/organisation.rb
@@ -33,6 +33,7 @@ class Organisation < ApplicationRecord
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }
scope :merged_during_open_collection_period, -> { where("merge_date >= ?", FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period) }
+ scope :absorbed_on, ->(date) { where("merge_date = ?", date) }
has_paper_trail
@@ -140,4 +141,19 @@ class Organisation < ApplicationRecord
def duplicate_sales_logs_sets
sales_logs.duplicate_sets.map { |array_str| array_str ? array_str.map(&:to_i) : [] }
end
+
+ def recent_merge_date
+ if merge_date.present?
+ merge_date
+ elsif absorbed_organisations.any?
+ absorbed_organisations.map(&:merge_date).compact.max
+ end
+ end
+
+ def recently_absorbed_organisations
+ return unless absorbed_organisations.any?
+ return absorbed_organisations if recent_merge_date.blank?
+
+ absorbed_organisations.absorbed_on(recent_merge_date)
+ end
end
diff --git a/app/views/organisations/_merged_organisation_details.html.erb b/app/views/organisations/_merged_organisation_details.html.erb
new file mode 100644
index 000000000..67a336c20
--- /dev/null
+++ b/app/views/organisations/_merged_organisation_details.html.erb
@@ -0,0 +1,32 @@
+<% if @organisation.absorbed_organisations.any? %>
+ <%= govuk_details(summary_text: "View all organisations that were merged into #{@organisation.name}") do %>
+
Merge date: <%= @organisation.recent_merge_date&.to_formatted_s(:govuk_date) %>
+ <%= govuk_table do |table| %>
+ <%= table.head do |head| %>
+ <%= head.row do |row| %>
+ <% row.cell(header: true, text: "Organisation name", html_attributes: { scope: "col" }) %>
+ <% row.cell(header: true, text: "Organisation ID", html_attributes: { scope: "col" }) %>
+ <% end %>
+ <% end %>
+ <% @organisation.recently_absorbed_organisations&.each do |absorbed_org| %>
+ <%= table.body do |body| %>
+ <%= body.row do |row| %>
+ <% if current_user.support? %>
+ <% row.cell(text: simple_format(govuk_link_to(absorbed_org.name, organisation_path(absorbed_org)), { class: "govuk-!-font-weight-bold scheme-name-cell" }, wrapper_tag: "div")) %>
+ <% else %>
+ <% row.cell(text: absorbed_org.name) %>
+ <% end %>
+ <% row.cell(text: "ORG#{absorbed_org.id}") %>
+ <% end %>
+ <% end %>
+ <% end %>
+ <% end %>
+ <% end %>
+<% end %>
+<% if @organisation.absorbing_organisation.present? %>
+ <% if current_user.support? %>
+ <%= @organisation.name %> was merged into <%= govuk_link_to(@organisation.absorbing_organisation.name, organisation_path(@organisation.absorbing_organisation)) %><%= @organisation.merge_date ? " on #{@organisation.merge_date.to_formatted_s(:govuk_date)}" : "" %>.
+ <% else %>
+ <%= @organisation.name %> was merged into <%= @organisation.absorbing_organisation.name %><%= @organisation.merge_date ? " on #{@organisation.merge_date.to_formatted_s(:govuk_date)}" : "" %>.
+ <% end %>
+<% end %>
diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb
index 47b023fbd..fa0bd9e0a 100644
--- a/app/views/organisations/show.html.erb
+++ b/app/views/organisations/show.html.erb
@@ -39,6 +39,7 @@
<% if FeatureToggle.merge_organisations_enabled? %>
To report a merge or update your organisation details, <%= govuk_link_to "contact the helpdesk", "https://dluhcdigital.atlassian.net/servicedesk/customer/portal/6/group/11" %>.
<% end %>
+ <%= render partial: "organisations/merged_organisation_details" %>
diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb
index ab4b72136..1bd19393e 100644
--- a/spec/requests/organisations_controller_spec.rb
+++ b/spec/requests/organisations_controller_spec.rb
@@ -279,6 +279,70 @@ RSpec.describe OrganisationsController, type: :request do
expect(page).to have_content("To report a merge or update your organisation details, ")
expect(page).to have_link("contact the helpdesk", href: "https://dluhcdigital.atlassian.net/servicedesk/customer/portal/6/group/11")
end
+
+ it "does not display merge history if there is none" do
+ expect(page).not_to have_content("View all organisations that were merged into #{organisation.name}")
+ end
+
+ context "when the organisation has absorbed other organisations" do
+ let!(:absorbed_organisation) { create(:organisation, name: "First Absorbed Organisation") }
+ let!(:other_absorbed_organisation) { create(:organisation, name: "Other Absorbed Organisation") }
+ let!(:old_absorbed_organisation) { create(:organisation, name: "Old Absorbed Organisation") }
+
+ before do
+ absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation: organisation)
+ other_absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation: organisation)
+ old_absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 2), absorbing_organisation: organisation)
+ get "/organisations/#{organisation.id}/details", headers:, params: {}
+ end
+
+ it "displays a list of absorbed organisations" do
+ expect(page).to have_content("View all organisations that were merged into #{organisation.name}")
+ expect(page).to have_content("Merge date: 3 April 2023")
+ expect(page).to have_content("First Absorbed Organisation")
+ expect(page).to have_content("Other Absorbed Organisation")
+ expect(page).not_to have_content("Old Absorbed Organisation")
+ expect(page).to have_content("ORG#{absorbed_organisation.id}")
+ expect(page).to have_content("ORG#{other_absorbed_organisation.id}")
+ expect(page).not_to have_content("ORG#{old_absorbed_organisation.id}")
+ end
+ end
+
+ context "when the organisation has absorbed other organisations without merge dates" do
+ let!(:absorbed_organisation) { create(:organisation, name: "First Absorbed Organisation") }
+ let!(:other_absorbed_organisation) { create(:organisation, name: "Other Absorbed Organisation") }
+
+ before do
+ absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation: organisation)
+ other_absorbed_organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation: organisation)
+ get "/organisations/#{organisation.id}/details", headers:, params: {}
+ end
+
+ it "displays a list of absorbed organisations" do
+ expect(page).to have_content("View all organisations that were merged into #{organisation.name}")
+ expect(page).to have_content("Merge date:")
+ expect(page).to have_content("First Absorbed Organisation")
+ expect(page).to have_content("Other Absorbed Organisation")
+ expect(page).to have_content("ORG#{absorbed_organisation.id}")
+ expect(page).to have_content("ORG#{other_absorbed_organisation.id}")
+ end
+ end
+
+ context "when viewing absorbed organisation" do
+ let(:absorbing_organisation) { create(:organisation, name: "First Absorbing Organisation") }
+
+ context "and your organisation was absorbed" do
+ before do
+ organisation.update!(merge_date: Time.zone.local(2023, 4, 3), absorbing_organisation:)
+ get "/organisations/#{organisation.id}/details", headers:, params: {}
+ end
+
+ it "displays the organisation merge details" do
+ expect(response).not_to have_http_status(:not_found)
+ expect(page).to have_content("#{organisation.name} was merged into First Absorbing Organisation on 3 April 2023.")
+ end
+ end
+ end
end
context "with organisation that are not in scope for the user, i.e. that they do not belong to" do