Class: Coupon::QueryBuilder

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

Overview

Service object: query builder.

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



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

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

Instance Method Details

#for_customer(customer) ⇒ Object

Returns all the coupons applicable for a given customer



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

def for_customer(customer)
  qb = CustomerFilter::QueryBuilder.new(nil)
  matching_customer_filters_ids = qb.for_customer(customer).query.ids
  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



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

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



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

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.



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

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



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

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



93
94
95
96
97
# File 'app/services/coupon/query_builder.rb', line 93

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

#with_exclude_coupon_ids(exclude_coupon_ids) ⇒ Object



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

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



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

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



124
125
126
# File 'app/services/coupon/query_builder.rb', line 124

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

#with_opportunity_type(opportunity_type) ⇒ Object



128
129
130
# File 'app/services/coupon/query_builder.rb', line 128

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

#with_order_reception_type(reception_type) ⇒ Object



120
121
122
# File 'app/services/coupon/query_builder.rb', line 120

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

#with_order_type(order_type) ⇒ Object



116
117
118
# File 'app/services/coupon/query_builder.rb', line 116

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



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

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



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

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



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

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

#with_shipping_state_code(state_code) ⇒ Object



112
113
114
# File 'app/services/coupon/query_builder.rb', line 112

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

#with_ships_economy(ships_economy) ⇒ Object



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

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