use interface for decay constructor

This commit is contained in:
Moriz Wahl 2022-04-12 21:43:18 +02:00
parent a6e7036339
commit 0bcf2cbd8c
2 changed files with 30 additions and 26 deletions

View File

@ -1,20 +1,22 @@
import { ObjectType, Field, Int } from 'type-graphql'
import Decimal from 'decimal.js-light'
interface DecayInterface {
balance: Decimal
decay: Decimal
start: Date | null
end: Date | null
duration: number | null
}
@ObjectType()
export class Decay {
constructor(
balance: Decimal,
decay: Decimal,
start: Date | null,
end: Date | null,
duration: number | null,
) {
this.balance = balance
this.decay = decay
this.start = start
this.end = end
this.duration = duration
constructor(data: DecayInterface) {
this.balance = data.balance
this.decay = data.decay
this.start = data.start
this.end = data.end
this.duration = data.duration
}
@Field(() => Decimal)

View File

@ -17,21 +17,23 @@ export class Transaction {
this.balanceDate = transaction.balanceDate
if (!transaction.decayStart) {
// TODO: hot fix, we should separate decay calculation from decay graphql model
this.decay = new Decay(
transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
new Decimal(0),
null,
null,
null,
)
this.decay = new Decay({
balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
decay: new Decimal(0),
start: null,
end: null,
duration: null,
})
} else {
this.decay = new Decay(
transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
transaction.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR),
transaction.decayStart,
transaction.balanceDate,
Math.round((transaction.balanceDate.getTime() - transaction.decayStart.getTime()) / 1000),
)
this.decay = new Decay({
balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
decay: transaction.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR),
start: transaction.decayStart,
end: transaction.balanceDate,
duration: Math.round(
(transaction.balanceDate.getTime() - transaction.decayStart.getTime()) / 1000,
),
})
}
this.memo = transaction.memo
this.creationDate = transaction.creationDate