1
0
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:
Sylvain 2022-10-11 11:41:44 +02:00
parent da11a592e8
commit 3e6763f14a
2 changed files with 20 additions and 19 deletions

View File

@ -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

View File

@ -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