diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index c6bc5888e..61cd43674 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -47,17 +47,7 @@ class OrganisationsController < ApplicationController def duplicate_schemes authorize @organisation - duplicate_scheme_sets = @organisation.owned_schemes.duplicate_sets - @duplicate_schemes = duplicate_scheme_sets.map { |set| set.map { |id| @organisation.owned_schemes.find(id) } } - @duplicate_locations = [] - @organisation.owned_schemes.each do |scheme| - duplicate_location_sets = scheme.locations.duplicate_sets - next unless duplicate_location_sets.any? - - duplicate_location_sets.each do |duplicate_set| - @duplicate_locations << { scheme: scheme, locations: duplicate_set.map { |id| scheme.locations.find(id) } } - end - end + get_duplicate_schemes_and_locations end def show @@ -311,6 +301,22 @@ class OrganisationsController < ApplicationController render json: org_data.to_json end + def confirm_duplicate_schemes + authorize @organisation + + if scheme_duplicates_checked_params[:scheme_duplicates_checked] == "true" + @organisation.schemes_deduplicated_at = Time.zone.now + if @organisation.save + flash[:notice] = I18n.t("organisation.duplicate_schemes_confirmed") + redirect_to schemes_organisation_path(@organisation) + end + else + @organisation.errors.add(:scheme_duplicates_checked, I18n.t("validations.organisation.scheme_duplicates_not_resolved")) + get_duplicate_schemes_and_locations + render :duplicate_schemes, status: :unprocessable_entity + end + end + private def filter_type @@ -341,6 +347,10 @@ private params.require(:organisation).permit(rent_periods: [], all_rent_periods: []) end + def scheme_duplicates_checked_params + params.require(:organisation).permit(:scheme_duplicates_checked) + end + def codes_only_export? params.require(:codes_only) == "true" end @@ -360,4 +370,18 @@ private def find_resource @organisation = Organisation.find(params[:id]) end + + def get_duplicate_schemes_and_locations + duplicate_scheme_sets = @organisation.owned_schemes.duplicate_sets + @duplicate_schemes = duplicate_scheme_sets.map { |set| set.map { |id| @organisation.owned_schemes.find(id) } } + @duplicate_locations = [] + @organisation.owned_schemes.each do |scheme| + duplicate_location_sets = scheme.locations.duplicate_sets + next unless duplicate_location_sets.any? + + duplicate_location_sets.each do |duplicate_set| + @duplicate_locations << { scheme: scheme, locations: duplicate_set.map { |id| scheme.locations.find(id) } } + end + end + end end diff --git a/app/policies/organisation_policy.rb b/app/policies/organisation_policy.rb index 97fd4582e..9c5fc4449 100644 --- a/app/policies/organisation_policy.rb +++ b/app/policies/organisation_policy.rb @@ -38,4 +38,8 @@ class OrganisationPolicy def duplicate_schemes? user.support? || (user.data_coordinator? && user.organisation == organisation) end + + def confirm_duplicate_schemes? + duplicate_schemes? + end end diff --git a/app/views/organisations/duplicate_schemes.html.erb b/app/views/organisations/duplicate_schemes.html.erb index d002f2989..ecbea184e 100644 --- a/app/views/organisations/duplicate_schemes.html.erb +++ b/app/views/organisations/duplicate_schemes.html.erb @@ -1,114 +1,129 @@ <% content_for :before_content do %> <%= govuk_back_link href: schemes_organisation_path(@organisation) %> <% end %> +<%= form_with model: @organisation, url: schemes_duplicates_organisation_path(@organisation), method: "post" do |f| %> + <%= f.govuk_error_summary %> -<% title = "Review these sets of schemes and locations" %> -<% content_for :title, title %> + <% title = "Review these sets of schemes and locations" %> + <% content_for :title, title %> -<% if current_user.support? %> - <%= render SubNavigationComponent.new( - items: secondary_items(request.path, @organisation.id), - ) %> -<% end %> + <% if current_user.support? %> + <%= render SubNavigationComponent.new( + items: secondary_items(request.path, @organisation.id), + ) %> + <% end %> -
Since your organisation recently merged, we’ve reviewed your schemes for possible duplicates.
-These sets of schemes and locations might be duplicates because they have the same answers for certain fields.
-If you need help with this, <%= govuk_link_to "contact the helpdesk (opens in a new tab)", GlobalConstants::HELPDESK_URL, target: "#" %>.
+Since your organisation recently merged, we’ve reviewed your schemes for possible duplicates.
+These sets of schemes and locations might be duplicates because they have the same answers for certain fields.
+If you need help with this, <%= govuk_link_to "contact the helpdesk (opens in a new tab)", GlobalConstants::HELPDESK_URL, target: "#" %>.
- <% if @duplicate_schemes.any? %> -- These schemes have the same answers for the following fields: -
-+ These schemes have the same answers for the following fields: +
+- These locations belong to the same scheme and have the same answers for the following fields: -
-+ These locations belong to the same scheme and have the same answers for the following fields: +
+