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
42
43
44
45
46
47
48
49
|
# 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
total_paid_vote_credits = unallocated_line_items.reject { |li| li.product.claimable_vote_package }.sum do |line_item|
SpreeCmCommissioner::VotingCredits::CreditCalculator.new(line_item).call
end
total_free_vote_credits = unallocated_line_items.select { |li| li.product.claimable_vote_package }.sum do |line_item|
SpreeCmCommissioner::VotingCredits::CreditCalculator.new(line_item).call
end
voting_credit.with_lock do
voting_credit.paid_votes_amount += total_paid_vote_credits
voting_credit.free_votes_amount += total_free_vote_credits
voting_credit.save!
end
record_transaction(voting_credit, total_paid_vote_credits) if total_paid_vote_credits.positive?
record_transaction_free(voting_credit, total_free_vote_credits) if total_free_vote_credits.positive?
end
record_free_claims
rescue ActiveRecord::RecordNotUnique
rescue StandardError => e
CmAppLogger.error(
label: 'VotingCredits::Allocate failed',
data: {
order_id: @order.try(:id),
error_class: e.class.name,
error_message: e.message
}
)
false
end
|