From e54c6b1be4e28a470891bf389fad379ab7546a55 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 7 Feb 2022 17:17:02 +0000 Subject: [PATCH] add validation for declaration on log submission --- app/controllers/form_controller.rb | 8 +- app/models/case_log.rb | 2 + .../validations/submission_validations.rb | 10 ++ app/views/form/page.html.erb | 8 +- config/forms/2021_2022.json | 7 +- config/locales/en.yml | 3 + db/migrate/20220207091117_add_declaration.rb | 7 + db/schema.rb | 4 +- spec/factories/case_log.rb | 122 ++++++++++++++++++ spec/features/form/validations_spec.rb | 30 +++++ spec/fixtures/complete_case_log.json | 3 +- spec/fixtures/forms/2021_2022.json | 13 +- 12 files changed, 202 insertions(+), 15 deletions(-) create mode 100644 app/models/validations/submission_validations.rb create mode 100644 db/migrate/20220207091117_add_declaration.rb diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 573c8631a..3dbc0de7f 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -8,8 +8,12 @@ class FormController < ApplicationController @page = @case_log.form.get_page(params[:case_log][:page]) responses_for_page = responses_for_page(@page) if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors? - redirect_path = @case_log.form.next_page_redirect_path(@page, @case_log) - redirect_to(send(redirect_path, @case_log)) + if @page.id == "declaration" + redirect_to(case_logs_path) + else + redirect_path = @case_log.form.next_page_redirect_path(@page, @case_log) + redirect_to(send(redirect_path, @case_log)) + end else @subsection = @case_log.form.subsection_for_page(@page) render "form/page", status: :unprocessable_entity diff --git a/app/models/case_log.rb b/app/models/case_log.rb index c8922029a..1cb3a4cc9 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -7,6 +7,7 @@ class CaseLogValidator < ActiveModel::Validator include Validations::TenancyValidations include Validations::DateValidations include Validations::LocalAuthorityValidations + include Validations::SubmissionValidations def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } @@ -140,6 +141,7 @@ class CaseLog < ApplicationRecord enum is_carehome: POLAR, _suffix: true enum nocharge: POLAR, _suffix: true enum referral: REFERRAL, _suffix: true + enum declaration: POLAR, _suffix: true AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing].freeze diff --git a/app/models/validations/submission_validations.rb b/app/models/validations/submission_validations.rb new file mode 100644 index 000000000..9f8bf76d0 --- /dev/null +++ b/app/models/validations/submission_validations.rb @@ -0,0 +1,10 @@ +module Validations::SubmissionValidations + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well + + def validate_declaration(record) + if record.declaration == "No" + record.errors.add :declaration, I18n.t("validations.declaration.missing") + end + end +end diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb index 893cd10dc..8799351eb 100644 --- a/app/views/form/page.html.erb +++ b/app/views/form/page.html.erb @@ -47,8 +47,12 @@ <% end %> <%= f.hidden_field :page, value: @page.id %> - <%= f.govuk_submit "Save and continue" %> - + <% if @page.id == "declaration" %> + <%= f.govuk_submit "Submit lettings log" %> + <%else %> + <%= f.govuk_submit "Save and continue" %> + <%end %> + <% end %> <% end %> <% end %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 7ebdfdfef..9bf27343d 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -3226,9 +3226,12 @@ "questions": { "declaration": { "check_answer_label": "", - "header": "What is the tenant code?", + "header": "Submit your lettings log ", "hint_text": "", - "type": "text" + "type": "checkbox", + "answer_options": { + "declaration": "The tenant has seen the Department for Levelling Up, Housing & Communities (DLUHC) privacy notice" + } } } } diff --git a/config/locales/en.yml b/config/locales/en.yml index 17e5ce3aa..c40da4bda 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -115,6 +115,9 @@ en: referral: rsnvac_non_temp: "Answer cannot be this source of referral as you already told us this is a re-let to tenant who occupied the same property as temporary accommodation" + declaration: + missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log." + soft_validations: net_income: hint_text: "This is based on the tenant’s work situation: %{ecstat1}" diff --git a/db/migrate/20220207091117_add_declaration.rb b/db/migrate/20220207091117_add_declaration.rb new file mode 100644 index 000000000..f9b6be7e3 --- /dev/null +++ b/db/migrate/20220207091117_add_declaration.rb @@ -0,0 +1,7 @@ +class AddDeclaration < ActiveRecord::Migration[7.0] + def change + change_table :case_logs, bulk: true do |t| + t.column :declaration, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 29e45f85d..90b61c3ea 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,8 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_02_07_1123100) do +ActiveRecord::Schema.define(version: 2022_02_07_091117) do + # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -192,6 +193,7 @@ ActiveRecord::Schema.define(version: 2022_02_07_1123100) do t.decimal "tcharge", precision: 10, scale: 2 t.decimal "tshortfall", precision: 10, scale: 2 t.decimal "chcharge", precision: 10, scale: 2 + t.integer "declaration" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index 0a73e7750..308e2fa85 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -152,6 +152,128 @@ FactoryBot.define do chcharge { 7 } letting_in_sheltered_accomodation { "No" } la_known { "Yes" } + declaration { "Yes" } + end + trait :completed_without_declaration do + status { 1 } + tenant_code { "BZ737" } + postcode { "NW1 7TY" } + age1 { 35 } + sex1 { "Female" } + ethnic { 2 } + national { 4 } + prevten { "Private sector tenancy" } + ecstat1 { 2 } + other_hhmemb { 1 } + hhmemb { 2 } + relat2 { "Partner" } + age2 { 32 } + sex2 { "Male" } + ecstat2 { "Not seeking work" } + homeless { "Yes - other homelessness" } + underoccupation_benefitcap { "No" } + leftreg { "No - they left up to 5 years ago" } + reservist { "No" } + illness { "Yes" } + preg_occ { "Yes" } + accessibility_requirements { "No" } + condition_effects { "dummy" } + tenancy_code { "BZ757" } + startertenancy { "No" } + tenancylength { 5 } + tenancy { "Secure (including flexible)" } + lettype { "Affordable Rent General needs LA" } + landlord { "This landlord" } + previous_postcode { "SE2 6RT" } + rsnvac { "Tenant abandoned property" } + unittype_gn { "House" } + beds { 3 } + property_void_date { "03/11/2019" } + offered { 2 } + wchair { "Yes" } + earnings { 68 } + benefits { "Some" } + period { "Every 2 weeks" } + brent { 200 } + scharge { 50 } + pscharge { 40 } + supcharg { 35 } + tcharge { 325 } + layear { "1 to 2 years" } + lawaitlist { "Less than 1 year" } + property_postcode { "NW1 5TY" } + reasonpref { "Yes" } + reasonable_preference_reason { "dummy" } + cbl { "Yes" } + chr { "Yes" } + cap { "No" } + other_reason_for_leaving_last_settled_home { nil } + housingneeds_a { "Yes" } + housingneeds_b { "No" } + housingneeds_c { "No" } + housingneeds_f { "No" } + housingneeds_g { "No" } + housingneeds_h { "No" } + accessibility_requirements_prefer_not_to_say { 0 } + illness_type_1 { "No" } + illness_type_2 { "Yes" } + illness_type_3 { "No" } + illness_type_4 { "No" } + illness_type_8 { "No" } + illness_type_5 { "No" } + illness_type_6 { "No" } + illness_type_7 { "No" } + illness_type_9 { "No" } + illness_type_10 { "No" } + rp_homeless { "Yes" } + rp_insan_unsat { "No" } + rp_medwel { "No" } + rp_hardship { "No" } + rp_dontknow { "No" } + discarded_at { nil } + tenancyother { nil } + override_net_income_validation { nil } + net_income_known { "Weekly" } + gdpr_acceptance { "Yes" } + gdpr_declined { "No" } + property_owner_organisation { "Test" } + property_manager_organisation { "Test" } + renewal { 1 } + rent_type { 1 } + intermediate_rent_product_name { 2 } + needstype { 1 } + purchaser_code { 798_794 } + reason { "Permanently decanted from another property owned by this landlord" } + propcode { "123" } + majorrepairs { "Yes" } + la { "Barnet" } + prevloc { "Ashford" } + hb { 1 } + hbrentshortfall { "Yes" } + tshortfall { 12 } + postcod2 { "w3" } + ppostc1 { "w3" } + ppostc2 { "w3" } + property_relet { "No" } + mrcdate { Time.zone.now } + mrcday { 5 } + mrcmonth { 5 } + mrcyear { 2020 } + incref { 0 } + sale_completion_date { nil } + startdate { Time.zone.now } + day { Time.zone.now.day } + month { Time.zone.now.month } + year { 2021 } + armedforces { 1 } + builtype { 1 } + unitletas { 2 } + household_charge { "Yes" } + has_benefits { "Yes" } + is_carehome { "No" } + chcharge { 7 } + letting_in_sheltered_accomodation { "No" } + la_known { "Yes" } end created_at { Time.zone.now } updated_at { Time.zone.now } diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 246f6af30..40df3d41d 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -25,6 +25,14 @@ RSpec.describe "validations" do managing_organisation: user.organisation, ) end + let(:completed_without_declaration) do + FactoryBot.create( + :case_log, + :completed_without_declaration, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + ) + end let(:id) { case_log.id } describe "Question validation" do @@ -162,4 +170,26 @@ RSpec.describe "validations" do end end end + + describe "Submission validation" do + context "when tenant has not seen the privacy notice" do + it "shows a warning" do + visit("/logs/#{completed_without_declaration.id}/declaration") + expect(page).to have_current_path("/logs/#{completed_without_declaration.id}/declaration") + click_button("Submit lettings log") + # expect(page).to have_current_path("/logs/#{completed_case_log.id}/declaration") + expect(page).to have_content("You must show the DLUHC privacy notice to the tenant") + end + end + + context "when tenant has seen the privacy notice" do + it "lets submit the log" do + completed_without_declaration.update!({ declaration: "Yes" }) + visit("/logs/#{completed_without_declaration.id}/declaration") + expect(page).to have_current_path("/logs/#{completed_without_declaration.id}/declaration") + click_button("Submit lettings log") + expect(page).to have_current_path("/logs") + end + end + end end diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 577523dc9..c3619f2c9 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -147,6 +147,7 @@ "household_charge": "Yes", "is_carehome": "Yes", "chcharge": "6", - "letting_in_sheltered_accomodation": "No" + "letting_in_sheltered_accomodation": "No", + "declaration": "Yes" } } diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 3a322369e..c135d602c 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -661,19 +661,18 @@ "household_characteristics": "completed", "household_needs": "completed", "tenancy_information": "completed", - "property_information": "completed", - "income_and_benefits": "completed", - "rent_and_charges": "completed", - "local_authority": "completed" + "property_information": "completed" }], "pages": { "declaration": { "questions": { "declaration": { "check_answer_label": "", - "header": "What is the tenant code?", - "type": "text", - "width": 10 + "header": "Submit your lettings log ", + "type": "checkbox", + "answer_options": { + "declaration": "The tenant has seen the Department for Levelling Up, Housing & Communities (DLUHC) privacy notice" + } } } }