From 55ff0ee96437bb60fb495520c243c264c9ed47e8 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 12 Apr 2022 22:52:51 +0200 Subject: [PATCH] add roundedDecay field to Decay Model. This is used to calibrate the decay since the last transaction --- backend/src/graphql/model/Decay.ts | 5 +++++ backend/src/graphql/model/Transaction.ts | 3 +++ backend/src/util/decay.ts | 5 +++++ backend/src/util/virtualTransactions.ts | 4 ++-- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/src/graphql/model/Decay.ts b/backend/src/graphql/model/Decay.ts index 4416a738f..591294df5 100644 --- a/backend/src/graphql/model/Decay.ts +++ b/backend/src/graphql/model/Decay.ts @@ -4,6 +4,7 @@ import Decimal from 'decimal.js-light' interface DecayInterface { balance: Decimal decay: Decimal + roundedDecay: Decimal start: Date | null end: Date | null duration: number | null @@ -14,6 +15,7 @@ export class Decay { constructor(data: DecayInterface) { this.balance = data.balance this.decay = data.decay + this.roundedDecay = data.roundedDecay this.start = data.start this.end = data.end this.duration = data.duration @@ -25,6 +27,9 @@ export class Decay { @Field(() => Decimal) decay: Decimal + @Field(() => Decimal) + roundedDecay: Decimal + @Field(() => Date, { nullable: true }) start: Date | null diff --git a/backend/src/graphql/model/Transaction.ts b/backend/src/graphql/model/Transaction.ts index 523ca06bc..b6f75f745 100644 --- a/backend/src/graphql/model/Transaction.ts +++ b/backend/src/graphql/model/Transaction.ts @@ -20,6 +20,7 @@ export class Transaction { this.decay = new Decay({ balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN), decay: new Decimal(0), + roundedDecay: new Decimal(0), start: null, end: null, duration: null, @@ -28,6 +29,8 @@ export class Transaction { this.decay = new Decay({ balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN), decay: transaction.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR), + // TODO + roundedDecay: new Decimal(0), start: transaction.decayStart, end: transaction.balanceDate, duration: Math.round( diff --git a/backend/src/util/decay.ts b/backend/src/util/decay.ts index 36f83f23f..48674dc50 100644 --- a/backend/src/util/decay.ts +++ b/backend/src/util/decay.ts @@ -29,6 +29,7 @@ function calculateDecay( const decay: Decay = { balance: amount, decay: new Decimal(0), + roundedDecay: new Decimal(0), start: null, end: null, duration: null, @@ -52,6 +53,10 @@ function calculateDecay( decay.end = to decay.balance = decayFormula(amount, decay.duration) decay.decay = decay.balance.minus(amount) + decay.roundedDecay = amount + .toDecimalPlaces(2, Decimal.ROUND_DOWN) + .minus(decay.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN).toString()) + .mul(-1) return decay } diff --git a/backend/src/util/virtualTransactions.ts b/backend/src/util/virtualTransactions.ts index 920fc4201..2f37ad188 100644 --- a/backend/src/util/virtualTransactions.ts +++ b/backend/src/util/virtualTransactions.ts @@ -68,12 +68,12 @@ const virtualDecayTransaction = ( userId: -1, previous: -1, typeId: TransactionTypeId.DECAY, - 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 + amount: decay.decay ? decay.roundedDecay : 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.toDecimalPlaces(2, Decimal.ROUND_FLOOR) : new Decimal(0), + decay: decay.decay ? decay.roundedDecay : new Decimal(0), decayStart: decay.start, memo: '', creationDate: null,