mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
calculateBalance: If we redeem a link, make sure we do not consider its amount as blocked when calculating the balance
This commit is contained in:
parent
a9ba92f67b
commit
fa7f2b9d87
@ -4,6 +4,7 @@ import { Transaction } from '@entity/Transaction'
|
||||
import { Decay } from '@model/Decay'
|
||||
import { getCustomRepository } from '@dbTools/typeorm'
|
||||
import { TransactionLinkRepository } from '@repository/TransactionLink'
|
||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||
|
||||
function isStringBoolean(value: string): boolean {
|
||||
const lowerValue = value.toLowerCase()
|
||||
@ -21,6 +22,7 @@ async function calculateBalance(
|
||||
userId: number,
|
||||
amount: Decimal,
|
||||
time: Date,
|
||||
transactionLink?: dbTransactionLink | null,
|
||||
): Promise<{ balance: Decimal; decay: Decay; lastTransactionId: number } | null> {
|
||||
const lastTransaction = await Transaction.findOne({ userId }, { order: { balanceDate: 'DESC' } })
|
||||
if (!lastTransaction) return null
|
||||
@ -32,7 +34,13 @@ async function calculateBalance(
|
||||
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
||||
const { sumHoldAvailableAmount } = await transactionLinkRepository.summary(userId, time)
|
||||
|
||||
if (balance.minus(sumHoldAvailableAmount.toString()).lessThan(0)) {
|
||||
// If we want to redeem a link we need to make sure that the link amount is not calculated as blocked
|
||||
// else we cannot redeem links which are more or equal to half of what an account actually owns
|
||||
const sumHoldAvailableAmountMinusTransactionLink = transactionLink
|
||||
? sumHoldAvailableAmount.minus(transactionLink.amount.toString())
|
||||
: sumHoldAvailableAmount
|
||||
|
||||
if (balance.minus(sumHoldAvailableAmountMinusTransactionLink.toString()).lessThan(0)) {
|
||||
return null
|
||||
}
|
||||
return { balance, lastTransactionId: lastTransaction.id, decay }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user