-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c36a6
commit 08b10cc
Showing
3 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 28 additions & 1 deletion
29
api/src/modules/custom-projects/dto/restoration-project-params.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
58 changes: 58 additions & 0 deletions
58
api/src/modules/custom-projects/validation/ecosystem-country.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/modules/custom-projects/validation/sequestration-rate-ecosystem.validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'; | ||
} | ||
} |