Module: Auth::Concerns::Shopping::DiscountConcern::ClassMethods
- Defined in:
- app/models/auth/concerns/shopping/discount_concern.rb
Instance Method Summary collapse
- #add_pending_discount(payment_id, discount_object_id) ⇒ Object
- #find_discounts(options) ⇒ Object
- #get_payment_status(payment, discount_object_id) ⇒ Object
-
#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.
Instance Method Details
#add_pending_discount(payment_id, discount_object_id) ⇒ Object
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 243 244 245 246 247 248 249 |
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 215 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
205 206 207 208 209 |
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 205 def find_discounts() conditions = {:resource_id => nil} conditions[:resource_id] = [:resource].id.to_s if [: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.
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 339 340 341 342 343 344 345 |
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 257 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.
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 388 389 390 391 392 393 394 |
# File 'app/models/auth/concerns/shopping/discount_concern.rb', line 355 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..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 |