Skip to content

Commit

Permalink
Add log for already processed assets
Browse files Browse the repository at this point in the history
We want to know if we are skipping assets because they have already been fixed,
so that we store a correct account of what's been changed.
  • Loading branch information
lauraghiorghisor-tw committed Dec 5, 2024
1 parent 4be7b2b commit b63b3ba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace :assets do
desc "Fix assets and draft replacements"
task :fix_assets_and_draft_replacements, %i[csv_path] => :environment do |_t, args|
csv_path = args.fetch(:csv_path)

processed_asset_ids = {}

process_file_in_memory(csv_path) do |row|
original_asset_id = row[0]
original_asset = Asset.where(_id: original_asset_id)&.first
Expand All @@ -67,9 +70,20 @@ namespace :assets do

replacement_asset = original_asset.replacement

if processed_asset_ids[original_asset_id]
puts "Asset ID: #{original_asset_id} - PROCESSED. Asset already processed as a replacement."
next
end

if replacement_asset && processed_asset_ids[replacement_asset.id.to_s]
puts "Asset ID: #{original_asset_id} - PROCESSED. Replacement #{replacement_asset.id} already processed."
next
end

if replacement_asset && replacement_asset.replacement.nil? && replacement_asset.draft?
begin
delete_and_update_draft(replacement_asset)
processed_asset_ids[replacement_asset.id.to_s] = true
puts "Asset ID: #{original_asset_id} - OK. Draft replacement #{replacement_asset.id} deleted and updated to false."
rescue StandardError
puts "Asset ID: #{original_asset_id} - ERROR. Asset replacement failed to save. Error: #{replacement_asset.errors.full_messages}."
Expand All @@ -80,6 +94,7 @@ namespace :assets do
if is_replacement && replacement_asset.nil? && original_asset.draft?
begin
delete_and_update_draft(original_asset)
processed_asset_ids[original_asset_id] = true
puts "Asset ID: #{original_asset_id} - is a replacement. Asset deleted and updated to false."
rescue StandardError
puts "Asset ID: #{original_asset_id} - ERROR. Asset failed to save. Error: #{original_asset.errors.full_messages}."
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/tasks/assets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
end

it "skips already processed replacement assets" do
file.open
csv_file = <<~CSV
6592008029c8c3e4dc76256c
6592008029c8c3e4dc76256d
CSV
file.write(csv_file)
file.close

replacement = FactoryBot.create(:asset, draft: true, deleted_at: nil, replacement_id: nil)
FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256c", replacement_id: replacement.id)
FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256d", replacement_id: replacement.id)

expected_output = <<~OUTPUT
Asset ID: 6592008029c8c3e4dc76256c - OK. Draft replacement #{replacement.id} deleted and updated to false.
Asset ID: 6592008029c8c3e4dc76256d - PROCESSED. Replacement #{replacement.id} already processed.
OUTPUT
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
end

it "skips already processed assets" do
file.open
csv_file = <<~CSV
6592008029c8c3e4dc76256c
6592008029c8c3e4dc76256d
CSV
file.write(csv_file)
file.close

replacement = FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256d", draft: true, deleted_at: nil, replacement_id: nil)
FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256c", replacement_id: replacement.id)

expected_output = <<~OUTPUT
Asset ID: 6592008029c8c3e4dc76256c - OK. Draft replacement #{replacement.id} deleted and updated to false.
Asset ID: 6592008029c8c3e4dc76256d - PROCESSED. Asset already processed as a replacement.
OUTPUT
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
end

it "only updates the draft state of the replacement if the asset replacement is already deleted" do
replacement = FactoryBot.create(:asset, draft: true, deleted_at: Time.zone.now, replacement_id: nil)
FactoryBot.create(:asset, id: asset_id, replacement_id: replacement.id)
Expand Down

0 comments on commit b63b3ba

Please sign in to comment.