diff --git a/lib/tasks/update_created_by_values.rake b/lib/tasks/update_created_by_values.rake index 572d88e63..bcbc86167 100644 --- a/lib/tasks/update_created_by_values.rake +++ b/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(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 - 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) 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(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 - 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) end end diff --git a/spec/lib/tasks/update_created_by_values_spec.rb b/spec/lib/tasks/update_created_by_values_spec.rb index 33df413f1..776e709f8 100644 --- a/spec/lib/tasks/update_created_by_values_spec.rb +++ b/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) 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