Browse Source

Update service charge validations (#1428)

* Update service charge validations

* Create location deactivation periods instead of setting a scheme end date

* Add test for deactivation period

* Update ranges in tests

* Remove scheme validation test

* Check that the location gets confirmed

---------

Co-authored-by: Kat <katrina@kosiak.co.uk>
bulk-upload-sentence-case
James Rose 2 years ago committed by GitHub
parent
commit
2440e8a2ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/models/validations/date_validations.rb
  2. 8
      app/models/validations/financial_validations.rb
  3. 24
      app/services/imports/scheme_location_import_service.rb
  4. 8
      config/locales/en.yml
  5. 8
      spec/models/validations/date_validations_spec.rb
  6. 42
      spec/models/validations/financial_validations_spec.rb
  7. 13
      spec/services/imports/scheme_location_import_service_spec.rb

7
app/models/validations/date_validations.rb

@ -37,13 +37,6 @@ module Validations::DateValidations
record.errors.add :startdate, I18n.t("validations.setup.startdate.later_than_14_days_after") record.errors.add :startdate, I18n.t("validations.setup.startdate.later_than_14_days_after")
end end
if record.scheme_id.present?
scheme_end_date = record.scheme.end_date
if scheme_end_date.present? && record.startdate > scheme_end_date
record.errors.add :startdate, I18n.t("validations.setup.startdate.before_scheme_end_date")
end
end
if record["voiddate"].present? && record.startdate < record["voiddate"] if record["voiddate"].present? && record.startdate < record["voiddate"]
record.errors.add :startdate, I18n.t("validations.setup.startdate.after_void_date") record.errors.add :startdate, I18n.t("validations.setup.startdate.after_void_date")
end end

8
app/models/validations/financial_validations.rb

@ -132,12 +132,12 @@ private
CHARGE_MAXIMUMS = { CHARGE_MAXIMUMS = {
scharge: { scharge: {
private_registered_provider: { private_registered_provider: {
general_needs: 55, general_needs: 155,
supported_housing: 280, supported_housing: 480,
}, },
local_authority: { local_authority: {
general_needs: 45, general_needs: 155,
supported_housing: 165, supported_housing: 365,
}, },
}, },
pscharge: { pscharge: {

24
app/services/imports/scheme_location_import_service.rb

@ -35,7 +35,6 @@ module Imports
primary_client_group: attributes["primary_client_group"], primary_client_group: attributes["primary_client_group"],
secondary_client_group: attributes["secondary_client_group"], secondary_client_group: attributes["secondary_client_group"],
sensitive: attributes["sensitive"], sensitive: attributes["sensitive"],
end_date: attributes["end_date"],
# These values were set by the scheme import (management groups) # These values were set by the scheme import (management groups)
owning_organisation_id: source_scheme.owning_organisation_id, owning_organisation_id: source_scheme.owning_organisation_id,
service_name: source_scheme.service_name, service_name: source_scheme.service_name,
@ -43,7 +42,7 @@ module Imports
old_id: source_scheme.old_id, old_id: source_scheme.old_id,
old_visible_id: source_scheme.old_visible_id, old_visible_id: source_scheme.old_visible_id,
) )
confirm_scheme(scheme) confirm_scheme_or_location(scheme)
scheme.save! && scheme scheme.save! && scheme
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
@logger.error("Scheme #{source_scheme.old_visible_id}: Failed to import") @logger.error("Scheme #{source_scheme.old_visible_id}: Failed to import")
@ -59,18 +58,17 @@ module Imports
primary_client_group: attributes["primary_client_group"], primary_client_group: attributes["primary_client_group"],
secondary_client_group: attributes["secondary_client_group"], secondary_client_group: attributes["secondary_client_group"],
sensitive: attributes["sensitive"], sensitive: attributes["sensitive"],
end_date: attributes["end_date"],
} }
confirm_scheme(scheme) confirm_scheme_or_location(scheme)
scheme.save! && scheme scheme.save! && scheme
end end
def confirm_scheme(scheme) def confirm_scheme_or_location(obj)
scheme.confirmed = true obj.confirmed = true
scheme.validate_confirmed obj.validate_confirmed
unless scheme.errors.empty? unless obj.errors.empty?
scheme.confirmed = false obj.confirmed = false
scheme.errors.clear obj.errors.clear
end end
end end
@ -99,7 +97,7 @@ module Imports
end end
def add_location(scheme, attributes) def add_location(scheme, attributes)
Location.create!( location = Location.create!(
name: attributes["location_name"], name: attributes["location_name"],
postcode: attributes["postcode"], postcode: attributes["postcode"],
mobility_type: attributes["mobility_type"], mobility_type: attributes["mobility_type"],
@ -110,6 +108,10 @@ module Imports
startdate: attributes["start_date"], startdate: attributes["start_date"],
scheme:, scheme:,
) )
if attributes["end_date"]
location.location_deactivation_periods.create!(deactivation_date: attributes["end_date"])
end
location
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
@logger.warn("Location is already present with legacy ID #{attributes['location_old_id']}, skipping") @logger.warn("Location is already present with legacy ID #{attributes['location_old_id']}, skipping")
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid

8
config/locales/en.yml

@ -253,11 +253,11 @@ en:
less_than_shortfall: "Enter an amount that is more than the shortfall in basic rent" less_than_shortfall: "Enter an amount that is more than the shortfall in basic rent"
scharge: scharge:
private_registered_provider: private_registered_provider:
general_needs: "Enter a value for the service charge between £0 and £55 per week if the landlord is a private registered provider and it is a general needs letting" general_needs: "Enter a value for the service charge between £0 and £155 per week if the landlord is a private registered provider and it is a general needs letting"
supported_housing: "Enter a value for the service charge between £0 and £280 per week if the landlord is a private registered provider and it is a supported housing letting" supported_housing: "Enter a value for the service charge between £0 and £480 per week if the landlord is a private registered provider and it is a supported housing letting"
local_authority: local_authority:
general_needs: "Enter a value for the service charge between £0 and £45 per week if the landlord is a local authority and it is a general needs letting" general_needs: "Enter a value for the service charge between £0 and £155 per week if the landlord is a local authority and it is a general needs letting"
supported_housing: "Enter a value for the service charge between £0 and £165 per week if the landlord is a local authority and it is a supported housing letting" supported_housing: "Enter a value for the service charge between £0 and £365 per week if the landlord is a local authority and it is a supported housing letting"
pscharge: pscharge:
private_registered_provider: private_registered_provider:
general_needs: "Enter a value for the personal service charge between £0 and £30 per week if the landlord is a private registered provider and it is a general needs letting" general_needs: "Enter a value for the personal service charge between £0 and £30 per week if the landlord is a private registered provider and it is a general needs letting"

8
spec/models/validations/date_validations_spec.rb

@ -21,14 +21,6 @@ RSpec.describe Validations::DateValidations do
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
it "validates that the tenancy start date is before the end date of the chosen scheme if it has an end date" do
record.startdate = Time.zone.today - 3.days
record.scheme = scheme
date_validator.validate_startdate(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.setup.startdate.before_scheme_end_date"))
end
it "validates that the tenancy start date is after the void date if it has a void date" do it "validates that the tenancy start date is after the void date if it has a void date" do
record.startdate = Time.zone.local(2022, 1, 1) record.startdate = Time.zone.local(2022, 1, 1)
record.voiddate = Time.zone.local(2022, 2, 1) record.voiddate = Time.zone.local(2022, 2, 1)

42
spec/models/validations/financial_validations_spec.rb

@ -263,15 +263,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 56 }, charge: { field: "scharge", value: 156 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 300 }, charge: { field: "scharge", value: 672 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 111 }, charge: { field: "scharge", value: 311 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
@ -308,15 +308,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 54 }, charge: { field: "scharge", value: 154 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 220 }, charge: { field: "scharge", value: 670 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 109 }, charge: { field: "scharge", value: 309 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
@ -357,15 +357,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 281 }, charge: { field: "scharge", value: 481 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 1225 }, charge: { field: "scharge", value: 2081 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 561 }, charge: { field: "scharge", value: 961 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
@ -402,15 +402,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 280 }, charge: { field: "scharge", value: 366 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 1200 }, charge: { field: "scharge", value: 1582 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 559 }, charge: { field: "scharge", value: 731 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
@ -455,15 +455,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 46 }, charge: { field: "scharge", value: 156 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 200 }, charge: { field: "scharge", value: 672 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 91 }, charge: { field: "scharge", value: 311 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
@ -549,15 +549,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 165.90 }, charge: { field: "scharge", value: 365.90 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 750 }, charge: { field: "scharge", value: 2081 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 330.50 }, charge: { field: "scharge", value: 961 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
@ -617,15 +617,15 @@ RSpec.describe Validations::FinancialValidations do
[{ [{
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },
charge: { field: "scharge", value: 120.88 }, charge: { field: "scharge", value: 364.88 },
}, },
{ {
period: { label: "monthly", value: 4 }, period: { label: "monthly", value: 4 },
charge: { field: "scharge", value: 608 }, charge: { field: "scharge", value: 1200 },
}, },
{ {
period: { label: "every 2 weeks", value: 2 }, period: { label: "every 2 weeks", value: 2 },
charge: { field: "scharge", value: 329.99 }, charge: { field: "scharge", value: 700.99 },
}, },
{ {
period: { label: "weekly", value: 1 }, period: { label: "weekly", value: 1 },

13
spec/services/imports/scheme_location_import_service_spec.rb

@ -16,6 +16,11 @@ RSpec.describe Imports::SchemeLocationImportService do
File.open("#{directory}/#{filename}.xml") File.open("#{directory}/#{filename}.xml")
end end
before do
WebMock.stub_request(:get, /api.postcodes.io\/postcodes/)
.to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E08000035"}}}', headers: {})
end
context "when importing scheme locations" do context "when importing scheme locations" do
let(:remote_folder) { "schemes" } let(:remote_folder) { "schemes" }
@ -145,6 +150,7 @@ RSpec.describe Imports::SchemeLocationImportService do
expect(location.old_visible_id).to eq("10") expect(location.old_visible_id).to eq("10")
expect(location.startdate).to eq("1900-01-01") expect(location.startdate).to eq("1900-01-01")
expect(location.scheme).to eq(scheme) expect(location.scheme).to eq(scheme)
expect(location.confirmed).to eq(true)
end end
it "matches expected schemes values" do it "matches expected schemes values" do
@ -156,10 +162,15 @@ RSpec.describe Imports::SchemeLocationImportService do
expect(location.scheme.primary_client_group).to eq("Older people with support needs") expect(location.scheme.primary_client_group).to eq("Older people with support needs")
expect(location.scheme.secondary_client_group).to be_nil expect(location.scheme.secondary_client_group).to be_nil
expect(location.scheme.sensitive).to eq("No") expect(location.scheme.sensitive).to eq("No")
expect(location.scheme.end_date).to eq("2050-12-31")
expect(location.scheme.confirmed).to be_truthy expect(location.scheme.confirmed).to be_truthy
end end
it "creates a deactivation period" do
location = location_service.create_scheme_location(location_xml)
expect(location.location_deactivation_periods.count).to eq(1)
expect(location.location_deactivation_periods.first.deactivation_date).to eq(Time.zone.local(2050, 12, 31))
end
context "and we import the same location twice" do context "and we import the same location twice" do
before { location_service.create_scheme_location(location_xml) } before { location_service.create_scheme_location(location_xml) }

Loading…
Cancel
Save