Class: CatalogItem::ScheduledPriceChanger

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/catalog_item/scheduled_price_changer.rb

Overview

This service will look for catalog item that have a scheduled price change
and apply those changes, then reporting via email to price_alert@warmlyyours.com
the changes made

Instance Attribute Summary collapse

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 Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



5
6
7
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 5

def annotations
  @annotations
end

#catalog_item_changesObject (readonly)

Returns the value of attribute catalog_item_changes.



5
6
7
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 5

def catalog_item_changes
  @catalog_item_changes
end

#reportObject (readonly)

Returns the value of attribute report.



5
6
7
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 5

def report
  @report
end

Instance Method Details

#add_catalog_item_changes(catalog_item, changes, annotation = nil) ⇒ Object

Here we compile all the catalog item changes in one changeset



51
52
53
54
55
56
57
58
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 51

def add_catalog_item_changes(catalog_item, changes, annotation = nil)
  @annotations[catalog_item.id] ||= []
  @annotations[catalog_item.id] << annotation if annotation.present?
  @catalog_item_changes ||= {}
  @catalog_item_changes[catalog_item.id] ||= {}
  @catalog_item_changes[catalog_item.id].merge!(changes)
  @catalog_item_changes[catalog_item.id]
end

#apply_catalog_item_changesObject

Here we apply all the accumulated catalog item changes



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
88
89
90
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 61

def apply_catalog_item_changes
  @report = []
  catalog_item_changes.each do |catalog_item_id, changes|
    result = Catalog::UpdateCatalogItem.new.process(catalog_item_id, changes)
    # What is the old amount
    price = result.catalog_item.amount
    price_was = result.changes[:amount]&.first
    coupon_id_was = result.changes[:coupon_id]&.first
    coupon_was = Coupon.find(coupon_id_was) if coupon_id_was

    @report << {
      catalog_item_id:,
      catalog_item: result.catalog_item,
      item_id: result.catalog_item.item.id,
      item_sku: result.catalog_item.item.sku,
      catalog_id: result.catalog_item.catalog_id,
      catalog_name: result.catalog_item.catalog.name,
      catalog_item_updated: result.catalog_item_updated?,
      currency_symbol: result.catalog_item.currency_symbol,
      price:,
      price_was:,
      sale_price: result.catalog_item.sale_price,
      coupon: result.catalog_item.coupon,
      coupon_was:,
      changes: result.changes,
      messages: result.messages + (@annotations[catalog_item_id] || [])
    }
  end
  @report
end

#flush_cacheObject



43
44
45
46
47
48
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 43

def flush_cache
  @product_lines.compact.each(&:touch)
  @items.compact.each(&:touch)
  @product_lines = []
  @items = []
end

#process(catalog_items: nil, as_of_day: nil, trial_run: false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 7

def process(catalog_items: nil, as_of_day: nil, trial_run: false)
  as_of_day ||= Date.current
  @catalog_item_changes = {}
  report = []
  @product_lines = []
  @items = []
  @annotations = {}
  CatalogItem.transaction do
    PaperTrail.request(whodunnit: 'CatalogItem::ScheduledPriceChanger') do
      process_scheduled_price_changes(catalog_items:, as_of_day:)

      # Catalog item changes should have accumulated all the necessary updates, let's apply them
      report = apply_catalog_item_changes
    end
    raise ActiveRecord::Rollback if trial_run
  end
  # Email Report
  InternalMailer.price_changed_alert(report).deliver_now if report.present?
  report
end

#process_scheduled_price_changes(catalog_items: nil, as_of_day: nil) ⇒ Object

This processes scheduled base catalog price changes (not sales).



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/catalog_item/scheduled_price_changer.rb', line 29

def process_scheduled_price_changes(catalog_items: nil, as_of_day: nil)
  catalog_items ||= CatalogItem.active
  catalog_items = catalog_items.where.not(new_price: nil).where.not(new_price_effective_date: nil).where(CatalogItem[:new_price_effective_date].lteq(as_of_day)).includes(:item, item: :primary_product_line)
  catalog_items.each do |ci|
    add_catalog_item_changes ci,
                             {
                               amount: ci.new_price,
                               new_price_effective_date: nil,
                               new_price: nil
                             },
                             'Updating price to previously scheduled price'
  end
end