diff --git a/db/migrate/20220114105351_additional_org_fields.rb b/db/migrate/20220114105351_additional_org_fields.rb new file mode 100644 index 000000000..3726a718b --- /dev/null +++ b/db/migrate/20220114105351_additional_org_fields.rb @@ -0,0 +1,45 @@ +class AdditionalOrgFields < ActiveRecord::Migration[7.0] + def up + change_table :organisations, bulk: true do |t| + t.column :active, :boolean + t.column :old_association_type, :integer + t.column :software_supplier_id, :string + t.column :housing_management_system, :string + t.column :choice_based_lettings, :boolean + t.column :common_housing_register, :boolean + t.column :choice_allocation_policy, :boolean + t.column :cbl_proportion_percentage, :integer + t.column :enter_affordable_logs, :boolean + t.column :owns_affordable_logs, :boolean + t.column :housing_registration_no, :string + t.column :general_needs_units, :integer + t.column :supported_housing_units, :integer + t.column :unspecified_units, :integer + t.column :old_org_id, :string + t.column :old_visible_id, :integer + t.change :phone, :string + end + end + + def down + change_table :organisations, bulk: true do |t| + t.remove :active + t.remove :old_association_type + t.remove :software_supplier_id + t.remove :housing_management_system + t.remove :choice_based_lettings + t.remove :common_housing_register + t.remove :choice_allocation_policy + t.remove :cbl_proportion_percentage + t.remove :enter_affordable_logs + t.remove :owns_affordable_logs + t.remove :housing_registration_no + t.remove :general_needs_units + t.remove :supported_housing_units + t.remove :unspecified_units + t.remove :old_org_id + t.remove :old_visible_id + t.change :phone, "integer USING phone::integer" + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0f352d747..3c667297a 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.define(version: 2022_01_12_151048) do +ActiveRecord::Schema.define(version: 2022_01_14_105351) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,7 +191,7 @@ ActiveRecord::Schema.define(version: 2022_01_12_151048) do create_table "organisations", force: :cascade do |t| t.string "name" - t.integer "phone" + t.string "phone" t.integer "providertype" t.string "address_line1" t.string "address_line2" @@ -202,6 +202,22 @@ ActiveRecord::Schema.define(version: 2022_01_12_151048) do t.string "managing_agents" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.boolean "active" + t.integer "old_association_type" + t.string "software_supplier_id" + t.string "housing_management_system" + t.boolean "choice_based_lettings" + t.boolean "common_housing_register" + t.boolean "choice_allocation_policy" + t.integer "cbl_proportion_percentage" + t.boolean "enter_affordable_logs" + t.boolean "owns_affordable_logs" + t.string "housing_registration_no" + t.integer "general_needs_units" + t.integer "supported_housing_units" + t.integer "unspecified_units" + t.string "old_org_id" + t.integer "old_visible_id" end create_table "users", force: :cascade do |t| diff --git a/lib/tasks/data_import/organisations.rake b/lib/tasks/data_import/organisations.rake new file mode 100644 index 000000000..5caf1ecef --- /dev/null +++ b/lib/tasks/data_import/organisations.rake @@ -0,0 +1,45 @@ +require "nokogiri" + +namespace :data_import do + desc "Import Organisation XMLs from Softwire system" + + # rake data_import:organisations['path/to/xml_files'] + task :organisations, %i[path] => :environment do |_task, args| + directory = args.path + Dir.glob("#{directory}/*.xml").each do |file| + doc = Nokogiri::XML(File.open(file)) + Organisation.create!( + name: field_value(doc, "name"), + providertype: field_value(doc, "institution-type"), + phone: field_value(doc, "telephone-number"), + holds_own_stock: to_boolean(field_value(doc, "holds-stock")), + active: to_boolean(field_value(doc, "active")), + old_association_type: field_value(doc, "old-association-type"), + software_supplier_id: field_value(doc, "software-supplier-id"), + housing_management_system: field_value(doc, "housing-management-system"), + choice_based_lettings: to_boolean(field_value(doc, "choice-based-lettings")), + common_housing_register: to_boolean(field_value(doc, "common-housing-register")), + choice_allocation_policy: to_boolean(field_value(doc, "choice-allocation-policy")), + cbl_proportion_percentage: field_value(doc, "cbl-proportion-percentage"), + enter_affordable_logs: to_boolean(field_value(doc, "enter-affordable-logs")), + owns_affordable_logs: to_boolean(field_value(doc, "owns-affordable-rent")), + housing_registration_no: field_value(doc, "housing-registration-no"), + general_needs_units: field_value(doc, "general-needs-units"), + supported_housing_units: field_value(doc, "supported-housing-units"), + unspecified_units: field_value(doc, "unspecified-units"), + old_org_id: field_value(doc, "id"), + old_visible_id: field_value(doc, "visible-id"), + ) + end + end +end + +private + +def field_value(doc, field) + doc.at_xpath("//institution:#{field}")&.text +end + +def to_boolean(input_string) + input_string == "true" +end diff --git a/spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml b/spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml new file mode 100644 index 000000000..f43ad86f1 --- /dev/null +++ b/spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml @@ -0,0 +1,23 @@ + + 7c5bd5fb549c09z2c55d9cb90d7ba84927e64618 + HA Ltd + HOUSING-ASSOCIATION + true + false + CHHA + xxxxxxxx + false + + false + false + false + + true + true + LH9999 + 1034 + true + 1104 + 217 + 0 + diff --git a/spec/lib/tasks/data_import/organisations_spec.rb b/spec/lib/tasks/data_import/organisations_spec.rb new file mode 100644 index 000000000..d20274ba1 --- /dev/null +++ b/spec/lib/tasks/data_import/organisations_spec.rb @@ -0,0 +1,18 @@ +require "rails_helper" +require "rake" + +describe "rake data_import:organisations", type: :task do + subject(:task) { Rake::Task["data_import:organisations"] } + let(:fixture_path) { "spec/fixtures/softwire_imports/organisations" } + + before do + Rake.application.rake_require("tasks/data_import/organisations") + Rake::Task.define_task(:environment) + task.reenable + end + + it "creates an organisation from the given XML file" do + expect { task.invoke(fixture_path) }.to change(Organisation, :count).by(1) + expect(Organisation.last.old_visible_id).to eq(1034) + end +end