From 08b10cc266d00d53a451c6a2223fc073e8ff1fe0 Mon Sep 17 00:00:00 2001 From: Catalin Oancea Date: Mon, 18 Nov 2024 18:26:01 +0200 Subject: [PATCH] Validator --- .../dto/restoration-project-params.dto.ts | 29 +++++++++- .../validation/ecosystem-country.validator.ts | 58 +++++++++++++++++++ .../sequestration-rate-ecosystem.validator.ts | 28 +++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 api/src/modules/custom-projects/validation/ecosystem-country.validator.ts create mode 100644 api/src/modules/custom-projects/validation/sequestration-rate-ecosystem.validator.ts diff --git a/api/src/modules/custom-projects/dto/restoration-project-params.dto.ts b/api/src/modules/custom-projects/dto/restoration-project-params.dto.ts index 6b7ba31c..843c51d3 100644 --- a/api/src/modules/custom-projects/dto/restoration-project-params.dto.ts +++ b/api/src/modules/custom-projects/dto/restoration-project-params.dto.ts @@ -1 +1,28 @@ -export class RestorationProjectParamsDto {} +import { SEQUESTRATION_RATE_TIER_TYPES } from '@shared/entities/carbon-inputs/sequestration-rate.entity'; +import { ECOSYSTEM } from '@shared/entities/ecosystem.enum'; +import { IsNotEmpty, IsNumber, IsEnum, Validate } from 'class-validator'; +import { ValidateEcosystemForTier2SequestrationRate } from '@api/modules/custom-projects/validation/sequestration-rate-ecosystem.validator'; +import { ValidateCountryForActivityType } from '../validation/ecosystem-country.validator'; + +export class RestorationProjectParamsDto { + @Validate(ValidateCountryForActivityType) + countryCode: string; + + @Validate(ValidateEcosystemForTier2SequestrationRate) + ecosystem: ECOSYSTEM; + @IsEnum(SEQUESTRATION_RATE_TIER_TYPES) + sequestrationRateUsed: SEQUESTRATION_RATE_TIER_TYPES; + + @IsNotEmpty({ + message: 'Planting success rate is required for restoration projects', + }) + @IsNumber() + plantingSuccessRate: number; + + @IsNotEmpty({ + message: + 'Project Specific Sequestration Rate is required for restoration projects', + }) + @IsNumber() + projectSpecificSequestrationRate: number; +} diff --git a/api/src/modules/custom-projects/validation/ecosystem-country.validator.ts b/api/src/modules/custom-projects/validation/ecosystem-country.validator.ts new file mode 100644 index 00000000..d12282c3 --- /dev/null +++ b/api/src/modules/custom-projects/validation/ecosystem-country.validator.ts @@ -0,0 +1,58 @@ +import { + ValidatorConstraint, + ValidatorConstraintInterface, + ValidationArguments, +} from 'class-validator'; +import { ECOSYSTEM } from '@shared/entities/ecosystem.enum'; +import { RESTORATION_ACTIVITY_SUBTYPE } from '@shared/entities/activity.enum'; + +@ValidatorConstraint({ + name: 'ValidateCountryForActivityType', + async: false, +}) +export class ValidateCountryForActivityType + implements ValidatorConstraintInterface +{ + validate(country: string, args: ValidationArguments): boolean { + const obj = args.object as any; + + return !( + (obj.ecosystem === ECOSYSTEM.MANGROVE && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.HYBRID && + country.toUpperCase() in + ['IDN', 'AUS', 'BHS', 'KEN', 'COL', 'IND', 'CHN', 'ECU']) || + (obj.ecosystem === ECOSYSTEM.MANGROVE && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY && + country.toUpperCase() === 'ECU') || + (obj.ecosystem === ECOSYSTEM.SEAGRASS && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.PLANTING && + country.toUpperCase() === 'ECU') || + (obj.ecosystem === ECOSYSTEM.SEAGRASS && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.HYBRID) || + (obj.ecosystem === ECOSYSTEM.SEAGRASS && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY) || + (obj.ecosystem === ECOSYSTEM.SALT_MARSH && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.PLANTING && + country.toUpperCase() === 'ECU') || + (obj.ecosystem === ECOSYSTEM.SALT_MARSH && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.HYBRID && + country.toUpperCase() in + ['IDN', 'AUS', 'BHS', 'KEN', 'COL', 'IND', 'CHN', 'ECU']) || + (obj.ecosystem === ECOSYSTEM.SALT_MARSH && + obj.restorationActivitySubtype === + RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY && + country.toUpperCase() === 'ECU') + ); + } + + defaultMessage(): string { + return 'No data available for the selected country and activity subtype type.'; + } +} diff --git a/api/src/modules/custom-projects/validation/sequestration-rate-ecosystem.validator.ts b/api/src/modules/custom-projects/validation/sequestration-rate-ecosystem.validator.ts new file mode 100644 index 00000000..b8824771 --- /dev/null +++ b/api/src/modules/custom-projects/validation/sequestration-rate-ecosystem.validator.ts @@ -0,0 +1,28 @@ +import { + ValidatorConstraint, + ValidatorConstraintInterface, + ValidationArguments, +} from 'class-validator'; +import { ECOSYSTEM } from '@shared/entities/ecosystem.enum'; +import { SEQUESTRATION_RATE_TIER_TYPES } from '@shared/entities/carbon-inputs/sequestration-rate.entity'; + +@ValidatorConstraint({ + name: 'ValidateEcosystemForTier2SequestrationRate', + async: false, +}) +export class ValidateEcosystemForTier2SequestrationRate + implements ValidatorConstraintInterface +{ + validate(value: ECOSYSTEM, args: ValidationArguments): boolean { + const obj = args.object as any; + + return !( + obj.emissionFactorUsed === SEQUESTRATION_RATE_TIER_TYPES.TIER_2 && + value !== ECOSYSTEM.MANGROVE + ); + } + + defaultMessage(): string { + return 'No default tier 2 data available for seagrass and salt marsh.'; + } +}