mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
refactroing wallet service, credit/debit in transaction of db, if have an error we rollback all
This commit is contained in:
parent
56f2ea4172
commit
9ba8e46450
@ -7,4 +7,5 @@ class WalletTransaction < ActiveRecord::Base
|
||||
belongs_to :transactable, polymorphic: true
|
||||
|
||||
validates_inclusion_of :transaction_type, in: %w( credit debit )
|
||||
validates :user, :wallet, presence: true
|
||||
end
|
||||
|
@ -4,25 +4,36 @@ class WalletService
|
||||
@wallet = wallet
|
||||
end
|
||||
|
||||
## credit an amount to wallet, if credit success then return a wallet transaction and notify to admin
|
||||
def credit(amount)
|
||||
if @wallet.credit(amount)
|
||||
transaction = WalletTransaction.create(user: @user, wallet: @wallet, transaction_type: 'credit', amount: amount)
|
||||
|
||||
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 true
|
||||
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
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
## debit an amount to wallet, if debit success then return a wallet transaction
|
||||
def debit(amount, transactable)
|
||||
if @wallet.debit(amount)
|
||||
WalletTransaction.create(user: @user, wallet: @wallet, transaction_type: 'debit', amount: amount, transactable: transactable)
|
||||
return true
|
||||
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
|
||||
end
|
||||
|
@ -2,7 +2,9 @@ require 'test_helper'
|
||||
|
||||
class WalletTransactionTest < ActiveSupport::TestCase
|
||||
test 'transaction type must be credit or debit' do
|
||||
transaction = WalletTransaction.new amount: 5
|
||||
@jdupond = User.find_by(username: 'jdupond')
|
||||
@jdupond_wallet = @jdupond.wallet
|
||||
transaction = WalletTransaction.new amount: 5, user: @jdupond, wallet: @jdupond_wallet
|
||||
transaction.transaction_type = 'credit'
|
||||
assert transaction.valid?
|
||||
transaction.transaction_type = 'debit'
|
||||
|
@ -18,22 +18,45 @@ class WalletServiceTest < ActiveSupport::TestCase
|
||||
|
||||
test 'create a credit transaction after credit amount to wallet' do
|
||||
service = WalletService.new(user: @admin, wallet: @jdupond_wallet)
|
||||
expected_amount = @jdupond_wallet.amount + 10
|
||||
assert_equal 0, @jdupond_wallet.wallet_transactions.count
|
||||
assert service.credit(10)
|
||||
transaction = @jdupond_wallet.wallet_transactions.first
|
||||
transaction = service.credit(10)
|
||||
@jdupond_wallet.reload
|
||||
assert transaction
|
||||
assert_equal @jdupond_wallet.amount, expected_amount
|
||||
assert_equal transaction.transaction_type, 'credit'
|
||||
assert_equal transaction.amount, 10
|
||||
assert_equal transaction.user, @admin
|
||||
assert_equal transaction.wallet, @jdupond_wallet
|
||||
end
|
||||
|
||||
test 'create a debit transaction after debit amoutn to wallet' do
|
||||
test 'create a debit transaction after debit amount to wallet' do
|
||||
service = WalletService.new(user: @vlonchamp, wallet: @vlonchamp_wallet)
|
||||
assert service.debit(5, nil)
|
||||
transaction = @vlonchamp_wallet.wallet_transactions.last
|
||||
expected_amount = @vlonchamp_wallet.amount - 5
|
||||
transaction = service.debit(5, nil)
|
||||
@vlonchamp_wallet.reload
|
||||
assert transaction
|
||||
assert_equal @vlonchamp_wallet.amount, expected_amount
|
||||
assert_equal transaction.transaction_type, 'debit'
|
||||
assert_equal transaction.amount, 5
|
||||
assert_equal transaction.user, @vlonchamp
|
||||
assert_equal transaction.wallet, @vlonchamp_wallet
|
||||
end
|
||||
|
||||
test 'dont debit amount > wallet amount' do
|
||||
service = WalletService.new(user: @vlonchamp, wallet: @vlonchamp_wallet)
|
||||
expected_amount = @vlonchamp_wallet.amount
|
||||
service.debit(100, nil)
|
||||
@vlonchamp_wallet.reload
|
||||
assert_equal @vlonchamp_wallet.amount, expected_amount
|
||||
end
|
||||
|
||||
test 'rollback debited amount if has an error when create wallet transaction' do
|
||||
service = WalletService.new(wallet: @vlonchamp_wallet)
|
||||
expected_amount = @vlonchamp_wallet.amount
|
||||
transaction = service.debit(5, nil)
|
||||
@vlonchamp_wallet.reload
|
||||
assert_equal @vlonchamp_wallet.amount, expected_amount
|
||||
assert_not transaction
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user