diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb
index b3bffb4f4..26d84dd28 100644
--- a/app/controllers/case_logs_controller.rb
+++ b/app/controllers/case_logs_controller.rb
@@ -109,9 +109,13 @@ private
day = params["case_log"]["#{question_key}(3i)"]
month = params["case_log"]["#{question_key}(2i)"]
year = params["case_log"]["#{question_key}(1i)"]
- next unless day.present? && month.present? && year.present?
+ next unless [day, month, year].any?(&:present?)
- result[question_key] = Date.new(year.to_i, month.to_i, day.to_i)
+ result[question_key] = if day.to_i.between?(1, 31) && month.to_i.between?(1, 12) && year.to_i.between?(2000, 2200)
+ Date.new(year.to_i, month.to_i, day.to_i)
+ else
+ Date.new(0, 1, 1)
+ end
end
next unless question_params
diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb
index f546ea56d..dcab1753f 100644
--- a/app/models/bulk_upload.rb
+++ b/app/models/bulk_upload.rb
@@ -26,12 +26,14 @@ class BulkUpload
else
data_range = FIRST_DATA_ROW..last_row
data_range.map do |row_num|
- case_log = CaseLog.create
+ case_log = CaseLog.create!
map_row(sheet.row(row_num)).each do |attr_key, attr_val|
- begin
- case_log.update_attribute(attr_key, attr_val)
- rescue ArgumentError
+ update = case_log.update(attr_key => attr_val)
+ unless update
+ # TODO: determine what to do when a bulk upload contains field values that don't pass validations
end
+ rescue ArgumentError
+ # TODO: determine what we want to do when bulk upload contains totally invalid data for a field.
end
end
end
diff --git a/app/models/case_log.rb b/app/models/case_log.rb
index 5b5dc6f40..2134a766a 100644
--- a/app/models/case_log.rb
+++ b/app/models/case_log.rb
@@ -5,6 +5,7 @@ class CaseLogValidator < ActiveModel::Validator
include PropertyValidations
include FinancialValidations
include TenancyValidations
+ include DateValidations
def validate(record)
# If we've come from the form UI we only want to validate the specific fields
diff --git a/app/validations/date_validations.rb b/app/validations/date_validations.rb
new file mode 100644
index 000000000..b44cb8d53
--- /dev/null
+++ b/app/validations/date_validations.rb
@@ -0,0 +1,21 @@
+module DateValidations
+ def validate_property_major_repairs(record)
+ date_valid?("mrcdate", record)
+ end
+
+ def validate_tenancy_start_date(record)
+ date_valid?("startdate", record)
+ end
+
+ def validate_sale_completion_date(record)
+ date_valid?("sale_completion_date", record)
+ end
+
+private
+
+ def date_valid?(question, record)
+ if record[question].is_a?(ActiveSupport::TimeWithZone) && record[question].year.zero?
+ record.errors.add question, "Please enter a valid date"
+ end
+ end
+end
diff --git a/app/views/case_logs/edit.html.erb b/app/views/case_logs/edit.html.erb
index 25668b2ac..94fabb9a9 100644
--- a/app/views/case_logs/edit.html.erb
+++ b/app/views/case_logs/edit.html.erb
@@ -1,6 +1,6 @@
<%= turbo_frame_tag "case_log_form", target: "_top" do %>
-
+
Tasklist for log
<%= @case_log.id %>
diff --git a/app/views/case_logs/index.html.erb b/app/views/case_logs/index.html.erb
index 7352c304c..ea3d45436 100644
--- a/app/views/case_logs/index.html.erb
+++ b/app/views/case_logs/index.html.erb
@@ -2,7 +2,7 @@
Your logs
-
+
<%= link_to "Create new log", case_logs_path, method: :post, class: "govuk-button" %>
diff --git a/app/views/form/check_answers.html.erb b/app/views/form/check_answers.html.erb
index 26b153f8f..46cb5a4f9 100644
--- a/app/views/form/check_answers.html.erb
+++ b/app/views/form/check_answers.html.erb
@@ -1,6 +1,6 @@
<%= turbo_frame_tag "case_log_form", target: "_top" do %>
-
+
Check the answers you gave for <%= subsection.humanize(capitalize: false) %>
<%= display_answered_questions_summary(subsection, @case_log, form) %>
<% form.pages_for_subsection(subsection).each do |page, page_info| %>
diff --git a/app/views/form/page.html.erb b/app/views/form/page.html.erb
index 238f11439..f29011b99 100644
--- a/app/views/form/page.html.erb
+++ b/app/views/form/page.html.erb
@@ -4,7 +4,7 @@
<%= turbo_frame_tag "case_log_form", target: "_top" do %>
-
+
<% if page_info["header"].present? %>
<%= page_info["header"] %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 497489b22..34e97e8b9 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -35,25 +35,12 @@
Skip to main content
diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json
index 7ae709b1c..b0ac1a524 100644
--- a/config/forms/2021_2022.json
+++ b/config/forms/2021_2022.json
@@ -1028,18 +1028,6 @@
"tenancy_information": {
"label": "Tenancy information",
"pages": {
- "tenancy_code": {
- "header": "",
- "description": "",
- "questions": {
- "tenancy_code": {
- "check_answer_label": "What is the tenancy code?",
- "header": "What is the tenancy code?",
- "hint_text": "",
- "type": "text"
- }
- }
- },
"starter_tenancy": {
"header": "",
"description": "",
@@ -1088,7 +1076,7 @@
"4": "Other"
},
"conditional_for": {
- "other_tenancy_type": ["Other"]
+ "tenancyother": ["Other"]
}
},
"tenancyother": {
diff --git a/db/migrate/20211116102527_change_datetime.rb b/db/migrate/20211116102527_change_datetime.rb
index f3b78282b..10a9c581f 100644
--- a/db/migrate/20211116102527_change_datetime.rb
+++ b/db/migrate/20211116102527_change_datetime.rb
@@ -1,5 +1,5 @@
class ChangeDatetime < ActiveRecord::Migration[6.1]
- def change
+ def up
change_table :case_logs, bulk: true do |t|
t.remove :sale_completion_date
t.column :sale_completion_date, :datetime
@@ -7,4 +7,13 @@ class ChangeDatetime < ActiveRecord::Migration[6.1]
t.column :startdate, :datetime
end
end
+
+ def down
+ change_table :case_logs, bulk: true do |t|
+ t.remove :sale_completion_date
+ t.column :sale_completion_date, :string
+ t.remove :startdate
+ t.column :startdate, :string
+ end
+ end
end
diff --git a/db/schema.rb b/db/schema.rb
index 0303012f1..fcf431435 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -121,6 +121,8 @@ ActiveRecord::Schema.define(version: 2021_11_16_102527) do
t.integer "rp_dontknow"
t.datetime "discarded_at"
t.string "tenancyother"
+ t.integer "override_net_income_validation"
+ t.string "net_income_known"
t.string "gdpr_acceptance"
t.string "gdpr_declined"
t.string "property_owner_organisation"
@@ -131,8 +133,6 @@ ActiveRecord::Schema.define(version: 2021_11_16_102527) do
t.string "intermediate_rent_product_name"
t.string "needs_type"
t.string "purchaser_code"
- t.integer "override_net_income_validation"
- t.string "net_income_known"
t.integer "reason"
t.string "propcode"
t.integer "majorrepairs"
diff --git a/spec/controllers/case_logs_controller_spec.rb b/spec/controllers/case_logs_controller_spec.rb
index 55a69e7e8..9a9d29b82 100644
--- a/spec/controllers/case_logs_controller_spec.rb
+++ b/spec/controllers/case_logs_controller_spec.rb
@@ -186,7 +186,7 @@ RSpec.describe CaseLogsController, type: :controller do
"mrcdate(1i)": "2021",
"mrcdate(2i)": "05",
"mrcdate(3i)": "04",
- page: "major_repairs_date",
+ page: "property_major_repairs",
}
end
it "saves full and partial dates" do
diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb
index f89d43863..330cfc499 100644
--- a/spec/features/case_log_spec.rb
+++ b/spec/features/case_log_spec.rb
@@ -493,4 +493,56 @@ RSpec.describe "Form Features" do
expect(page).to have_current_path("/case_logs/#{id}/rent")
end
end
+
+ describe "date validation", js: true do
+ def fill_in_date(case_log_id, question, day, month, year, path)
+ visit("/case_logs/#{case_log_id}/#{path}")
+ fill_in("#{question}_1i", with: year)
+ fill_in("#{question}_2i", with: month)
+ fill_in("#{question}_3i", with: day)
+ end
+ it "does not allow out of range dates to be submitted" do
+ fill_in_date(id, "case_log_mrcdate", 3100, 12, 2000, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+
+ fill_in_date(id, "case_log_mrcdate", 12, 1, 20_000, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+
+ fill_in_date(id, "case_log_mrcdate", 13, 100, 2020, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+
+ fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/local_authority/check_answers")
+ end
+
+ it "does not allow non numeric inputs to be submitted" do
+ fill_in_date(id, "case_log_mrcdate", "abc", "de", "ff", "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+ end
+
+ it "does not allow partial inputs to be submitted" do
+ fill_in_date(id, "case_log_mrcdate", 21, 12, nil, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+
+ fill_in_date(id, "case_log_mrcdate", 12, nil, 2000, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+
+ fill_in_date(id, "case_log_mrcdate", nil, 10, 2020, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/property_major_repairs")
+ end
+
+ it "allows valid inputs to be submitted" do
+ fill_in_date(id, "case_log_mrcdate", 21, 11, 2020, "property_major_repairs")
+ click_button("Save and continue")
+ expect(page).to have_current_path("/case_logs/#{id}/local_authority/check_answers")
+ end
+ end
end
diff --git a/spec/fixtures/forms/test_form.json b/spec/fixtures/forms/test_form.json
index 28feb44b9..4ac56645f 100644
--- a/spec/fixtures/forms/test_form.json
+++ b/spec/fixtures/forms/test_form.json
@@ -511,7 +511,7 @@
}
}
},
- "major_repairs_date": {
+ "property_major_repairs": {
"questions": {
"mrcdate": {
"check_answer_label": "What was the major repairs completion date?",
@@ -545,4 +545,4 @@
}
}
}
- }
\ No newline at end of file
+ }