Browse Source

Scheme import improvements (#741)

* Set owning organisation as agent when directly managed
* Map the mobility type
pull/742/head
Stéphane Meny 2 years ago committed by GitHub
parent
commit
0466410c74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      app/models/location.rb
  2. 45
      app/services/imports/scheme_import_service.rb
  3. 3
      app/services/imports/scheme_location_import_service.rb
  4. 7
      db/migrate/20220713095713_add_mobility_type_to_locations.rb
  5. 3
      db/schema.rb
  6. 1
      spec/factories/location.rb
  7. 2
      spec/models/case_log_spec.rb
  8. 13
      spec/services/imports/scheme_import_service_spec.rb
  9. 1
      spec/services/imports/scheme_location_import_service_spec.rb

10
app/models/location.rb

@ -11,6 +11,16 @@ class Location < ApplicationRecord
enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS
MOBILITY_TYPE = {
"Property fitted with equipment and adaptations (if not designed to above standards)": "A",
"Property designed to accessible general standard": "M",
"None": "N",
"Property designed to wheelchair user standard": "W",
"Missing": "X",
}.freeze
enum mobility_type: MOBILITY_TYPE
TYPE_OF_UNIT = { TYPE_OF_UNIT = {
"Self-contained flat or bedsit": 1, "Self-contained flat or bedsit": 1,
"Self-contained flat or bedsit with common facilities": 2, "Self-contained flat or bedsit with common facilities": 2,

45
app/services/imports/scheme_import_service.rb

@ -5,20 +5,18 @@ module Imports
end end
def create_scheme(xml_document) def create_scheme(xml_document)
old_id = string_or_nil(xml_document, "id") attributes = scheme_attributes(xml_document)
status = string_or_nil(xml_document, "status") if attributes["status"] == "Approved"
if status == "Approved"
Scheme.create!( Scheme.create!(
owning_organisation_id: find_owning_organisation_id(xml_document), owning_organisation_id: attributes["owning_organisation_id"],
managing_organisation_id: find_managing_organisation_id(xml_document), managing_organisation_id: attributes["managing_organisation_id"],
service_name: string_or_nil(xml_document, "name"), service_name: attributes["service_name"],
arrangement_type: string_or_nil(xml_document, "arrangement_type"), arrangement_type: attributes["arrangement_type"],
old_id:, old_id: attributes["old_id"],
old_visible_id: safe_string_as_integer(xml_document, "visible-id"), old_visible_id: attributes["old_visible_id"],
) )
else else
@logger.warn("Scheme with legacy ID #{old_id} is not approved (#{status}), skipping") @logger.warn("Scheme with legacy ID #{attributes['old_id']} is not approved (#{attributes['status']}), skipping")
end end
end end
@ -39,16 +37,33 @@ module Imports
Integer(str, exception: false) Integer(str, exception: false)
end end
def find_owning_organisation_id(xml_doc) def scheme_attributes(xml_doc)
old_org_id = string_or_nil(xml_doc, "institution") attributes = {}
attributes["old_id"] = string_or_nil(xml_doc, "id")
attributes["old_visible_id"] = string_or_nil(xml_doc, "visible-id")
attributes["status"] = string_or_nil(xml_doc, "status")
attributes["service_name"] = string_or_nil(xml_doc, "name")
attributes["arrangement_type"] = string_or_nil(xml_doc, "arrangement_type")
attributes["owning_org_old_id"] = string_or_nil(xml_doc, "institution")
attributes["owning_organisation_id"] = find_owning_organisation_id(attributes["owning_org_old_id"])
attributes["management_org_old_visible_id"] = safe_string_as_integer(xml_doc, "agent")
attributes["managing_organisation_id"] = find_managing_organisation_id(attributes["management_org_old_visible_id"])
if attributes["arrangement_type"] == "D" && attributes["managing_organisation_id"].nil?
attributes["managing_organisation_id"] = attributes["owning_organisation_id"]
end
attributes
end
def find_owning_organisation_id(old_org_id)
organisation = Organisation.find_by(old_org_id:) organisation = Organisation.find_by(old_org_id:)
raise "Organisation not found with legacy ID #{old_org_id}" if organisation.nil? raise "Organisation not found with legacy ID #{old_org_id}" if organisation.nil?
organisation.id organisation.id
end end
def find_managing_organisation_id(xml_doc) def find_managing_organisation_id(old_visible_id)
old_visible_id = safe_string_as_integer(xml_doc, "agent")
return unless old_visible_id return unless old_visible_id
organisation = Organisation.find_by(old_visible_id:) organisation = Organisation.find_by(old_visible_id:)

3
app/services/imports/scheme_location_import_service.rb

@ -67,6 +67,7 @@ module Imports
attributes["registered_under_care_act"] = registered_under_care_act.zero? ? nil : registered_under_care_act attributes["registered_under_care_act"] = registered_under_care_act.zero? ? nil : registered_under_care_act
attributes["support_type"] = safe_string_as_integer(xml_doc, "support-type") attributes["support_type"] = safe_string_as_integer(xml_doc, "support-type")
attributes["intended_stay"] = string_or_nil(xml_doc, "intended-stay") attributes["intended_stay"] = string_or_nil(xml_doc, "intended-stay")
attributes["mobility_type"] = string_or_nil(xml_doc, "mobility-type")
attributes["primary_client_group"] = string_or_nil(xml_doc, "client-group-1") attributes["primary_client_group"] = string_or_nil(xml_doc, "client-group-1")
attributes["secondary_client_group"] = string_or_nil(xml_doc, "client-group-2") attributes["secondary_client_group"] = string_or_nil(xml_doc, "client-group-2")
attributes["secondary_client_group"] = nil if attributes["primary_client_group"] == attributes["secondary_client_group"] attributes["secondary_client_group"] = nil if attributes["primary_client_group"] == attributes["secondary_client_group"]
@ -84,11 +85,11 @@ module Imports
def add_location(scheme, attributes) def add_location(scheme, attributes)
if attributes["end_date"].nil? || attributes["end_date"] >= Time.zone.now if attributes["end_date"].nil? || attributes["end_date"] >= Time.zone.now
# wheelchair_adaptation: string_or_nil(xml_doc, "mobility-type"),
begin begin
Location.create!( Location.create!(
name: attributes["location_name"], name: attributes["location_name"],
postcode: attributes["postcode"], postcode: attributes["postcode"],
mobility_type: attributes["mobility_type"],
units: attributes["units"], units: attributes["units"],
type_of_unit: attributes["type_of_unit"], type_of_unit: attributes["type_of_unit"],
old_visible_id: attributes["location_old_visible_id"], old_visible_id: attributes["location_old_visible_id"],

7
db/migrate/20220713095713_add_mobility_type_to_locations.rb

@ -0,0 +1,7 @@
class AddMobilityTypeToLocations < ActiveRecord::Migration[7.0]
def change
change_table :locations, bulk: true do |t|
t.column :mobility_type, :string
end
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_07_12_143943) do ActiveRecord::Schema[7.0].define(version: 2022_07_13_095713) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -250,6 +250,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_12_143943) do
t.integer "type_of_unit" t.integer "type_of_unit"
t.string "old_id" t.string "old_id"
t.integer "old_visible_id" t.integer "old_visible_id"
t.string "mobility_type"
t.index ["old_id"], name: "index_locations_on_old_id", unique: true t.index ["old_id"], name: "index_locations_on_old_id", unique: true
t.index ["scheme_id"], name: "index_locations_on_scheme_id" t.index ["scheme_id"], name: "index_locations_on_scheme_id"
end end

1
spec/factories/location.rb

@ -5,6 +5,7 @@ FactoryBot.define do
name { Faker::Address.street_name } name { Faker::Address.street_name }
type_of_unit { [1, 2, 3, 4, 6, 7].sample } type_of_unit { [1, 2, 3, 4, 6, 7].sample }
type_of_building { "Purpose built" } type_of_building { "Purpose built" }
mobility_type { %w[A M N W X].sample }
wheelchair_adaptation { 0 } wheelchair_adaptation { 0 }
county { Faker::Address.state } county { Faker::Address.state }
scheme scheme

2
spec/models/case_log_spec.rb

@ -1702,7 +1702,7 @@ RSpec.describe CaseLog do
context "and not renewal" do context "and not renewal" do
let(:scheme) { FactoryBot.create(:scheme) } let(:scheme) { FactoryBot.create(:scheme) }
let(:location) { FactoryBot.create(:location, scheme:, county: "E07000041", type_of_unit: 1, type_of_building: "Purpose built", wheelchair_adaptation: 0) } let(:location) { FactoryBot.create(:location, scheme:, county: "E07000041", type_of_unit: 1, type_of_building: "Purpose built") }
let!(:supported_housing_case_log) do let!(:supported_housing_case_log) do
described_class.create!({ described_class.create!({

13
spec/services/imports/scheme_import_service_spec.rb

@ -60,5 +60,18 @@ RSpec.describe Imports::SchemeImportService do
.not_to change(Scheme, :count) .not_to change(Scheme, :count)
end end
end end
context "and the scheme arrange type is direct" do
before do
scheme_xml.at_xpath("//mgmtgroup:arrangement_type").content = "D"
scheme_xml.at_xpath("//mgmtgroup:agent").content = ""
end
it "assigns both owning and managing organisation to the same one" do
scheme = scheme_service.create_scheme(scheme_xml)
expect(scheme.owning_organisation).to eq(owning_org)
expect(scheme.managing_organisation).to eq(owning_org)
end
end
end end
end end

1
spec/services/imports/scheme_location_import_service_spec.rb

@ -134,6 +134,7 @@ RSpec.describe Imports::SchemeLocationImportService do
expect(location.name).to eq("Location 1") expect(location.name).to eq("Location 1")
expect(location.postcode).to eq("S44 6EJ") expect(location.postcode).to eq("S44 6EJ")
expect(location.units).to eq(5) expect(location.units).to eq(5)
expect(location.mobility_type).to eq("Property fitted with equipment and adaptations (if not designed to above standards)")
expect(location.type_of_unit).to eq("Bungalow") expect(location.type_of_unit).to eq("Bungalow")
expect(location.old_id).to eq(first_location_id) expect(location.old_id).to eq(first_location_id)
expect(location.old_visible_id).to eq(10) expect(location.old_visible_id).to eq(10)

Loading…
Cancel
Save