mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
improved query
This commit is contained in:
parent
3aa3ead86e
commit
df94fa8e37
@ -4,7 +4,7 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||||
|
|
||||||
import { Resolver, Query, Args, Authorized, Ctx, Mutation } from 'type-graphql'
|
import { Resolver, Query, Args, Authorized, Ctx, Mutation } from 'type-graphql'
|
||||||
import { getCustomRepository, getConnection, MoreThan } from '@dbTools/typeorm'
|
import { getCustomRepository, getConnection } from '@dbTools/typeorm'
|
||||||
|
|
||||||
import CONFIG from '@/config'
|
import CONFIG from '@/config'
|
||||||
import { sendTransactionReceivedEmail } from '@/mailer/sendTransactionReceivedEmail'
|
import { sendTransactionReceivedEmail } from '@/mailer/sendTransactionReceivedEmail'
|
||||||
@ -23,7 +23,6 @@ import { TransactionLinkRepository } from '@repository/TransactionLink'
|
|||||||
|
|
||||||
import { User as dbUser } from '@entity/User'
|
import { User as dbUser } from '@entity/User'
|
||||||
import { Transaction as dbTransaction } from '@entity/Transaction'
|
import { Transaction as dbTransaction } from '@entity/Transaction'
|
||||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
|
||||||
|
|
||||||
import { apiPost } from '@/apis/HttpRequest'
|
import { apiPost } from '@/apis/HttpRequest'
|
||||||
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
||||||
@ -115,10 +114,8 @@ export class TransactionResolver {
|
|||||||
const transactions: Transaction[] = []
|
const transactions: Transaction[] = []
|
||||||
|
|
||||||
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
||||||
const { sumHoldAvailableAmount, sumAmount } = await transactionLinkRepository.sumAmounts(
|
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate } =
|
||||||
user.id,
|
await transactionLinkRepository.sumAmounts(user.id, now)
|
||||||
now,
|
|
||||||
)
|
|
||||||
|
|
||||||
// decay transaction
|
// decay transaction
|
||||||
if (!onlyCreations && currentPage === 1 && order === Order.DESC) {
|
if (!onlyCreations && currentPage === 1 && order === Order.DESC) {
|
||||||
@ -127,24 +124,14 @@ export class TransactionResolver {
|
|||||||
)
|
)
|
||||||
// open transaction link sum transaction
|
// open transaction link sum transaction
|
||||||
if (sumHoldAvailableAmount.greaterThan(0)) {
|
if (sumHoldAvailableAmount.greaterThan(0)) {
|
||||||
const lastTransactionLink = await dbTransactionLink.find({
|
|
||||||
where: { userId: self.id, redeemedBy: null, validUntil: MoreThan(now) },
|
|
||||||
order: { createdAt: 'DESC' },
|
|
||||||
take: 1,
|
|
||||||
})
|
|
||||||
const firstTransactionLink = await dbTransactionLink.find({
|
|
||||||
where: { userId: self.id, redeemedBy: null, validUntil: MoreThan(now) },
|
|
||||||
order: { createdAt: 'ASC' },
|
|
||||||
take: 1,
|
|
||||||
})
|
|
||||||
transactions.push(
|
transactions.push(
|
||||||
virtualLinkTransaction(
|
virtualLinkTransaction(
|
||||||
lastTransaction.balance,
|
lastTransaction.balance,
|
||||||
sumAmount,
|
sumAmount,
|
||||||
sumHoldAvailableAmount,
|
sumHoldAvailableAmount,
|
||||||
sumHoldAvailableAmount.minus(sumAmount.toString()),
|
sumHoldAvailableAmount.minus(sumAmount.toString()),
|
||||||
firstTransactionLink[0].createdAt,
|
firstDate || now,
|
||||||
lastTransactionLink[0].validUntil,
|
lastDate || now,
|
||||||
self,
|
self,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -39,7 +39,6 @@ const apolloLogPlugin = ApolloLogPlugin({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const plugins =
|
const plugins = process.env.NODE_ENV === 'development' ? [setHeadersPlugin] : [setHeadersPlugin] // , apolloLogPlugin
|
||||||
process.env.NODE_ENV === 'development' ? [setHeadersPlugin] : [setHeadersPlugin, apolloLogPlugin]
|
|
||||||
|
|
||||||
export default plugins
|
export default plugins
|
||||||
|
|||||||
@ -7,10 +7,18 @@ export class TransactionLinkRepository extends Repository<dbTransactionLink> {
|
|||||||
async sumAmounts(
|
async sumAmounts(
|
||||||
userId: number,
|
userId: number,
|
||||||
date: Date,
|
date: Date,
|
||||||
): Promise<{ sumHoldAvailableAmount: Decimal; sumAmount: Decimal }> {
|
): Promise<{
|
||||||
const { sumHoldAvailableAmount, sumAmount } = await this.createQueryBuilder('transactionLinks')
|
sumHoldAvailableAmount: Decimal
|
||||||
|
sumAmount: Decimal
|
||||||
|
lastDate: Date | null
|
||||||
|
firstDate: Date | null
|
||||||
|
}> {
|
||||||
|
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate } =
|
||||||
|
await this.createQueryBuilder('transactionLinks')
|
||||||
.select('SUM(transactionLinks.holdAvailableAmount)', 'sumHoldAvailableAmount')
|
.select('SUM(transactionLinks.holdAvailableAmount)', 'sumHoldAvailableAmount')
|
||||||
.addSelect('SUM(transactionLinks.amount)', 'sumAmount')
|
.addSelect('SUM(transactionLinks.amount)', 'sumAmount')
|
||||||
|
.addSelect('MAX(transactionLinks.validUntil)', 'lastDate')
|
||||||
|
.addSelect('MIN(transactionLinks.createdAt)', 'firstDate')
|
||||||
.where('transactionLinks.userId = :userId', { userId })
|
.where('transactionLinks.userId = :userId', { userId })
|
||||||
.andWhere('transactionLinks.redeemedAt is NULL')
|
.andWhere('transactionLinks.redeemedAt is NULL')
|
||||||
.andWhere('transactionLinks.validUntil > :date', { date })
|
.andWhere('transactionLinks.validUntil > :date', { date })
|
||||||
@ -21,6 +29,8 @@ export class TransactionLinkRepository extends Repository<dbTransactionLink> {
|
|||||||
? new Decimal(sumHoldAvailableAmount)
|
? new Decimal(sumHoldAvailableAmount)
|
||||||
: new Decimal(0),
|
: new Decimal(0),
|
||||||
sumAmount: sumAmount ? new Decimal(sumAmount) : new Decimal(0),
|
sumAmount: sumAmount ? new Decimal(sumAmount) : new Decimal(0),
|
||||||
|
lastDate: lastDate || null,
|
||||||
|
firstDate: firstDate || null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user