From 62abe962908b4e3c9fef7559cbd1c6117ebcbea5 Mon Sep 17 00:00:00 2001
From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com>
Date: Tue, 8 Apr 2025 15:43:08 +0100
Subject: [PATCH] CLDC-4019: Update error messaging on referral type question
 to reflect categories (#3045)

* Add validation for referral type when permanently decanted from PRP-only housing

* Remove unnecessary error adding, PRP and different landlord is possible

* Update tests
---
 app/models/lettings_log.rb                        |  4 ++++
 app/models/validations/household_validations.rb   | 15 +++++++++------
 .../locales/validations/lettings/household.en.yml |  3 +++
 .../validations/household_validations_spec.rb     |  8 +-------
 4 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb
index 2d29fb6db..823582132 100644
--- a/app/models/lettings_log.rb
+++ b/app/models/lettings_log.rb
@@ -381,6 +381,10 @@ class LettingsLog < Log
     referral == 1
   end
 
+  def is_from_prp_only_housing_register_or_waiting_list?
+    referral_type == 3
+  end
+
   def is_relet_to_temp_tenant?
     # 9: Re-let to tenant who occupied same property as temporary accommodation
     rsnvac == 9
diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb
index 723ab30b6..ce913531b 100644
--- a/app/models/validations/household_validations.rb
+++ b/app/models/validations/household_validations.rb
@@ -30,10 +30,15 @@ module Validations::HouseholdValidations
     end
     validate_other_field(record, 20, :reason, :reasonother)
 
-    if record.is_reason_permanently_decanted? && record.referral.present? && !record.is_internal_transfer?
-      record.errors.add :referral, I18n.t("validations.lettings.household.referral.leaving_last_settled_home.reason_permanently_decanted")
-      record.errors.add :referral_type, I18n.t("validations.lettings.household.referral.leaving_last_settled_home.reason_permanently_decanted")
-      record.errors.add :reason, I18n.t("validations.lettings.household.reason.leaving_last_settled_home.not_internal_transfer")
+    if record.is_reason_permanently_decanted?
+      if record.referral_type.present? && !record.is_from_prp_only_housing_register_or_waiting_list?
+        record.errors.add :referral_type, I18n.t("validations.lettings.household.referral_type.leaving_last_settled_home.reason_permanently_decanted")
+      end
+
+      if record.referral.present? && !record.is_internal_transfer?
+        record.errors.add :referral, I18n.t("validations.lettings.household.referral.leaving_last_settled_home.reason_permanently_decanted")
+        record.errors.add :reason, I18n.t("validations.lettings.household.reason.leaving_last_settled_home.not_internal_transfer")
+      end
     end
 
     return unless record.form.start_year_2024_or_later?
@@ -172,7 +177,6 @@ module Validations::HouseholdValidations
       label = record.form.get_question("prevten", record).present? ? record.form.get_question("prevten", record).label_from_value(record.prevten) : ""
       record.errors.add :prevten, :internal_transfer_non_social_housing, message: I18n.t("validations.lettings.household.prevten.internal_transfer", prevten: label)
       record.errors.add :referral, :internal_transfer_non_social_housing, message: I18n.t("validations.lettings.household.referral.prevten_invalid", prevten: label)
-      record.errors.add :referral_type, :internal_transfer_non_social_housing, message: I18n.t("validations.lettings.household.referral.prevten_invalid", prevten: label)
     end
   end
 
@@ -182,7 +186,6 @@ module Validations::HouseholdValidations
     if record.is_internal_transfer? && record.owning_organisation.provider_type == "PRP" && record.is_prevten_la_general_needs?
       record.errors.add :prevten, :internal_transfer_fixed_or_lifetime, message: I18n.t("validations.lettings.household.prevten.la_general_needs.internal_transfer")
       record.errors.add :referral, :internal_transfer_fixed_or_lifetime, message: I18n.t("validations.lettings.household.referral.la_general_needs.internal_transfer")
-      record.errors.add :referral_type, :internal_transfer_fixed_or_lifetime, message: I18n.t("validations.lettings.household.referral.la_general_needs.internal_transfer")
     end
   end
 
diff --git a/config/locales/validations/lettings/household.en.yml b/config/locales/validations/lettings/household.en.yml
index de2eed1ca..0040e1374 100644
--- a/config/locales/validations/lettings/household.en.yml
+++ b/config/locales/validations/lettings/household.en.yml
@@ -107,3 +107,6 @@ en:
           la_general_needs:
             internal_transfer: "Answer cannot be internal transfer as it’s the same landlord on the tenancy agreement and the household had either a fixed-term or lifetime local authority general needs tenancy immediately before this letting."
 
+        referral_type:
+          leaving_last_settled_home:
+            reason_permanently_decanted: "Answer must be from a PRP-only housing register or waiting list (no local authority involvement) as the tenant was permanently decanted from another property owned by this landlord."
diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb
index e761476ea..3bad55f9b 100644
--- a/spec/models/validations/household_validations_spec.rb
+++ b/spec/models/validations/household_validations_spec.rb
@@ -137,7 +137,7 @@ RSpec.describe Validations::HouseholdValidations do
         expect(record.errors["referral"])
           .to include(match(I18n.t("validations.lettings.household.referral.leaving_last_settled_home.reason_permanently_decanted")))
         expect(record.errors["referral_type"])
-          .to include(match(I18n.t("validations.lettings.household.referral.leaving_last_settled_home.reason_permanently_decanted")))
+          .to include(match(I18n.t("validations.lettings.household.referral_type.leaving_last_settled_home.reason_permanently_decanted")))
       end
     end
 
@@ -163,8 +163,6 @@ RSpec.describe Validations::HouseholdValidations do
         household_validator.validate_referral(record)
         expect(record.errors["referral"])
           .to include(match(I18n.t("validations.lettings.household.referral.la_general_needs.internal_transfer")))
-        expect(record.errors["referral_type"])
-          .to include(match(I18n.t("validations.lettings.household.referral.la_general_needs.internal_transfer")))
         expect(record.errors["prevten"])
           .to include(match(I18n.t("validations.lettings.household.prevten.la_general_needs.internal_transfer")))
 
@@ -172,8 +170,6 @@ RSpec.describe Validations::HouseholdValidations do
         household_validator.validate_referral(record)
         expect(record.errors["referral"])
           .to include(match(I18n.t("validations.lettings.household.referral.la_general_needs.internal_transfer")))
-        expect(record.errors["referral_type"])
-          .to include(match(I18n.t("validations.lettings.household.referral.la_general_needs.internal_transfer")))
         expect(record.errors["prevten"])
           .to include(match(I18n.t("validations.lettings.household.prevten.la_general_needs.internal_transfer")))
       end
@@ -771,8 +767,6 @@ RSpec.describe Validations::HouseholdValidations do
             .to include(match I18n.t("validations.lettings.household.prevten.internal_transfer", prevten: label))
           expect(record.errors["referral"])
             .to include(match I18n.t("validations.lettings.household.referral.prevten_invalid", prevten: ""))
-          expect(record.errors["referral_type"])
-            .to include(match I18n.t("validations.lettings.household.referral.prevten_invalid", prevten: ""))
         end
       end
     end