Module: Auth::Concerns::Shopping::DiscountConcern::ClassMethods

Defined in:
app/models/auth/concerns/shopping/discount_concern.rb

Instance Method Summary collapse

Instance Method Details

#add_pending_discount(payment_id, discount_object_id) ⇒ Object

@param payment_id @return the document after the update or nil, in case nothing was updated.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 208

def add_pending_discount(payment_id,discount_object_id)
	Auth.configuration.discount_class.constantize.
	where({
		"$and" => [
			{
				"verified" => {
					"$ne" => payment_id
				}
			},
			{
				"pending" => {
					"$ne" => payment_id
				}
			},
			{
				"declined" => {
					"$ne" => payment_id
				}
			},
			{
				"_id" => BSON::ObjectId(discount_object_id)
			}
		]
	})
	.find_one_and_update(
		{
			"$push" => {
				:pending => payment_id
			}
		},
		{
			:return_document => :after	
		}
	)
end

#find_discounts(options) ⇒ Object



196
197
198
199
200
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 196

def find_discounts(options)
	conditions = {:resource_id => nil}
	conditions[:resource_id] = options[:resource].id.to_s if options[:resource]
	Auth.configuration.discount_class.constantize.where(conditions)
end

#get_payment_status(payment, discount_object_id) ⇒ Object

@param payment_id : the id of the payment @param discount_object_id : the id of the discount object @return payment_status: returns the status that should be set for the payment. @working: this def will match a discount object with the provided, id, and then filter the resulting fields to include any of the three arrays “pending”,“verified” or “declined” as long as they contain the payment id. It will return a payment_status that is to be set for the payment, based on teh result. If it finds that none of the arrays contain the payment id, then it will do a find and update to add the payment_id to the pending_array.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 250

def get_payment_status(payment,discount_object_id)



	results = Auth.configuration.discount_class.constantize.collection.aggregate([

		{
			"$match" => {
			"_id" => BSON::ObjectId(discount_object_id)
			}
		},
		{
			"$project" => {
				"verified" => {
					"$filter" => {
	                 	"input" => "$verified",
	                 	"as" => "verified",
	                 	"cond" => { 
	                 		"$eq" => 
	                 			[ "$$verified", payment.id.to_s 
	                 			] 
	                 	}
	            	}
				},
				"pending" => {
					"$filter" => {
	                 	"input" => "$pending",
	                 	"as" => "pending",
	                 	"cond" => { 
	                 		"$eq" => 
	                 			[ "$$pending", payment.id.to_s 
	                 			] 
	                 	}
	            	}
				},
				"declined" => {
					"$filter" => {
	                 	"input" => "$declined",
	                 	"as" => "declined",
	                 	"cond" => { 
	                 		"$eq" => 
	                 			[ "$$declined", payment.id.to_s 
	                 			] 
	                 	}
	            	}
				}
			}
		}

	])

	results_as_mongoid_docs = []

	results.each do |r|
		results_as_mongoid_docs << Mongoid::Factory.from_db(Auth.configuration.discount_class.constantize,r)
	end

	if results_as_mongoid_docs.empty?
		## this discont object does not exist
		## return 0
		#puts "------------------------there was no such discount object-----------------------------"
		0
	elsif !results_as_mongoid_docs[0].verified.empty?
		## it has been verified
		## so return 1
		puts "it has been verified"
		1
	elsif !results_as_mongoid_docs[0].declined.empty?
		## it has been declined
		## so return 0
		puts "it has been declined"
		0
	elsif !results_as_mongoid_docs[0].pending.empty?
		## it has not yet been acted on
		## so return nil
		puts "it is still pending."
		nil 
	else
		## it has to still be added
		## so in this case also the payment status will remain nil
		## but this has to indicate that it should be still added.
		## execute a find_and_update, where the payment id is not present in any of the three arrays, and then add it to the pending array.
		puts "came to add pending discount."
		doc_after = self.add_pending_discount(payment.id.to_s,discount_object_id)
		return 0 unless doc_after
		return nil
	end

end

#use_discount(discount, payment, enforce_single_use = true) ⇒ Object

decrements the count by 1 if enforce_single_use is passed as false, a given user can utilize the discount any number of times. checks that the count of the discount_object is greater than one before doing this. returns the updated document after the update is complete. if the document returned afterwards is nil, then it means that the payment_status will be set as failed. otherwise, set the payment_status as 1.



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 348

def use_discount(discount,payment,enforce_single_use = true)

	## so we can check here if the cart has any items pending, before running this.
	## so we can only use the discount if all cart items are accepted
	if discount.cart_id
		discount.cart_can_create_discount_coupons
	end

	if discount.errors.full_messages.empty?
	
		query_hash = {
			:_id => discount.id.to_s,
			:pending => {"$ne" => payment.id.to_s},
			:declined => {"$ne" => payment.id.to_s},
			:count => {"$gte" => 1}
		}
		if enforce_single_use == true
			query_hash[:used_by_users => {"$ne" => payment.resource_id.to_s}]
		end
		updated_doc = Auth.configuration.discount_class.constantize.where(query_hash).find_one_and_update({
				"$inc" => {
					:count => -1
				},
				"$push" => {
					:used_by_users => payment.resource_id.to_s
				}
			},
			{
				:return_document => :after
			}
		)

		return 0 if updated_doc.nil?
		return 1

	else
		return 0
	end

end