mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #1699 from gradido/fix_decay
Fix: Correct calculation of decay
This commit is contained in:
commit
06e5c93fb5
@ -54,19 +54,23 @@ export class BalanceResolver {
|
||||
},
|
||||
})
|
||||
|
||||
// The decay is always calculated on the last booked transaction
|
||||
const calculatedDecay = calculateDecay(
|
||||
lastTransaction.balance,
|
||||
lastTransaction.balanceDate,
|
||||
now,
|
||||
)
|
||||
|
||||
// The final balance is reduced by the link amount withheld
|
||||
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
||||
const { sumHoldAvailableAmount } = context.sumHoldAvailableAmount
|
||||
? { sumHoldAvailableAmount: context.sumHoldAvailableAmount }
|
||||
: await transactionLinkRepository.summary(user.id, now)
|
||||
|
||||
const calculatedDecay = calculateDecay(
|
||||
lastTransaction.balance.minus(sumHoldAvailableAmount.toString()),
|
||||
lastTransaction.balanceDate,
|
||||
now,
|
||||
)
|
||||
|
||||
return new Balance({
|
||||
balance: calculatedDecay.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN), // round towards zero
|
||||
balance: calculatedDecay.balance
|
||||
.minus(sumHoldAvailableAmount.toString())
|
||||
.toDecimalPlaces(2, Decimal.ROUND_DOWN), // round towards zero
|
||||
decay: calculatedDecay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR), // round towards - infinity
|
||||
lastBookedBalance: lastTransaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
|
||||
balanceGDT,
|
||||
|
||||
@ -203,12 +203,15 @@ export class TransactionResolver {
|
||||
|
||||
// decay & link transactions
|
||||
if (currentPage === 1 && order === Order.DESC) {
|
||||
// The virtual decay is always on the booked amount, not including the generated, not yet booked links,
|
||||
// since the decay is substantially different when the amount is less
|
||||
transactions.push(
|
||||
virtualDecayTransaction(
|
||||
lastTransaction.balance.minus(sumHoldAvailableAmount.toString()),
|
||||
lastTransaction.balance,
|
||||
lastTransaction.balanceDate,
|
||||
now,
|
||||
self,
|
||||
sumHoldAvailableAmount,
|
||||
),
|
||||
)
|
||||
// virtual transaction for pending transaction-links sum
|
||||
|
||||
@ -59,6 +59,7 @@ const virtualDecayTransaction = (
|
||||
balanceDate: Date,
|
||||
time: Date = new Date(),
|
||||
user: User,
|
||||
holdAvailabeAmount: Decimal,
|
||||
): Transaction => {
|
||||
const decay = calculateDecay(balance, balanceDate, time)
|
||||
// const balance = decay.balance.minus(lastTransaction.balance)
|
||||
@ -67,10 +68,12 @@ const virtualDecayTransaction = (
|
||||
userId: -1,
|
||||
previous: -1,
|
||||
typeId: TransactionTypeId.DECAY,
|
||||
amount: decay.decay ? decay.decay : new Decimal(0), // new Decimal(0), // this kinda is wrong, but helps with the frontend query
|
||||
balance: decay.balance,
|
||||
amount: decay.decay ? decay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR) : new Decimal(0), // new Decimal(0), // this kinda is wrong, but helps with the frontend query
|
||||
balance: decay.balance
|
||||
.minus(holdAvailabeAmount.toString())
|
||||
.toDecimalPlaces(2, Decimal.ROUND_DOWN),
|
||||
balanceDate: time,
|
||||
decay: decay.decay ? decay.decay : new Decimal(0),
|
||||
decay: decay.decay ? decay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR) : new Decimal(0),
|
||||
decayStart: decay.start,
|
||||
memo: '',
|
||||
creationDate: null,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user