-
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.
add general custom project validation
- Loading branch information
Showing
11 changed files
with
183 additions
and
63 deletions.
There are no files selected for viewing
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
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
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
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
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
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
53 changes: 53 additions & 0 deletions
53
shared/dtos/custom-projects/conservation-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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { LOSS_RATE_USED } from "@shared/schemas/custom-projects/create-custom-project.schema"; | ||
import { | ||
IsEnum, | ||
IsNegative, | ||
IsNotEmpty, | ||
IsNumber, | ||
ValidateIf, | ||
} from "class-validator"; | ||
import { EMISSION_FACTORS_TIER_TYPES } from "@shared/entities/carbon-inputs/emission-factors.entity"; | ||
import { CreateCustomProjectDto } from "@shared/dtos/custom-projects/create-custom-project-dto.deprecated"; | ||
import { ACTIVITY } from "@shared/entities/activity.enum"; | ||
|
||
export enum PROJECT_SPECIFIC_EMISSION { | ||
ONE_EMISSION_FACTOR = "One emission factor", | ||
TWO_EMISSION_FACTORS = "Two emission factors", | ||
} | ||
export enum TIER_3_EMISSION_FACTORS { | ||
TIER_3 = "Tier 3 - Project specific emission factor", | ||
} | ||
|
||
export class ConservationProjectParamDto { | ||
@ValidateIf((o) => o.acitvity === ACTIVITY.CONSERVATION) | ||
@IsEnum(LOSS_RATE_USED) | ||
lossRateUsed: LOSS_RATE_USED; | ||
|
||
@ValidateIf((o) => o.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC) | ||
@IsNotEmpty({ | ||
message: | ||
"Project Specific Loss Rate is required when lossRateUsed is projectSpecific", | ||
}) | ||
@IsNumber() | ||
@IsNegative({ message: "Project Specific Loss Rate must be negative" }) | ||
projectSpecificLossRate?: number; | ||
|
||
@IsEnum(EMISSION_FACTORS_TIER_TYPES || TIER_3_EMISSION_FACTORS) | ||
emissionFactorUsed: EMISSION_FACTORS_TIER_TYPES | TIER_3_EMISSION_FACTORS; | ||
|
||
// This should be set at later stages for the calculations, but it is not required for the consumer | ||
//emissionFactor: number; | ||
|
||
@ValidateIf( | ||
(o) => o.emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_2, | ||
) | ||
emissionFactorAGB: number; | ||
|
||
emissionFactorSOC: number; | ||
|
||
@ValidateIf((o) => o.emissionFactorUsed === TIER_3_EMISSION_FACTORS.TIER_3) | ||
@IsEnum(PROJECT_SPECIFIC_EMISSION) | ||
projectSpecificEmission: PROJECT_SPECIFIC_EMISSION; | ||
|
||
projectSpecificEmissionFactor: number; | ||
} |
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
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,35 @@ | ||
import { | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
ValidationArguments, | ||
validateSync, | ||
} from "class-validator"; | ||
|
||
import { ACTIVITY } from "@shared/entities/activity.enum"; | ||
import { ConservationProjectParamDto } from "@shared/dtos/custom-projects/conservation-project-params.dto"; | ||
import { RestorationProjectParamsDto } from "@shared/dtos/custom-projects/restoration-project-params.dto"; | ||
import { CreateCustomProjectDto } from "@shared/dtos/custom-projects/create-custom-project-dto.deprecated"; | ||
|
||
@ValidatorConstraint({ name: "ProjectParameterValidator", async: false }) | ||
export class ProjectParamsValidator implements ValidatorConstraintInterface { | ||
validate(value: CreateCustomProjectDto, args: ValidationArguments): boolean { | ||
const object = args.object as any; | ||
const { activity } = object; | ||
|
||
let dto; | ||
if (activity === ACTIVITY.CONSERVATION) { | ||
dto = new ConservationProjectParamDto(); | ||
} else if (activity === ACTIVITY.RESTORATION) { | ||
dto = new RestorationProjectParamsDto(); | ||
} else { | ||
return false; | ||
} | ||
|
||
const validationErrors = validateSync(Object.assign(dto, value)); | ||
return validationErrors.length === 0; | ||
} | ||
|
||
defaultMessage(args: ValidationArguments): string { | ||
return `Invalid parameters for the selected activity type.`; | ||
} | ||
} |
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 @@ | ||
export class RestorationProjectParamsDto {} |
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