Browse Source

* Introduce created_by field

* Handles deactivated/deleted users at import
* Links case logs created_by with user
pull/528/head
Stéphane Meny 3 years ago
parent
commit
1443e2dc0e
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 3
      app/models/case_log.rb
  2. 5
      app/services/imports/case_logs_import_service.rb
  3. 8
      app/services/imports/user_import_service.rb
  4. 7
      db/migrate/20220427160536_add_created_by_to_case_logs.rb
  5. 4
      db/schema.rb
  6. 2
      spec/controllers/admin/case_logs_controller_spec.rb
  7. 1
      spec/factories/case_log.rb
  8. 1
      spec/services/imports/user_import_service_spec.rb

3
app/models/case_log.rb

@ -32,6 +32,7 @@ class CaseLog < ApplicationRecord
belongs_to :owning_organisation, class_name: "Organisation" belongs_to :owning_organisation, class_name: "Organisation"
belongs_to :managing_organisation, class_name: "Organisation" belongs_to :managing_organisation, class_name: "Organisation"
belongs_to :created_by, class_name: "User"
scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) }
scope :filter_by_status, ->(status, _user = nil) { where status: } scope :filter_by_status, ->(status, _user = nil) { where status: }
@ -45,7 +46,7 @@ class CaseLog < ApplicationRecord
scope :filter_by_user, lambda { |selected_user, user| scope :filter_by_user, lambda { |selected_user, user|
if !selected_user.include?("all") && user.present? if !selected_user.include?("all") && user.present?
where(id: PaperTrail::Version.where(item_type: "CaseLog", event: "create", whodunnit: user.to_global_id.uri.to_s).map(&:item_id)) where(created_by: user)
end end
} }

5
app/services/imports/case_logs_import_service.rb

@ -172,6 +172,11 @@ module Imports
previous_status = field_value(xml_doc, "meta", "status") previous_status = field_value(xml_doc, "meta", "status")
owner_id = field_value(xml_doc, "meta", "owner-user-id")
if owner_id.present?
attributes["created_by"] = User.find_by(old_id: owner_id)
end
case_log = CaseLog.new(attributes) case_log = CaseLog.new(attributes)
save_case_log(case_log, attributes) save_case_log(case_log, attributes)
compute_differences(case_log, attributes) compute_differences(case_log, attributes)

8
app/services/imports/user_import_service.rb

@ -14,13 +14,19 @@ module Imports
organisation = Organisation.find_by(old_org_id: user_field_value(xml_document, "institution")) organisation = Organisation.find_by(old_org_id: user_field_value(xml_document, "institution"))
old_user_id = user_field_value(xml_document, "id") old_user_id = user_field_value(xml_document, "id")
name = user_field_value(xml_document, "full-name") name = user_field_value(xml_document, "full-name")
email = user_field_value(xml_document, "email") email = user_field_value(xml_document, "email").downcase.strip
deleted = user_field_value(xml_document, "deleted")
date_deactivated = user_field_value(xml_document, "date-deactivated")
if User.find_by(old_user_id:, organisation:) if User.find_by(old_user_id:, organisation:)
@logger.warn("User #{name} with old user id #{old_user_id} is already present, skipping.") @logger.warn("User #{name} with old user id #{old_user_id} is already present, skipping.")
elsif deleted == "true" || date_deactivated.present?
@logger.warn("User #{name} with old user id #{old_user_id} is deleted or deactivated, skipping.")
elsif (user = User.find_by(email:, organisation:)) elsif (user = User.find_by(email:, organisation:))
is_dpo = user.is_data_protection_officer? || is_dpo?(user_field_value(xml_document, "user-type")) is_dpo = user.is_data_protection_officer? || is_dpo?(user_field_value(xml_document, "user-type"))
role = highest_role(user.role, role(user_field_value(xml_document, "user-type"))) role = highest_role(user.role, role(user_field_value(xml_document, "user-type")))
user.update!(role:, is_dpo:) user.update!(role:, is_dpo:)
@logger.info("Found duplicated email, updating user #{user.id} with role #{role} and is_dpo #{is_dpo}")
else else
User.create!( User.create!(
email:, email:,

7
db/migrate/20220427160536_add_created_by_to_case_logs.rb

@ -0,0 +1,7 @@
class AddCreatedByToCaseLogs < ActiveRecord::Migration[7.0]
def change
change_table :case_logs, bulk: true do |t|
t.belongs_to :created_by, class_name: "User"
end
end
end

4
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_04_26_122618) do ActiveRecord::Schema[7.0].define(version: 2022_04_27_160536) 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"
@ -220,6 +220,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_26_122618) do
t.integer "irproduct" t.integer "irproduct"
t.string "old_id" t.string "old_id"
t.integer "joint" t.integer "joint"
t.bigint "created_by_id"
t.index ["created_by_id"], name: "index_case_logs_on_created_by_id"
t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["old_id"], name: "index_case_logs_on_old_id", unique: true t.index ["old_id"], name: "index_case_logs_on_old_id", unique: true
t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id"

2
spec/controllers/admin/case_logs_controller_spec.rb

@ -11,6 +11,7 @@ describe Admin::CaseLogsController, type: :controller do
let(:resource_title) { "Logs" } let(:resource_title) { "Logs" }
let(:valid_session) { {} } let(:valid_session) { {} }
let(:admin_user) { FactoryBot.create(:admin_user) } let(:admin_user) { FactoryBot.create(:admin_user) }
let(:user) { FactoryBot.create(:user) }
describe "Get case logs" do describe "Get case logs" do
let!(:case_log) { FactoryBot.create(:case_log, :in_progress) } let!(:case_log) { FactoryBot.create(:case_log, :in_progress) }
@ -35,6 +36,7 @@ describe Admin::CaseLogsController, type: :controller do
"case_log": { "case_log": {
"owning_organisation_id": owning_organisation.id, "owning_organisation_id": owning_organisation.id,
"managing_organisation_id": managing_organisation.id, "managing_organisation_id": managing_organisation.id,
"created_by": user,
}, },
} }
end end

1
spec/factories/case_log.rb

@ -2,6 +2,7 @@ FactoryBot.define do
factory :case_log do factory :case_log do
owning_organisation { FactoryBot.create(:organisation) } owning_organisation { FactoryBot.create(:organisation) }
managing_organisation { FactoryBot.create(:organisation) } managing_organisation { FactoryBot.create(:organisation) }
created_by { FactoryBot.create(:user) }
trait :about_completed do trait :about_completed do
renewal { 0 } renewal { 0 }
needstype { 1 } needstype { 1 }

1
spec/services/imports/user_import_service_spec.rb

@ -17,6 +17,7 @@ RSpec.describe Imports::UserImportService do
allow(storage_service).to receive(:get_file_io) allow(storage_service).to receive(:get_file_io)
.with("user_directory/#{old_user_id}.xml") .with("user_directory/#{old_user_id}.xml")
.and_return(user_file) .and_return(user_file)
allow(logger).to receive(:info)
end end
it "successfully create a user with the expected data" do it "successfully create a user with the expected data" do

Loading…
Cancel
Save