From 1c03a70fa2268e293c59453ec7789c6c94349452 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:20:28 +0000 Subject: [PATCH] CLDC-3769 Fix some check errors flows (#2826) * Fix some check errors flows * Only show check errors journey for question on different pages * Update restore_error_field_values --- app/controllers/form_controller.rb | 11 ++++++++--- app/helpers/form_page_error_helper.rb | 5 +++-- .../form/sales/pages/owning_organisation.rb | 2 ++ app/models/validations/property_validations.rb | 2 ++ .../validations/sales/property_validations.rb | 2 ++ app/views/form/page.html.erb | 4 ++-- spec/features/form/page_routing_spec.rb | 16 +++++++++++++++- spec/requests/check_errors_controller_spec.rb | 2 +- 8 files changed, 35 insertions(+), 9 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 7ce63e609..3e029371d 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -105,8 +105,13 @@ private def restore_error_field_values(previous_responses) return unless previous_responses - previous_responses_to_reset = previous_responses.reject do |key, _| - @log.form.get_question(key, @log)&.type == "date" + previous_responses_to_reset = previous_responses.reject do |key, value| + if @log.form.get_question(key, @log)&.type == "date" && value.present? + year = value.split("-").first.to_i + year&.zero? + else + false + end end @log.assign_attributes(previous_responses_to_reset) @@ -433,7 +438,7 @@ private @log.valid? @log.reload error_attributes = @log.errors.map(&:attribute) - @questions = @log.form.questions.select { |q| error_attributes.include?(q.id.to_sym) } + @questions = @log.form.questions.select { |q| error_attributes.include?(q.id.to_sym) && q.page.routed_to?(@log, current_user) } end render "form/check_errors" end diff --git a/app/helpers/form_page_error_helper.rb b/app/helpers/form_page_error_helper.rb index 2c90bfd2a..bf4e1db08 100644 --- a/app/helpers/form_page_error_helper.rb +++ b/app/helpers/form_page_error_helper.rb @@ -13,7 +13,8 @@ module FormPageErrorHelper end end - def all_questions_affected_by_errors(log) - (log.errors.map(&:attribute) - [:base]).uniq + def all_pages_affected_by_errors(log) + question_ids = (log.errors.map(&:attribute) - [:base]).uniq + question_ids.map { |id| log.form.get_question(id, log)&.page&.id }.compact.uniq end end diff --git a/app/models/form/sales/pages/owning_organisation.rb b/app/models/form/sales/pages/owning_organisation.rb index f0c9e4e68..f1875d52d 100644 --- a/app/models/form/sales/pages/owning_organisation.rb +++ b/app/models/form/sales/pages/owning_organisation.rb @@ -20,11 +20,13 @@ class Form::Sales::Pages::OwningOrganisation < ::Form::Page if current_user.organisation.holds_own_stock? return true if current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?) return true if stock_owners.count >= 1 + return false if log.owning_organisation == current_user.organisation log.update!(owning_organisation: current_user.organisation) else return false if stock_owners.count.zero? return true if stock_owners.count > 1 + return false if log.owning_organisation == stock_owners.first log.update!(owning_organisation: stock_owners.first) end diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index e9eba8184..1cf710857 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -41,6 +41,8 @@ module Validations::PropertyValidations def validate_property_postcode(record) postcode = record.postcode_full + return unless postcode + if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) error_message = I18n.t("validations.lettings.property.postcode_full.invalid") record.errors.add :postcode_full, :wrong_format, message: error_message diff --git a/app/models/validations/sales/property_validations.rb b/app/models/validations/sales/property_validations.rb index 2238a634a..7fd4d2440 100644 --- a/app/models/validations/sales/property_validations.rb +++ b/app/models/validations/sales/property_validations.rb @@ -31,6 +31,8 @@ module Validations::Sales::PropertyValidations def validate_property_postcode(record) postcode = record.postcode_full + return unless postcode + if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) error_message = I18n.t("validations.sales.property_information.postcode_full.invalid") record.errors.add :postcode_full, :wrong_format, message: error_message diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index 95dc38ec6..7379de0eb 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -16,7 +16,7 @@ <%= form_with model: @log, url: request.original_url, method: "post", local: true do |f| %>