Browse Source

Check for user type when updating created_by (#2401)

pull/2402/head v0.4.38
kosiakkatrina 8 months ago committed by GitHub
parent
commit
c39b98e207
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      lib/tasks/update_created_by_values.rake
  2. 33
      spec/lib/tasks/update_created_by_values_spec.rb

12
lib/tasks/update_created_by_values.rake

@ -3,14 +3,22 @@ task update_created_by_values: :environment do
LettingsLog.filter_by_years(%w[2023 2024]).where.not(bulk_upload_id: nil).update_all("created_by_id = (SELECT user_id FROM bulk_uploads WHERE bulk_uploads.id = lettings_logs.bulk_upload_id)") LettingsLog.filter_by_years(%w[2023 2024]).where.not(bulk_upload_id: nil).update_all("created_by_id = (SELECT user_id FROM bulk_uploads WHERE bulk_uploads.id = lettings_logs.bulk_upload_id)")
LettingsLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil, created_by: nil).find_each do |lettings_log| LettingsLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil, created_by: nil).find_each do |lettings_log|
user = PaperTrail::Version.find_by(item_id: lettings_log.id, item_type: "LettingsLog", event: "create")&.actor user = PaperTrail::Version.find_by(item_id: lettings_log.id, item_type: "LettingsLog", event: "create")&.actor
lettings_log.created_by = user.presence || lettings_log.assigned_to lettings_log.created_by = if user.present? && user.is_a?(User)
user
else
lettings_log.assigned_to
end
lettings_log.save!(touch: false, validate: false) lettings_log.save!(touch: false, validate: false)
end end
SalesLog.filter_by_years(%w[2023 2024]).where.not(bulk_upload_id: nil).update_all("created_by_id = (SELECT user_id FROM bulk_uploads WHERE bulk_uploads.id = sales_logs.bulk_upload_id)") SalesLog.filter_by_years(%w[2023 2024]).where.not(bulk_upload_id: nil).update_all("created_by_id = (SELECT user_id FROM bulk_uploads WHERE bulk_uploads.id = sales_logs.bulk_upload_id)")
SalesLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil, created_by: nil).find_each do |sales_log| SalesLog.filter_by_years(%w[2023 2024]).where(bulk_upload_id: nil, created_by: nil).find_each do |sales_log|
user = PaperTrail::Version.find_by(item_id: sales_log.id, item_type: "SalesLog", event: "create")&.actor user = PaperTrail::Version.find_by(item_id: sales_log.id, item_type: "SalesLog", event: "create")&.actor
sales_log.created_by = user.presence || sales_log.assigned_to sales_log.created_by = if user.present? && user.is_a?(User)
user
else
sales_log.assigned_to
end
sales_log.save!(touch: false, validate: false) sales_log.save!(touch: false, validate: false)
end end
end end

33
spec/lib/tasks/update_created_by_values_spec.rb

@ -107,6 +107,39 @@ RSpec.describe "update_created_by_values" do
expect(sales_log.updated_at).to eq(initial_updated_at) expect(sales_log.updated_at).to eq(initial_updated_at)
end end
end end
context "and version whodunnit is not a User for create" do
let(:lettings_log) { create(:lettings_log, :completed, assigned_to: user, created_by_id: nil, updated_at: Time.zone.yesterday) }
let(:sales_log) { create(:sales_log, :completed, assigned_to: user, created_by_id: nil, updated_at: Time.zone.yesterday) }
let(:other_user) { create(:user, organisation: user.organisation) }
before do
PaperTrail::Version.find_by(item_id: lettings_log.id, item_type: "LettingsLog", event: "create").update!(whodunnit: other_user.email)
PaperTrail::Version.find_by(item_id: sales_log.id, item_type: "SalesLog", event: "create").update!(whodunnit: other_user.email)
end
it "sets created_by to assigned_to for lettings" do
initial_updated_at = lettings_log.updated_at
expect(lettings_log.created_by_id).to eq(nil)
expect(lettings_log.assigned_to_id).to eq(user.id)
task.invoke
lettings_log.reload
expect(lettings_log.created_by_id).to eq(user.id)
expect(lettings_log.assigned_to_id).to eq(user.id)
expect(lettings_log.updated_at).to eq(initial_updated_at)
end
it "sets created_by to assigned_to for sales" do
initial_updated_at = sales_log.updated_at
expect(sales_log.created_by_id).to eq(nil)
expect(sales_log.assigned_to_id).to eq(user.id)
task.invoke
sales_log.reload
expect(sales_log.created_by_id).to eq(user.id)
expect(sales_log.assigned_to_id).to eq(user.id)
expect(sales_log.updated_at).to eq(initial_updated_at)
end
end
end end
end end
end end

Loading…
Cancel
Save