Class: ImageProfileBulkSyncWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::StatusBroadcastable
Defined in:
app/workers/image_profile_bulk_sync_worker.rb

Overview

Copies all image profiles from a reference item to all other items in its
Amazon variation. Replaces existing profiles for the target marketplace.

Usage:
job_id = ImageProfileBulkSyncWorker.perform_async(
'reference_item_id' => item.id,
'marketplace' => 'WYS',
'content_locale' => 'en',
'return_to' => '/crm/image_profile_manager/123?marketplace=WYS'
)
redirect_to job_path(job_id)

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/workers/image_profile_bulk_sync_worker.rb', line 20

def perform(options = {})
  reference_item = Item.find(options['reference_item_id'])
  marketplace    = options['marketplace'].presence || 'AMZ'
  locale         = options['content_locale'].presence || 'en'
  return_to      = options['return_to']

  source_profiles = reference_item.image_profiles
                                  .where(locale: locale)
                                  .where('image_type LIKE ?', "#{marketplace}_%")

  if source_profiles.empty?
    store error_message: 'No profiles to copy from the reference item',
          redirect_to: return_to
    return
  end

  target_items = if reference_item.amazon_variation_id.present?
                   reference_item.amazon_variation.items.where.not(id: reference_item.id)
                 else
                   Item.none
                 end

  if target_items.empty?
    store error_message: 'No other items in variation to sync to',
          redirect_to: return_to
    return
  end

  target_list  = target_items.to_a
  profile_data = source_profiles.map { |p| { image_type: p.image_type, image_id: p.image_id, transform_params: p.transform_params } }

  total(target_list.size)
  success_count = 0
  errors = []

  target_list.each_with_index do |item, index|
    at(index + 1, "Syncing #{item.sku} (#{index + 1}/#{target_list.size})")

    ActiveRecord::Base.transaction do
      item.image_profiles
          .where(locale: locale)
          .where('image_type LIKE ?', "#{marketplace}_%")
          .destroy_all

      profile_data.each do |data|
        item.image_profiles.create!(
          image_type: data[:image_type],
          locale: locale,
          image_id: data[:image_id],
          transform_params: data[:transform_params]
        )
      end

      success_count += 1
    end
  rescue ActiveRecord::RecordInvalid, StandardError => e
    errors << "#{item.sku}: #{e.message}"
  end

  message = "Copied #{profile_data.size} profiles to #{success_count} of #{target_list.size} items"
  message += ". Errors: #{errors.first(3).join('; ')}" if errors.any?

  if errors.any? && success_count == 0
    store error_message: message, redirect_to: return_to
  else
    store complete_message: message, redirect_to: return_to
  end
end