refactor balance resolver code

This commit is contained in:
Ulf Gebhardt 2021-10-02 12:34:40 +02:00
parent 16d530489d
commit 9953945bb5
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD

View File

@ -20,23 +20,23 @@ export class BalanceResolver {
const userEntity = await userRepository.findByPubkeyHex(context.pubKey) const userEntity = await userRepository.findByPubkeyHex(context.pubKey)
const balanceEntity = await balanceRepository.findByUser(userEntity.id) const balanceEntity = await balanceRepository.findByUser(userEntity.id)
let balance: Balance
const now = new Date() const now = new Date()
if (balanceEntity) {
balance = new Balance({ // No balance found
balance: roundFloorFrom4(balanceEntity.amount), if (!balanceEntity) {
decay: roundFloorFrom4( return new Balance({
await calculateDecay(balanceEntity.amount, balanceEntity.recordDate, now),
),
decay_date: now.toString(),
})
} else {
balance = new Balance({
balance: 0, balance: 0,
decay: 0, decay: 0,
decay_date: now.toString(), decay_date: now.toString(),
}) })
} }
return balance
return new Balance({
balance: roundFloorFrom4(balanceEntity.amount),
decay: roundFloorFrom4(
await calculateDecay(balanceEntity.amount, balanceEntity.recordDate, now),
),
decay_date: now.toString(),
})
} }
} }