Class: Coupon::Qualifier

Inherits:
Object
  • Object
show all
Includes:
Memery
Defined in:
app/services/coupon/qualifier.rb

Overview

This class is responsible for finding coupons and auto apply coupons
for itemizables, it interfaces with the coupon query builder

Instance Method Summary collapse

Instance Method Details

#apply_product_filters(itemizable:, coupons:) ⇒ Object



38
39
40
# File 'app/services/coupon/qualifier.rb', line 38

def apply_product_filters(itemizable:, coupons: )
  coupons.includes(:product_filters).to_a.select{|cp| cp.qualifying_line_items?(itemizable) }
end

#find_coupons(itemizable:, auto_apply_coupon: false, role_ids: nil, searchable_only: false, exclude_coupon_ids: nil, include_coupon_ids: nil, search_query: nil, wild_search: true) ⇒ Object

Build the condition that will find all coupons matching the criteria set
in the options initializer, returns a relation



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/coupon/qualifier.rb', line 9

def find_coupons(itemizable:,
                 auto_apply_coupon: false,
                 role_ids: nil,
                 searchable_only: false,
                 exclude_coupon_ids: nil,
                 include_coupon_ids: nil,
                 search_query: nil,
                 wild_search: true )
  qb = Coupon::QueryBuilder.new(nil, auto_apply_coupon: auto_apply_coupon)
  qb = qb.for_itemizable(itemizable)
  qb = qb.with_include_coupon_ids(include_coupon_ids)
  qb = qb.with_role_ids(role_ids)
  qb = qb.with_searchable_only(searchable_only)
  qb = qb.with_exclude_coupon_ids(exclude_coupon_ids)
  qb = qb.with_search_query(search_query, wild_search: wild_search)
  #Now which one of these coupons have the right product mix, eventually we can sqlize this too
  coupons_array = apply_product_filters(itemizable: itemizable, coupons: qb.query)
  # For now we'll need to do it this way so that the return value of this method is a relation
  qualified_coupon_ids = coupons_array.map(&:id)
  coupons = Coupon.where(id: qualified_coupon_ids)
  # Now we have to reinject the serial number if the match was serial number based
  coupons.each do |c|
    matched_coupon = coupons_array.detect{|c2| c2.id == c.id}
    c.serial_number = matched_coupon.try(:matched_serial_number)
  end
  coupons
end