combine logic for both listTransactionLinks and listTransactionLinksAdmin

This commit is contained in:
Ulf Gebhardt 2023-02-14 21:28:58 +01:00
parent 9ff045fdbf
commit 66a5175dc1
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
5 changed files with 78 additions and 76 deletions

View File

@ -61,8 +61,8 @@ export class TransactionLink {
@ObjectType() @ObjectType()
export class TransactionLinkResult { export class TransactionLinkResult {
@Field(() => Int) @Field(() => Int)
linkCount: number count: number
@Field(() => [TransactionLink]) @Field(() => [TransactionLink])
linkList: TransactionLink[] links: TransactionLink[]
} }

View File

@ -611,8 +611,8 @@ describe('TransactionLinkResolver', () => {
expect.objectContaining({ expect.objectContaining({
data: { data: {
listTransactionLinksAdmin: { listTransactionLinksAdmin: {
linkCount: 6, count: 6,
linkList: expect.not.arrayContaining([ links: expect.not.arrayContaining([
expect.objectContaining({ expect.objectContaining({
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
createdAt: expect.any(String), createdAt: expect.any(String),
@ -647,8 +647,8 @@ describe('TransactionLinkResolver', () => {
expect.objectContaining({ expect.objectContaining({
data: { data: {
listTransactionLinksAdmin: { listTransactionLinksAdmin: {
linkCount: 6, count: 6,
linkList: expect.not.arrayContaining([ links: expect.not.arrayContaining([
expect.objectContaining({ expect.objectContaining({
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
createdAt: expect.any(String), createdAt: expect.any(String),
@ -681,8 +681,8 @@ describe('TransactionLinkResolver', () => {
expect.objectContaining({ expect.objectContaining({
data: { data: {
listTransactionLinksAdmin: { listTransactionLinksAdmin: {
linkCount: 7, count: 7,
linkList: expect.arrayContaining([ links: expect.arrayContaining([
expect.not.objectContaining({ expect.not.objectContaining({
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
createdAt: expect.any(String), createdAt: expect.any(String),
@ -715,8 +715,8 @@ describe('TransactionLinkResolver', () => {
expect.objectContaining({ expect.objectContaining({
data: { data: {
listTransactionLinksAdmin: { listTransactionLinksAdmin: {
linkCount: 7, count: 7,
linkList: expect.arrayContaining([ links: expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
createdAt: expect.any(String), createdAt: expect.any(String),
@ -752,8 +752,8 @@ describe('TransactionLinkResolver', () => {
expect.objectContaining({ expect.objectContaining({
data: { data: {
listTransactionLinksAdmin: { listTransactionLinksAdmin: {
linkCount: 6, count: 6,
linkList: expect.arrayContaining([ links: expect.arrayContaining([
expect.not.objectContaining({ expect.not.objectContaining({
memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(',
createdAt: expect.any(String), createdAt: expect.any(String),

View File

@ -35,6 +35,7 @@ import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK'
import LogError from '@/server/LogError' import LogError from '@/server/LogError'
import { getLastTransaction } from './util/getLastTransaction' import { getLastTransaction } from './util/getLastTransaction'
import { filter } from 'lodash'
// TODO: do not export, test it inside the resolver // TODO: do not export, test it inside the resolver
export const transactionLinkCode = (date: Date): string => { export const transactionLinkCode = (date: Date): string => {
@ -141,30 +142,6 @@ export class TransactionLinkResolver {
} }
} }
@Authorized([RIGHTS.LIST_TRANSACTION_LINKS])
@Query(() => [TransactionLink])
async listTransactionLinks(
@Args()
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
@Ctx() context: Context,
): Promise<TransactionLink[]> {
const user = getUser(context)
// const now = new Date()
const transactionLinks = await DbTransactionLink.find({
where: {
userId: user.id,
redeemedBy: null,
// validUntil: MoreThan(now),
},
order: {
createdAt: order,
},
skip: (currentPage - 1) * pageSize,
take: pageSize,
})
return transactionLinks.map((tl) => new TransactionLink(tl, new User(user)))
}
@Authorized([RIGHTS.REDEEM_TRANSACTION_LINK]) @Authorized([RIGHTS.REDEEM_TRANSACTION_LINK])
@Mutation(() => Boolean) @Mutation(() => Boolean)
async redeemTransactionLink( async redeemTransactionLink(
@ -338,43 +315,66 @@ export class TransactionLinkResolver {
} }
} }
@Authorized([RIGHTS.LIST_TRANSACTION_LINKS])
@Query(() => [TransactionLink])
async listTransactionLinks(
@Args()
paginated: Paginated,
@Ctx() context: Context,
): Promise<TransactionLinkResult> {
const user = getUser(context)
return transactionLinkList(
paginated,
{
withDeleted: false,
withExpired: true,
withRedeemed: false,
},
user.id,
)
}
@Authorized([RIGHTS.LIST_TRANSACTION_LINKS_ADMIN]) @Authorized([RIGHTS.LIST_TRANSACTION_LINKS_ADMIN])
@Query(() => TransactionLinkResult) @Query(() => TransactionLinkResult)
async listTransactionLinksAdmin( async listTransactionLinksAdmin(
@Args() @Args()
{ currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated, paginated: Paginated,
@Arg('filters', () => TransactionLinkFilters, { nullable: true }) @Arg('filters', () => TransactionLinkFilters, { nullable: true })
filters: TransactionLinkFilters, filters: TransactionLinkFilters | null,
@Arg('userId', () => Int) @Arg('userId', () => Int)
userId: number, userId: number,
): Promise<TransactionLinkResult> { ): Promise<TransactionLinkResult> {
const user = await DbUser.findOneOrFail({ id: userId }) return transactionLinkList(paginated, filters, userId)
const where: { }
userId: number }
redeemedBy?: number | null
validUntil?: FindOperator<Date> | null const transactionLinkList = async (
} = { { currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated,
userId, filters: TransactionLinkFilters | null,
redeemedBy: null, userId: number,
validUntil: MoreThan(new Date()), ): Promise<TransactionLinkResult> => {
} const user = await DbUser.findOneOrFail({ id: userId })
if (filters) { const { withDeleted, withExpired, withRedeemed } = filters || {
if (filters.withRedeemed) delete where.redeemedBy withDeleted: false,
if (filters.withExpired) delete where.validUntil withExpired: false,
} withRedeemed: false,
const [transactionLinks, count] = await DbTransactionLink.findAndCount({ }
where, const [transactionLinks, count] = await DbTransactionLink.findAndCount({
withDeleted: filters ? filters.withDeleted : false, where: {
order: { userId,
createdAt: order, ...(!withRedeemed && { redeemedBy: null }),
}, ...(!withExpired && { validUntil: MoreThan(new Date()) }),
skip: (currentPage - 1) * pageSize, },
take: pageSize, withDeleted,
}) order: {
createdAt: order,
return { },
linkCount: count, skip: (currentPage - 1) * pageSize,
linkList: transactionLinks.map((tl) => new TransactionLink(tl, new User(user))), take: pageSize,
} })
return {
count,
links: transactionLinks.map((tl) => new TransactionLink(tl, new User(user))),
} }
} }

View File

@ -224,8 +224,8 @@ export const listTransactionLinksAdmin = gql`
currentPage: $currentPage currentPage: $currentPage
pageSize: $pageSize pageSize: $pageSize
) { ) {
linkCount count
linkList { links {
id id
amount amount
holdAvailableAmount holdAvailableAmount

View File

@ -126,14 +126,16 @@ export const queryTransactionLink = gql`
export const listTransactionLinks = gql` export const listTransactionLinks = gql`
query($currentPage: Int = 1, $pageSize: Int = 5) { query($currentPage: Int = 1, $pageSize: Int = 5) {
listTransactionLinks(currentPage: $currentPage, pageSize: $pageSize) { listTransactionLinks(currentPage: $currentPage, pageSize: $pageSize) {
id links {
amount id
holdAvailableAmount amount
memo holdAvailableAmount
link memo
createdAt link
validUntil createdAt
redeemedAt validUntil
redeemedAt
}
} }
} }
` `