module Workarea decorate Order, with: :kount do decorated do field :kount_session_id, type: String field :kount_decision, type: Symbol scope :not_under_review, -> { where(:kount_decision.ne => :review) } before_save :set_kount_decision, if: :fraud_decision? end class_methods do # 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. # # Doesn't include orders under review or declined by kount # # @return [Mongoid::Criteria] def expired_in_checkout super.nin(kount_decision: [:review, :decline]) end # Overriding method from core/models/order/queries.rb module def need_reminding super.where(kount_decision: nil) end # Find a current cart for a session. Returns a new order if one cannot be found. # # @param params [Hash] # @return [Order] # def find_current(params = {}) if params[:id].present? Order.not_placed.not_under_review.find(params[:id].to_s) elsif params[:user_id].present? Order.recently_updated.not_placed.not_under_review.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 end # Whether the FraudDecision#decision is `:review`. def under_review? !placed? && !canceled? && kount_decision == :review end def abandoned? super && kount_decision.nil? end def set_kount_decision self.kount_decision = fraud_decision.decision end end end