diff --git a/app/models/data_protection_confirmation.rb b/app/models/data_protection_confirmation.rb new file mode 100644 index 000000000..b73004766 --- /dev/null +++ b/app/models/data_protection_confirmation.rb @@ -0,0 +1,4 @@ +class DataProtectionConfirmation < ApplicationRecord + belongs_to :organisation + belongs_to :data_protection_officer, class_name: "User" +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index b13a9d075..dd14006ef 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -2,6 +2,7 @@ class Organisation < ApplicationRecord has_many :users has_many :owned_case_logs, class_name: "CaseLog", foreign_key: "owning_organisation_id" has_many :managed_case_logs, class_name: "CaseLog", foreign_key: "managing_organisation_id" + has_many :data_protection_confirmations has_paper_trail diff --git a/db/migrate/20220323094418_create_data_protection_confirmation.rb b/db/migrate/20220323094418_create_data_protection_confirmation.rb new file mode 100644 index 000000000..d4c6a9de9 --- /dev/null +++ b/db/migrate/20220323094418_create_data_protection_confirmation.rb @@ -0,0 +1,11 @@ +class CreateDataProtectionConfirmation < ActiveRecord::Migration[7.0] + def change + create_table :data_protection_confirmations do |t| + t.belongs_to :organisation + t.belongs_to :data_protection_officer, class_name: "User", index: { name: :dpo_user_id } + t.column :confirmed, :boolean + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e6782acfb..d90d7d661 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -233,6 +233,19 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" end + create_table "data_protection_confirmations", force: :cascade do |t| + t.bigint "organisation_id" + t.bigint "data_protection_officer_id" + t.boolean "confirmed" + t.string "old_id" + t.string "old_org_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["data_protection_officer_id"], name: "dpo_user_id" + t.index ["organisation_id", "data_protection_officer_id", "confirmed"], name: "data_protection_confirmations_unique", unique: true + t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id" + end + create_table "la_rent_ranges", force: :cascade do |t| t.integer "ranges_rent_id" t.integer "lettype" diff --git a/spec/fixtures/softwire_imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml b/spec/fixtures/softwire_imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml new file mode 100644 index 000000000..730d93b97 --- /dev/null +++ b/spec/fixtures/softwire_imports/data_protection_confirmations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml @@ -0,0 +1,7 @@ + + 7c5bd5fb549c09a2c55d7cb90d7ba84927e64618 + 7c5bd5fb549c09a2c55d7cb90d7ba84927e64618 + true + John Doe + 05/06/2018 10:36:49:34 + diff --git a/spec/services/imports/data_protection_confirmation_service_spec.rb b/spec/services/imports/data_protection_confirmation_service_spec.rb new file mode 100644 index 000000000..2fbca5144 --- /dev/null +++ b/spec/services/imports/data_protection_confirmation_service_spec.rb @@ -0,0 +1,48 @@ +require "rails_helper" + +RSpec.describe Imports::UserImportService do + let(:fixture_directory) { "spec/fixtures/softwire_imports/users" } + let(:old_org_id) { "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618" } + let(:old_data_protection_confirmation_id) { old_org_id } + let(:import_file) { File.open("#{fixture_directory}/#{old_data_protection_confirmation_id}.xml") } + let(:storage_service) { instance_double(StorageService) } + + context "when importing users" do + subject(:import_service) { described_class.new(storage_service) } + + before do + allow(storage_service) + .to receive(:list_files) + .and_return(["data_protection_directory/#{old_data_protection_confirmation_id}.xml"]) + allow(storage_service) + .to receive(:get_file_io) + .with("user_directory/#{old_data_protection_confirmation_id}.xml") + .and_return(import_file) + end + + it "successfully create a data protection confirmation record with the expected data" do + FactoryBot.create(:organisation, old_org_id:) + import_service.create_data_protection_confirmations("data_protection_directory") + + confirmation = Organisation.find_by(old_user_id:).data_protection_confirmations.last + expect(confirmation.user.name).to eq("John Doe") + expect(confirmation.confirmed).to be_truthy + end + + it "refuses to create a data protection confirmation belonging to a non existing organisation" do + expect { import_service.create_data_protection_confirmations("data_protection_directory") } + .to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/) + end + + context "when the data protection record has already been imported previously" do + before do + org = FactoryBot.create(:organisation, old_org_id:) + end + + it "logs that the user already exists" do + expect(Rails.logger).to receive(:warn) + import_service.create_data_protection_confirmations("data_protection_directory") + end + end + end +end