Class: SpreeCmCommissioner::VotingCredits::AdminGrant

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/voting_credits/admin_grant.rb

Constant Summary collapse

VOTABLE_CLASSES =
{
  'VotingSession' => SpreeCmCommissioner::VotingSession,
  'ShowEpisode' => SpreeCmCommissioner::ShowEpisode,
  'Show' => SpreeCmCommissioner::Show
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(user:, votable_type:, votable_id:, granted_by:, **amounts) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/spree_cm_commissioner/voting_credits/admin_grant.rb', line 12

def call(user:, votable_type:, votable_id:, granted_by:, **amounts)
  klass = VOTABLE_CLASSES[votable_type]
  return failure(nil, "Invalid votable type: #{votable_type}") unless klass

  votable = klass.find(votable_id)

  ActiveRecord::Base.transaction do
    credit = SpreeCmCommissioner::VotingCredit.create!(
      user: user,
      votable: votable,
      category: :admin_grant,
      credit_type: :non_expiring,
      free_votes_amount: amounts[:free_votes_amount].to_i,
      paid_votes_amount: amounts[:paid_votes_amount].to_i,
      tenant_id: user.tenant_id,
      created_by: granted_by
    )

    success(credit)
  end
rescue ActiveRecord::RecordInvalid => e
  failure(nil, e.record.errors.full_messages.join(', '))
end