Browse Source

CLDC-3897 Re-export merged users (#2969)

* Update values_updated_at for merged users

* Export users when they have been manually updated
pull/2997/head
kosiakkatrina 1 week ago committed by GitHub
parent
commit
5ac1291c65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      app/services/exports/user_export_service.rb
  2. 2
      app/services/merge/merge_organisations_service.rb
  3. 5
      db/migrate/20250305092900_add_values_updated_at_to_user.rb
  4. 3
      db/schema.rb
  5. 21
      spec/services/exports/user_export_service_spec.rb
  6. 2
      spec/services/merge/merge_organisations_service_spec.rb

2
app/services/exports/user_export_service.rb

@ -28,7 +28,7 @@ module Exports
def retrieve_resources(recent_export, full_update, _year) def retrieve_resources(recent_export, full_update, _year)
if !full_update && recent_export if !full_update && recent_export
params = { from: recent_export.started_at, to: @start_time } params = { from: recent_export.started_at, to: @start_time }
User.where("(updated_at >= :from AND updated_at <= :to)", params) User.where("(updated_at >= :from AND updated_at <= :to) OR (values_updated_at IS NOT NULL AND values_updated_at >= :from AND values_updated_at <= :to)", params)
else else
params = { to: @start_time } params = { to: @start_time }
User.where("updated_at <= :to", params) User.where("updated_at <= :to", params)

2
app/services/merge/merge_organisations_service.rb

@ -62,7 +62,7 @@ private
def merge_users(merging_organisation) def merge_users(merging_organisation)
users_to_merge = users_to_merge(merging_organisation) users_to_merge = users_to_merge(merging_organisation)
@merged_users[merging_organisation.name] = users_to_merge.map { |user| { name: user.name, email: user.email } } @merged_users[merging_organisation.name] = users_to_merge.map { |user| { name: user.name, email: user.email } }
users_to_merge.update_all(organisation_id: @absorbing_organisation.id) users_to_merge.update_all(organisation_id: @absorbing_organisation.id, values_updated_at: Time.zone.now)
end end
def merge_schemes_and_locations(merging_organisation) def merge_schemes_and_locations(merging_organisation)

5
db/migrate/20250305092900_add_values_updated_at_to_user.rb

@ -0,0 +1,5 @@
class AddValuesUpdatedAtToUser < ActiveRecord::Migration[7.2]
def change
add_column :users, :values_updated_at, :datetime
end
end

3
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.2].define(version: 2025_02_25_180643) do ActiveRecord::Schema[7.2].define(version: 2025_03_05_092900) 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"
@ -853,6 +853,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_02_25_180643) do
t.boolean "reactivate_with_organisation" t.boolean "reactivate_with_organisation"
t.datetime "discarded_at" t.datetime "discarded_at"
t.string "phone_extension" t.string "phone_extension"
t.datetime "values_updated_at"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["encrypted_otp_secret_key"], name: "index_users_on_encrypted_otp_secret_key", unique: true t.index ["encrypted_otp_secret_key"], name: "index_users_on_encrypted_otp_secret_key", unique: true

21
spec/services/exports/user_export_service_spec.rb

@ -202,7 +202,26 @@ RSpec.describe Exports::UserExportService do
before do before do
create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:) create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:)
create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:) create(:user, updated_at: Time.zone.local(2022, 4, 27), organisation:)
Export.create!(started_at: Time.zone.local(2022, 4, 26), base_number: 1, increment_number: 1) Export.create!(started_at: Time.zone.local(2022, 4, 26), base_number: 1, increment_number: 1, empty_export: true, collection: "users")
end
it "generates an XML manifest file with the expected content within the ZIP file" do
expected_content = replace_record_number(local_manifest_file.read, 2)
expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content|
entry = Zip::File.open_buffer(content).find_entry(expected_manifest_filename)
expect(entry).not_to be_nil
expect(entry.get_input_stream.read).to eq(expected_content)
end
expect(export_service.export_xml_users).to eq({ expected_zip_filename.gsub(".zip", "") => start_time })
end
end
context "and a user has been manually updated since the previous partial export" do
before do
create(:user, updated_at: Time.zone.local(2022, 4, 25), values_updated_at: Time.zone.local(2022, 4, 27), organisation:)
create(:user, updated_at: Time.zone.local(2022, 4, 25), values_updated_at: Time.zone.local(2022, 4, 27), organisation:)
Export.create!(started_at: Time.zone.local(2022, 4, 26), base_number: 1, increment_number: 1, empty_export: true, collection: "users")
end end
it "generates an XML manifest file with the expected content within the ZIP file" do it "generates an XML manifest file with the expected content within the ZIP file" do

2
spec/services/merge/merge_organisations_service_spec.rb

@ -31,10 +31,12 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(Rails.logger).to receive(:info).with("\t#{merging_organisation.data_protection_officers.first.name} (#{merging_organisation.data_protection_officers.first.email})") expect(Rails.logger).to receive(:info).with("\t#{merging_organisation.data_protection_officers.first.name} (#{merging_organisation.data_protection_officers.first.email})")
expect(Rails.logger).to receive(:info).with("\tfake name (fake@email.com)") expect(Rails.logger).to receive(:info).with("\tfake name (fake@email.com)")
expect(Rails.logger).to receive(:info).with("New schemes from fake org:") expect(Rails.logger).to receive(:info).with("New schemes from fake org:")
expect(merging_organisation_user.values_updated_at).to be_nil
merge_organisations_service.call merge_organisations_service.call
merging_organisation_user.reload merging_organisation_user.reload
expect(merging_organisation_user.organisation).to eq(absorbing_organisation) expect(merging_organisation_user.organisation).to eq(absorbing_organisation)
expect(merging_organisation_user.values_updated_at).not_to be_nil
end end
it "sets merge date on merged organisation" do it "sets merge date on merged organisation" do

Loading…
Cancel
Save