mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
balance includes transactions to hold available, calculateBalance checks for pending transaction links
This commit is contained in:
parent
96dcdf0170
commit
8107066d2b
@ -2,7 +2,7 @@
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
import { Resolver, Args, Authorized, Ctx, Mutation } from 'type-graphql'
|
||||
import { getCustomRepository, MoreThan } from '@dbTools/typeorm'
|
||||
import { getCustomRepository } from '@dbTools/typeorm'
|
||||
import { TransactionLink } from '@model/TransactionLink'
|
||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||
import TransactionLinkArgs from '@arg/TransactionLinkArgs'
|
||||
@ -43,24 +43,10 @@ export class TransactionLinkResolver {
|
||||
const createdDate = new Date()
|
||||
const validUntil = transactionLinkExpireDate(createdDate)
|
||||
|
||||
const holdAvailableAmount = amount.add(
|
||||
calculateDecay(amount, createdDate, validUntil).decay.mul(-1),
|
||||
)
|
||||
|
||||
const openTransactionLinks = await dbTransactionLink.find({
|
||||
select: ['holdAvailableAmount'],
|
||||
where: { userId: user.id, redeemedAt: null, validUntil: MoreThan(createdDate) },
|
||||
})
|
||||
|
||||
const holdAvailable = openTransactionLinks.reduce(
|
||||
(previousValue, currentValue) =>
|
||||
previousValue.add(currentValue.holdAvailableAmount.toString()),
|
||||
holdAvailableAmount,
|
||||
)
|
||||
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
|
||||
|
||||
// validate amount
|
||||
// TODO taken from transaction resolver, duplicate code
|
||||
const sendBalance = await calculateBalance(user.id, holdAvailable.mul(-1), createdDate)
|
||||
const sendBalance = await calculateBalance(user.id, holdAvailableAmount.mul(-1), createdDate)
|
||||
if (!sendBalance) {
|
||||
throw new Error("user hasn't enough GDD or amount is < 0")
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ import { Transaction as dbTransaction } from '@entity/Transaction'
|
||||
|
||||
import { apiPost } from '@/apis/HttpRequest'
|
||||
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
||||
import { calculateBalance, isHexPublicKey } from '@/util/validate'
|
||||
import { calculateBalance, isHexPublicKey, holdAvailable } from '@/util/validate'
|
||||
import { RIGHTS } from '@/auth/RIGHTS'
|
||||
import { User } from '@model/User'
|
||||
import { communityUser } from '@/util/communityUser'
|
||||
@ -127,9 +127,13 @@ export class TransactionResolver {
|
||||
transactions.push(new Transaction(userTransaction, self, linkedUser))
|
||||
})
|
||||
|
||||
const toHoldAvailable = await holdAvailable(user.id, now)
|
||||
|
||||
// Construct Result
|
||||
return new TransactionList(
|
||||
calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, now).balance,
|
||||
calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, now).balance.minus(
|
||||
toHoldAvailable.toString(),
|
||||
),
|
||||
transactions,
|
||||
userTransactionsCount,
|
||||
balanceGDT,
|
||||
|
||||
@ -2,6 +2,8 @@ import { calculateDecay } from './decay'
|
||||
import Decimal from 'decimal.js-light'
|
||||
import { Transaction } from '@entity/Transaction'
|
||||
import { Decay } from '@model/Decay'
|
||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||
import { MoreThan } from '@dbTools/typeorm'
|
||||
|
||||
function isStringBoolean(value: string): boolean {
|
||||
const lowerValue = value.toLowerCase()
|
||||
@ -24,12 +26,26 @@ async function calculateBalance(
|
||||
if (!lastTransaction) return null
|
||||
|
||||
const decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, time)
|
||||
|
||||
// TODO why we have to use toString() here?
|
||||
const balance = decay.balance.add(amount.toString())
|
||||
if (balance.lessThan(0)) {
|
||||
const toHoldAvailable = await holdAvailable(userId, time)
|
||||
if (balance.minus(toHoldAvailable.toString()).lessThan(0)) {
|
||||
return null
|
||||
}
|
||||
return { balance, lastTransactionId: lastTransaction.id, decay }
|
||||
}
|
||||
|
||||
export { isHexPublicKey, calculateBalance, isStringBoolean }
|
||||
async function holdAvailable(userId: number, date: Date): Promise<Decimal> {
|
||||
const openTransactionLinks = await dbTransactionLink.find({
|
||||
select: ['holdAvailableAmount'],
|
||||
where: { userId, redeemedAt: null, validUntil: MoreThan(date) },
|
||||
})
|
||||
|
||||
return openTransactionLinks.reduce(
|
||||
(previousValue, currentValue) => previousValue.add(currentValue.holdAvailableAmount.toString()),
|
||||
new Decimal(0),
|
||||
)
|
||||
}
|
||||
|
||||
export { isHexPublicKey, calculateBalance, isStringBoolean, holdAvailable }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user