mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
(bug) wallet transation not returned if success
This commit is contained in:
parent
da11a592e8
commit
3e6763f14a
@ -9,33 +9,33 @@ class WalletService
|
||||
|
||||
## credit an amount to wallet, if credit success then return a wallet transaction and notify to admin
|
||||
def credit(amount)
|
||||
transaction = nil
|
||||
ActiveRecord::Base.transaction do
|
||||
if @wallet.credit(amount)
|
||||
if @wallet&.credit(amount)
|
||||
transaction = WalletTransaction.new(
|
||||
invoicing_profile: @user.invoicing_profile,
|
||||
invoicing_profile: @user&.invoicing_profile,
|
||||
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_and_managers,
|
||||
attached_object: transaction
|
||||
transaction
|
||||
end
|
||||
raise ActiveRecord::Rollback unless 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_and_managers,
|
||||
attached_object: transaction
|
||||
end
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
false
|
||||
transaction
|
||||
end
|
||||
|
||||
## debit an amount to wallet, if debit success then return a wallet transaction
|
||||
def debit(amount)
|
||||
transaction = nil
|
||||
ActiveRecord::Base.transaction do
|
||||
if @wallet.debit(amount)
|
||||
if @wallet&.debit(amount)
|
||||
transaction = WalletTransaction.new(
|
||||
invoicing_profile: @user&.invoicing_profile,
|
||||
wallet: @wallet,
|
||||
@ -43,11 +43,10 @@ class WalletService
|
||||
amount: amount
|
||||
)
|
||||
|
||||
transaction if transaction.save
|
||||
raise ActiveRecord::Rollback unless transaction.save
|
||||
end
|
||||
raise ActiveRecord::Rollback
|
||||
end
|
||||
false
|
||||
transaction
|
||||
end
|
||||
|
||||
## create a refund invoice associated with the given wallet transaction
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class WalletServiceTest < ActiveSupport::TestCase
|
||||
@ -54,9 +56,9 @@ class WalletServiceTest < ActiveSupport::TestCase
|
||||
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)
|
||||
transaction = service.debit('error')
|
||||
@vlonchamp_wallet.reload
|
||||
assert_equal @vlonchamp_wallet.amount, expected_amount
|
||||
assert_equal expected_amount, @vlonchamp_wallet.amount
|
||||
assert_not transaction
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user