mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
add validator for positive amount value with Decimal Type
This commit is contained in:
parent
191103a812
commit
5e3d732e20
@ -3,6 +3,7 @@ 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'
|
||||
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
|
||||
|
||||
@InputType()
|
||||
@ArgsType()
|
||||
@ -11,6 +12,7 @@ export class AdminCreateContributionArgs {
|
||||
email: string
|
||||
|
||||
@Field(() => Decimal)
|
||||
@IsPositiveDecimal()
|
||||
amount: Decimal
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -3,6 +3,7 @@ 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'
|
||||
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
|
||||
|
||||
@ArgsType()
|
||||
export class AdminUpdateContributionArgs {
|
||||
@ -10,6 +11,7 @@ export class AdminUpdateContributionArgs {
|
||||
id: number
|
||||
|
||||
@Field(() => Decimal)
|
||||
@IsPositiveDecimal()
|
||||
amount: Decimal
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -3,11 +3,13 @@ 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'
|
||||
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
|
||||
|
||||
@InputType()
|
||||
@ArgsType()
|
||||
export class ContributionArgs {
|
||||
@Field(() => Decimal)
|
||||
@IsPositiveDecimal()
|
||||
amount: Decimal
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -8,10 +8,12 @@ import {
|
||||
CONTRIBUTIONLINK_NAME_MIN_CHARS,
|
||||
CONTRIBUTIONLINK_NAME_MAX_CHARS,
|
||||
} from '@/graphql/resolver/const/const'
|
||||
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
|
||||
|
||||
@ArgsType()
|
||||
export class ContributionLinkArgs {
|
||||
@Field(() => Decimal)
|
||||
@IsPositiveDecimal()
|
||||
amount: Decimal
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
import { MaxLength, MinLength } from 'class-validator'
|
||||
import { IsAlpha, 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'
|
||||
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
|
||||
|
||||
@ArgsType()
|
||||
export class TransactionLinkArgs {
|
||||
@Field(() => Decimal)
|
||||
@IsPositiveDecimal()
|
||||
amount: Decimal
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -3,6 +3,7 @@ import { Decimal } from 'decimal.js-light'
|
||||
import { ArgsType, Field } from 'type-graphql'
|
||||
|
||||
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from '@/graphql/resolver/const/const'
|
||||
import { IsPositiveDecimal } from '@/graphql/validator/Decimal'
|
||||
|
||||
@ArgsType()
|
||||
export class TransactionSendArgs {
|
||||
@ -10,6 +11,7 @@ export class TransactionSendArgs {
|
||||
identifier: string
|
||||
|
||||
@Field(() => Decimal)
|
||||
@IsPositiveDecimal()
|
||||
amount: Decimal
|
||||
|
||||
@Field(() => String)
|
||||
|
||||
@ -41,10 +41,6 @@ export class ContributionLinkResolver {
|
||||
): Promise<ContributionLink> {
|
||||
isStartEndDateValid(validFrom, validTo)
|
||||
|
||||
if (!new Decimal(amount).isPositive()) {
|
||||
throw new LogError('The amount must be a positiv value', amount)
|
||||
}
|
||||
|
||||
const dbContributionLink = new DbContributionLink()
|
||||
dbContributionLink.amount = amount
|
||||
dbContributionLink.name = name
|
||||
|
||||
@ -73,10 +73,6 @@ export class TransactionLinkResolver {
|
||||
const createdDate = new Date()
|
||||
const validUntil = transactionLinkExpireDate(createdDate)
|
||||
|
||||
if (amount.lessThanOrEqualTo(0)) {
|
||||
throw new LogError('Amount must be a positive number', amount)
|
||||
}
|
||||
|
||||
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
|
||||
|
||||
// validate amount
|
||||
|
||||
@ -308,9 +308,6 @@ export class TransactionResolver {
|
||||
@Ctx() context: Context,
|
||||
): Promise<boolean> {
|
||||
logger.info(`sendCoins(identifier=${identifier}, amount=${amount}, memo=${memo})`)
|
||||
if (amount.lte(0)) {
|
||||
throw new LogError('Amount to send must be positive', amount)
|
||||
}
|
||||
|
||||
const senderUser = getUser(context)
|
||||
|
||||
|
||||
22
backend/src/graphql/validator/Decimal.ts
Normal file
22
backend/src/graphql/validator/Decimal.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator'
|
||||
import { Decimal } from 'decimal.js-light'
|
||||
|
||||
export function IsPositiveDecimal(validationOptions?: ValidationOptions) {
|
||||
return function (object: NonNullable<unknown>, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: 'isPositiveDecimal',
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
target: object.constructor,
|
||||
propertyName,
|
||||
options: validationOptions,
|
||||
validator: {
|
||||
validate(value: Decimal) {
|
||||
return value.greaterThan(0)
|
||||
},
|
||||
defaultMessage(args: ValidationArguments) {
|
||||
return `The ${propertyName} must be a positive value ${args.property}`
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user