Class: SpreeCmCommissioner::VotingCredits::Allocate
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::VotingCredits::Allocate
- Defined in:
- app/services/spree_cm_commissioner/voting_credits/allocate.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(order) ⇒ Allocate
constructor
A new instance of Allocate.
Constructor Details
#initialize(order) ⇒ Allocate
Returns a new instance of Allocate.
4 5 6 |
# File 'app/services/spree_cm_commissioner/voting_credits/allocate.rb', line 4 def initialize(order) @order = order end |
Instance Method Details
#call ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/services/spree_cm_commissioner/voting_credits/allocate.rb', line 8 def call return unless @order.completed? return if episode_line_items.empty? ActiveRecord::Base.transaction do voting_credit = find_or_create_credit allocations = episode_line_items.map do |line_item| SpreeCmCommissioner::VotingCredits::CreditCalculator.new(line_item).call end total_paid_vote_credits = allocations.sum # Use with_lock to ensure we have a lock on the voting credit record while we: # - Modify data inside the block # - Need to prevent race conditions voting_credit.with_lock do voting_credit.paid_votes_amount += total_paid_vote_credits voting_credit.save! end record_transaction(voting_credit, total_paid_vote_credits) if total_paid_vote_credits.positive? end rescue ActiveRecord::RecordNotUnique # already allocated — safe to ignore rescue StandardError => e CmAppLogger.error( label: 'VotingCredits::Allocate failed', data: { order_id: @order.try(:id), error_class: e.class.name, error_message: e. } ) false end |