Browse Source

Cldc 1223 pregnancy soft validations (#602)

* update hard validation limits for pregnancy age, remove hard validation if there are no females at all

* Add soft validations for pregnancy

* make the error message consistent

* Only check the values for the members with details known in the household

* Show interruption screen when resident details are updated

* Route back to check answers after an interruption screen and back to previous page if no is selected on the interruption screen

Co-authored-by: baarkerlounger  <baarkerlounger@users.noreply.github.com>

* update validation messages

* fix a test

* fix a typo

Co-authored-by: baarkerlounger  <baarkerlounger@users.noreply.github.com>
pull/619/head
kosiakkatrina 3 years ago committed by baarkerlounger
parent
commit
88fec8e5ab
  1. 23
      app/controllers/form_controller.rb
  2. 10
      app/models/validations/household_validations.rb
  3. 32
      app/models/validations/soft_validations.rb
  4. 2154
      config/forms/2021_2022.json
  5. 2084
      config/forms/2022_2023.json
  6. 11
      config/locales/en.yml
  7. 5
      db/migrate/20220523150557_add_pregnancy_value_check.rb
  8. 3
      db/schema.rb
  9. 37
      spec/models/validations/household_validations_spec.rb
  10. 39
      spec/models/validations/soft_validations_spec.rb
  11. 37
      spec/requests/form_controller_spec.rb

23
app/controllers/form_controller.rb

@ -9,12 +9,7 @@ class FormController < ApplicationController
responses_for_page = responses_for_page(@page)
if @case_log.update(responses_for_page)
session[:errors] = nil
if is_referrer_check_answers? && !@case_log.form.next_page(@page, @case_log).to_s.include?("value_check")
redirect_to(send("case_log_#{@case_log.form.subsection_for_page(@page).id}_check_answers_path", @case_log))
else
redirect_path = @case_log.form.next_page_redirect_path(@page, @case_log)
redirect_to(send(redirect_path, @case_log))
end
redirect_to(successful_redirect_path)
else
redirect_path = "case_log_#{@page.id}_path"
session[:errors] = @case_log.errors.to_json
@ -108,4 +103,20 @@ private
referrer = request.headers["HTTP_REFERER"].presence || ""
referrer.present? && CGI.parse(referrer.split("?")[-1]).present? && CGI.parse(referrer.split("?")[-1])["referrer"][0] == "check_answers"
end
def successful_redirect_path
if is_referrer_check_answers?
page_ids = @case_log.form.subsection_for_page(@page).pages.map(&:id)
page_index = page_ids.index(@page.id)
next_page = @case_log.form.next_page(@page, @case_log)
previous_page = @case_log.form.previous_page(page_ids, page_index, @case_log)
if next_page.to_s.include?("value_check") || next_page == previous_page
return "/logs/#{@case_log.id}/#{next_page.dasherize}?referrer=check_answers"
else
return send("case_log_#{@case_log.form.subsection_for_page(@page).id}_check_answers_path", @case_log)
end
end
redirect_path = @case_log.form.next_page_redirect_path(@page, @case_log)
send(redirect_path, @case_log)
end
end

10
app/models/validations/household_validations.rb

@ -36,7 +36,7 @@ module Validations::HouseholdValidations
end
def validate_pregnancy(record)
if (record.has_pregnancy? || record.pregnancy_refused?) && !women_of_child_bearing_age_in_household(record)
if (record.has_pregnancy? || record.pregnancy_refused?) && women_in_household(record) && !women_of_child_bearing_age_in_household(record)
record.errors.add :preg_occ, I18n.t("validations.household.preg_occ.no_female")
end
end
@ -136,7 +136,13 @@ private
(1..8).any? do |n|
next if record["sex#{n}"].nil? || record["age#{n}"].nil?
(record["sex#{n}"]) == "F" && record["age#{n}"] >= 16 && record["age#{n}"] <= 50
(record["sex#{n}"]) == "F" && record["age#{n}"] >= 11 && record["age#{n}"] <= 65
end
end
def women_in_household(record)
(1..8).any? do |n|
record["sex#{n}"] == "F"
end
end

32
app/models/validations/soft_validations.rb

@ -48,8 +48,40 @@ module Validations::SoftValidations
end
end
def no_females_in_a_pregnant_household?
!females_in_the_household? && all_tenants_age_and_gender_information_completed? && preg_occ == 1
end
def female_in_pregnant_household_in_soft_validation_range?
all_tenants_age_and_gender_information_completed? && (females_in_age_range(11, 15) || females_in_age_range(51, 65)) && !females_in_age_range(16, 50) && preg_occ == 1
end
def all_tenants_age_and_gender_information_completed?
(1..hhmemb).all? do |n|
public_send("sex#{n}").present? && public_send("age#{n}").present? && details_known_or_lead_tenant?(n) && public_send("age#{n}_known").present? && public_send("age#{n}_known").zero?
end
end
private
def details_known_or_lead_tenant?(tenant_number)
return true if tenant_number == 1
public_send("details_known_#{tenant_number}").zero?
end
def females_in_age_range(min, max)
(1..hhmemb).any? do |n|
public_send("sex#{n}") == "F" && public_send("age#{n}").present? && public_send("age#{n}").between?(min, max)
end
end
def females_in_the_household?
(1..hhmemb).any? do |n|
public_send("sex#{n}") == "F" || public_send("sex#{n}").nil?
end
end
def tenant_is_retired?(economic_status)
economic_status == 5
end

2154
config/forms/2021_2022.json

File diff suppressed because it is too large Load Diff

2084
config/forms/2022_2023.json

File diff suppressed because it is too large Load Diff

11
config/locales/en.yml

@ -157,7 +157,7 @@ en:
question_required: "You must answer whether the person is still serving in the UK armed forces as you told us they’re a current or former regular"
question_not_required: "You cannot answer whether the person is still serving in the UK armed forces as you told us they’re not a current or former regular"
preg_occ:
no_female: "You must answer ‘no’ as there are no females aged 16-50 in the household"
no_female: "You must answer ‘no’ as there are no female tenants aged 11-65 in the household"
age:
retired_male: "Male tenant who is retired must be 65 or over"
retired_female: "Female tenant who is retired must be 60 or over"
@ -227,7 +227,6 @@ en:
not_joint: "This cannot be a joint tenancy as you've told us there's only one person in the household"
joint_more_than_one_member: "There must be more than one person in the household as you've told us this is a joint tenancy"
declaration:
missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log."
@ -241,9 +240,9 @@ en:
message: "Net income is higher than expected based on the lead tenant’s working situation. Are you sure this is correct?"
rent:
min:
hint_text: "<h1 class=\"govuk-heading-l app-panel--interruption\">You told us the rent is %{brent}</h1><p>The minimum rent for this type of property in %{la} is £%{soft_min_for_period}.</p>"
hint_text: '<h1 class="govuk-heading-l app-panel--interruption">You told us the rent is %{brent}</h1><p>The minimum rent for this type of property in %{la} is £%{soft_min_for_period}.</p>'
max:
hint_text: "<h1 class=\"govuk-heading-l app-panel--interruption\">You told us the rent is %{brent}</h1><p>The maximum rent for this type of property in %{la} is £%{soft_max_for_period}.</p>"
hint_text: '<h1 class="govuk-heading-l app-panel--interruption">You told us the rent is %{brent}</h1><p>The maximum rent for this type of property in %{la} is £%{soft_max_for_period}.</p>'
retirement:
min:
title: "You told us this person is under %{age} and retired"
@ -251,6 +250,10 @@ en:
max:
title: "You told us this person is %{age} or over and not retired"
hint_text: "The minimum expected retirement age for %{gender} in England is %{age}."
pregnancy:
title: "You told us somebody in the household is pregnant"
no_females: "You also told us there are no female tenants living at the property."
females_not_in_soft_age_range: "You also told us that any female tenants living at the property are in the following age ranges:<ul><li>11 to 16</li><li>50 to 65</li></ul>"
devise:
two_factor_authentication:

5
db/migrate/20220523150557_add_pregnancy_value_check.rb

@ -0,0 +1,5 @@
class AddPregnancyValueCheck < ActiveRecord::Migration[7.0]
def change
add_column :case_logs, :pregnancy_value_check, :integer
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_05_19_112604) do
ActiveRecord::Schema[7.0].define(version: 2022_05_23_150557) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -224,6 +224,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_05_19_112604) do
t.integer "tshortfall_known"
t.integer "shelteredaccom"
t.integer "retirement_value_check"
t.integer "pregnancy_value_check"
t.index ["created_by_id"], name: "index_case_logs_on_created_by_id"
t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["old_id"], name: "index_case_logs_on_old_id", unique: true

37
spec/models/validations/household_validations_spec.rb

@ -57,40 +57,38 @@ RSpec.describe Validations::HouseholdValidations do
describe "pregnancy validations" do
context "when there are no female tenants" do
it "validates that pregnancy cannot be yes" do
it "validates that pregnancy can be yes" do
record.preg_occ = 1
record.sex1 = "M"
household_validator.validate_pregnancy(record)
expect(record.errors["preg_occ"])
.to include(match I18n.t("validations.household.preg_occ.no_female"))
expect(record.errors["preg_occ"]).to be_empty
end
it "validates that pregnancy cannot be prefer not to say" do
it "validates that pregnancy can be prefer not to say" do
record.preg_occ = 3
record.sex1 = "M"
household_validator.validate_pregnancy(record)
expect(record.errors["preg_occ"])
.to include(match I18n.t("validations.household.preg_occ.no_female"))
expect(record.errors["preg_occ"]).to be_empty
end
end
context "when there are female tenants" do
context "but they are older than 50" do
context "but they are older than 65" do
it "validates that pregnancy cannot be yes" do
record.preg_occ = 1
record.sex1 = "F"
record.age1 = 51
record.age1 = 66
household_validator.validate_pregnancy(record)
expect(record.errors["preg_occ"])
.to include(match I18n.t("validations.household.preg_occ.no_female"))
end
end
context "and they are the lead tenant and under 51" do
context "and they are the lead tenant and under 65" do
it "pregnancy can be yes" do
record.preg_occ = 0
record.preg_occ = 1
record.sex1 = "F"
record.age1 = 32
record.age1 = 64
household_validator.validate_pregnancy(record)
expect(record.errors["preg_occ"]).to be_empty
end
@ -98,15 +96,28 @@ RSpec.describe Validations::HouseholdValidations do
context "and they are another household member and under 51" do
it "pregnancy can be yes" do
record.preg_occ = 0
record.preg_occ = 1
record.sex1 = "M"
record.age1 = 25
record.sex3 = "F"
record.age3 = 32
record.age3 = 64
household_validator.validate_pregnancy(record)
expect(record.errors["preg_occ"]).to be_empty
end
end
context "and they are another household member and under 11" do
it "pregnancy can be yes" do
record.preg_occ = 1
record.sex1 = "M"
record.age1 = 25
record.sex3 = "F"
record.age3 = 10
household_validator.validate_pregnancy(record)
expect(record.errors["preg_occ"])
.to include(match I18n.t("validations.household.preg_occ.no_female"))
end
end
end
end

39
spec/models/validations/soft_validations_spec.rb

@ -164,4 +164,43 @@ RSpec.describe Validations::SoftValidations do
end
end
end
describe "pregnancy soft validations" do
context "when there are no female tenants" do
it "shows the interruption screen" do
record.update!(age1: 43, sex1: "M", preg_occ: 1, hhmemb: 1, age1_known: 0)
expect(record.no_females_in_a_pregnant_household?).to be true
end
end
context "when female tenants are in 11-16 age range" do
it "shows the interruption screen" do
record.update!(age2: 14, sex2: "F", preg_occ: 1, hhmemb: 2, details_known_2: 0, age2_known: 0, age1: 18, sex1: "M", age1_known: 0)
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true
end
end
context "when female tenants are in 50-65 age range" do
it "shows the interruption screen" do
record.update!(age1: 54, sex1: "F", preg_occ: 1, hhmemb: 1, age1_known: 0)
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true
end
end
context "when female tenants are outside of soft validation ranges" do
it "does not show the interruption screen" do
record.update!(age1: 44, sex1: "F", preg_occ: 1, hhmemb: 1)
expect(record.no_females_in_a_pregnant_household?).to be false
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false
end
end
context "when the information about the tenants is not given" do
it "does not show the interruption screen" do
record.update!(preg_occ: 1, hhmemb: 2)
expect(record.no_females_in_a_pregnant_household?).to be false
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false
end
end
end
end

37
spec/requests/form_controller_spec.rb

@ -342,6 +342,43 @@ RSpec.describe FormController, type: :request do
end
end
context "when coming from check answers page" do
context "and navigating to an interruption screen" do
let(:interrupt_params) do
{
id: completed_case_log.id,
case_log: {
page: "net_income_value_check",
net_income_value_check: value,
},
}
end
let(:referrer) { "/logs/#{completed_case_log.id}/net-income-value-check?referrer=check_answers" }
before do
completed_case_log.update!(ecstat1: 1, earnings: 130, hhmemb: 1) # we're not routing to that page, so it gets cleared?§
allow(completed_case_log).to receive(:net_income_soft_validation_triggered?).and_return(true)
post "/logs/#{completed_case_log.id}/form", params: interrupt_params, headers: headers.merge({ "HTTP_REFERER" => referrer })
end
context "when yes is answered" do
let(:value) { 0 }
it "redirects back to check answers if 'yes' is selected" do
expect(response).to redirect_to("/logs/#{completed_case_log.id}/income-and-benefits/check-answers")
end
end
context "when no is answered" do
let(:value) { 1 }
it "redirects to the previous question if 'no' is selected" do
expect(response).to redirect_to("/logs/#{completed_case_log.id}/net-income?referrer=check_answers")
end
end
end
end
context "with case logs that are not owned or managed by your organisation" do
let(:answer) { 25 }
let(:other_organisation) { FactoryBot.create(:organisation) }

Loading…
Cancel
Save