From 4f0c562c2ace785a7ccb9118b9891eb06cf17493 Mon Sep 17 00:00:00 2001 From: Jack <113976590+bibblobcode@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:14:56 +0100 Subject: [PATCH] CLDC-2056 - Ask absorbing organisation (#1580) * CLDC-2056 Add absorbing organisation question * Handle next page and previous_template with `page` * Hardcode backlinks * Refactor flow to store value when selecting new org option * Handle unanswered question * Update error copy --- app/controllers/merge_requests_controller.rb | 76 +++++++++-- app/models/merge_request.rb | 1 + .../absorbing_organisation.html.erb | 37 ++++++ .../confirm_telephone_number.html.erb | 13 ++ .../merge_requests/new_org_name.html.erb | 7 + .../merge_requests/organisations.html.erb | 62 ++++----- config/locales/en.yml | 1 + config/routes.rb | 2 + ...absorbing_organisation_to_merge_request.rb | 8 ++ db/schema.rb | 8 +- .../merge_requests_controller_spec.rb | 120 ++++++++++++++---- 11 files changed, 269 insertions(+), 66 deletions(-) create mode 100644 app/views/merge_requests/absorbing_organisation.html.erb create mode 100644 app/views/merge_requests/confirm_telephone_number.html.erb create mode 100644 app/views/merge_requests/new_org_name.html.erb create mode 100644 db/migrate/20230421124536_add_absorbing_organisation_to_merge_request.rb diff --git a/app/controllers/merge_requests_controller.rb b/app/controllers/merge_requests_controller.rb index 3d1aa3f5f..047925722 100644 --- a/app/controllers/merge_requests_controller.rb +++ b/app/controllers/merge_requests_controller.rb @@ -1,7 +1,20 @@ class MergeRequestsController < ApplicationController - before_action :find_resource, only: %i[update organisations update_organisations remove_merging_organisation] + before_action :find_resource, only: %i[ + update + organisations + update_organisations + remove_merging_organisation + absorbing_organisation + confirm_telephone_number + new_org_name + ] before_action :authenticate_user! before_action :authenticate_scope!, except: [:create] + before_action :validate_response, only: %i[update] + + def absorbing_organisation; end + def confirm_telephone_number; end + def new_org_name; end def create ActiveRecord::Base.transaction do @@ -43,6 +56,31 @@ class MergeRequestsController < ApplicationController private + def page + params.dig(:merge_request, :page) + end + + def next_page_path + case page + when "absorbing_organisation" + if create_new_org? + new_org_name_merge_request_path(@merge_request) + else + confirm_telephone_number_merge_request_path(@merge_request) + end + when "organisations" + absorbing_organisation_merge_request_path(@merge_request) + end + end + + def previous_template + page + end + + def create_new_org? + params.dig(:merge_request, :absorbing_organisation_id) == "other" + end + def organisations_answer_options answer_options = { "" => "Select an option" } @@ -53,31 +91,47 @@ private end def merge_request_params - merge_params = params.fetch(:merge_request, {}).permit(:requesting_organisation_id, :other_merging_organisations, :status) + merge_params = params.fetch(:merge_request, {}).permit( + :requesting_organisation_id, + :other_merging_organisations, + :status, + :absorbing_organisation_id, + ) if merge_params[:requesting_organisation_id].present? && (current_user.data_coordinator? || current_user.data_provider?) merge_params[:requesting_organisation_id] = current_user.organisation.id end + if merge_params[:absorbing_organisation_id].present? + if create_new_org? + merge_params[:new_absorbing_organisation] = true + merge_params[:absorbing_organisation_id] = nil + else + merge_params[:new_absorbing_organisation] = false + end + end + merge_params end + def validate_response + if page == "absorbing_organisation" && merge_request_params[:absorbing_organisation_id].blank? && merge_request_params[:new_absorbing_organisation].blank? + @merge_request.errors.add(:absorbing_organisation_id, I18n.t("validations.merge_request.absorbing_organisation_blank")) + render previous_template + end + end + def merge_request_organisation_params - { merge_request: @merge_request, merging_organisation_id: params[:merge_request][:merging_organisation] } + { + merge_request: @merge_request, + merging_organisation_id: params.dig(:merge_request, :merging_organisation), + } end def find_resource @merge_request = MergeRequest.find(params[:id]) end - def next_page_path - absorbing_organisation_merge_request_path(@merge_request) - end - - def previous_template - :organisations - end - def authenticate_scope! if current_user.organisation != @merge_request.requesting_organisation && !current_user.support? render_not_found diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 111d25321..94e2f6d00 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1,6 +1,7 @@ class MergeRequest < ApplicationRecord belongs_to :requesting_organisation, class_name: "Organisation" has_many :merge_request_organisations + belongs_to :absorbing_organisation, class_name: "Organisation", optional: true has_many :merging_organisations, through: :merge_request_organisations, source: :merging_organisation STATUS = { diff --git a/app/views/merge_requests/absorbing_organisation.html.erb b/app/views/merge_requests/absorbing_organisation.html.erb new file mode 100644 index 000000000..cd2441d2b --- /dev/null +++ b/app/views/merge_requests/absorbing_organisation.html.erb @@ -0,0 +1,37 @@ +<% content_for :before_content do %> + <% title = "Tell us if your organisation is merging" %> + <% content_for :title, title %> + <%= govuk_back_link href: organisations_merge_request_path(id: @merge_request) %> +<% end %> + +
Select the organisation that the other organisations are merging into.
+ + <%= form_with model: @merge_request, url: merge_request_path, method: :patch do |f| %> + <%= f.govuk_error_summary %> + + <%= f.govuk_radio_buttons_fieldset( + :absorbing_organisation_id, + hint: { text: "For example, if Skype and Yammer merged into Microsoft, you would select Microsoft." }, + legend: nil, + ) do %> + <% @merge_request.merging_organisations.order(:name).each do |org| %> + <%= f.govuk_radio_button( + :absorbing_organisation_id, + org.id, + label: { text: org.name }, + ) %> + <% end %> + <%= f.govuk_radio_divider %> + <%= f.govuk_radio_button :absorbing_organisation_id, "other", checked: @merge_request.new_absorbing_organisation?, label: { text: "These organisations are merging into a new one" } %> + <% end %> + + <%= f.hidden_field :page, value: "absorbing_organisation" %> + + <%= f.govuk_submit %> + <% end %> +Confirm the telephone number on file, or enter a new one.
+Add all organisations to be merged - we have already added your own.
++ Add all organisations to be merged - we have already added your own. +
-<%= form_with model: @merge_request, url: organisations_merge_request_path, method: :patch do |f| %> - <%= f.govuk_error_summary %> -Start typing to search
- <%= render partial: "organisation_relationships/related_organisation_select_question", locals: { - field: :merging_organisation, - question: Form::Question.new("", { "answer_options" => @answer_options }, nil), - f:, - } %> - <%= f.govuk_submit "Add organisation", classes: "govuk-button--secondary" %> - <%= govuk_table do |table| %> - <% @merge_request.merging_organisations.each do |merging_organisation| %> - <%= table.body do |body| %> - <%= body.row do |row| %> - <% row.cell(text: merging_organisation.name) %> - <% row.cell(html_attributes: { - scope: "row", - class: "govuk-!-text-align-right", - }) do %> - <% if @merge_request.requesting_organisation != merging_organisation %> - <%= govuk_link_to("Remove", organisations_remove_merge_request_path(merge_request: { merging_organisation: merging_organisation.id })) %> + <%= form_with model: @merge_request, url: organisations_merge_request_path, method: :patch do |f| %> + <%= f.govuk_error_summary %> +Start typing to search
+ <%= render partial: "organisation_relationships/related_organisation_select_question", locals: { + field: :merging_organisation, + question: Form::Question.new("", { "answer_options" => @answer_options }, nil), + f:, + } %> + <%= f.govuk_submit "Add organisation", classes: "govuk-button--secondary" %> + <%= govuk_table do |table| %> + <% @merge_request.merging_organisations.order(:name).each do |merging_organisation| %> + <%= table.body do |body| %> + <%= body.row do |row| %> + <% row.cell(text: merging_organisation.name) %> + <% row.cell(html_attributes: { + scope: "row", + class: "govuk-!-text-align-right", + }) do %> + <% if @merge_request.requesting_organisation != merging_organisation %> + <%= govuk_link_to("Remove", organisations_remove_merge_request_path(merge_request: { merging_organisation: merging_organisation.id })) %> + <% end %> <% end %> <% end %> <% end %> <% end %> <% end %> <% end %> -<% end %> -<%= form_with model: @merge_request, url: merge_request_path(id: @merge_request.id), method: :patch do |f| %> - <%= govuk_details(summary_text: "I cannot find an organisation on the list") do %> - <%= f.govuk_text_area :other_merging_organisations, label: { text: "Other organisations" }, hint: { text: "List other organisations that are part of the merge but not registered on CORE." }, rows: 9 %> - <% end %> - <% if @merge_request.merging_organisations.count > 1 %> - <%= f.govuk_submit "Continue" %> + <%= form_with model: @merge_request, url: merge_request_path(id: @merge_request.id), method: :patch do |f| %> + <%= govuk_details(summary_text: "I cannot find an organisation on the list") do %> + <%= f.govuk_text_area :other_merging_organisations, label: { text: "Other organisations" }, hint: { text: "List other organisations that are part of the merge but not registered on CORE." }, rows: 9 %> + <% end %> + <% if @merge_request.merging_organisations.count > 1 %> + <%= f.hidden_field :page, value: "organisations" %> + <%= f.govuk_submit "Continue" %> + <% end %> <% end %> -<% end %>