From 7c4d55928fa3a1eeeaecbc5f3f26aaa23deb11db Mon Sep 17 00:00:00 2001 From: Einhornimmond Date: Wed, 9 Aug 2023 13:29:41 +0200 Subject: [PATCH] move memo length validation into args object like it is recommended for typed graphql --- .../src/graphql/arg/AdminCreateContributionArgs.ts | 5 +++++ .../src/graphql/arg/AdminUpdateContributionArgs.ts | 5 +++++ backend/src/graphql/arg/ContributionArgs.ts | 5 +++++ backend/src/graphql/arg/ContributionLinkArgs.ts | 5 +++++ backend/src/graphql/arg/TransactionLinkArgs.ts | 5 +++++ backend/src/graphql/arg/TransactionSendArgs.ts | 5 +++++ .../graphql/resolver/ContributionLinkResolver.ts | 13 +------------ .../src/graphql/resolver/ContributionResolver.ts | 13 ------------- backend/src/graphql/resolver/TransactionResolver.ts | 9 --------- 9 files changed, 31 insertions(+), 34 deletions(-) diff --git a/backend/src/graphql/arg/AdminCreateContributionArgs.ts b/backend/src/graphql/arg/AdminCreateContributionArgs.ts index 8e2fa28da..1c6007c48 100644 --- a/backend/src/graphql/arg/AdminCreateContributionArgs.ts +++ b/backend/src/graphql/arg/AdminCreateContributionArgs.ts @@ -1,6 +1,9 @@ +import { MaxLength, MinLength } from 'class-validator' import { Decimal } from 'decimal.js-light' import { ArgsType, Field, InputType } from 'type-graphql' +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const' + @InputType() @ArgsType() export class AdminCreateContributionArgs { @@ -11,6 +14,8 @@ export class AdminCreateContributionArgs { amount: Decimal @Field(() => String) + @MaxLength(MEMO_MAX_CHARS) + @MinLength(MEMO_MIN_CHARS) memo: string @Field(() => String) diff --git a/backend/src/graphql/arg/AdminUpdateContributionArgs.ts b/backend/src/graphql/arg/AdminUpdateContributionArgs.ts index e79260c63..4958b4346 100644 --- a/backend/src/graphql/arg/AdminUpdateContributionArgs.ts +++ b/backend/src/graphql/arg/AdminUpdateContributionArgs.ts @@ -1,6 +1,9 @@ +import { MaxLength, MinLength } from 'class-validator' import { Decimal } from 'decimal.js-light' import { ArgsType, Field, Int } from 'type-graphql' +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const' + @ArgsType() export class AdminUpdateContributionArgs { @Field(() => Int) @@ -10,6 +13,8 @@ export class AdminUpdateContributionArgs { amount: Decimal @Field(() => String) + @MaxLength(MEMO_MAX_CHARS) + @MinLength(MEMO_MIN_CHARS) memo: string @Field(() => String) diff --git a/backend/src/graphql/arg/ContributionArgs.ts b/backend/src/graphql/arg/ContributionArgs.ts index db688d811..e2449ea95 100644 --- a/backend/src/graphql/arg/ContributionArgs.ts +++ b/backend/src/graphql/arg/ContributionArgs.ts @@ -1,6 +1,9 @@ +import { MaxLength, MinLength } from 'class-validator' import { Decimal } from 'decimal.js-light' import { ArgsType, Field, InputType } from 'type-graphql' +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const' + @InputType() @ArgsType() export class ContributionArgs { @@ -8,6 +11,8 @@ export class ContributionArgs { amount: Decimal @Field(() => String) + @MaxLength(MEMO_MAX_CHARS) + @MinLength(MEMO_MIN_CHARS) memo: string @Field(() => String) diff --git a/backend/src/graphql/arg/ContributionLinkArgs.ts b/backend/src/graphql/arg/ContributionLinkArgs.ts index cef72148a..9d12a6128 100644 --- a/backend/src/graphql/arg/ContributionLinkArgs.ts +++ b/backend/src/graphql/arg/ContributionLinkArgs.ts @@ -1,6 +1,9 @@ +import { MaxLength, MinLength } from 'class-validator' import { Decimal } from 'decimal.js-light' import { ArgsType, Field, Int } from 'type-graphql' +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const' + @ArgsType() export class ContributionLinkArgs { @Field(() => Decimal) @@ -10,6 +13,8 @@ export class ContributionLinkArgs { name: string @Field(() => String) + @MaxLength(MEMO_MAX_CHARS) + @MinLength(MEMO_MIN_CHARS) memo: string @Field(() => String) diff --git a/backend/src/graphql/arg/TransactionLinkArgs.ts b/backend/src/graphql/arg/TransactionLinkArgs.ts index f44ef0356..2ffd91d33 100644 --- a/backend/src/graphql/arg/TransactionLinkArgs.ts +++ b/backend/src/graphql/arg/TransactionLinkArgs.ts @@ -1,11 +1,16 @@ +import { MaxLength, MinLength } from 'class-validator' import { Decimal } from 'decimal.js-light' import { ArgsType, Field } from 'type-graphql' +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const' + @ArgsType() export class TransactionLinkArgs { @Field(() => Decimal) amount: Decimal @Field(() => String) + @MaxLength(MEMO_MAX_CHARS) + @MinLength(MEMO_MIN_CHARS) memo: string } diff --git a/backend/src/graphql/arg/TransactionSendArgs.ts b/backend/src/graphql/arg/TransactionSendArgs.ts index ecda848d1..1279f2051 100644 --- a/backend/src/graphql/arg/TransactionSendArgs.ts +++ b/backend/src/graphql/arg/TransactionSendArgs.ts @@ -1,6 +1,9 @@ +import { MaxLength, MinLength } from 'class-validator' import { Decimal } from 'decimal.js-light' import { ArgsType, Field } from 'type-graphql' +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const' + @ArgsType() export class TransactionSendArgs { @Field(() => String) @@ -10,5 +13,7 @@ export class TransactionSendArgs { amount: Decimal @Field(() => String) + @MaxLength(MEMO_MAX_CHARS) + @MinLength(MEMO_MIN_CHARS) memo: string } diff --git a/backend/src/graphql/resolver/ContributionLinkResolver.ts b/backend/src/graphql/resolver/ContributionLinkResolver.ts index 808bd584e..42593dec1 100644 --- a/backend/src/graphql/resolver/ContributionLinkResolver.ts +++ b/backend/src/graphql/resolver/ContributionLinkResolver.ts @@ -18,12 +18,7 @@ import { import { Context, getUser } from '@/server/context' import { LogError } from '@/server/LogError' -import { - CONTRIBUTIONLINK_NAME_MAX_CHARS, - CONTRIBUTIONLINK_NAME_MIN_CHARS, - MEMO_MAX_CHARS, - MEMO_MIN_CHARS, -} from './const/const' +import { CONTRIBUTIONLINK_NAME_MAX_CHARS, CONTRIBUTIONLINK_NAME_MIN_CHARS } from './const/const' import { transactionLinkCode as contributionLinkCode } from './TransactionLinkResolver' import { isStartEndDateValid } from './util/creations' @@ -52,12 +47,6 @@ export class ContributionLinkResolver { if (name.length > CONTRIBUTIONLINK_NAME_MAX_CHARS) { throw new LogError('The value of name is too long', name.length) } - if (memo.length < MEMO_MIN_CHARS) { - throw new LogError('The value of memo is too short', memo.length) - } - if (memo.length > MEMO_MAX_CHARS) { - throw new LogError('The value of memo is too long', memo.length) - } if (!new Decimal(amount).isPositive()) { throw new LogError('The amount must be a positiv value', amount) } diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index 80ea3e783..7b4c21708 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -45,7 +45,6 @@ import { calculateDecay } from '@/util/decay' import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK' import { fullName } from '@/util/utilities' -import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' import { getUserCreation, validateContribution, @@ -65,12 +64,6 @@ export class ContributionResolver { @Ctx() context: Context, ): Promise { const clientTimezoneOffset = getClientTimezoneOffset(context) - if (memo.length < MEMO_MIN_CHARS) { - throw new LogError('Memo text is too short', memo.length) - } - if (memo.length > MEMO_MAX_CHARS) { - throw new LogError('Memo text is too long', memo.length) - } const user = getUser(context) const creations = await getUserCreation(user.id, clientTimezoneOffset) @@ -186,12 +179,6 @@ export class ContributionResolver { @Ctx() context: Context, ): Promise { const clientTimezoneOffset = getClientTimezoneOffset(context) - if (memo.length < MEMO_MIN_CHARS) { - throw new LogError('Memo text is too short', memo.length) - } - if (memo.length > MEMO_MAX_CHARS) { - throw new LogError('Memo text is too long', memo.length) - } const user = getUser(context) diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 5f42fb36e..721cba8fa 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -33,7 +33,6 @@ import { calculateBalance } from '@/util/validate' import { virtualLinkTransaction, virtualDecayTransaction } from '@/util/virtualTransactions' import { BalanceResolver } from './BalanceResolver' -import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' import { findUserByIdentifier } from './util/findUserByIdentifier' import { getLastTransaction } from './util/getLastTransaction' import { getTransactionList } from './util/getTransactionList' @@ -55,14 +54,6 @@ export const executeTransaction = async ( throw new LogError('Sender and Recipient are the same', sender.id) } - if (memo.length < MEMO_MIN_CHARS) { - throw new LogError('Memo text is too short', memo.length) - } - - if (memo.length > MEMO_MAX_CHARS) { - throw new LogError('Memo text is too long', memo.length) - } - // validate amount const receivedCallDate = new Date() const sendBalance = await calculateBalance(