Skip to content

Commit

Permalink
Add check for already processed replacement assets
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraghiorghisor-tw committed Nov 29, 2024
1 parent 40ce4e7 commit 31374d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ namespace :assets do
desc "Delete and update replacement draft"
task :delete_and_update_replacement_draft, %i[csv_path] => :environment do |_t, args|
csv_path = args.fetch(:csv_path)

processed_replacement_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 @@ -70,6 +72,11 @@ namespace :assets do
next
end

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

unless replacement_asset.draft?
puts "Asset ID: #{original_asset_id} - SKIPPED. Replacement #{replacement_asset.id} is not in draft."
next
Expand All @@ -95,6 +102,7 @@ namespace :assets do
message += " Replacement draft updated to false."

puts message
processed_replacement_ids[replacement_asset.id] = true
rescue StandardError
puts "Asset ID: #{original_asset_id} - ERROR. Replacement #{replacement_asset.id} failed to save. Error: #{replacement_asset.errors.full_messages}."
end
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/tasks/assets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@
describe "assets:bulk_fix:delete_and_update_replacement_draft" do
let(:task) { Rake::Task["assets:bulk_fix:delete_and_update_replacement_draft"] }

it "skips any repeating replacements" 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. Replacement #{replacement.id} has been deleted. Replacement draft 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 the asset and prints exception if asset is not found" do
expected_output = <<~OUTPUT
Asset ID: #{asset_id} - SKIPPED. No asset found.
Expand Down

0 comments on commit 31374d9

Please sign in to comment.