mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
combined functions
This commit is contained in:
parent
6f36906723
commit
b02076fa49
@ -32,12 +32,103 @@ import { hasUserAmount, isHexPublicKey } from '../../util/validate'
|
|||||||
import { RIGHTS } from '../../auth/RIGHTS'
|
import { RIGHTS } from '../../auth/RIGHTS'
|
||||||
import { randomInt } from 'crypto'
|
import { randomInt } from 'crypto'
|
||||||
|
|
||||||
// Helper function
|
// helper helper function
|
||||||
async function calculateAndAddDecayTransactions(
|
async function updateStateBalance(
|
||||||
userTransactions: dbTransaction[],
|
user: dbUser,
|
||||||
decay: boolean,
|
centAmount: number,
|
||||||
skipFirstTransaction: boolean,
|
received: Date,
|
||||||
): Promise<Transaction[]> {
|
queryRunner: QueryRunner,
|
||||||
|
): Promise<dbBalance> {
|
||||||
|
let balance = await dbBalance.findOne({ userId: user.id })
|
||||||
|
if (!balance) {
|
||||||
|
balance = new dbBalance()
|
||||||
|
balance.userId = user.id
|
||||||
|
balance.amount = centAmount
|
||||||
|
balance.modified = received
|
||||||
|
} else {
|
||||||
|
const decayedBalance = calculateDecay(balance.amount, balance.recordDate, received).balance
|
||||||
|
balance.amount = Number(decayedBalance) + centAmount
|
||||||
|
balance.modified = new Date()
|
||||||
|
}
|
||||||
|
if (balance.amount <= 0) {
|
||||||
|
throw new Error('error new balance <= 0')
|
||||||
|
}
|
||||||
|
balance.recordDate = received
|
||||||
|
return queryRunner.manager.save(balance).catch((error) => {
|
||||||
|
throw new Error('error saving balance:' + error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function calculateNewBalance(
|
||||||
|
userId: number,
|
||||||
|
transactionDate: Date,
|
||||||
|
centAmount: number,
|
||||||
|
): Promise<BigInt> {
|
||||||
|
let newBalance = centAmount
|
||||||
|
const transactionRepository = getCustomRepository(TransactionRepository)
|
||||||
|
const lastUserTransaction = await transactionRepository.findLastForUser(userId)
|
||||||
|
if (lastUserTransaction) {
|
||||||
|
newBalance += Number(
|
||||||
|
calculateDecay(
|
||||||
|
Number(lastUserTransaction.balance),
|
||||||
|
lastUserTransaction.balanceDate,
|
||||||
|
transactionDate,
|
||||||
|
).balance,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newBalance <= 0) {
|
||||||
|
throw new Error('error new balance <= 0')
|
||||||
|
}
|
||||||
|
|
||||||
|
return BigInt(newBalance)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Resolver()
|
||||||
|
export class TransactionResolver {
|
||||||
|
@Authorized([RIGHTS.TRANSACTION_LIST])
|
||||||
|
@Query(() => TransactionList)
|
||||||
|
async transactionList(
|
||||||
|
@Args()
|
||||||
|
{
|
||||||
|
currentPage = 1,
|
||||||
|
pageSize = 25,
|
||||||
|
order = Order.DESC,
|
||||||
|
onlyCreations = false,
|
||||||
|
userId,
|
||||||
|
}: Paginated,
|
||||||
|
@Ctx() context: any,
|
||||||
|
): Promise<TransactionList> {
|
||||||
|
// load user
|
||||||
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
|
const user = userId
|
||||||
|
? await userRepository.findOneOrFail({ id: userId }, { withDeleted: true })
|
||||||
|
: await userRepository.findByPubkeyHex(context.pubKey)
|
||||||
|
let limit = pageSize
|
||||||
|
let offset = 0
|
||||||
|
let skipFirstTransaction = false
|
||||||
|
if (currentPage > 1) {
|
||||||
|
offset = (currentPage - 1) * pageSize - 1
|
||||||
|
limit++
|
||||||
|
}
|
||||||
|
if (offset && order === Order.ASC) {
|
||||||
|
offset--
|
||||||
|
}
|
||||||
|
const transactionRepository = getCustomRepository(TransactionRepository)
|
||||||
|
const [userTransactions, userTransactionsCount] = await transactionRepository.findByUserPaged(
|
||||||
|
user.id,
|
||||||
|
limit,
|
||||||
|
offset,
|
||||||
|
order,
|
||||||
|
onlyCreations,
|
||||||
|
)
|
||||||
|
skipFirstTransaction = userTransactionsCount > offset + limit
|
||||||
|
const decay = !(currentPage > 1)
|
||||||
|
let transactions: Transaction[] = []
|
||||||
|
if (userTransactions.length) {
|
||||||
|
if (order === Order.DESC) {
|
||||||
|
userTransactions.reverse()
|
||||||
|
}
|
||||||
const finalTransactions: Transaction[] = []
|
const finalTransactions: Transaction[] = []
|
||||||
const involvedUserIds: number[] = []
|
const involvedUserIds: number[] = []
|
||||||
|
|
||||||
@ -143,111 +234,8 @@ async function calculateAndAddDecayTransactions(
|
|||||||
finalTransactions.push(decayTransaction)
|
finalTransactions.push(decayTransaction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return finalTransactions
|
transactions = finalTransactions
|
||||||
}
|
|
||||||
|
|
||||||
// helper helper function
|
|
||||||
async function updateStateBalance(
|
|
||||||
user: dbUser,
|
|
||||||
centAmount: number,
|
|
||||||
received: Date,
|
|
||||||
queryRunner: QueryRunner,
|
|
||||||
): Promise<dbBalance> {
|
|
||||||
let balance = await dbBalance.findOne({ userId: user.id })
|
|
||||||
if (!balance) {
|
|
||||||
balance = new dbBalance()
|
|
||||||
balance.userId = user.id
|
|
||||||
balance.amount = centAmount
|
|
||||||
balance.modified = received
|
|
||||||
} else {
|
|
||||||
const decayedBalance = calculateDecay(balance.amount, balance.recordDate, received).balance
|
|
||||||
balance.amount = Number(decayedBalance) + centAmount
|
|
||||||
balance.modified = new Date()
|
|
||||||
}
|
|
||||||
if (balance.amount <= 0) {
|
|
||||||
throw new Error('error new balance <= 0')
|
|
||||||
}
|
|
||||||
balance.recordDate = received
|
|
||||||
return queryRunner.manager.save(balance).catch((error) => {
|
|
||||||
throw new Error('error saving balance:' + error)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function calculateNewBalance(
|
|
||||||
userId: number,
|
|
||||||
transactionDate: Date,
|
|
||||||
centAmount: number,
|
|
||||||
): Promise<BigInt> {
|
|
||||||
let newBalance = centAmount
|
|
||||||
const transactionRepository = getCustomRepository(TransactionRepository)
|
|
||||||
const lastUserTransaction = await transactionRepository.findLastForUser(userId)
|
|
||||||
if (lastUserTransaction) {
|
|
||||||
newBalance += Number(
|
|
||||||
calculateDecay(
|
|
||||||
Number(lastUserTransaction.balance),
|
|
||||||
lastUserTransaction.balanceDate,
|
|
||||||
transactionDate,
|
|
||||||
).balance,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newBalance <= 0) {
|
|
||||||
throw new Error('error new balance <= 0')
|
|
||||||
}
|
|
||||||
|
|
||||||
return BigInt(newBalance)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Resolver()
|
|
||||||
export class TransactionResolver {
|
|
||||||
@Authorized([RIGHTS.TRANSACTION_LIST])
|
|
||||||
@Query(() => TransactionList)
|
|
||||||
async transactionList(
|
|
||||||
@Args()
|
|
||||||
{
|
|
||||||
currentPage = 1,
|
|
||||||
pageSize = 25,
|
|
||||||
order = Order.DESC,
|
|
||||||
onlyCreations = false,
|
|
||||||
userId,
|
|
||||||
}: Paginated,
|
|
||||||
@Ctx() context: any,
|
|
||||||
): Promise<TransactionList> {
|
|
||||||
// load user
|
|
||||||
const userRepository = getCustomRepository(UserRepository)
|
|
||||||
const user = userId
|
|
||||||
? await userRepository.findOneOrFail({ id: userId }, { withDeleted: true })
|
|
||||||
: await userRepository.findByPubkeyHex(context.pubKey)
|
|
||||||
let limit = pageSize
|
|
||||||
let offset = 0
|
|
||||||
let skipFirstTransaction = false
|
|
||||||
if (currentPage > 1) {
|
|
||||||
offset = (currentPage - 1) * pageSize - 1
|
|
||||||
limit++
|
|
||||||
}
|
|
||||||
if (offset && order === Order.ASC) {
|
|
||||||
offset--
|
|
||||||
}
|
|
||||||
const transactionRepository = getCustomRepository(TransactionRepository)
|
|
||||||
const [userTransactions, userTransactionsCount] = await transactionRepository.findByUserPaged(
|
|
||||||
user.id,
|
|
||||||
limit,
|
|
||||||
offset,
|
|
||||||
order,
|
|
||||||
onlyCreations,
|
|
||||||
)
|
|
||||||
skipFirstTransaction = userTransactionsCount > offset + limit
|
|
||||||
const decay = !(currentPage > 1)
|
|
||||||
let transactions: Transaction[] = []
|
|
||||||
if (userTransactions.length) {
|
|
||||||
if (order === Order.DESC) {
|
|
||||||
userTransactions.reverse()
|
|
||||||
}
|
|
||||||
transactions = await calculateAndAddDecayTransactions(
|
|
||||||
userTransactions,
|
|
||||||
decay,
|
|
||||||
skipFirstTransaction,
|
|
||||||
)
|
|
||||||
if (order === Order.DESC) {
|
if (order === Order.DESC) {
|
||||||
transactions.reverse()
|
transactions.reverse()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user