Class: Workarea::DeactivateStaleDiscounts

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/workarea/deactivate_stale_discounts.rb

Instance Method Summary collapse

Instance Method Details

#all_active_discount_idsObject



30
31
32
33
34
35
36
# File 'app/workers/workarea/deactivate_stale_discounts.rb', line 30

def all_active_discount_ids
  Pricing::Discount
    .where(:updated_at.lt => Workarea.config.discount_staleness_ttl.ago)
    .select(&:active?)
    .map(&:id)
    .map(&:to_s)
end

#order_discount_idsObject



20
21
22
# File 'app/workers/workarea/deactivate_stale_discounts.rb', line 20

def order_discount_ids
  Order.since(Workarea.config.discount_staleness_ttl.ago).discount_ids
end

#perform(*args) ⇒ Object



5
6
7
8
9
# File 'app/workers/workarea/deactivate_stale_discounts.rb', line 5

def perform(*args)
  Mongoid::AuditLog.record do
    Pricing::Discount.any_in(id: unused_discount_ids).auto_deactivate
  end
end

#shipping_discount_idsObject



24
25
26
27
28
# File 'app/workers/workarea/deactivate_stale_discounts.rb', line 24

def shipping_discount_ids
  Shipping
    .since(Workarea.config.discount_staleness_ttl.ago)
    .discount_ids
end

#unused_discount_idsObject



11
12
13
# File 'app/workers/workarea/deactivate_stale_discounts.rb', line 11

def unused_discount_ids
  all_active_discount_ids - used_discount_ids
end

#used_discount_idsObject



15
16
17
18
# File 'app/workers/workarea/deactivate_stale_discounts.rb', line 15

def used_discount_ids
  @used_discount_ids ||=
    (order_discount_ids + shipping_discount_ids).map(&:to_s).uniq
end