diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index e900a12ff..b482b81f0 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -69,7 +69,7 @@ private end def location_params - required_params = params.require(:location).permit(:postcode, :name, :total_units, :type_of_unit, :wheelchair_adaptation, :add_another_location).merge(scheme_id: @scheme.id) + required_params = params.require(:location).permit(:postcode, :name, :units, :type_of_unit, :wheelchair_adaptation, :add_another_location).merge(scheme_id: @scheme.id) required_params[:postcode] = PostcodeService.clean(required_params[:postcode]) if required_params[:postcode] required_params end diff --git a/app/models/location.rb b/app/models/location.rb index 6718c79a7..bb2453625 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -16,8 +16,8 @@ class Location < ApplicationRecord "Self-contained flat or bedsit with common facilities": 2, "Shared flat": 3, "Shared house or hostel": 4, - "Bungalow": 5, - "Self-contained house": 6, + "Bungalow": 6, + "Self-contained house": 7, }.freeze enum type_of_unit: TYPE_OF_UNIT diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 13f090797..bafa7d395 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -17,10 +17,10 @@ class Scheme < ApplicationRecord enum sensitive: SENSITIVE, _suffix: true REGISTERED_UNDER_CARE_ACT = { - "No": 0, - "Yes – registered care home providing nursing care": 1, - "Yes – registered care home providing personal care": 2, - "Yes – part registered as a care home": 3, + "No": 1, + "Yes – registered care home providing nursing care": 4, + "Yes – registered care home providing personal care": 3, + "Yes – part registered as a care home": 2, }.freeze enum registered_under_care_act: REGISTERED_UNDER_CARE_ACT diff --git a/app/services/imports/scheme_import_service.rb b/app/services/imports/scheme_import_service.rb new file mode 100644 index 000000000..b68e7e7da --- /dev/null +++ b/app/services/imports/scheme_import_service.rb @@ -0,0 +1,60 @@ +module Imports + class SchemeImportService < ImportService + def create_schemes(folder) + import_from(folder, :create_scheme) + end + + def create_scheme(xml_document) + old_id = string_or_nil(xml_document, "id") + status = string_or_nil(xml_document, "status") + + if 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"), + ) + else + @logger.warn("Scheme with legacy ID #{old_id} is not approved (#{status}), skipping") + end + end + + private + + def scheme_field_value(xml_document, field) + field_value(xml_document, "mgmtgroup", field) + end + + def string_or_nil(xml_doc, attribute) + str = scheme_field_value(xml_doc, attribute) + str.presence + end + + # Safe: A string that represents only an integer (or empty/nil) + def safe_string_as_integer(xml_doc, attribute) + str = scheme_field_value(xml_doc, attribute) + Integer(str, exception: false) + end + + def find_owning_organisation_id(xml_doc) + old_org_id = string_or_nil(xml_doc, "institution") + 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") + return unless old_visible_id + + organisation = Organisation.find_by(old_visible_id:) + raise "Organisation not found with legacy visible ID #{old_visible_id}" if organisation.nil? + + organisation.id + end + end +end diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb new file mode 100644 index 000000000..203df64c9 --- /dev/null +++ b/app/services/imports/scheme_location_import_service.rb @@ -0,0 +1,173 @@ +module Imports + class SchemeLocationImportService < ImportService + def create_scheme_locations(folder) + import_from(folder, :create_scheme_location) + end + + def create_scheme_location(xml_document) + management_group = location_field_value(xml_document, "mgmtgroup") + schemes = Scheme.where(old_id: management_group) + raise "Scheme not found with legacy ID #{management_group}" if schemes.empty? + + if schemes.size == 1 && schemes.first.locations&.empty? + scheme = update_scheme(schemes.first, xml_document) + else + scheme = find_scheme_to_merge(xml_document) + scheme ||= duplicate_scheme(schemes, xml_document) + end + add_location(scheme, xml_document) + end + + private + + REGISTERED_UNDER_CARE_ACT = { + 2 => "(Part-registered care home)", + 3 => "(Registered personal care home)", + 4 => "(Registered nursing care home)", + }.freeze + + def create_scheme(source_scheme, xml_doc) + attributes = scheme_attributes(xml_doc) + attributes["owning_organisation_id"] = source_scheme.owning_organisation_id + attributes["managing_organisation_id"] = source_scheme.managing_organisation_id + attributes["service_name"] = source_scheme.service_name + attributes["arrangement_type"] = source_scheme.arrangement_type + attributes["old_id"] = source_scheme.old_id + attributes["old_visible_id"] = source_scheme.old_visible_id + Scheme.create!(attributes) + end + + def update_scheme(scheme, xml_doc) + attributes = scheme_attributes(xml_doc) + scheme.update!(attributes) + scheme + end + + def scheme_attributes(xml_doc) + attributes = {} + attributes["scheme_type"] = safe_string_as_integer(xml_doc, "scheme-type") + registered_under_care_act = safe_string_as_integer(xml_doc, "reg-home-type") + 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["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"] + attributes["sensitive"] = sensitive(xml_doc) + attributes["end_date"] = parse_end_date(xml_doc) + attributes + end + + def add_location(scheme, xml_doc) + end_date = parse_end_date(xml_doc) + old_id = string_or_nil(xml_doc, "id") + + if end_date.nil? || end_date >= Time.zone.now + # wheelchair_adaptation: string_or_nil(xml_doc, "mobility-type"), + begin + Location.create!( + name: string_or_nil(xml_doc, "name"), + postcode: string_or_nil(xml_doc, "postcode"), + units: safe_string_as_integer(xml_doc, "total-units"), + type_of_unit: safe_string_as_integer(xml_doc, "unit-type"), + old_visible_id: safe_string_as_integer(xml_doc, "visible-id"), + old_id:, + scheme:, + ) + rescue ActiveRecord::RecordNotUnique + @logger.warn("Location is already present with legacy ID #{old_id}, skipping") + end + else + @logger.warn("Location with legacy ID #{old_id} is expired (#{end_date}), skipping") + end + end + + def find_scheme_to_merge(xml_doc) + attributes = scheme_attributes(xml_doc) + + Scheme.find_by( + scheme_type: attributes["scheme_type"], + registered_under_care_act: attributes["registered_under_care_act"], + support_type: attributes["support_type"], + intended_stay: attributes["intended_stay"], + primary_client_group: attributes["primary_client_group"], + secondary_client_group: attributes["secondary_client_group"], + ) + end + + def duplicate_scheme(schemes, xml_doc) + # Since all schemes in the array are different, pick the first one + # In the future, consider a better selection method if needed + old_scheme = schemes.first + new_scheme = create_scheme(old_scheme, xml_doc) + + if old_scheme.scheme_type != new_scheme.scheme_type + rename_schemes(old_scheme, new_scheme, :scheme_type) + elsif old_scheme.registered_under_care_act != new_scheme.registered_under_care_act + rename_registered_care(old_scheme, new_scheme) + elsif old_scheme.support_type != new_scheme.support_type + rename_schemes(old_scheme, new_scheme, :support_type) + elsif old_scheme.intended_stay != new_scheme.intended_stay + rename_schemes(old_scheme, new_scheme, :intended_stay) + elsif old_scheme.primary_client_group != new_scheme.primary_client_group + rename_schemes(old_scheme, new_scheme, :primary_client_group) + elsif old_scheme.secondary_client_group != new_scheme.secondary_client_group + rename_schemes(old_scheme, new_scheme, :secondary_client_group) + end + + new_scheme + end + + def rename_registered_care(*schemes) + schemes.each do |scheme| + if REGISTERED_UNDER_CARE_ACT.key?(scheme.registered_under_care_act_before_type_cast) + suffix = REGISTERED_UNDER_CARE_ACT[scheme.registered_under_care_act_before_type_cast] + scheme.update!(service_name: "#{scheme.service_name} - #{suffix}") + end + end + end + + def rename_schemes(old_scheme, new_scheme, attribute) + old_scheme_attribute = old_scheme.send(attribute) + new_scheme_attribute = new_scheme.send(attribute) + + if old_scheme_attribute + old_scheme_name = "#{old_scheme.service_name} - #{old_scheme_attribute}" + old_scheme.update!(service_name: old_scheme_name) + end + if new_scheme_attribute + new_scheme_name = "#{new_scheme.service_name} - #{new_scheme_attribute}" + new_scheme.update!(service_name: new_scheme_name) + end + end + + def location_field_value(xml_doc, field) + field_value(xml_doc, "scheme", field) + end + + def string_or_nil(xml_doc, attribute) + str = location_field_value(xml_doc, attribute) + str.presence + end + + # Safe: A string that represents only an integer (or empty/nil) + def safe_string_as_integer(xml_doc, attribute) + str = location_field_value(xml_doc, attribute) + Integer(str, exception: false) + end + + def sensitive(xml_doc) + value = string_or_nil(xml_doc, "sensitive") + if value == "true" + 1 + else + 0 + end + end + + def parse_end_date(xml_doc) + end_date = string_or_nil(xml_doc, "end-date") + Time.zone.parse(end_date) if end_date + end + end +end diff --git a/app/views/locations/edit.html.erb b/app/views/locations/edit.html.erb index 4d516b020..8e1da602c 100644 --- a/app/views/locations/edit.html.erb +++ b/app/views/locations/edit.html.erb @@ -23,7 +23,7 @@ label: { text: "Name (optional)", size: "m" }, hint: { text: "This is how you refer to this location within your organisation" } %> - <%= f.govuk_number_field :total_units, + <%= f.govuk_number_field :units, label: { text: "Total number of units at this location", size: "m" }, width: 2, hint: { text: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff.s" }, diff --git a/app/views/locations/index.html.erb b/app/views/locations/index.html.erb index 3770c612a..2fa495d2a 100644 --- a/app/views/locations/index.html.erb +++ b/app/views/locations/index.html.erb @@ -37,7 +37,7 @@ <%= body.row do |row| %> <% row.cell(text: location.id) %> <% row.cell(text: simple_format(location_cell(location, "/schemes/#{@scheme.id}/locations/#{location.id}/edit-name"), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %> - <% row.cell(text: location.total_units) %> + <% row.cell(text: location.units) %> <% row.cell(text: simple_format("#{location.type_of_unit}#{location.wheelchair_adaptation == 'Yes' ? "\nWith wheelchair adaptations" : ''}")) %> <% end %> <% end %> diff --git a/app/views/locations/new.html.erb b/app/views/locations/new.html.erb index 19c2ae125..a599eacbe 100644 --- a/app/views/locations/new.html.erb +++ b/app/views/locations/new.html.erb @@ -23,7 +23,7 @@ label: { text: "Name (optional)", size: "m" }, hint: { text: "This is how you refer to this location within your organisation" } %> - <%= f.govuk_number_field :total_units, + <%= f.govuk_number_field :units, label: { text: "Total number of units at this location", size: "m" }, width: 2, hint: { text: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff.s" }, diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index f211ba784..eb9fe5ef3 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -88,7 +88,7 @@ <%= body.row do |row| %> <% row.cell(text: location.id) %> <% row.cell(text: simple_format(location_cell(location, "/schemes/#{@scheme.id}/locations/#{location.id}/edit"), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %> - <% row.cell(text: location.total_units) %> + <% row.cell(text: location.units) %> <% row.cell(text: simple_format("#{location.type_of_unit}#{location.wheelchair_adaptation == 'Yes' ? "\nWith wheelchair adaptations" : ''}")) %> <% end %> <% end %> diff --git a/db/migrate/20220708112603_add_missing_fields_to_scheme.rb b/db/migrate/20220708112603_add_missing_fields_to_scheme.rb new file mode 100644 index 000000000..82669a5b2 --- /dev/null +++ b/db/migrate/20220708112603_add_missing_fields_to_scheme.rb @@ -0,0 +1,9 @@ +class AddMissingFieldsToScheme < ActiveRecord::Migration[7.0] + def change + change_table :schemes, bulk: true do |t| + t.column :arrangement_type, :string + t.column :old_id, :string + t.column :old_visible_id, :integer + end + end +end diff --git a/db/migrate/20220708133052_add_missing_fields_to_location.rb b/db/migrate/20220708133052_add_missing_fields_to_location.rb new file mode 100644 index 000000000..2d140c49e --- /dev/null +++ b/db/migrate/20220708133052_add_missing_fields_to_location.rb @@ -0,0 +1,8 @@ +class AddMissingFieldsToLocation < ActiveRecord::Migration[7.0] + def change + change_table :locations, bulk: true do |t| + t.column :old_id, :string + t.column :old_visible_id, :integer + end + end +end diff --git a/db/migrate/20220711081400_rename_units_from_locations.rb b/db/migrate/20220711081400_rename_units_from_locations.rb new file mode 100644 index 000000000..fef1c4132 --- /dev/null +++ b/db/migrate/20220711081400_rename_units_from_locations.rb @@ -0,0 +1,6 @@ +class RenameUnitsFromLocations < ActiveRecord::Migration[7.0] + def change + rename_column :locations, :total_units, :units + add_column :schemes, :total_units, :integer + end +end diff --git a/db/migrate/20220711152629_add_unique_index_for_old_ids.rb b/db/migrate/20220711152629_add_unique_index_for_old_ids.rb new file mode 100644 index 000000000..401ece0bd --- /dev/null +++ b/db/migrate/20220711152629_add_unique_index_for_old_ids.rb @@ -0,0 +1,5 @@ +class AddUniqueIndexForOldIds < ActiveRecord::Migration[7.0] + def change + add_index :locations, :old_id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 10a390d90..3aa1c43ca 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_06_104313) do +ActiveRecord::Schema[7.0].define(version: 2022_07_11_152629) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,9 +191,9 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_06_104313) do t.integer "joint" t.bigint "created_by_id" t.integer "illness_type_0" - t.integer "retirement_value_check" t.integer "tshortfall_known" t.integer "sheltered" + t.integer "retirement_value_check" t.integer "pregnancy_value_check" t.integer "hhtype" t.integer "new_old" @@ -247,14 +247,17 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_06_104313) do t.string "county" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.integer "total_units" + t.integer "units" t.integer "type_of_unit" + t.string "old_id" + t.integer "old_visible_id" + t.index ["old_id"], name: "index_locations_on_old_id", unique: true t.index ["scheme_id"], name: "index_locations_on_scheme_id" end create_table "logs_exports", force: :cascade do |t| t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } - t.datetime "started_at", null: false + t.datetime "started_at", precision: nil, null: false t.integer "base_number", default: 1, null: false t.integer "increment_number", default: 1, null: false t.boolean "empty_export", default: false, null: false @@ -314,6 +317,10 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_06_104313) do t.datetime "end_date" t.integer "has_other_client_group" t.bigint "managing_organisation_id" + t.string "arrangement_type" + t.string "old_id" + t.integer "old_visible_id" + t.integer "total_units" t.index ["managing_organisation_id"], name: "index_schemes_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id" end diff --git a/lib/tasks/data_import.rake b/lib/tasks/data_import.rake index 6a64c1fd0..54ebe60d4 100644 --- a/lib/tasks/data_import.rake +++ b/lib/tasks/data_import.rake @@ -10,12 +10,14 @@ namespace :core do case type when "organisation" Imports::OrganisationImportService.new(storage_service).create_organisations(path) + when "scheme" + Imports::SchemeImportService.new(storage_service).create_schemes(path) + when "scheme-location" + Imports::SchemeLocationImportService.new(storage_service).create_scheme_locations(path) when "user" Imports::UserImportService.new(storage_service).create_users(path) when "data-protection-confirmation" Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path) - when "organisation-las" - Imports::OrganisationLaImportService.new(storage_service).create_organisation_las(path) when "organisation-rent-periods" Imports::OrganisationRentPeriodImportService.new(storage_service).create_organisation_rent_periods(path) when "case-logs" diff --git a/spec/factories/location.rb b/spec/factories/location.rb index efec1aa76..7aebbbcde 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -3,7 +3,7 @@ FactoryBot.define do location_code { Faker::Name.initials(number: 10) } postcode { Faker::Address.postcode.delete(" ") } name { Faker::Address.street_name } - type_of_unit { Faker::Number.within(range: 1..6) } + type_of_unit { [1, 2, 3, 4, 6, 7].sample } type_of_building { "Purpose built" } wheelchair_adaptation { 0 } county { Faker::Address.state } diff --git a/spec/factories/scheme.rb b/spec/factories/scheme.rb index 5317d9e1d..ecd83a930 100644 --- a/spec/factories/scheme.rb +++ b/spec/factories/scheme.rb @@ -2,7 +2,7 @@ FactoryBot.define do factory :scheme do service_name { Faker::Name.name } sensitive { Faker::Number.within(range: 0..1) } - registered_under_care_act { Faker::Number.within(range: 0..1) } + registered_under_care_act { 1 } support_type { Faker::Number.within(range: 0..6) } scheme_type { 0 } intended_stay { %w[M P S V X].sample } diff --git a/spec/fixtures/softwire_imports/case_logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml b/spec/fixtures/imports/case_logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml similarity index 100% rename from spec/fixtures/softwire_imports/case_logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml rename to spec/fixtures/imports/case_logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml diff --git a/spec/fixtures/softwire_imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml b/spec/fixtures/imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml similarity index 100% rename from spec/fixtures/softwire_imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml rename to spec/fixtures/imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml diff --git a/spec/fixtures/softwire_imports/case_logs/166fc004-392e-47a8-acb8-1c018734882b.xml b/spec/fixtures/imports/case_logs/166fc004-392e-47a8-acb8-1c018734882b.xml similarity index 100% rename from spec/fixtures/softwire_imports/case_logs/166fc004-392e-47a8-acb8-1c018734882b.xml rename to spec/fixtures/imports/case_logs/166fc004-392e-47a8-acb8-1c018734882b.xml diff --git a/spec/fixtures/softwire_imports/case_logs/5ybz29dj-l33t-k1l0-hj86-n4k4ma77xkcd.xml b/spec/fixtures/imports/case_logs/5ybz29dj-l33t-k1l0-hj86-n4k4ma77xkcd.xml similarity index 100% rename from spec/fixtures/softwire_imports/case_logs/5ybz29dj-l33t-k1l0-hj86-n4k4ma77xkcd.xml rename to spec/fixtures/imports/case_logs/5ybz29dj-l33t-k1l0-hj86-n4k4ma77xkcd.xml diff --git a/spec/fixtures/softwire_imports/case_logs/893ufj2s-lq77-42m4-rty6-ej09gh585uy1.xml b/spec/fixtures/imports/case_logs/893ufj2s-lq77-42m4-rty6-ej09gh585uy1.xml similarity index 100% rename from spec/fixtures/softwire_imports/case_logs/893ufj2s-lq77-42m4-rty6-ej09gh585uy1.xml rename to spec/fixtures/imports/case_logs/893ufj2s-lq77-42m4-rty6-ej09gh585uy1.xml diff --git a/spec/fixtures/softwire_imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml b/spec/fixtures/imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml similarity index 100% rename from spec/fixtures/softwire_imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml rename to spec/fixtures/imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml diff --git a/spec/fixtures/softwire_imports/organisation_rent_periods/ebd22326d33e389e9f1bfd546979d2c05f9e68d6.xml b/spec/fixtures/imports/organisation_rent_periods/ebd22326d33e389e9f1bfd546979d2c05f9e68d6.xml similarity index 100% rename from spec/fixtures/softwire_imports/organisation_rent_periods/ebd22326d33e389e9f1bfd546979d2c05f9e68d6.xml rename to spec/fixtures/imports/organisation_rent_periods/ebd22326d33e389e9f1bfd546979d2c05f9e68d6.xml diff --git a/spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml b/spec/fixtures/imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml similarity index 100% rename from spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml rename to spec/fixtures/imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml diff --git a/spec/fixtures/imports/scheme_locations/0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e.xml b/spec/fixtures/imports/scheme_locations/0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e.xml new file mode 100644 index 000000000..68eda865e --- /dev/null +++ b/spec/fixtures/imports/scheme_locations/0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e.xml @@ -0,0 +1,25 @@ + + 0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e + Location 1 + 134 + S44 6EJ + true + false + 5 + True + 7 + 6 + 1 + 2 + A + P + M + + 1900-01-01 + 2050-12-31 + 6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d + Approved + 10 + 1 + 08:12.0 + diff --git a/spec/fixtures/imports/scheme_locations/0bb3836b70b4dd9903263d5a764a5c45b964a89d.xml b/spec/fixtures/imports/scheme_locations/0bb3836b70b4dd9903263d5a764a5c45b964a89d.xml new file mode 100644 index 000000000..d62b40b78 --- /dev/null +++ b/spec/fixtures/imports/scheme_locations/0bb3836b70b4dd9903263d5a764a5c45b964a89d.xml @@ -0,0 +1,23 @@ + + 0bb3836b70b4dd9903263d5a764a5c45b964a89d + Location 2 + 134 + NG19 8SW + false + false + 11 + 7 + 6 + 1 + 2 + W + False + P + M + + 2014-04-07 + + 6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d + Approved + 001 + diff --git a/spec/fixtures/imports/schemes/6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d.xml b/spec/fixtures/imports/schemes/6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d.xml new file mode 100644 index 000000000..5a7b23b5b --- /dev/null +++ b/spec/fixtures/imports/schemes/6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d.xml @@ -0,0 +1,9 @@ + + 6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d + Management Group + O + 456 + 7c5bd5fb549c09z2c55d9cb90d7ba84927e64618 + Approved + 123 + diff --git a/spec/fixtures/softwire_imports/users/10c887710550844e2551b3e0fb88dc9b4a8a642b.xml b/spec/fixtures/imports/users/10c887710550844e2551b3e0fb88dc9b4a8a642b.xml similarity index 100% rename from spec/fixtures/softwire_imports/users/10c887710550844e2551b3e0fb88dc9b4a8a642b.xml rename to spec/fixtures/imports/users/10c887710550844e2551b3e0fb88dc9b4a8a642b.xml diff --git a/spec/fixtures/softwire_imports/users/9ed81a262215a1634f0809effa683e38924d8bcb.xml b/spec/fixtures/imports/users/9ed81a262215a1634f0809effa683e38924d8bcb.xml similarity index 100% rename from spec/fixtures/softwire_imports/users/9ed81a262215a1634f0809effa683e38924d8bcb.xml rename to spec/fixtures/imports/users/9ed81a262215a1634f0809effa683e38924d8bcb.xml diff --git a/spec/fixtures/softwire_imports/users/b7829b1a5dfb68bb1e01c08445830c0add40907c.xml b/spec/fixtures/imports/users/b7829b1a5dfb68bb1e01c08445830c0add40907c.xml similarity index 100% rename from spec/fixtures/softwire_imports/users/b7829b1a5dfb68bb1e01c08445830c0add40907c.xml rename to spec/fixtures/imports/users/b7829b1a5dfb68bb1e01c08445830c0add40907c.xml diff --git a/spec/fixtures/softwire_imports/users/d4729b1a5dfb68bb1e01c08445830c0add40907c.xml b/spec/fixtures/imports/users/d4729b1a5dfb68bb1e01c08445830c0add40907c.xml similarity index 100% rename from spec/fixtures/softwire_imports/users/d4729b1a5dfb68bb1e01c08445830c0add40907c.xml rename to spec/fixtures/imports/users/d4729b1a5dfb68bb1e01c08445830c0add40907c.xml diff --git a/spec/fixtures/softwire_imports/users/d6717836154cd9a58f9e2f1d3077e3ab81e07613.xml b/spec/fixtures/imports/users/d6717836154cd9a58f9e2f1d3077e3ab81e07613.xml similarity index 100% rename from spec/fixtures/softwire_imports/users/d6717836154cd9a58f9e2f1d3077e3ab81e07613.xml rename to spec/fixtures/imports/users/d6717836154cd9a58f9e2f1d3077e3ab81e07613.xml diff --git a/spec/fixtures/softwire_imports/users/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml b/spec/fixtures/imports/users/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml similarity index 100% rename from spec/fixtures/softwire_imports/users/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml rename to spec/fixtures/imports/users/fc7625a02b24ae16162aa63ae7cb33feeec0c373.xml diff --git a/spec/lib/tasks/data_import_spec.rb b/spec/lib/tasks/data_import_spec.rb index fb3794414..1d49e6c70 100644 --- a/spec/lib/tasks/data_import_spec.rb +++ b/spec/lib/tasks/data_import_spec.rb @@ -22,7 +22,7 @@ describe "rake core:data_import", type: :task do context "when importing organisation data" do let(:type) { "organisation" } let(:import_service) { instance_double(Imports::OrganisationImportService) } - let(:fixture_path) { "spec/fixtures/softwire_imports/organisations" } + let(:fixture_path) { "spec/fixtures/imports/organisations" } before do allow(Imports::OrganisationImportService).to receive(:new).and_return(import_service) @@ -40,7 +40,7 @@ describe "rake core:data_import", type: :task do context "when importing user data" do let(:type) { "user" } let(:import_service) { instance_double(Imports::UserImportService) } - let(:fixture_path) { "spec/fixtures/softwire_imports/users" } + let(:fixture_path) { "spec/fixtures/imports/users" } before do allow(Imports::UserImportService).to receive(:new).and_return(import_service) @@ -58,7 +58,7 @@ describe "rake core:data_import", type: :task do context "when importing data protection confirmation data" do let(:type) { "data-protection-confirmation" } let(:import_service) { instance_double(Imports::DataProtectionConfirmationImportService) } - let(:fixture_path) { "spec/fixtures/softwire_imports/data_protection_confirmations" } + let(:fixture_path) { "spec/fixtures/imports/data_protection_confirmations" } before do allow(Imports::DataProtectionConfirmationImportService).to receive(:new).and_return(import_service) @@ -76,7 +76,7 @@ describe "rake core:data_import", type: :task do context "when importing organisation rent period data" do let(:type) { "organisation-rent-periods" } let(:import_service) { instance_double(Imports::OrganisationRentPeriodImportService) } - let(:fixture_path) { "spec/fixtures/softwire_imports/organisation_rent_periods" } + let(:fixture_path) { "spec/fixtures/imports/organisation_rent_periods" } before do allow(Imports::OrganisationRentPeriodImportService).to receive(:new).and_return(import_service) @@ -94,7 +94,7 @@ describe "rake core:data_import", type: :task do context "when importing case logs" do let(:type) { "case-logs" } let(:import_service) { instance_double(Imports::CaseLogsImportService) } - let(:fixture_path) { "spec/fixtures/softwire_imports/case_logs" } + let(:fixture_path) { "spec/fixtures/imports/case_logs" } before do allow(Imports::CaseLogsImportService).to receive(:new).and_return(import_service) @@ -109,6 +109,42 @@ describe "rake core:data_import", type: :task do end end + context "when importing scheme data" do + let(:type) { "scheme" } + let(:import_service) { instance_double(Imports::SchemeImportService) } + let(:fixture_path) { "spec/fixtures/imports/schemes" } + + before do + allow(Imports::SchemeImportService).to receive(:new).and_return(import_service) + end + + it "creates a scheme from the given XML file" do + expect(StorageService).to receive(:new).with(paas_config_service, instance_name) + expect(Imports::SchemeImportService).to receive(:new).with(storage_service) + expect(import_service).to receive(:create_schemes).with(fixture_path) + + task.invoke(type, fixture_path) + end + end + + context "when importing scheme location data" do + let(:type) { "scheme-location" } + let(:import_service) { instance_double(Imports::SchemeLocationImportService) } + let(:fixture_path) { "spec/fixtures/imports/organisations" } + + before do + allow(Imports::SchemeLocationImportService).to receive(:new).and_return(import_service) + end + + it "creates a scheme location from the given XML file" do + expect(StorageService).to receive(:new).with(paas_config_service, instance_name) + expect(Imports::SchemeLocationImportService).to receive(:new).with(storage_service) + expect(import_service).to receive(:create_scheme_locations).with(fixture_path) + + task.invoke(type, fixture_path) + end + end + it "raises an exception if no parameters are provided" do expect { task.invoke }.to raise_error(/Usage/) end diff --git a/spec/lib/tasks/date_import_field_spec.rb b/spec/lib/tasks/date_import_field_spec.rb index 8a11d6631..a5a74b2e8 100644 --- a/spec/lib/tasks/date_import_field_spec.rb +++ b/spec/lib/tasks/date_import_field_spec.rb @@ -21,7 +21,7 @@ describe "rake core:data_import_field", type: :task do context "when importing a case log field" do let(:import_service) { instance_double(Imports::CaseLogsFieldImportService) } - let(:fixture_path) { "spec/fixtures/softwire_imports/case_logs" } + let(:fixture_path) { "spec/fixtures/imports/case_logs" } before do allow(Imports::CaseLogsFieldImportService).to receive(:new).and_return(import_service) diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 8d1fce823..c67b4358a 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -90,7 +90,7 @@ RSpec.describe LocationsController, type: :request do context "when signed in as a data coordinator" do let(:user) { FactoryBot.create(:user, :data_coordinator) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } before do sign_in user @@ -108,13 +108,13 @@ RSpec.describe LocationsController, type: :request do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") expect(Location.last.postcode).to eq("ZZ11ZZ") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end context "when postcode is submitted with lower case" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } it "creates a new location for scheme with postcode " do expect(Location.last.postcode).to eq("ZZ11ZZ") @@ -123,7 +123,7 @@ RSpec.describe LocationsController, type: :request do context "when trying to add location to a scheme that belongs to another organisation" do let(:another_scheme) { FactoryBot.create(:scheme) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } it "displays the new page with an error message" do post "/schemes/#{another_scheme.id}/locations", params: params @@ -132,7 +132,7 @@ RSpec.describe LocationsController, type: :request do end context "when required postcode param is missing" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_entity) @@ -141,7 +141,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as yes" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } it "creates a new location for scheme with valid params and redirects to correct page" do expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) @@ -153,14 +153,14 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is selected as no" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } it "creates a new location for scheme with valid params and redirects to correct page" do expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) @@ -172,14 +172,14 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is not selected" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } it "creates a new location for scheme with valid params and redirects to correct page" do expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) @@ -191,7 +191,7 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end @@ -201,7 +201,7 @@ RSpec.describe LocationsController, type: :request do context "when signed in as a support user" do let(:user) { FactoryBot.create(:user, :support) } let!(:scheme) { FactoryBot.create(:scheme) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) @@ -219,13 +219,13 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.name).to eq("Test") expect(Location.last.postcode).to eq("ZZ11ZZ") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end context "when postcode is submitted with lower case" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz" } } } it "creates a new location for scheme with postcode " do expect(Location.last.postcode).to eq("ZZ11ZZ") @@ -233,7 +233,7 @@ RSpec.describe LocationsController, type: :request do end context "when required postcode param is missing" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No" } } } it "displays the new page with an error message" do post "/schemes/#{scheme.id}/locations", params: params @@ -243,7 +243,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as yes" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ" } } } it "creates a new location for scheme with valid params and redirects to correct page" do expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) @@ -254,14 +254,14 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is selected as no" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ" } } } it "creates a new location for scheme with valid params and redirects to correct page" do expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) @@ -272,14 +272,14 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is not selected" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ" } } } it "creates a new location for scheme with valid params and redirects to correct page" do expect { post "/schemes/#{scheme.id}/locations", params: }.to change(Location, :count).by(1) @@ -290,7 +290,7 @@ RSpec.describe LocationsController, type: :request do it "creates a new location for scheme with valid params" do expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end @@ -390,7 +390,7 @@ RSpec.describe LocationsController, type: :request do let(:user) { FactoryBot.create(:user, :data_coordinator) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } before do sign_in user @@ -407,7 +407,7 @@ RSpec.describe LocationsController, type: :request do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") expect(Location.last.postcode).to eq("ZZ11ZZ") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end @@ -427,7 +427,7 @@ RSpec.describe LocationsController, type: :request do end context "when postcode is submitted with lower case" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz", page: "edit" } } } it "updates existing location for scheme with postcode " do expect(Location.last.postcode).to eq("ZZ11ZZ") @@ -437,7 +437,7 @@ RSpec.describe LocationsController, type: :request do context "when trying to update location for a scheme that belongs to another organisation" do let(:another_scheme) { FactoryBot.create(:scheme) } let(:another_location) { FactoryBot.create(:location) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "displays the new page with an error message" do patch "/schemes/#{another_scheme.id}/locations/#{another_location.id}", params: params @@ -446,7 +446,7 @@ RSpec.describe LocationsController, type: :request do end context "when required postcode param is invalid" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid", page: "edit" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_entity) @@ -455,7 +455,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as yes" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates existing location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -466,14 +466,14 @@ RSpec.describe LocationsController, type: :request do it "updates existing location for scheme with valid params" do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is selected as no" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates existing location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -484,14 +484,14 @@ RSpec.describe LocationsController, type: :request do it "updates existing location for scheme with valid params" do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is not selected" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates existing location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -502,7 +502,7 @@ RSpec.describe LocationsController, type: :request do it "updates existing location for scheme with valid params" do expect(Location.last.scheme.owning_organisation_id).to eq(user.organisation_id) expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end @@ -513,7 +513,7 @@ RSpec.describe LocationsController, type: :request do let(:user) { FactoryBot.create(:user, :data_coordinator) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } before do allow(user).to receive(:need_two_factor_authentication?).and_return(false) @@ -530,7 +530,7 @@ RSpec.describe LocationsController, type: :request do it "updates existing location for scheme with valid params" do expect(Location.last.name).to eq("Test") expect(Location.last.postcode).to eq("ZZ11ZZ") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end @@ -550,7 +550,7 @@ RSpec.describe LocationsController, type: :request do end context "when postcode is submitted with lower case" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "zz1 1zz", page: "edit" } } } it "updates a location for scheme with postcode " do expect(Location.last.postcode).to eq("ZZ11ZZ") @@ -558,7 +558,7 @@ RSpec.describe LocationsController, type: :request do end context "when required postcode param is missing" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "invalid", page: "edit" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_entity) @@ -567,7 +567,7 @@ RSpec.describe LocationsController, type: :request do end context "when do you want to add another location is selected as yes" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "Yes", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -577,14 +577,14 @@ RSpec.describe LocationsController, type: :request do it "updates existing location for scheme with valid params" do expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is selected as no" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", add_another_location: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates a location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -594,14 +594,14 @@ RSpec.describe LocationsController, type: :request do it "updates existing location for scheme with valid params" do expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end end context "when do you want to add another location is not selected" do - let(:params) { { location: { name: "Test", total_units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } + let(:params) { { location: { name: "Test", units: "5", type_of_unit: "Bungalow", wheelchair_adaptation: "No", postcode: "ZZ1 1ZZ", page: "edit" } } } it "updates a location for scheme with valid params and redirects to correct page" do follow_redirect! @@ -611,7 +611,7 @@ RSpec.describe LocationsController, type: :request do it "updates a location for scheme with valid params" do expect(Location.last.name).to eq("Test") - expect(Location.last.total_units).to eq(5) + expect(Location.last.units).to eq(5) expect(Location.last.type_of_unit).to eq("Bungalow") expect(Location.last.wheelchair_adaptation).to eq("No") end diff --git a/spec/services/imports/case_logs_field_import_service_spec.rb b/spec/services/imports/case_logs_field_import_service_spec.rb index 4b1084a25..22c1371b0 100644 --- a/spec/services/imports/case_logs_field_import_service_spec.rb +++ b/spec/services/imports/case_logs_field_import_service_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Imports::CaseLogsFieldImportService do let(:logger) { instance_double(ActiveSupport::Logger) } let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } - let(:fixture_directory) { "spec/fixtures/softwire_imports/case_logs" } + let(:fixture_directory) { "spec/fixtures/imports/case_logs" } let(:case_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } let(:case_log_file) { open_file(fixture_directory, case_log_id) } diff --git a/spec/services/imports/case_logs_import_service_spec.rb b/spec/services/imports/case_logs_import_service_spec.rb index 9d86772f6..e44d15f31 100644 --- a/spec/services/imports/case_logs_import_service_spec.rb +++ b/spec/services/imports/case_logs_import_service_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Imports::CaseLogsImportService do let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") } - let(:fixture_directory) { "spec/fixtures/softwire_imports/case_logs" } + let(:fixture_directory) { "spec/fixtures/imports/case_logs" } let(:organisation) { FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") } def open_file(directory, filename) diff --git a/spec/services/imports/data_protection_confirmation_import_service_spec.rb b/spec/services/imports/data_protection_confirmation_import_service_spec.rb index 12f260618..e14ce6b1a 100644 --- a/spec/services/imports/data_protection_confirmation_import_service_spec.rb +++ b/spec/services/imports/data_protection_confirmation_import_service_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe Imports::DataProtectionConfirmationImportService do - let(:fixture_directory) { "spec/fixtures/softwire_imports/data_protection_confirmations" } + let(:fixture_directory) { "spec/fixtures/imports/data_protection_confirmations" } let(:old_org_id) { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" } let(:old_id) { old_org_id } let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") } diff --git a/spec/services/imports/organisation_import_service_spec.rb b/spec/services/imports/organisation_import_service_spec.rb index c2f23e199..b91b566ef 100644 --- a/spec/services/imports/organisation_import_service_spec.rb +++ b/spec/services/imports/organisation_import_service_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Imports::OrganisationImportService do let(:logger) { instance_double(Rails::Rack::Logger) } let(:folder_name) { "organisations" } let(:filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] } - let(:fixture_directory) { "spec/fixtures/softwire_imports/organisations" } + let(:fixture_directory) { "spec/fixtures/imports/organisations" } def create_organisation_file(fixture_directory, visible_id, name = nil) file = File.open("#{fixture_directory}/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml") diff --git a/spec/services/imports/organisation_rent_period_import_service_spec.rb b/spec/services/imports/organisation_rent_period_import_service_spec.rb index 2921e5a4e..e15d10a22 100644 --- a/spec/services/imports/organisation_rent_period_import_service_spec.rb +++ b/spec/services/imports/organisation_rent_period_import_service_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe Imports::OrganisationRentPeriodImportService do - let(:fixture_directory) { "spec/fixtures/softwire_imports/organisation_rent_periods" } + let(:fixture_directory) { "spec/fixtures/imports/organisation_rent_periods" } let(:old_org_id) { "44026acc7ed5c29516b26f2a5deb639e5e37966d" } let(:old_id) { "ebd22326d33e389e9f1bfd546979d2c05f9e68d6" } let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") } diff --git a/spec/services/imports/scheme_import_service_spec.rb b/spec/services/imports/scheme_import_service_spec.rb new file mode 100644 index 000000000..3cfb76c96 --- /dev/null +++ b/spec/services/imports/scheme_import_service_spec.rb @@ -0,0 +1,64 @@ +require "rails_helper" + +RSpec.describe Imports::SchemeImportService do + subject(:scheme_service) { described_class.new(storage_service, logger) } + + let(:storage_service) { instance_double(StorageService) } + let(:logger) { instance_double(ActiveSupport::Logger) } + + let(:fixture_directory) { "spec/fixtures/imports/schemes" } + let(:scheme_id) { "6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d" } + + let!(:owning_org) { FactoryBot.create(:organisation, old_org_id: "7c5bd5fb549c09z2c55d9cb90d7ba84927e64618") } + let!(:managing_org) { FactoryBot.create(:organisation, old_visible_id: 456) } + + def open_file(directory, filename) + File.open("#{directory}/#{filename}.xml") + end + + context "when importing schemes" do + let(:remote_folder) { "mgmtgroups" } + + before do + # Stub the S3 file listing and download + allow(storage_service).to receive(:list_files) + .and_return(%W[#{remote_folder}/#{scheme_id}.xml]) + allow(storage_service).to receive(:get_file_io) + .with("#{remote_folder}/#{scheme_id}.xml") + .and_return(open_file(fixture_directory, scheme_id)) + end + + it "successfully create all schemes" do + expect(logger).not_to receive(:error) + expect(logger).not_to receive(:warn) + expect(logger).not_to receive(:info) + expect { scheme_service.create_schemes(remote_folder) } + .to change(Scheme, :count).by(1) + end + end + + context "when importing a specific scheme" do + let(:scheme_file) { open_file(fixture_directory, scheme_id) } + let(:scheme_xml) { Nokogiri::XML(scheme_file) } + + it "matches expected values" do + scheme = scheme_service.create_scheme(scheme_xml) + expect(scheme.owning_organisation).to eq(owning_org) + expect(scheme.managing_organisation).to eq(managing_org) + expect(scheme.old_id).to eq("6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d") + expect(scheme.old_visible_id).to eq(123) + expect(scheme.service_name).to eq("Management Group") + expect(scheme.arrangement_type).to eq("O") + end + + context "and the scheme status is not approved" do + before { scheme_xml.at_xpath("//mgmtgroup:status").content = "Temporary" } + + it "does not create the scheme" do + expect(logger).to receive(:warn).with("Scheme with legacy ID 6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d is not approved (Temporary), skipping") + expect { scheme_service.create_scheme(scheme_xml) } + .not_to change(Scheme, :count) + 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 new file mode 100644 index 000000000..f87fc3724 --- /dev/null +++ b/spec/services/imports/scheme_location_import_service_spec.rb @@ -0,0 +1,180 @@ +require "rails_helper" + +RSpec.describe Imports::SchemeLocationImportService do + subject(:location_service) { described_class.new(storage_service, logger) } + + let(:storage_service) { instance_double(StorageService) } + let(:logger) { instance_double(ActiveSupport::Logger) } + + let(:fixture_directory) { "spec/fixtures/imports/scheme_locations" } + let(:first_location_id) { "0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e" } + let(:second_location_id) { "0bb3836b70b4dd9903263d5a764a5c45b964a89d" } + + let!(:scheme) { FactoryBot.create(:scheme, service_name: "Management Group", old_id: "6d6d7618b58affe2a150a5ef2e9f4765fa6cd05d") } + + def open_file(directory, filename) + File.open("#{directory}/#{filename}.xml") + end + + context "when importing scheme locations" do + let(:remote_folder) { "schemes" } + + before do + # Stub the S3 file listing and download + allow(storage_service).to receive(:list_files) + .and_return(%W[#{remote_folder}/#{first_location_id}.xml #{remote_folder}/#{second_location_id}.xml]) + allow(storage_service).to receive(:get_file_io) + .with("#{remote_folder}/#{first_location_id}.xml") + .and_return(open_file(fixture_directory, first_location_id)) + allow(storage_service).to receive(:get_file_io) + .with("#{remote_folder}/#{second_location_id}.xml") + .and_return(open_file(fixture_directory, second_location_id)) + end + + it "successfully create all scheme locations" do + expect(logger).not_to receive(:error) + expect(logger).not_to receive(:warn) + expect(logger).not_to receive(:info) + expect { location_service.create_scheme_locations(remote_folder) } + .to change(Location, :count).by(2) + .and(change(Scheme, :count).by(0)) + end + end + + context "when importing different scheme locations" do + let(:location_xml_1) { Nokogiri::XML(open_file(fixture_directory, first_location_id)) } + let(:location_xml_2) { Nokogiri::XML(open_file(fixture_directory, second_location_id)) } + + before { location_service.create_scheme_location(location_xml_1) } + + context "and the scheme type is different" do + before { location_xml_2.at_xpath("//scheme:scheme-type").content = "5" } + + it "renames the location scheme name" do + location = location_service.create_scheme_location(location_xml_2) + old_scheme = Scheme.find(scheme.id) + new_scheme = location.scheme + + expect(old_scheme.service_name).to eq("Management Group - Housing for older people") + expect(new_scheme.service_name).to eq("Management Group - Direct Access Hostel") + end + end + + context "and the registered under care act is different" do + before { location_xml_2.at_xpath("//scheme:reg-home-type").content = "2" } + + it "renames both scheme names" do + location = location_service.create_scheme_location(location_xml_2) + old_scheme = Scheme.find(scheme.id) + new_scheme = location.scheme + + expect(old_scheme.service_name).to eq("Management Group") + expect(new_scheme.service_name).to eq("Management Group - (Part-registered care home)") + end + end + + context "and the support type is different" do + before { location_xml_2.at_xpath("//scheme:support-type").content = "3" } + + it "renames both scheme names" do + location = location_service.create_scheme_location(location_xml_2) + old_scheme = Scheme.find(scheme.id) + new_scheme = location.scheme + + expect(old_scheme.service_name).to eq("Management Group - Low levels of support") + expect(new_scheme.service_name).to eq("Management Group - Medium levels of support") + end + end + + context "and the intended stay is different" do + before { location_xml_2.at_xpath("//scheme:intended-stay").content = "S" } + + it "renames both scheme names" do + location = location_service.create_scheme_location(location_xml_2) + old_scheme = Scheme.find(scheme.id) + new_scheme = location.scheme + + expect(old_scheme.service_name).to eq("Management Group - Permanent") + expect(new_scheme.service_name).to eq("Management Group - Short stay") + end + end + + context "and the primary client group is different" do + before { location_xml_2.at_xpath("//scheme:client-group-1").content = "F" } + + it "renames both scheme names" do + location = location_service.create_scheme_location(location_xml_2) + old_scheme = Scheme.find(scheme.id) + new_scheme = location.scheme + + expect(old_scheme.service_name).to eq("Management Group - Older people with support needs") + expect(new_scheme.service_name).to eq("Management Group - People with drug problems") + end + end + + context "and the secondary client group is different" do + before { location_xml_2.at_xpath("//scheme:client-group-2").content = "S" } + + it "renames both scheme names" do + location = location_service.create_scheme_location(location_xml_2) + old_scheme = Scheme.find(scheme.id) + new_scheme = location.scheme + + expect(old_scheme.service_name).to eq("Management Group") + expect(new_scheme.service_name).to eq("Management Group - Rough sleepers") + end + end + end + + context "when importing a specific scheme location" do + let(:location_xml) { Nokogiri::XML(open_file(fixture_directory, first_location_id)) } + + it "matches expected location values" do + location = location_service.create_scheme_location(location_xml) + expect(location.name).to eq("Location 1") + expect(location.postcode).to eq("S44 6EJ") + expect(location.units).to eq(5) + 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) + expect(location.scheme).to eq(scheme) + end + + it "matches expected schemes values" do + location = location_service.create_scheme_location(location_xml) + expect(location.scheme.scheme_type).to eq("Housing for older people") + expect(location.scheme.registered_under_care_act).to eq("No") + expect(location.scheme.support_type).to eq("Low levels of support") + expect(location.scheme.intended_stay).to eq("Permanent") + 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.sensitive).to eq("No") + expect(location.scheme.end_date).to eq("2050-12-31") + end + + context "and the end date is before the current date" do + before do + Timecop.freeze(2022, 6, 1) + location_xml.at_xpath("//scheme:end-date").content = "2022-05-01" + end + + after { Timecop.unfreeze } + + it "does not create the location" do + expect(logger).to receive(:warn).with("Location with legacy ID 0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e is expired (2022-05-01 00:00:00 +0100), skipping") + expect { location_service.create_scheme_location(location_xml) } + .not_to change(Location, :count) + end + end + + context "and we import the same location twice" do + before { location_service.create_scheme_location(location_xml) } + + it "does not create the location" do + expect(logger).to receive(:warn).with("Location is already present with legacy ID 0ae7ad6dc0f1cf7ef33c18cc8c108bebc1b4923e, skipping") + expect { location_service.create_scheme_location(location_xml) } + .not_to change(Location, :count) + end + end + end +end diff --git a/spec/services/imports/user_import_service_spec.rb b/spec/services/imports/user_import_service_spec.rb index 305613545..02a776ecc 100644 --- a/spec/services/imports/user_import_service_spec.rb +++ b/spec/services/imports/user_import_service_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe Imports::UserImportService do - let(:fixture_directory) { "spec/fixtures/softwire_imports/users" } + let(:fixture_directory) { "spec/fixtures/imports/users" } let(:old_user_id) { "fc7625a02b24ae16162aa63ae7cb33feeec0c373" } let(:old_org_id) { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" } let(:user_file) { File.open("#{fixture_directory}/#{old_user_id}.xml") }