Class: Coupon::QueryBuilder

Inherits:
BaseQueryBuilder show all
Defined in:
app/services/coupon/query_builder.rb

Instance Attribute Summary

Attributes inherited from BaseQueryBuilder

#query

Instance Method Summary collapse

Methods inherited from BaseQueryBuilder

#array_check, #habtm_check, #reflect

Constructor Details

#initialize(query = nil, options = {}) ⇒ QueryBuilder

When initializing this class make sure to always pass a nil if you
run into a wrong number of arguments (0 for 1) error, for
reasons i cannot explain the default = nil is ignored and it tried
to initialize the superclass instead



7
8
9
10
11
12
13
14
15
# File 'app/services/coupon/query_builder.rb', line 7

def initialize(query = nil, options = {})
  @options = options
  @auto_apply_coupon = @options[:auto_apply_coupon]
  query ||= Coupon.unscoped.active
  if @auto_apply_coupon
    query = query.where(auto_apply: true)
  end
  super( query )
end

Instance Method Details

#for_customer(customer) ⇒ Object

Returns all the coupons applicable for a given customer



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

def for_customer(customer)
  qb = CustomerFilter::QueryBuilder.new(nil)
  matching_customer_filters_ids = qb.for_customer(customer).query.pluck(:id)
  matching_customer_filters_ids << nil
  reflect(
    if @auto_apply_coupon
      query.where(customer_filter_id: matching_customer_filters_ids, auto_apply_customer_filter_id: matching_customer_filters_ids)
    else
      query.where(customer_filter_id: matching_customer_filters_ids)
    end
  )
end

#for_itemizable(itemizable) ⇒ Object

Returns all the coupons applicable for a given itemizable, Order or Quote



18
19
20
21
22
23
24
25
26
27
# File 'app/services/coupon/query_builder.rb', line 18

def for_itemizable(itemizable)
  case itemizable.class.name
  when 'Order'
    for_order(itemizable)
  when 'Quote'
    for_quote(itemizable)
  else
    raise "Unsupported itemizable type"
  end
end

#for_order(order) ⇒ Object

Returns all the coupons applicable for a given itemizable



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/coupon/query_builder.rb', line 62

def for_order(order)
  customer = order.customer
  shipping_state = order.shipping_address.try(:state_code)
  reflect(
    for_customer(customer).
    with_effective_date(order.effective_date_for_coupon).
    with_shipping_state_code(shipping_state).
    with_order_type(order.order_type).
    with_ships_economy(order.ships_economy_package?).
    with_order_reception_type(order.order_reception_type).
    query
  )
end

#for_quote(quote, customer_association_mode = :customer_filter) ⇒ Object

Params:
quote
customer_association_mode, one of :customer_filter or :auto_apply_customer_filter
the filtering might be different for determining the auto apply rule.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/services/coupon/query_builder.rb', line 80

def for_quote(quote, customer_association_mode = :customer_filter)
  customer = quote.customer
  shipping_state = quote.shipping_address.try(:state_code) || quote.opportunity.state_code

  reflect(
    for_customer(customer).
    with_effective_date(quote.effective_date_for_coupon).
    with_shipping_state_code(shipping_state).
    with_opportunity_reception_type(quote.opportunity.opportunity_reception_type).
    with_opportunity_type(quote.opportunity.opportunity_type).
    with_ships_economy(quote.ships_economy_package?).
    query
  )
end

#for_specific_customer(customer) ⇒ Object

Only look at coupons using a coupon expressely setup for customer ids



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/services/coupon/query_builder.rb', line 44

def for_specific_customer(customer)
  matching_customer_filters_ids = CustomerFilter.where("
    exists(select 1 from customer_filters_parties
      where customer_filters_parties.party_id = ?
      and customer_filters_parties.customer_filter_id = customer_filters.id )",
    customer.id
  )

  reflect(
    if matching_customer_filters_ids.present?
      query.where(customer_filter_id: matching_customer_filters_ids)
    else
      query.none
    end
  )
end

#with_effective_date(date) ⇒ Object



95
96
97
98
99
# File 'app/services/coupon/query_builder.rb', line 95

def with_effective_date(date)
  reflect(
    query.in_effect_on(date)
  )
end

#with_exclude_coupon_ids(exclude_coupon_ids) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'app/services/coupon/query_builder.rb', line 154

def with_exclude_coupon_ids(exclude_coupon_ids)
  reflect(
    if exclude_coupon_ids.present?
      query.where.not(id: exclude_coupon_ids)
    else
      query
    end
  )
end

#with_include_coupon_ids(coupon_ids) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'app/services/coupon/query_builder.rb', line 164

def with_include_coupon_ids(coupon_ids)
  reflect(
    if coupon_ids.present?
      query.where(id: coupon_ids)
    else
      query
    end
  )
end

#with_opportunity_reception_type(reception_type) ⇒ Object



126
127
128
# File 'app/services/coupon/query_builder.rb', line 126

def with_opportunity_reception_type(reception_type)
  array_check(:opportunity_reception_types_filter, reception_type)
end

#with_opportunity_type(opportunity_type) ⇒ Object



130
131
132
# File 'app/services/coupon/query_builder.rb', line 130

def with_opportunity_type(opportunity_type)
  array_check(:opportunity_types_filter, opportunity_type)
end

#with_order_reception_type(reception_type) ⇒ Object



122
123
124
# File 'app/services/coupon/query_builder.rb', line 122

def with_order_reception_type(reception_type)
  array_check(:order_reception_types_filter, reception_type)
end

#with_order_type(order_type) ⇒ Object



118
119
120
# File 'app/services/coupon/query_builder.rb', line 118

def with_order_type(order_type)
  array_check(:order_types_filter, order_type)
end

#with_role_ids(role_ids_restriction) ⇒ Object

Coupon has to be modifiable by one of the specified roles or be empty
This is not smart, make sure you feed me all roles to check including
descendants, not this class's responsibility



104
105
106
107
108
109
110
111
112
# File 'app/services/coupon/query_builder.rb', line 104

def with_role_ids(role_ids_restriction)
  reflect(
    if role_ids_restriction.present?
      query.where("coupons.role_ids_restriction = '{}' or ? && coupons.role_ids_restriction", "{#{role_ids_restriction.join(',')}}" )
    else
      query
    end
  )
end

#with_search_query(search_query, wild_search: true) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/services/coupon/query_builder.rb', line 174

def with_search_query(search_query, wild_search: true)
  reflect(
    if search_query.present?
      subquery_serial_number = "exists(select 1 from coupon_serial_numbers csn where NOT csn.used AND csn.id::varchar = :search_term_fixed AND csn.coupon_id = coupons.id)"
      subquery_serial_number_col = ActiveRecord::Base.escape_sql("(select csn.id from coupon_serial_numbers csn where NOT csn.used AND csn.id::varchar = ? AND csn.coupon_id = coupons.id limit 1)", search_query)
      if wild_search
        query.select_append("#{subquery_serial_number_col} as matched_serial_number").where("coupons.code ilike :search_term or title ilike :search_term or #{subquery_serial_number}", search_term: "%#{search_query}%", search_term_fixed: search_query)
      else
        query.select_append("#{subquery_serial_number_col} as matched_serial_number").where("(NOT coupons.serial_numbers_only AND (coupons.code ilike :search_term OR #{subquery_serial_number})) OR (coupons.serial_numbers_only AND #{subquery_serial_number})", search_term: search_query, search_term_fixed: search_query)
      end
    else
      query
    end
  )
end

#with_searchable_only(searchable_only) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'app/services/coupon/query_builder.rb', line 144

def with_searchable_only(searchable_only)
  reflect(
    if searchable_only
      query.tier1and3.active
    else
      query
    end
  )
end

#with_shipping_state_code(state_code) ⇒ Object



114
115
116
# File 'app/services/coupon/query_builder.rb', line 114

def with_shipping_state_code(state_code)
  array_check(:state_codes, state_code)
end

#with_ships_economy(ships_economy) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'app/services/coupon/query_builder.rb', line 134

def with_ships_economy(ships_economy)
  reflect(
    if ships_economy
      query
    else
      query.exclude_ships_economy_only
    end
  )
end