diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 8c7e1d61..bc3112b8 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -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 @@ -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}." @@ -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}." diff --git a/spec/lib/tasks/assets_spec.rb b/spec/lib/tasks/assets_spec.rb index 9840d0d8..c85cba27 100644 --- a/spec/lib/tasks/assets_spec.rb +++ b/spec/lib/tasks/assets_spec.rb @@ -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)