From 4784fc23abc954eba76ee87a2fcf7548dd3f1260 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 9 Mar 2022 19:30:31 +0100 Subject: [PATCH] delete transaction link mutation --- .../resolver/TransactionLinkResolver.ts | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index d60146096..9f9f5e493 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { Resolver, Args, Authorized, Ctx, Mutation } from 'type-graphql' +import { Resolver, Args, Arg, Authorized, Ctx, Mutation } from 'type-graphql' import { getCustomRepository } from '@dbTools/typeorm' import { TransactionLink } from '@model/TransactionLink' import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink' @@ -67,4 +67,32 @@ export class TransactionLinkResolver { return new TransactionLink(transactionLink, new User(user)) } + + @Authorized([RIGHTS.DELETE_TRANSACTION_LINK]) + @Mutation(() => Date, { nullable: true }) + async deleteTransactionLink( + @Arg('id') id: number, + @Ctx() context: any, + ): Promise { + const userRepository = getCustomRepository(UserRepository) + const user = await userRepository.findByPubkeyHex(context.pubKey) + + const transactionLink = await dbTransactionLink.findOne({ id }) + if (!transactionLink) { + throw new Error('Transaction Link not found!') + } + + // TODO: admin can delete links? + if (transactionLink.userId !== user.id) { + throw new Error('Transaction Link cannot be deleted!') + } + + if (transactionLink.redeemedBy) { + throw new Error('Transaction Link already redeemed!') + } + + await transactionLink.softRemove() + const newLink = await dbTransactionLink.findOne({ id }, { withDeleted: true }) + return newLink ? newLink.deletedAt : null + } }