diff --git a/admin/src/components/TransactionLinkList.spec.js b/admin/src/components/TransactionLinkList.spec.js index 3deaefa23..486b53859 100644 --- a/admin/src/components/TransactionLinkList.spec.js +++ b/admin/src/components/TransactionLinkList.spec.js @@ -9,8 +9,8 @@ const apolloQueryMock = jest.fn() apolloQueryMock.mockResolvedValue({ data: { listTransactionLinksAdmin: { - linkCount: 8, - linkList: [ + count: 8, + links: [ { amount: '19.99', code: '62ef8236ace7217fbd066c5a', diff --git a/admin/src/components/TransactionLinkList.vue b/admin/src/components/TransactionLinkList.vue index 564865440..eb58903c6 100644 --- a/admin/src/components/TransactionLinkList.vue +++ b/admin/src/components/TransactionLinkList.vue @@ -42,8 +42,8 @@ export default { }, }) .then((result) => { - this.rows = result.data.listTransactionLinksAdmin.linkCount - this.items = result.data.listTransactionLinksAdmin.linkList + this.rows = result.data.listTransactionLinksAdmin.count + this.items = result.data.listTransactionLinksAdmin.links }) .catch((error) => { this.toastError(error.message) diff --git a/admin/src/graphql/listTransactionLinksAdmin.js b/admin/src/graphql/listTransactionLinksAdmin.js index 2e4171f02..c069bafd9 100644 --- a/admin/src/graphql/listTransactionLinksAdmin.js +++ b/admin/src/graphql/listTransactionLinksAdmin.js @@ -8,8 +8,8 @@ export const listTransactionLinksAdmin = gql` userId: $userId filters: { withRedeemed: true, withExpired: true, withDeleted: true } ) { - linkCount - linkList { + count + links { id amount holdAvailableAmount 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 dda693b22..60b4551be 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts @@ -600,6 +600,26 @@ describe('TransactionLinkResolver', () => { resetToken() }) + describe('', () => { + it('throws error when user does not exists', async () => { + jest.clearAllMocks() + await expect( + mutate({ + mutation: listTransactionLinksAdmin, + variables: { + userId: -1, + }, + }), + ).resolves.toMatchObject({ + errors: [new GraphQLError('Could not find requested User')], + }) + }) + + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith('Could not find requested User', -1) + }) + }) + describe('without any filters', () => { it('finds 6 open transaction links and no deleted or redeemed', async () => { await expect( @@ -611,8 +631,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 +667,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 +701,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 +735,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 +772,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 16766bdd6..ab5b52bad 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -1,7 +1,7 @@ import { randomBytes } from 'crypto' import Decimal from 'decimal.js-light' -import { getConnection, MoreThan, FindOperator } from '@dbTools/typeorm' +import { getConnection } from '@dbTools/typeorm' import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink' import { User as DbUser } from '@entity/User' @@ -13,7 +13,6 @@ import { User } from '@model/User' import { ContributionLink } from '@model/ContributionLink' import { Decay } from '@model/Decay' import { TransactionLink, TransactionLinkResult } from '@model/TransactionLink' -import { Order } from '@enum/Order' import { ContributionType } from '@enum/ContributionType' import { ContributionStatus } from '@enum/ContributionStatus' import { TransactionTypeId } from '@enum/TransactionTypeId' @@ -35,6 +34,7 @@ import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK' import LogError from '@/server/LogError' import { getLastTransaction } from './util/getLastTransaction' +import transactionLinkList from './util/transactionLinkList' // TODO: do not export, test it inside the resolver export const transactionLinkCode = (date: Date): string => { @@ -145,30 +145,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( @@ -342,43 +318,38 @@ export class TransactionLinkResolver { } } + @Authorized([RIGHTS.LIST_TRANSACTION_LINKS]) + @Query(() => TransactionLinkResult) + async listTransactionLinks( + @Args() + paginated: Paginated, + @Ctx() context: Context, + ): Promise { + return transactionLinkList( + paginated, + { + withDeleted: false, + withExpired: true, + withRedeemed: false, + }, + getUser(context), + ) + } + @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))), + const user = await DbUser.findOne({ id: userId }) + if (!user) { + throw new LogError('Could not find requested User', userId) } + return transactionLinkList(paginated, filters, user) } } diff --git a/backend/src/graphql/resolver/util/transactionLinkList.ts b/backend/src/graphql/resolver/util/transactionLinkList.ts new file mode 100644 index 000000000..2d151b94a --- /dev/null +++ b/backend/src/graphql/resolver/util/transactionLinkList.ts @@ -0,0 +1,38 @@ +import { MoreThan } from '@dbTools/typeorm' +import { TransactionLink as DbTransactionLink } from '@entity/TransactionLink' +import { User as DbUser } from '@entity/User' +import { Order } from '@enum/Order' +import Paginated from '@arg/Paginated' +import TransactionLinkFilters from '@arg/TransactionLinkFilters' +import { TransactionLink, TransactionLinkResult } from '@model/TransactionLink' +import { User } from '@/graphql/model/User' + +export default async function transactionLinkList( + { currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated, + filters: TransactionLinkFilters | null, + user: DbUser, +): Promise { + const { withDeleted, withExpired, withRedeemed } = filters || { + withDeleted: false, + withExpired: false, + withRedeemed: false, + } + const [transactionLinks, count] = await DbTransactionLink.findAndCount({ + where: { + userId: user.id, + ...(!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 71d305dbb..c17665e4d 100644 --- a/backend/src/seeds/graphql/queries.ts +++ b/backend/src/seeds/graphql/queries.ts @@ -250,8 +250,8 @@ export const listTransactionLinksAdmin = gql` currentPage: $currentPage pageSize: $pageSize ) { - linkCount - linkList { + count + links { id amount holdAvailableAmount diff --git a/frontend/src/components/Transactions/TransactionLinkSummary.spec.js b/frontend/src/components/Transactions/TransactionLinkSummary.spec.js index d0086ceab..46041f87f 100644 --- a/frontend/src/components/Transactions/TransactionLinkSummary.spec.js +++ b/frontend/src/components/Transactions/TransactionLinkSummary.spec.js @@ -47,51 +47,53 @@ describe('TransactionLinkSummary', () => { beforeEach(() => { apolloQueryMock.mockResolvedValue({ data: { - listTransactionLinks: [ - { - amount: '75', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 86, - memo: - 'Hokuspokus Haselnuss, Vogelbein und Fliegenfuß, damit der Trick gelingen muss!', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - { - amount: '85', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 107, - memo: 'Mäusespeck und Katzenbuckel, Tricks und Tracks und Zauberkugel!', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - { - amount: '95', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 92, - memo: - 'Abrakadabra 1,2,3, die Sonne kommt herbei. Schweinepups und Spuckebrei, der Regen ist vorbei.', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - { - amount: '150', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 16, - memo: - 'Abrakadabra 1,2,3 was verschwunden ist komme herbei.Wieseldreck und Schweinemist, zaubern das ist keine List.', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - ], + listTransactionLinks: { + links: [ + { + amount: '75', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 86, + memo: + 'Hokuspokus Haselnuss, Vogelbein und Fliegenfuß, damit der Trick gelingen muss!', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + { + amount: '85', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 107, + memo: 'Mäusespeck und Katzenbuckel, Tricks und Tracks und Zauberkugel!', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + { + amount: '95', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 92, + memo: + 'Abrakadabra 1,2,3, die Sonne kommt herbei. Schweinepups und Spuckebrei, der Regen ist vorbei.', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + { + amount: '150', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 16, + memo: + 'Abrakadabra 1,2,3 was verschwunden ist komme herbei.Wieseldreck und Schweinemist, zaubern das ist keine List.', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + ], + }, }, }) @@ -166,51 +168,53 @@ describe('TransactionLinkSummary', () => { jest.clearAllMocks() apolloQueryMock.mockResolvedValue({ data: { - listTransactionLinks: [ - { - amount: '76', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 87, - memo: - 'Hat jemand die Nummer von der Hexe aus Schneewittchen? Ich bräuchte mal ein paar Äpfel.', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - { - amount: '86', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 108, - memo: - 'Die Windfahn´ krächzt am Dach, Der Uhu im Geklüfte; Was wispert wie ein Ach Verhallend in die Lüfte?', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - { - amount: '96', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 93, - memo: - 'Verschlafen kräht der Hahn, Ein Blitz noch, und ein trüber, Umwölbter Tag bricht an – Walpurgisnacht vorüber!', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - { - amount: '150', - link: 'http://localhost/redeem/ce28664b5308c17f931c0367', - createdAt: '2022-03-16T14:22:40.000Z', - holdAvailableAmount: '5.13109484759482747111', - id: 17, - memo: 'Eene meene Flaschenschrank, fertig ist der Hexentrank!', - redeemedAt: null, - validUntil: '2022-03-30T14:22:40.000Z', - }, - ], + listTransactionLinks: { + links: [ + { + amount: '76', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 87, + memo: + 'Hat jemand die Nummer von der Hexe aus Schneewittchen? Ich bräuchte mal ein paar Äpfel.', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + { + amount: '86', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 108, + memo: + 'Die Windfahn´ krächzt am Dach, Der Uhu im Geklüfte; Was wispert wie ein Ach Verhallend in die Lüfte?', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + { + amount: '96', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 93, + memo: + 'Verschlafen kräht der Hahn, Ein Blitz noch, und ein trüber, Umwölbter Tag bricht an – Walpurgisnacht vorüber!', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + { + amount: '150', + link: 'http://localhost/redeem/ce28664b5308c17f931c0367', + createdAt: '2022-03-16T14:22:40.000Z', + holdAvailableAmount: '5.13109484759482747111', + id: 17, + memo: 'Eene meene Flaschenschrank, fertig ist der Hexentrank!', + redeemedAt: null, + validUntil: '2022-03-30T14:22:40.000Z', + }, + ], + }, }, }) await wrapper.setData({ diff --git a/frontend/src/components/Transactions/TransactionLinkSummary.vue b/frontend/src/components/Transactions/TransactionLinkSummary.vue index b54c0e687..c3a62357b 100644 --- a/frontend/src/components/Transactions/TransactionLinkSummary.vue +++ b/frontend/src/components/Transactions/TransactionLinkSummary.vue @@ -90,7 +90,10 @@ export default { fetchPolicy: 'network-only', }) .then((result) => { - this.transactionLinks = [...this.transactionLinks, ...result.data.listTransactionLinks] + this.transactionLinks = [ + ...this.transactionLinks, + ...result.data.listTransactionLinks.links, + ] this.$emit('update-transactions') this.pending = false }) 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 + } } } `