diff --git a/backend/src/auth/RIGHTS.ts b/backend/src/auth/RIGHTS.ts index dcd0e44d6..a9270cd36 100644 --- a/backend/src/auth/RIGHTS.ts +++ b/backend/src/auth/RIGHTS.ts @@ -38,4 +38,5 @@ export enum RIGHTS { CREATION_TRANSACTION_LIST = 'CREATION_TRANSACTION_LIST', LIST_TRANSACTION_LINKS_ADMIN = 'LIST_TRANSACTION_LINKS_ADMIN', CREATE_CONTRIBUTION_LINK = 'CREATE_CONTRIBUTION_LINK', + LIST_CONTRIBUTION_LINKS = 'LIST_CONTRIBUTION_LINKS', } diff --git a/backend/src/graphql/model/ContributionLinkList.ts b/backend/src/graphql/model/ContributionLinkList.ts new file mode 100644 index 000000000..412d0bf7b --- /dev/null +++ b/backend/src/graphql/model/ContributionLinkList.ts @@ -0,0 +1,11 @@ +import { ObjectType, Field } from 'type-graphql' +import { ContributionLink } from '@model/ContributionLink' + +@ObjectType() +export class ContributionLinkList { + @Field(() => [ContributionLink]) + links: ContributionLink[] + + @Field(() => Number) + count: number +} diff --git a/backend/src/graphql/resolver/AdminResolver.test.ts b/backend/src/graphql/resolver/AdminResolver.test.ts index 6b0f295c7..8e700ce9b 100644 --- a/backend/src/graphql/resolver/AdminResolver.test.ts +++ b/backend/src/graphql/resolver/AdminResolver.test.ts @@ -27,6 +27,7 @@ import { login, searchUsers, listTransactionLinksAdmin, + listContributionLinks, } from '@/seeds/graphql/queries' import { GraphQLError } from 'graphql' import { User } from '@entity/User' @@ -1618,6 +1619,16 @@ describe('AdminResolver', () => { ) }) }) + + describe('listContributionLinks', () => { + it.only('returns an error', async () => { + await expect(query({ query: listContributionLinks })).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) }) describe('authenticated', () => { @@ -1644,6 +1655,16 @@ describe('AdminResolver', () => { ) }) }) + + describe('listContributionLinks', () => { + it.only('returns an error', async () => { + await expect(query({ query: listContributionLinks })).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) }) describe('with admin rights', () => { @@ -1708,6 +1729,37 @@ describe('AdminResolver', () => { ) }) }) + + describe('listContributionLinks', () => { + describe('one link in DB', () => { + it.only('returns the link and count 1', async () => { + await expect(query({ query: listContributionLinks })).resolves.toEqual( + expect.objectContaining({ + data: { + listContributionLinks: { + links: expect.arrayContaining([ + expect.objectContaining({ + amount: '200', + code: expect.stringMatching(/^CL-[0-9a-f]{24,24}$/), + link: expect.any(String), + createdAt: expect.any(String), + name: 'Dokumenta 2022', + memo: 'Danke für deine Teilnahme an der Dokumenta 2022', + validFrom: expect.any(String), + validTo: expect.any(String), + maxAmountPerMonth: '200', + cycle: 'once', + maxPerCycle: 1, + }), + ]), + count: 1, + }, + }, + }), + ) + }) + }) + }) }) }) }) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index d7c843611..e13a013b6 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -15,6 +15,7 @@ import { PendingCreation } from '@model/PendingCreation' import { CreatePendingCreations } from '@model/CreatePendingCreations' import { UpdatePendingCreation } from '@model/UpdatePendingCreation' import { ContributionLink } from '@model/ContributionLink' +import { ContributionLinkList } from '@model/ContributionLinkList' import { RIGHTS } from '@/auth/RIGHTS' import { UserRepository } from '@repository/User' import CreatePendingCreationArgs from '@arg/CreatePendingCreationArgs' @@ -494,6 +495,23 @@ export class AdminResolver { dbContributionLink.save() return new ContributionLink(dbContributionLink) } + + @Authorized([RIGHTS.LIST_CONTRIBUTION_LINKS]) + @Query(() => ContributionLinkList) + async listContributionLinks( + @Args() + { currentPage = 1, pageSize = 5, order = Order.DESC }: Paginated, + ): Promise { + const [links, count] = await DbContributionLink.findAndCount({ + order: { createdAt: order }, + skip: (currentPage - 1) * pageSize, + take: pageSize, + }) + return { + links: links.map((link: DbContributionLink) => new ContributionLink(link)), + count, + } + } } interface CreationMap { diff --git a/backend/src/seeds/graphql/queries.ts b/backend/src/seeds/graphql/queries.ts index 03ee3b53e..9a0e00be3 100644 --- a/backend/src/seeds/graphql/queries.ts +++ b/backend/src/seeds/graphql/queries.ts @@ -217,3 +217,25 @@ export const listTransactionLinksAdmin = gql` } } ` + +export const listContributionLinks = gql` + query ($pageSize: Int = 25, $currentPage: Int = 1, $order: Order) { + listContributionLinks(pageSize: $pageSize, currentPage: $currentPage, order: $order) { + links { + id + amount + name + memo + code + link + createdAt + validFrom + validTo + maxAmountPerMonth + cycle + maxPerCycle + } + count + } + } +`