diff --git a/backend/src/graphql/model/TransactionLink.ts b/backend/src/graphql/model/TransactionLink.ts index 18a601948..416527ec9 100644 --- a/backend/src/graphql/model/TransactionLink.ts +++ b/backend/src/graphql/model/TransactionLink.ts @@ -61,8 +61,8 @@ export class TransactionLink { @ObjectType() export class TransactionLinkResult { @Field(() => Int) - linkCount: number + count: number @Field(() => [TransactionLink]) - linkList: TransactionLink[] + links: TransactionLink[] } diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts index 09f2f9a02..03f412a43 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts @@ -611,8 +611,8 @@ describe('TransactionLinkResolver', () => { expect.objectContaining({ data: { listTransactionLinksAdmin: { - linkCount: 6, - linkList: expect.not.arrayContaining([ + count: 6, + links: expect.not.arrayContaining([ expect.objectContaining({ memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', createdAt: expect.any(String), @@ -647,8 +647,8 @@ describe('TransactionLinkResolver', () => { expect.objectContaining({ data: { listTransactionLinksAdmin: { - linkCount: 6, - linkList: expect.not.arrayContaining([ + count: 6, + links: expect.not.arrayContaining([ expect.objectContaining({ memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', createdAt: expect.any(String), @@ -681,8 +681,8 @@ describe('TransactionLinkResolver', () => { expect.objectContaining({ data: { listTransactionLinksAdmin: { - linkCount: 7, - linkList: expect.arrayContaining([ + count: 7, + links: expect.arrayContaining([ expect.not.objectContaining({ memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', createdAt: expect.any(String), @@ -715,8 +715,8 @@ describe('TransactionLinkResolver', () => { expect.objectContaining({ data: { listTransactionLinksAdmin: { - linkCount: 7, - linkList: expect.arrayContaining([ + count: 7, + links: expect.arrayContaining([ expect.objectContaining({ memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', createdAt: expect.any(String), @@ -752,8 +752,8 @@ describe('TransactionLinkResolver', () => { expect.objectContaining({ data: { listTransactionLinksAdmin: { - linkCount: 6, - linkList: expect.arrayContaining([ + count: 6, + links: expect.arrayContaining([ expect.not.objectContaining({ memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', createdAt: expect.any(String), diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index 4647dde60..945ef766c 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -35,6 +35,7 @@ import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK' import LogError from '@/server/LogError' import { getLastTransaction } from './util/getLastTransaction' +import { filter } from 'lodash' // TODO: do not export, test it inside the resolver 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 { - 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]) @Mutation(() => Boolean) 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 { + const user = getUser(context) + return transactionLinkList( + paginated, + { + withDeleted: false, + withExpired: true, + withRedeemed: false, + }, + user.id, + ) + } + @Authorized([RIGHTS.LIST_TRANSACTION_LINKS_ADMIN]) @Query(() => TransactionLinkResult) async listTransactionLinksAdmin( @Args() - { currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated, + paginated: Paginated, @Arg('filters', () => TransactionLinkFilters, { nullable: true }) - filters: TransactionLinkFilters, + filters: TransactionLinkFilters | null, @Arg('userId', () => Int) userId: number, ): Promise { - const user = await DbUser.findOneOrFail({ id: userId }) - const where: { - userId: number - redeemedBy?: number | null - validUntil?: FindOperator | null - } = { - userId, - redeemedBy: null, - validUntil: MoreThan(new Date()), - } - if (filters) { - if (filters.withRedeemed) delete where.redeemedBy - if (filters.withExpired) delete where.validUntil - } - const [transactionLinks, count] = await DbTransactionLink.findAndCount({ - where, - withDeleted: filters ? filters.withDeleted : false, - order: { - createdAt: order, - }, - skip: (currentPage - 1) * pageSize, - take: pageSize, - }) - - return { - linkCount: count, - linkList: transactionLinks.map((tl) => new TransactionLink(tl, new User(user))), - } + return transactionLinkList(paginated, filters, userId) + } +} + +const transactionLinkList = async ( + { currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated, + filters: TransactionLinkFilters | null, + userId: number, +): Promise => { + const user = await DbUser.findOneOrFail({ id: userId }) + const { withDeleted, withExpired, withRedeemed } = filters || { + withDeleted: false, + withExpired: false, + withRedeemed: false, + } + const [transactionLinks, count] = await DbTransactionLink.findAndCount({ + where: { + userId, + ...(!withRedeemed && { redeemedBy: null }), + ...(!withExpired && { validUntil: MoreThan(new Date()) }), + }, + withDeleted, + order: { + createdAt: order, + }, + skip: (currentPage - 1) * pageSize, + take: pageSize, + }) + + return { + count, + links: transactionLinks.map((tl) => new TransactionLink(tl, new User(user))), } } diff --git a/backend/src/seeds/graphql/queries.ts b/backend/src/seeds/graphql/queries.ts index 385a69479..2c4473ab5 100644 --- a/backend/src/seeds/graphql/queries.ts +++ b/backend/src/seeds/graphql/queries.ts @@ -224,8 +224,8 @@ export const listTransactionLinksAdmin = gql` currentPage: $currentPage pageSize: $pageSize ) { - linkCount - linkList { + count + links { id amount holdAvailableAmount diff --git a/frontend/src/graphql/queries.js b/frontend/src/graphql/queries.js index e199a3730..6e8d420b8 100644 --- a/frontend/src/graphql/queries.js +++ b/frontend/src/graphql/queries.js @@ -126,14 +126,16 @@ export const queryTransactionLink = gql` export const listTransactionLinks = gql` query($currentPage: Int = 1, $pageSize: Int = 5) { listTransactionLinks(currentPage: $currentPage, pageSize: $pageSize) { - id - amount - holdAvailableAmount - memo - link - createdAt - validUntil - redeemedAt + links { + id + amount + holdAvailableAmount + memo + link + createdAt + validUntil + redeemedAt + } } } `