diff --git a/app/controllers/merge_requests_controller.rb b/app/controllers/merge_requests_controller.rb index 930a79b04..17959e264 100644 --- a/app/controllers/merge_requests_controller.rb +++ b/app/controllers/merge_requests_controller.rb @@ -6,16 +6,17 @@ class MergeRequestsController < ApplicationController remove_merging_organisation absorbing_organisation confirm_telephone_number - new_org_name + new_organisation_name + new_organisation_address merge_date ] 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 new_organisation_name; end + def new_organisation_address; end def merge_date; end def create @@ -33,7 +34,9 @@ class MergeRequestsController < ApplicationController end def update - if @merge_request.update(merge_request_params) + validate_response + + if @merge_request.errors.blank? && @merge_request.update(merge_request_params) redirect_to next_page_path else render previous_template, status: :unprocessable_entity @@ -65,8 +68,8 @@ private def next_page_path case page when "absorbing_organisation" - if create_new_org? - new_org_name_merge_request_path(@merge_request) + if create_new_organisation? + new_organisation_name_merge_request_path(@merge_request) else confirm_telephone_number_merge_request_path(@merge_request) end @@ -74,6 +77,8 @@ private absorbing_organisation_merge_request_path(@merge_request) when "confirm_telephone_number" merge_date_merge_request_path(@merge_request) + when "new_organisation_name" + new_organisation_address_merge_request_path(@merge_request) end end @@ -81,7 +86,7 @@ private page end - def create_new_org? + def create_new_organisation? params.dig(:merge_request, :absorbing_organisation_id) == "other" end @@ -102,6 +107,7 @@ private :absorbing_organisation_id, :telephone_number_correct, :new_telephone_number, + :new_organisation_name, ) if merge_params[:requesting_organisation_id].present? && (current_user.data_coordinator? || current_user.data_provider?) @@ -109,7 +115,7 @@ private end if merge_params[:absorbing_organisation_id].present? - if create_new_org? + if create_new_organisation? merge_params[:new_absorbing_organisation] = true merge_params[:absorbing_organisation_id] = nil else @@ -117,7 +123,7 @@ private end end - if merge_params[:telephone_number_correct] == "1" + if merge_params[:telephone_number_correct] == "true" merge_params[:new_telephone_number] = nil end @@ -125,23 +131,18 @@ private 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, status: :unprocessable_entity - end - - if page == "confirm_telephone_number" - if merge_request_params[:telephone_number_correct].blank? && merge_request_params[:new_telephone_number].blank? - if @merge_request.absorbing_organisation.phone.present? - @merge_request.errors.add(:telephone_number_correct, I18n.t("validations.merge_request.telephone_number_correct_blank")) - else - @merge_request.errors.add(:telephone_number_correct, I18n.t("validations.merge_request.new_telephone_number_blank")) - end - render previous_template, status: :unprocessable_entity - elsif merge_request_params[:telephone_number_correct] == "0" && merge_request_params[:new_telephone_number].blank? - @merge_request.errors.add(:new_telephone_number, I18n.t("validations.merge_request.new_telephone_number_blank")) - render previous_template, status: :unprocessable_entity + case page + when "absorbing_organisation" + if merge_request_params[:absorbing_organisation_id].blank? && merge_request_params[:new_absorbing_organisation].blank? + @merge_request.errors.add(:absorbing_organisation_id, :blank) + end + when "confirm_telephone_number" + if merge_request_params[:telephone_number_correct].blank? + @merge_request.errors.add(:telephone_number_correct, :blank) if @merge_request.absorbing_organisation.phone.present? + @merge_request.errors.add(:new_telephone_number, :blank) if @merge_request.absorbing_organisation.phone.blank? end + when "new_organisation_name" + @merge_request.errors.add(:new_organisation_name, :blank) if merge_request_params[:new_organisation_name].blank? end end diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 94e2f6d00..e62b1d39d 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -3,10 +3,18 @@ class MergeRequest < ApplicationRecord 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 + validate :organisation_name_uniqueness, if: :new_organisation_name + validates :new_telephone_number, presence: true, if: -> { telephone_number_correct == false } STATUS = { "unsubmitted" => 0, "submitted" => 1, }.freeze enum status: STATUS + + def organisation_name_uniqueness + if Organisation.where("lower(name) = ?", new_organisation_name&.downcase).exists? + errors.add(:new_organisation_name, :invalid) + end + end end diff --git a/app/views/merge_requests/confirm_telephone_number.html.erb b/app/views/merge_requests/confirm_telephone_number.html.erb index 9a05ee54e..0c92a4f28 100644 --- a/app/views/merge_requests/confirm_telephone_number.html.erb +++ b/app/views/merge_requests/confirm_telephone_number.html.erb @@ -20,8 +20,8 @@ <% if @merge_request.absorbing_organisation.phone.present? %> <%= f.govuk_radio_buttons_fieldset(:telephone_number_correct, legend: nil) do %> - <%= f.govuk_radio_button :telephone_number_correct, 1, checked: @merge_request.telephone_number_correct?, label: { text: "This telephone number is correct" }, link_errors: true %> - <%= f.govuk_radio_button :telephone_number_correct, 0, checked: @merge_request.new_telephone_number.present?, label: { text: "Enter a new phone number" } do %> + <%= f.govuk_radio_button :telephone_number_correct, true, checked: @merge_request.telephone_number_correct?, label: { text: "This telephone number is correct" }, link_errors: true %> + <%= f.govuk_radio_button :telephone_number_correct, false, checked: @merge_request.new_telephone_number.present?, label: { text: "Enter a new phone number" } do %> <%= f.govuk_text_field :new_telephone_number, label: { text: "Telephone number" }, width: "two-thirds" %> <% end %> <%= f.hidden_field :page, value: "confirm_telephone_number" %> diff --git a/app/views/merge_requests/new_org_name.html.erb b/app/views/merge_requests/new_org_name.html.erb deleted file mode 100644 index 9c4075735..000000000 --- a/app/views/merge_requests/new_org_name.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -<% content_for :before_content do %> - <% title = "Tell us if your organisation is merging" %> - <% content_for :title, title %> - <%= govuk_back_link href: absorbing_organisation_merge_request_path(id: @merge_request) %> -<% end %> - -Provide new org name diff --git a/app/views/merge_requests/new_organisation_address.html.erb b/app/views/merge_requests/new_organisation_address.html.erb new file mode 100644 index 000000000..32dc57c86 --- /dev/null +++ b/app/views/merge_requests/new_organisation_address.html.erb @@ -0,0 +1,5 @@ +<% content_for :before_content do %> + <% title = "New organisation address" %> + <% content_for :title, title %> + <%= govuk_back_link href: new_organisation_name_merge_request_path(id: @merge_request) %> +<% end %> diff --git a/app/views/merge_requests/new_organisation_name.html.erb b/app/views/merge_requests/new_organisation_name.html.erb new file mode 100644 index 000000000..8b391e735 --- /dev/null +++ b/app/views/merge_requests/new_organisation_name.html.erb @@ -0,0 +1,19 @@ +<% content_for :before_content do %> + <% title = "New organisation name" %> + <% content_for :title, title %> + <%= govuk_back_link href: absorbing_organisation_merge_request_path(id: @merge_request) %> +<% end %> + +<%= form_with model: @merge_request, url: merge_request_path, method: :patch do |f| %> + <%= f.govuk_error_summary %> + +