mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
middleware to ensure best possible decay displayed
This commit is contained in:
parent
166debcc7d
commit
466b755a3d
@ -7,7 +7,7 @@ import { Transaction as dbTransaction } from '@entity/Transaction'
|
||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||
import { User as dbUser } from '@entity/User'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
import { Resolver, Query, Args, Authorized, Ctx, Mutation } from 'type-graphql'
|
||||
import { Resolver, Query, Args, Authorized, Ctx, Mutation, UseMiddleware } from 'type-graphql'
|
||||
|
||||
import Paginated from '@arg/Paginated'
|
||||
import TransactionSendArgs from '@arg/TransactionSendArgs'
|
||||
@ -25,6 +25,7 @@ import {
|
||||
sendTransactionReceivedEmail,
|
||||
} from '@/emails/sendEmailVariants'
|
||||
import { EVENT_TRANSACTION_RECEIVE, EVENT_TRANSACTION_SEND } from '@/event/Event'
|
||||
import { fixDecayCalculation } from '@/middleware/fixDecayCalculation'
|
||||
import { Context, getUser } from '@/server/context'
|
||||
import LogError from '@/server/LogError'
|
||||
import { backendLogger as logger } from '@/server/logger'
|
||||
@ -184,6 +185,7 @@ export const executeTransaction = async (
|
||||
export class TransactionResolver {
|
||||
@Authorized([RIGHTS.TRANSACTION_LIST])
|
||||
@Query(() => TransactionList)
|
||||
@UseMiddleware(fixDecayCalculation)
|
||||
async transactionList(
|
||||
@Args()
|
||||
{ currentPage = 1, pageSize = 25, order = Order.DESC }: Paginated,
|
||||
|
||||
24
backend/src/middleware/fixDecayCalculation.ts
Normal file
24
backend/src/middleware/fixDecayCalculation.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
import { MiddlewareFn } from 'type-graphql'
|
||||
|
||||
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
||||
import { Transaction } from '@model/Transaction'
|
||||
import { TransactionList } from '@model/TransactionList'
|
||||
|
||||
export const fixDecayCalculation: MiddlewareFn = async (
|
||||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
|
||||
{ root, args, context, info },
|
||||
next,
|
||||
) => {
|
||||
const result: TransactionList = (await next()) as TransactionList
|
||||
const { transactions } = result
|
||||
transactions.forEach((transaction: Transaction) => {
|
||||
if (transaction.typeId !== TransactionTypeId.DECAY) {
|
||||
const { balance, previousBalance, amount } = transaction
|
||||
transaction.decay.decay = new Decimal(
|
||||
Number(balance) - Number(amount) - Number(previousBalance),
|
||||
).toDecimalPlaces(2, Decimal.ROUND_HALF_UP)
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user