1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/services/wallet_service.rb

41 lines
1.4 KiB
Ruby
Raw Normal View History

2016-07-05 13:20:25 +02:00
class WalletService
def initialize(user: nil, wallet: nil)
@user = user
@wallet = wallet
end
## credit an amount to wallet, if credit success then return a wallet transaction and notify to admin
2016-07-05 13:20:25 +02:00
def credit(amount)
ActiveRecord::Base.transaction do
if @wallet.credit(amount)
transaction = WalletTransaction.new(user: @user, wallet: @wallet, transaction_type: 'credit', amount: amount)
if transaction.save
NotificationCenter.call type: 'notify_user_wallet_is_credited',
receiver: @wallet.user,
attached_object: transaction
NotificationCenter.call type: 'notify_admin_user_wallet_is_credited',
receiver: User.admins,
attached_object: transaction
return transaction
end
end
raise ActiveRecord::Rollback
2016-07-05 13:20:25 +02:00
end
return false
end
## debit an amount to wallet, if debit success then return a wallet transaction
def debit(amount, transactable)
ActiveRecord::Base.transaction do
if @wallet.debit(amount)
transaction = WalletTransaction.new(user: @user, wallet: @wallet, transaction_type: 'debit', amount: amount, transactable: transactable)
if transaction.save
return transaction
end
end
raise ActiveRecord::Rollback
end
return false
2016-07-05 13:20:25 +02:00
end
end