From b23cb8915ccf50ae0c3db0cb48648423abf0e571 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 9 Mar 2022 19:06:15 +0100 Subject: [PATCH 1/7] set roles and rights --- backend/src/auth/RIGHTS.ts | 1 + backend/src/auth/ROLES.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/backend/src/auth/RIGHTS.ts b/backend/src/auth/RIGHTS.ts index a18f0132a..fa9dda805 100644 --- a/backend/src/auth/RIGHTS.ts +++ b/backend/src/auth/RIGHTS.ts @@ -19,6 +19,7 @@ export enum RIGHTS { UPDATE_USER_INFOS = 'UPDATE_USER_INFOS', HAS_ELOPAGE = 'HAS_ELOPAGE', CREATE_TRANSACTION_LINK = 'CREATE_TRANSACTION_LINK', + DELETE_TRANSACTION_LINK = 'DELETE_TRANSACTION_LINK', // Admin SEARCH_USERS = 'SEARCH_USERS', CREATE_PENDING_CREATION = 'CREATE_PENDING_CREATION', diff --git a/backend/src/auth/ROLES.ts b/backend/src/auth/ROLES.ts index 37a4e3a67..2a86b5bab 100644 --- a/backend/src/auth/ROLES.ts +++ b/backend/src/auth/ROLES.ts @@ -19,6 +19,7 @@ export const ROLE_USER = new Role('user', [ RIGHTS.UPDATE_USER_INFOS, RIGHTS.HAS_ELOPAGE, RIGHTS.CREATE_TRANSACTION_LINK, + RIGHTS.DELETE_TRANSACTION_LINK, ]) export const ROLE_ADMIN = new Role('admin', Object.values(RIGHTS)) // all rights From 4784fc23abc954eba76ee87a2fcf7548dd3f1260 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 9 Mar 2022 19:30:31 +0100 Subject: [PATCH 2/7] 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 + } } From 95c786f48477469676f387e4493014f11f6c8245 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 10 Mar 2022 17:33:01 +0100 Subject: [PATCH 3/7] migration to remove showEmail --- .../TransactionLink.ts | 61 +++++++++++++++++++ database/entity/TransactionLink.ts | 2 +- ...-remove_sendEmail_from_transaction_link.ts | 14 +++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts create mode 100644 database/migrations/0031-remove_sendEmail_from_transaction_link.ts diff --git a/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts b/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts new file mode 100644 index 000000000..789644782 --- /dev/null +++ b/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts @@ -0,0 +1,61 @@ +import Decimal from 'decimal.js-light' +import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, DeleteDateColumn } from 'typeorm' +import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer' + +@Entity('transaction_links') +export class TransactionLink extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ unsigned: true, nullable: false }) + userId: number + + @Column({ + type: 'decimal', + precision: 40, + scale: 20, + nullable: false, + transformer: DecimalTransformer, + }) + amount: Decimal + + @Column({ + type: 'decimal', + name: 'hold_available_amount', + precision: 40, + scale: 20, + nullable: false, + transformer: DecimalTransformer, + }) + holdAvailableAmount: Decimal + + @Column({ length: 255, nullable: false, collation: 'utf8mb4_unicode_ci' }) + memo: string + + @Column({ length: 24, nullable: false, collation: 'utf8mb4_unicode_ci' }) + code: string + + @Column({ + type: 'datetime', + nullable: false, + }) + createdAt: Date + + @DeleteDateColumn() + deletedAt?: Date | null + + @Column({ + type: 'datetime', + nullable: false, + }) + validUntil: Date + + @Column({ + type: 'datetime', + nullable: true, + }) + redeemedAt?: Date | null + + @Column({ type: 'int', unsigned: true, nullable: true }) + redeemedBy?: number | null +} diff --git a/database/entity/TransactionLink.ts b/database/entity/TransactionLink.ts index fde2ba9e0..a483f0171 100644 --- a/database/entity/TransactionLink.ts +++ b/database/entity/TransactionLink.ts @@ -1 +1 @@ -export { TransactionLink } from './0030-transaction_link/TransactionLink' +export { TransactionLink } from './0031-remove_sendEmail_from_transaction_link/TransactionLink' diff --git a/database/migrations/0031-remove_sendEmail_from_transaction_link.ts b/database/migrations/0031-remove_sendEmail_from_transaction_link.ts new file mode 100644 index 000000000..76b2ee742 --- /dev/null +++ b/database/migrations/0031-remove_sendEmail_from_transaction_link.ts @@ -0,0 +1,14 @@ +/* MIGRATION TO REMOVE sendEmail FIELD FROM TRANSACTION_LINK TABLE */ + +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn('ALTER TABLE `transaction_links` DROP COLUMN `showEmail`;') +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn( + 'ALTER TABLE `transaction_links` ADD COLUMN `showEmail` boolean NOT NULL DEFAULT false AFTER `validUntil`;', + ) +} From 91ffc16ed018a2263b0b5a8ff803299eaa64fcd2 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 10 Mar 2022 17:35:15 +0100 Subject: [PATCH 4/7] remove showEmail from transaction link model, alter database version --- backend/src/config/index.ts | 2 +- backend/src/graphql/model/TransactionLink.ts | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 4cd428153..b1fb8e397 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -10,7 +10,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0030-transaction_link', + DB_VERSION: '0031-remove_sendEmail_from_transaction_link', DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0 } diff --git a/backend/src/graphql/model/TransactionLink.ts b/backend/src/graphql/model/TransactionLink.ts index 0f19df466..e90d4efd9 100644 --- a/backend/src/graphql/model/TransactionLink.ts +++ b/backend/src/graphql/model/TransactionLink.ts @@ -31,9 +31,6 @@ export class TransactionLink { @Field(() => Date) validUntil: Date - @Field(() => Boolean) - showEmail: boolean - @Field(() => Date, { nullable: true }) redeemedAt: Date | null From cb4e6da31725cd01c4d392f2e44f0b34f9e97a65 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 10 Mar 2022 18:25:54 +0100 Subject: [PATCH 5/7] deleteTransactionLink mutation returns boolean, no more to does --- .../graphql/resolver/TransactionLinkResolver.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index 9f9f5e493..a3a044ded 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -69,11 +69,8 @@ export class TransactionLinkResolver { } @Authorized([RIGHTS.DELETE_TRANSACTION_LINK]) - @Mutation(() => Date, { nullable: true }) - async deleteTransactionLink( - @Arg('id') id: number, - @Ctx() context: any, - ): Promise { + @Mutation(() => Boolean) + async deleteTransactionLink(@Arg('id') id: number, @Ctx() context: any): Promise { const userRepository = getCustomRepository(UserRepository) const user = await userRepository.findByPubkeyHex(context.pubKey) @@ -82,7 +79,6 @@ export class TransactionLinkResolver { 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!') } @@ -91,8 +87,10 @@ export class TransactionLinkResolver { throw new Error('Transaction Link already redeemed!') } - await transactionLink.softRemove() - const newLink = await dbTransactionLink.findOne({ id }, { withDeleted: true }) - return newLink ? newLink.deletedAt : null + await transactionLink.softRemove().catch(() => { + throw new Error('Transaction Link could not be deleted!') + }) + + return true } } From 681f27d4b0d39f1cce351d0de3117be97078e504 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 10 Mar 2022 18:28:42 +0100 Subject: [PATCH 6/7] update entity as in master --- .../TransactionLink.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts b/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts index 789644782..9cd42258a 100644 --- a/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts +++ b/database/entity/0031-remove_sendEmail_from_transaction_link/TransactionLink.ts @@ -42,7 +42,7 @@ export class TransactionLink extends BaseEntity { createdAt: Date @DeleteDateColumn() - deletedAt?: Date | null + deletedAt: Date | null @Column({ type: 'datetime', @@ -54,8 +54,8 @@ export class TransactionLink extends BaseEntity { type: 'datetime', nullable: true, }) - redeemedAt?: Date | null + redeemedAt: Date | null @Column({ type: 'int', unsigned: true, nullable: true }) - redeemedBy?: number | null + redeemedBy: number | null } From 55455d023609a94fcefc9ba22c8fd060688df758 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 10 Mar 2022 18:31:15 +0100 Subject: [PATCH 7/7] remove showEmail form model, args and resolver --- backend/src/graphql/arg/TransactionLinkArgs.ts | 3 --- backend/src/graphql/model/TransactionLink.ts | 1 - backend/src/graphql/resolver/TransactionLinkResolver.ts | 3 +-- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/backend/src/graphql/arg/TransactionLinkArgs.ts b/backend/src/graphql/arg/TransactionLinkArgs.ts index 5ccb967d3..553efcfbe 100644 --- a/backend/src/graphql/arg/TransactionLinkArgs.ts +++ b/backend/src/graphql/arg/TransactionLinkArgs.ts @@ -8,7 +8,4 @@ export default class TransactionLinkArgs { @Field(() => String) memo: string - - @Field(() => Boolean, { nullable: true }) - showEmail?: boolean } diff --git a/backend/src/graphql/model/TransactionLink.ts b/backend/src/graphql/model/TransactionLink.ts index e8e045d92..414bba73f 100644 --- a/backend/src/graphql/model/TransactionLink.ts +++ b/backend/src/graphql/model/TransactionLink.ts @@ -14,7 +14,6 @@ export class TransactionLink { this.code = transactionLink.code this.createdAt = transactionLink.createdAt this.validUntil = transactionLink.validUntil - this.showEmail = transactionLink.showEmail this.deletedAt = transactionLink.deletedAt this.redeemedAt = transactionLink.redeemedAt this.redeemedBy = redeemedBy diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index d60146096..8be9bfae0 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -35,7 +35,7 @@ export class TransactionLinkResolver { @Authorized([RIGHTS.CREATE_TRANSACTION_LINK]) @Mutation(() => TransactionLink) async createTransactionLink( - @Args() { amount, memo, showEmail = false }: TransactionLinkArgs, + @Args() { amount, memo }: TransactionLinkArgs, @Ctx() context: any, ): Promise { const userRepository = getCustomRepository(UserRepository) @@ -60,7 +60,6 @@ export class TransactionLinkResolver { transactionLink.code = transactionLinkCode(createdDate) transactionLink.createdAt = createdDate transactionLink.validUntil = validUntil - transactionLink.showEmail = showEmail await dbTransactionLink.save(transactionLink).catch(() => { throw new Error('Unable to save transaction link') })