mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +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
|
## credit an amount to wallet, if credit success then return a wallet transaction and notify to admin
|
||||||
def credit(amount)
|
def credit(amount)
|
||||||
|
transaction = nil
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
if @wallet.credit(amount)
|
if @wallet&.credit(amount)
|
||||||
transaction = WalletTransaction.new(
|
transaction = WalletTransaction.new(
|
||||||
invoicing_profile: @user.invoicing_profile,
|
invoicing_profile: @user&.invoicing_profile,
|
||||||
wallet: @wallet,
|
wallet: @wallet,
|
||||||
transaction_type: 'credit',
|
transaction_type: 'credit',
|
||||||
amount: amount
|
amount: amount
|
||||||
)
|
)
|
||||||
if transaction.save
|
raise ActiveRecord::Rollback unless transaction.save
|
||||||
NotificationCenter.call type: 'notify_user_wallet_is_credited',
|
|
||||||
receiver: @wallet.user,
|
NotificationCenter.call type: 'notify_user_wallet_is_credited',
|
||||||
attached_object: transaction
|
receiver: @wallet&.user,
|
||||||
NotificationCenter.call type: 'notify_admin_user_wallet_is_credited',
|
attached_object: transaction
|
||||||
receiver: User.admins_and_managers,
|
NotificationCenter.call type: 'notify_admin_user_wallet_is_credited',
|
||||||
attached_object: transaction
|
receiver: User.admins_and_managers,
|
||||||
transaction
|
attached_object: transaction
|
||||||
end
|
|
||||||
end
|
end
|
||||||
raise ActiveRecord::Rollback
|
|
||||||
end
|
end
|
||||||
false
|
transaction
|
||||||
end
|
end
|
||||||
|
|
||||||
## debit an amount to wallet, if debit success then return a wallet transaction
|
## debit an amount to wallet, if debit success then return a wallet transaction
|
||||||
def debit(amount)
|
def debit(amount)
|
||||||
|
transaction = nil
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
if @wallet.debit(amount)
|
if @wallet&.debit(amount)
|
||||||
transaction = WalletTransaction.new(
|
transaction = WalletTransaction.new(
|
||||||
invoicing_profile: @user&.invoicing_profile,
|
invoicing_profile: @user&.invoicing_profile,
|
||||||
wallet: @wallet,
|
wallet: @wallet,
|
||||||
@ -43,11 +43,10 @@ class WalletService
|
|||||||
amount: amount
|
amount: amount
|
||||||
)
|
)
|
||||||
|
|
||||||
transaction if transaction.save
|
raise ActiveRecord::Rollback unless transaction.save
|
||||||
end
|
end
|
||||||
raise ActiveRecord::Rollback
|
|
||||||
end
|
end
|
||||||
false
|
transaction
|
||||||
end
|
end
|
||||||
|
|
||||||
## create a refund invoice associated with the given wallet transaction
|
## create a refund invoice associated with the given wallet transaction
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class WalletServiceTest < ActiveSupport::TestCase
|
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
|
test 'rollback debited amount if has an error when create wallet transaction' do
|
||||||
service = WalletService.new(wallet: @vlonchamp_wallet)
|
service = WalletService.new(wallet: @vlonchamp_wallet)
|
||||||
expected_amount = @vlonchamp_wallet.amount
|
expected_amount = @vlonchamp_wallet.amount
|
||||||
transaction = service.debit(5)
|
transaction = service.debit('error')
|
||||||
@vlonchamp_wallet.reload
|
@vlonchamp_wallet.reload
|
||||||
assert_equal @vlonchamp_wallet.amount, expected_amount
|
assert_equal expected_amount, @vlonchamp_wallet.amount
|
||||||
assert_not transaction
|
assert_not transaction
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user