Module: Workarea::Order::Queries::ClassMethods
- Defined in:
- app/models/workarea/order/queries.rb
Instance Method Summary collapse
-
#abandoned_count(start_time = 30.years.ago, end_time = Time.current) ⇒ Object
TODO: Remove in an appropriate future version; no longer used in Base.
-
#abandoned_product_ids(start_time = 30.years.ago, end_time = Time.current, limit = 5) ⇒ Object
TODO: Remove in an appropriate future version; no longer used in Base.
-
#abandoned_value(start_time = 30.years.ago, end_time = Time.current) ⇒ Object
TODO: Remove in an appropriate future version; no longer used in Base.
-
#average_order_value(start_time = 30.years.ago, end_time = Time.current) ⇒ Money
Find the average order value for placed orders in a given time period.
-
#expired ⇒ Mongoid::Criteria
Query for orders that are expired, meaning they had not been updated, placed, nor completed checkout for longer than the
Workarea.config.order_expiration_period
. -
#expired_in_checkout ⇒ Mongoid::Criteria
Query for orders which have expired in checkout, meaning they have been not been placed or updated for longer than the
Workarea.config.order_expiration_period
, but have started checkout. - #find_by_token(token) ⇒ Object
-
#find_current(params = {}) ⇒ Order
Find a current cart for a session.
- #need_reminding ⇒ Object
- #recent(user_id, limit = 3) ⇒ Object
- #recent_placed(limit = 5) ⇒ Object
- #total_placed(start_time = Time.current - 30.years, end_time = Time.current) ⇒ Object
- #totals(start_time = Time.current - 30.years, end_time = Time.current) ⇒ Object
Instance Method Details
#abandoned_count(start_time = 30.years.ago, end_time = Time.current) ⇒ Object
TODO: Remove in an appropriate future version; no longer used in Base
128 129 130 131 132 133 134 135 |
# File 'app/models/workarea/order/queries.rb', line 128 def abandoned_count(start_time = 30.years.ago, end_time = Time.current) Order .where(:created_at.lte => Workarea.config.order_active_period.ago) .where(:created_at.gte => start_time, :created_at.lt => end_time) .where(:'items.0.sku'.exists => true) .any_of({ placed_at: nil }, { :placed_at.exists => false }) .count end |
#abandoned_product_ids(start_time = 30.years.ago, end_time = Time.current, limit = 5) ⇒ Object
TODO: Remove in an appropriate future version; no longer used in Base
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'app/models/workarea/order/queries.rb', line 149 def abandoned_product_ids(start_time = 30.years.ago, end_time = Time.current, limit = 5) results = Order.collection.aggregate([ { '$match' => { '$and' => [ { 'created_at' => { '$gte' => Time.mongoize(start_time), '$lt' => Time.mongoize(end_time) } }, { 'created_at' => { '$lte' => Time.mongoize(Workarea.config.order_active_period.ago) } } ], '$or' => [ { 'placed_at' => nil }, { 'placed_at' => { '$exists' => false } } ], 'items.0.sku' => { '$exists' => true } }, }, { '$unwind' => '$items' }, { '$group' => { '_id' => '$items.product_id', 'total' => { '$sum' => '$items.total_value.cents' }, 'count' => { '$sum' => 1 } } }, { '$sort' => { 'count' => -1 } }, { '$limit' => limit } ]) results.reduce({}) do |memo, result| memo.merge!( result['_id'] => { count: result['count'], total: Money.new(result['total']) } ) end end |
#abandoned_value(start_time = 30.years.ago, end_time = Time.current) ⇒ Object
TODO: Remove in an appropriate future version; no longer used in Base
138 139 140 141 142 143 144 145 146 |
# File 'app/models/workarea/order/queries.rb', line 138 def abandoned_value(start_time = 30.years.ago, end_time = Time.current) cents = Order .where(:created_at.lte => Workarea.config.order_active_period.ago) .where(:created_at.gte => start_time, :created_at.lt => end_time) .any_of({ placed_at: nil }, { :placed_at.exists => false }) .sum('total_value.cents') Money.new(cents || 0) end |
#average_order_value(start_time = 30.years.ago, end_time = Time.current) ⇒ Money
Find the average order value for placed orders in a given time period. This is defined as the order total without taxes or shipping costs.
119 120 121 122 123 124 125 |
# File 'app/models/workarea/order/queries.rb', line 119 def average_order_value(start_time = 30.years.ago, end_time = Time.current) cents = Order .where(:placed_at.gte => start_time, :placed_at.lt => end_time) .avg('total_price.cents') Money.new(cents || 0) end |
#expired ⇒ Mongoid::Criteria
Query for orders that are expired, meaning they had not been updated, placed, nor completed checkout for longer than the Workarea.config.order_expiration_period
. Contrast this with the Order.expired_in_checkout
query, which does factor in orders that have entered the checkout process.
24 25 26 27 28 29 30 |
# File 'app/models/workarea/order/queries.rb', line 24 def expired Order.where( :updated_at.lt => Time.current - Workarea.config.order_expiration_period, checkout_started_at: nil, placed_at: nil ) end |
#expired_in_checkout ⇒ Mongoid::Criteria
Query for orders which have expired in checkout, meaning they have been not been placed or updated for longer than the Workarea.config.order_expiration_period
, but have started checkout. Contrast this with Order.expired
, which does not factor in orders that have started checkout.
39 40 41 42 43 44 45 |
# File 'app/models/workarea/order/queries.rb', line 39 def expired_in_checkout Order.where( :updated_at.lt => Time.current - Workarea.config.order_expiration_period, :checkout_started_at.lt => Time.current - Workarea.config.order_expiration_period, placed_at: nil ) end |
#find_by_token(token) ⇒ Object
106 107 108 |
# File 'app/models/workarea/order/queries.rb', line 106 def find_by_token(token) Order.find_by(token: token) rescue nil end |
#find_current(params = {}) ⇒ Order
Find a current cart for a session. Returns a new order if one cannot be found.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/workarea/order/queries.rb', line 52 def find_current(params = {}) if params[:id].present? Order.not_placed.find(params[:id].to_s) elsif params[:user_id].present? Order.recently_updated.not_placed.find_by(params.slice(:user_id)) else Order.new(user_id: params[:user_id]) end rescue Mongoid::Errors::DocumentNotFound Order.new(user_id: params[:user_id]) end |
#need_reminding ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/workarea/order/queries.rb', line 93 def need_reminding Order.where( placed_at: nil, reminded_at: nil, fraud_suspected_at: nil, :checkout_started_at.lte => Workarea.config.order_active_period.ago, :email.exists => true, :email.ne => '', :items.exists => true, :items.ne => [] ) end |
#recent(user_id, limit = 3) ⇒ Object
64 65 66 67 68 69 70 |
# File 'app/models/workarea/order/queries.rb', line 64 def recent(user_id, limit = 3) Order .where(user_id: user_id.to_s, :placed_at.exists => true) .excludes(placed_at: nil) .order_by([:placed_at, :desc]) .limit(limit) end |
#recent_placed(limit = 5) ⇒ Object
86 87 88 89 90 91 |
# File 'app/models/workarea/order/queries.rb', line 86 def recent_placed(limit = 5) Order. excludes(placed_at: nil). order_by([:placed_at, :desc]). limit(limit) end |
#total_placed(start_time = Time.current - 30.years, end_time = Time.current) ⇒ Object
80 81 82 83 84 |
# File 'app/models/workarea/order/queries.rb', line 80 def total_placed(start_time = Time.current - 30.years, end_time = Time.current) Order. where(:placed_at.gte => start_time, :placed_at.lt => end_time). count end |
#totals(start_time = Time.current - 30.years, end_time = Time.current) ⇒ Object
72 73 74 75 76 77 78 |
# File 'app/models/workarea/order/queries.rb', line 72 def totals(start_time = Time.current - 30.years, end_time = Time.current) cents = Order. where(:placed_at.gte => start_time, :placed_at.lt => end_time). sum('total_price.cents') Money.new(cents || 0) end |