Class: Amazon::MigrateAmazonDataToSpecs

Inherits:
BaseService show all
Defined in:
app/services/amazon/migrate_amazon_data_to_specs.rb

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#import_item(item) ⇒ 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
# File 'app/services/amazon/migrate_amazon_data_to_specs.rb', line 20

def import_item(item)
  grouping = 'X-Amazon'
  ps_created = []
  logger.info "*** Migrating amazon data from item #{item.id} #{item.sku} to specs"
  fields = %i[legacy_amazon_title legacy_amazon_keywords legacy_amazon_description]
  fields += (1..9).to_a.map { |i| :"legacy_amazon_feature_#{i}" }
  fields.each do |field|
    # Our field label
    name = field.to_s.sub('legacy_amazon_', '').humanize.capitalize
    token = field
    logger.debug "*** Evaluating #{field}"
    # Do english
    if (text_blurb_en = item.send(field)).present?
      logger.debug "*** English value contains #{text_blurb_en}"
      ps_created << item.create_or_set_spec_value(token:, name:, grouping:, text_blurb: text_blurb_en, skip_spec_refresh: true)&.id
    else
      logger.warn '*** No English value detected'
    end
    # Do other languages
    item.translations.each do |locale, translated_values|
      old_field_name = field.to_s.sub('legacy_', '')
      if (text_blurb = translated_values[old_field_name.to_s]).present?
        ps = item.create_or_set_spec_value(token:, name:, grouping:, text_blurb:, locale: locale.to_sym, skip_spec_refresh: true)
        # If our english text was missing, we will try to auto translate it
        source_text_lang_code = locale.first(2).upcase
        ps.name_en ||= DeepL.translate(ps.name(locale: locale.to_sym), source_text_lang_code, 'EN').text
        ps.text_blurb_en ||= DeepL.translate(text_blurb, source_text_lang_code, 'EN').text
        ps.save
        ps_created << ps&.id
      else
        logger.warn "*** No #{locale} value detected"
      end
    end
  end
  ps_created.compact
end

#process(items: nil, limit: nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/services/amazon/migrate_amazon_data_to_specs.rb', line 2

def process(items: nil, limit: nil)
  ps_created = {}
  items_with_error = []
  items ||= query_items_to_migrate

  items = items.limit(limit) if limit
  Item.transaction do
    items.find_each do |item|
      ps_created[item.id] = import_item(item)
    rescue StandardError => e
      logger.error "Problem with importing from item #{item.id} #{item.sku}"
      logger.error e
      items_with_error << { id: item.id }
    end
  end
  { ps_created: ps_created, items_with_error: items_with_error }
end

#query_items_to_migrateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/amazon/migrate_amazon_data_to_specs.rb', line 57

def query_items_to_migrate
  columns = %i[
    legacy_amazon_title
    legacy_amazon_description
    legacy_amazon_keywords
    legacy_amazon_feature_1
    legacy_amazon_feature_2
    legacy_amazon_feature_3
    legacy_amazon_feature_4
    legacy_amazon_feature_5
    legacy_amazon_feature_6
    legacy_amazon_feature_7
    legacy_amazon_feature_8
    legacy_amazon_feature_9
  ]
  columns.inject(Item.none) do |scope, column|
    scope.or(Item.where.not(column => [nil, '']))
  end
end