From 7776d90828c1a9b7e4126cb103547fc0de5e7de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Wed, 13 Jul 2022 14:53:29 +0100 Subject: [PATCH] Scheme import improvements * Set owning organisation as agent when directly managed * Map the mobility type --- app/models/location.rb | 10 +++++ app/services/imports/scheme_import_service.rb | 45 ++++++++++++------- .../imports/scheme_location_import_service.rb | 3 +- ...13095713_add_mobility_type_to_locations.rb | 7 +++ db/schema.rb | 3 +- spec/factories/location.rb | 1 + spec/models/case_log_spec.rb | 2 +- .../imports/scheme_import_service_spec.rb | 13 ++++++ .../scheme_location_import_service_spec.rb | 1 + 9 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20220713095713_add_mobility_type_to_locations.rb diff --git a/app/models/location.rb b/app/models/location.rb index bb2453625..ca5063215 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -11,6 +11,16 @@ class Location < ApplicationRecord 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 = { "Self-contained flat or bedsit": 1, "Self-contained flat or bedsit with common facilities": 2, diff --git a/app/services/imports/scheme_import_service.rb b/app/services/imports/scheme_import_service.rb index b68e7e7da..8be6b4603 100644 --- a/app/services/imports/scheme_import_service.rb +++ b/app/services/imports/scheme_import_service.rb @@ -5,20 +5,18 @@ module Imports end def create_scheme(xml_document) - old_id = string_or_nil(xml_document, "id") - status = string_or_nil(xml_document, "status") - - if status == "Approved" + attributes = scheme_attributes(xml_document) + if attributes["status"] == "Approved" Scheme.create!( - owning_organisation_id: find_owning_organisation_id(xml_document), - managing_organisation_id: find_managing_organisation_id(xml_document), - service_name: string_or_nil(xml_document, "name"), - arrangement_type: string_or_nil(xml_document, "arrangement_type"), - old_id:, - old_visible_id: safe_string_as_integer(xml_document, "visible-id"), + owning_organisation_id: attributes["owning_organisation_id"], + managing_organisation_id: attributes["managing_organisation_id"], + service_name: attributes["service_name"], + arrangement_type: attributes["arrangement_type"], + old_id: attributes["old_id"], + old_visible_id: attributes["old_visible_id"], ) 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 @@ -39,16 +37,33 @@ module Imports Integer(str, exception: false) end - def find_owning_organisation_id(xml_doc) - old_org_id = string_or_nil(xml_doc, "institution") + def scheme_attributes(xml_doc) + 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:) raise "Organisation not found with legacy ID #{old_org_id}" if organisation.nil? organisation.id end - def find_managing_organisation_id(xml_doc) - old_visible_id = safe_string_as_integer(xml_doc, "agent") + def find_managing_organisation_id(old_visible_id) return unless old_visible_id organisation = Organisation.find_by(old_visible_id:) diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb index f05381c3b..698c715b5 100644 --- a/app/services/imports/scheme_location_import_service.rb +++ b/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["support_type"] = safe_string_as_integer(xml_doc, "support-type") 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["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"] @@ -84,11 +85,11 @@ module Imports def add_location(scheme, attributes) if attributes["end_date"].nil? || attributes["end_date"] >= Time.zone.now - # wheelchair_adaptation: string_or_nil(xml_doc, "mobility-type"), begin Location.create!( name: attributes["location_name"], postcode: attributes["postcode"], + mobility_type: attributes["mobility_type"], units: attributes["units"], type_of_unit: attributes["type_of_unit"], old_visible_id: attributes["location_old_visible_id"], diff --git a/db/migrate/20220713095713_add_mobility_type_to_locations.rb b/db/migrate/20220713095713_add_mobility_type_to_locations.rb new file mode 100644 index 000000000..9f3284c0c --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 95e134f2a..60780a3eb 100644 --- a/db/schema.rb +++ b/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_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 enable_extension "plpgsql" @@ -250,6 +250,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_12_143943) do t.integer "type_of_unit" t.string "old_id" t.integer "old_visible_id" + t.string "mobility_type" t.index ["old_id"], name: "index_locations_on_old_id", unique: true t.index ["scheme_id"], name: "index_locations_on_scheme_id" end diff --git a/spec/factories/location.rb b/spec/factories/location.rb index 7aebbbcde..532a29da6 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -5,6 +5,7 @@ FactoryBot.define do name { Faker::Address.street_name } type_of_unit { [1, 2, 3, 4, 6, 7].sample } type_of_building { "Purpose built" } + mobility_type { %w[A M N W X].sample } wheelchair_adaptation { 0 } county { Faker::Address.state } scheme diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index d5cbed1cc..2cf69e23d 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1702,7 +1702,7 @@ RSpec.describe CaseLog do context "and not renewal" do 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 described_class.create!({ diff --git a/spec/services/imports/scheme_import_service_spec.rb b/spec/services/imports/scheme_import_service_spec.rb index 3cfb76c96..9071007cc 100644 --- a/spec/services/imports/scheme_import_service_spec.rb +++ b/spec/services/imports/scheme_import_service_spec.rb @@ -60,5 +60,18 @@ RSpec.describe Imports::SchemeImportService do .not_to change(Scheme, :count) 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 diff --git a/spec/services/imports/scheme_location_import_service_spec.rb b/spec/services/imports/scheme_location_import_service_spec.rb index f87fc3724..6d276ef37 100644 --- a/spec/services/imports/scheme_location_import_service_spec.rb +++ b/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.postcode).to eq("S44 6EJ") 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.old_id).to eq(first_location_id) expect(location.old_visible_id).to eq(10)