move memo length validation into args object like it is recommended for typed graphql

This commit is contained in:
Einhornimmond 2023-08-09 13:29:41 +02:00
parent 5538e8918e
commit 7c4d55928f
9 changed files with 31 additions and 34 deletions

View File

@ -1,6 +1,9 @@
import { MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light' import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, InputType } from 'type-graphql' import { ArgsType, Field, InputType } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
@InputType() @InputType()
@ArgsType() @ArgsType()
export class AdminCreateContributionArgs { export class AdminCreateContributionArgs {
@ -11,6 +14,8 @@ export class AdminCreateContributionArgs {
amount: Decimal amount: Decimal
@Field(() => String) @Field(() => String)
@MaxLength(MEMO_MAX_CHARS)
@MinLength(MEMO_MIN_CHARS)
memo: string memo: string
@Field(() => String) @Field(() => String)

View File

@ -1,6 +1,9 @@
import { MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light' import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, Int } from 'type-graphql' import { ArgsType, Field, Int } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
@ArgsType() @ArgsType()
export class AdminUpdateContributionArgs { export class AdminUpdateContributionArgs {
@Field(() => Int) @Field(() => Int)
@ -10,6 +13,8 @@ export class AdminUpdateContributionArgs {
amount: Decimal amount: Decimal
@Field(() => String) @Field(() => String)
@MaxLength(MEMO_MAX_CHARS)
@MinLength(MEMO_MIN_CHARS)
memo: string memo: string
@Field(() => String) @Field(() => String)

View File

@ -1,6 +1,9 @@
import { MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light' import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, InputType } from 'type-graphql' import { ArgsType, Field, InputType } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
@InputType() @InputType()
@ArgsType() @ArgsType()
export class ContributionArgs { export class ContributionArgs {
@ -8,6 +11,8 @@ export class ContributionArgs {
amount: Decimal amount: Decimal
@Field(() => String) @Field(() => String)
@MaxLength(MEMO_MAX_CHARS)
@MinLength(MEMO_MIN_CHARS)
memo: string memo: string
@Field(() => String) @Field(() => String)

View File

@ -1,6 +1,9 @@
import { MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light' import { Decimal } from 'decimal.js-light'
import { ArgsType, Field, Int } from 'type-graphql' import { ArgsType, Field, Int } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
@ArgsType() @ArgsType()
export class ContributionLinkArgs { export class ContributionLinkArgs {
@Field(() => Decimal) @Field(() => Decimal)
@ -10,6 +13,8 @@ export class ContributionLinkArgs {
name: string name: string
@Field(() => String) @Field(() => String)
@MaxLength(MEMO_MAX_CHARS)
@MinLength(MEMO_MIN_CHARS)
memo: string memo: string
@Field(() => String) @Field(() => String)

View File

@ -1,11 +1,16 @@
import { MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light' import { Decimal } from 'decimal.js-light'
import { ArgsType, Field } from 'type-graphql' import { ArgsType, Field } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
@ArgsType() @ArgsType()
export class TransactionLinkArgs { export class TransactionLinkArgs {
@Field(() => Decimal) @Field(() => Decimal)
amount: Decimal amount: Decimal
@Field(() => String) @Field(() => String)
@MaxLength(MEMO_MAX_CHARS)
@MinLength(MEMO_MIN_CHARS)
memo: string memo: string
} }

View File

@ -1,6 +1,9 @@
import { MaxLength, MinLength } from 'class-validator'
import { Decimal } from 'decimal.js-light' import { Decimal } from 'decimal.js-light'
import { ArgsType, Field } from 'type-graphql' import { ArgsType, Field } from 'type-graphql'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
@ArgsType() @ArgsType()
export class TransactionSendArgs { export class TransactionSendArgs {
@Field(() => String) @Field(() => String)
@ -10,5 +13,7 @@ export class TransactionSendArgs {
amount: Decimal amount: Decimal
@Field(() => String) @Field(() => String)
@MaxLength(MEMO_MAX_CHARS)
@MinLength(MEMO_MIN_CHARS)
memo: string memo: string
} }

View File

@ -18,12 +18,7 @@ import {
import { Context, getUser } from '@/server/context' import { Context, getUser } from '@/server/context'
import { LogError } from '@/server/LogError' import { LogError } from '@/server/LogError'
import { import { CONTRIBUTIONLINK_NAME_MAX_CHARS, CONTRIBUTIONLINK_NAME_MIN_CHARS } from './const/const'
CONTRIBUTIONLINK_NAME_MAX_CHARS,
CONTRIBUTIONLINK_NAME_MIN_CHARS,
MEMO_MAX_CHARS,
MEMO_MIN_CHARS,
} from './const/const'
import { transactionLinkCode as contributionLinkCode } from './TransactionLinkResolver' import { transactionLinkCode as contributionLinkCode } from './TransactionLinkResolver'
import { isStartEndDateValid } from './util/creations' import { isStartEndDateValid } from './util/creations'
@ -52,12 +47,6 @@ export class ContributionLinkResolver {
if (name.length > CONTRIBUTIONLINK_NAME_MAX_CHARS) { if (name.length > CONTRIBUTIONLINK_NAME_MAX_CHARS) {
throw new LogError('The value of name is too long', name.length) 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()) { if (!new Decimal(amount).isPositive()) {
throw new LogError('The amount must be a positiv value', amount) throw new LogError('The amount must be a positiv value', amount)
} }

View File

@ -45,7 +45,6 @@ import { calculateDecay } from '@/util/decay'
import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK' import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK'
import { fullName } from '@/util/utilities' import { fullName } from '@/util/utilities'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const'
import { import {
getUserCreation, getUserCreation,
validateContribution, validateContribution,
@ -65,12 +64,6 @@ export class ContributionResolver {
@Ctx() context: Context, @Ctx() context: Context,
): Promise<UnconfirmedContribution> { ): Promise<UnconfirmedContribution> {
const clientTimezoneOffset = getClientTimezoneOffset(context) 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 user = getUser(context)
const creations = await getUserCreation(user.id, clientTimezoneOffset) const creations = await getUserCreation(user.id, clientTimezoneOffset)
@ -186,12 +179,6 @@ export class ContributionResolver {
@Ctx() context: Context, @Ctx() context: Context,
): Promise<UnconfirmedContribution> { ): Promise<UnconfirmedContribution> {
const clientTimezoneOffset = getClientTimezoneOffset(context) 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 user = getUser(context)

View File

@ -33,7 +33,6 @@ import { calculateBalance } from '@/util/validate'
import { virtualLinkTransaction, virtualDecayTransaction } from '@/util/virtualTransactions' import { virtualLinkTransaction, virtualDecayTransaction } from '@/util/virtualTransactions'
import { BalanceResolver } from './BalanceResolver' import { BalanceResolver } from './BalanceResolver'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const'
import { findUserByIdentifier } from './util/findUserByIdentifier' import { findUserByIdentifier } from './util/findUserByIdentifier'
import { getLastTransaction } from './util/getLastTransaction' import { getLastTransaction } from './util/getLastTransaction'
import { getTransactionList } from './util/getTransactionList' import { getTransactionList } from './util/getTransactionList'
@ -55,14 +54,6 @@ export const executeTransaction = async (
throw new LogError('Sender and Recipient are the same', sender.id) 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 // validate amount
const receivedCallDate = new Date() const receivedCallDate = new Date()
const sendBalance = await calculateBalance( const sendBalance = await calculateBalance(