-
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
12 changed files
with
284 additions
and
205 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
174 changes: 88 additions & 86 deletions
174
api/src/modules/custom-projects/validation/conservation-project.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 |
---|---|---|
@@ -1,86 +1,88 @@ | ||
import { CreateCustomProjectDto } from '@shared/dtos/custom-projects/create-custom-project.dto'; | ||
import { LOSS_RATE_USED } from '@shared/schemas/custom-projects/create-custom-project.schema'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { BaseDataView } from '@shared/entities/base-data.view'; | ||
import { EMISSION_FACTORS_TIER_TYPES } from '@shared/entities/carbon-inputs/emission-factors.entity'; | ||
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum'; | ||
|
||
export class ConservationProjectValidator { | ||
validatedProject: Record<any, any>; | ||
data: BaseDataView; | ||
createDto: CreateCustomProjectDto & { projectSpecificLossRate: number }; | ||
constructor(dto, data: BaseDataView) { | ||
this.createDto = dto; | ||
this.data = data; | ||
} | ||
// Probably SET PROJECT PARAMETERS WOULD BE A BETTER NAME, and just return the validated project parameters | ||
validate() { | ||
this.setLossRate(); | ||
return this.createDto; | ||
} | ||
private setLossRate() { | ||
if ( | ||
this.createDto.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC && | ||
!this.createDto.projectSpecificLossRate | ||
) { | ||
throw new BadRequestException( | ||
'projectSpecificLossRate is required when lossRateUsed is projectSpecific', | ||
); | ||
} | ||
if ( | ||
this.createDto.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC && | ||
this.createDto.projectSpecificLossRate >= 0 | ||
) { | ||
throw new BadRequestException( | ||
'projectSpecificLossRate should be negative when lossRateUsed is projectSpecific', | ||
); | ||
} | ||
if ( | ||
this.createDto.lossRateUsed === LOSS_RATE_USED.NATIONAL_AVERAGE && | ||
this.createDto.projectSpecificLossRate | ||
) { | ||
throw new BadRequestException( | ||
'projectSpecificLossRate should not be provided when lossRateUsed is not projectSpecific', | ||
); | ||
} | ||
if (this.createDto.lossRateUsed === LOSS_RATE_USED.NATIONAL_AVERAGE) { | ||
this.validatedProject.lossRate = this.data.ecosystemLossRate; | ||
} | ||
if (this.createDto.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC) { | ||
this.validatedProject.lossRate = this.createDto.projectSpecificLossRate; | ||
} | ||
} | ||
|
||
private setEmissionFactor() { | ||
if ( | ||
this.createDto.emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_1 | ||
) { | ||
this.validatedProject.emissionFactor = this.data.tier1EmissionFactor; | ||
return; | ||
} | ||
if ( | ||
this.createDto.emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_2 | ||
) { | ||
if (this.createDto.ecosystem !== ECOSYSTEM.MANGROVE) { | ||
throw new BadRequestException( | ||
`No Tier 2 emission factors for ${this.createDto.ecosystem}`, | ||
); | ||
} | ||
this.validatedProject.emissionFactorAGB = this.data.emissionFactorAgb; | ||
this.validatedProject.emissionFactorSOC = this.data.emissionFactorSoc; | ||
return; | ||
} | ||
if (this.createDto.projectSpecificEmission === 'One emission factor') { | ||
this.validatedProject.emissionFactor = | ||
this.createDto.projectSpecificEmissionFactor; | ||
this.validatedProject.emissionFactorAGB = 0; | ||
this.validatedProject.emissionFactorSOC = 0; | ||
} else { | ||
this.validatedProject.emissionFactor = null; | ||
this.validatedProject.emissionFactorAGB = | ||
this.createDto.emissionFactorAGB; | ||
this.validatedProject.emissionFactorSOC = | ||
this.createDto.emissionFactorSOC; | ||
} | ||
} | ||
} | ||
// import { LOSS_RATE_USED } from '@shared/schemas/custom-projects/create-custom-project.schema'; | ||
// import { BadRequestException } from '@nestjs/common'; | ||
// import { BaseDataView } from '@shared/entities/base-data.view'; | ||
// import { EMISSION_FACTORS_TIER_TYPES } from '@shared/entities/carbon-inputs/emission-factors.entity'; | ||
// import { ECOSYSTEM } from '@shared/entities/ecosystem.enum'; | ||
// import { CreateCustomProjectDto } from '@shared/dtos/custom-projects/create-custom-project-dto.deprecated'; | ||
// | ||
// export class ConservationProjectValidator { | ||
// validatedProject: Record<any, any>; | ||
// data: BaseDataView; | ||
// createDto: CreateCustomProjectDto & { | ||
// projectSpecificLossRate: number; | ||
// }; | ||
// constructor(dto, data: BaseDataView) { | ||
// this.createDto = dto; | ||
// this.data = data; | ||
// } | ||
// // Probably SET PROJECT PARAMETERS WOULD BE A BETTER NAME, and just return the validated project parameters | ||
// validate() { | ||
// this.setLossRate(); | ||
// return this.createDto; | ||
// } | ||
// private setLossRate() { | ||
// if ( | ||
// this.createDto.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC && | ||
// !this.createDto.projectSpecificLossRate | ||
// ) { | ||
// throw new BadRequestException( | ||
// 'projectSpecificLossRate is required when lossRateUsed is projectSpecific', | ||
// ); | ||
// } | ||
// if ( | ||
// this.createDto.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC && | ||
// this.createDto.projectSpecificLossRate >= 0 | ||
// ) { | ||
// throw new BadRequestException( | ||
// 'projectSpecificLossRate should be negative when lossRateUsed is projectSpecific', | ||
// ); | ||
// } | ||
// if ( | ||
// this.createDto.lossRateUsed === LOSS_RATE_USED.NATIONAL_AVERAGE && | ||
// this.createDto.projectSpecificLossRate | ||
// ) { | ||
// throw new BadRequestException( | ||
// 'projectSpecificLossRate should not be provided when lossRateUsed is not projectSpecific', | ||
// ); | ||
// } | ||
// if (this.createDto.lossRateUsed === LOSS_RATE_USED.NATIONAL_AVERAGE) { | ||
// this.validatedProject.lossRate = this.data.ecosystemLossRate; | ||
// } | ||
// if (this.createDto.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC) { | ||
// this.validatedProject.lossRate = this.createDto.projectSpecificLossRate; | ||
// } | ||
// } | ||
// | ||
// private setEmissionFactor() { | ||
// if ( | ||
// this.createDto.emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_1 | ||
// ) { | ||
// this.validatedProject.emissionFactor = this.data.tier1EmissionFactor; | ||
// return; | ||
// } | ||
// if ( | ||
// this.createDto.emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_2 | ||
// ) { | ||
// if (this.createDto.ecosystem !== ECOSYSTEM.MANGROVE) { | ||
// throw new BadRequestException( | ||
// `No Tier 2 emission factors for ${this.createDto.ecosystem}`, | ||
// ); | ||
// } | ||
// this.validatedProject.emissionFactorAGB = this.data.emissionFactorAgb; | ||
// this.validatedProject.emissionFactorSOC = this.data.emissionFactorSoc; | ||
// return; | ||
// } | ||
// if (this.createDto.projectSpecificEmission === 'One emission factor') { | ||
// this.validatedProject.emissionFactor = | ||
// this.createDto.projectSpecificEmissionFactor; | ||
// this.validatedProject.emissionFactorAGB = 0; | ||
// this.validatedProject.emissionFactorSOC = 0; | ||
// } else { | ||
// this.validatedProject.emissionFactor = null; | ||
// this.validatedProject.emissionFactorAGB = | ||
// this.createDto.emissionFactorAGB; | ||
// this.validatedProject.emissionFactorSOC = | ||
// this.createDto.emissionFactorSOC; | ||
// } | ||
// } | ||
// } |
47 changes: 23 additions & 24 deletions
47
api/src/modules/custom-projects/validation/create-custom-project.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 |
---|---|---|
@@ -1,24 +1,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DataSource } from 'typeorm'; | ||
import { CreateCustomProjectDto } from '@shared/dtos/custom-projects/create-custom-project.dto'; | ||
import { ACTIVITY } from '@shared/entities/activity.enum'; | ||
import { ConservationProjectValidator } from '@api/modules/custom-projects/validation/conservation-project.validator'; | ||
import { CalculationEngine } from '@api/modules/calculations/calculation.engine'; | ||
|
||
@Injectable() | ||
export class CreateCustomProjectValidator { | ||
project: Record<any, any>; | ||
constructor(private readonly calculationEngine: CalculationEngine) {} | ||
|
||
async validate(dto: CreateCustomProjectDto) { | ||
const { countryCode, ecosystem, activity, projectName } = dto; | ||
const { baseData } = await this.calculationEngine.getBaseData({ | ||
countryCode, | ||
ecosystem, | ||
activity, | ||
}); | ||
if (dto.activity === ACTIVITY.CONSERVATION) { | ||
return new ConservationProjectValidator(dto, baseData).validate(); | ||
} | ||
} | ||
} | ||
// import { Injectable } from '@nestjs/common'; | ||
// import { ACTIVITY } from '@shared/entities/activity.enum'; | ||
// import { ConservationProjectValidator } from '@api/modules/custom-projects/validation/conservation-project.validator'; | ||
// import { CalculationEngine } from '@api/modules/calculations/calculation.engine'; | ||
// import { CreateCustomProjectDto } from '@shared/dtos/custom-projects/create-custom-project-dto.deprecated'; | ||
// | ||
// @Injectable() | ||
// export class CreateCustomProjectValidator { | ||
// project: Record<any, any>; | ||
// constructor(private readonly calculationEngine: CalculationEngine) {} | ||
// | ||
// async validate(dto: CreateCustomProjectDto) { | ||
// const { countryCode, ecosystem, activity, projectName } = dto; | ||
// const { baseData } = await this.calculationEngine.getBaseData({ | ||
// countryCode, | ||
// ecosystem, | ||
// activity, | ||
// }); | ||
// if (dto.activity === ACTIVITY.CONSERVATION) { | ||
// return new ConservationProjectValidator(dto, baseData).validate(); | ||
// } | ||
// } | ||
// } |
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
Oops, something went wrong.