Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TBCCT-275] fix(api): Remove ecosystem dependant assumptions for conservation activity type (Refactor to reduce technical debt needed) #252

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions api/src/modules/calculations/assumptions.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
COMMON_OVERRIDABLE_ASSUMPTION_NAMES,
ECOSYSTEM_RESTORATION_RATE_NAMES,
} from '@shared/schemas/assumptions/assumptions.enums';
import { OverridableAssumptions } from '@api/modules/custom-projects/dto/project-assumptions.dto';
import { ModelAssumptions } from '@shared/entities/model-assumptions.entity';
import { OverridableAssumptionsDto } from '@api/modules/custom-projects/dto/create-custom-project.dto';

const NON_OVERRIDABLE_ASSUMPTION_NAMES_MAP = {
'Annual cost increase': 'annualCostIncrease',
Expand Down Expand Up @@ -57,9 +57,6 @@ export class AssumptionsRepository extends Repository<ModelAssumptions> {
})
.orderBy('name', 'ASC')
.getRawMany();
if (assumptions.length !== 7) {
throw new Error('Not all required overridable assumptions were found');
}
return assumptions;
}

Expand All @@ -68,7 +65,9 @@ export class AssumptionsRepository extends Repository<ModelAssumptions> {
): string[] {
const { ecosystem, activity } = dto;
const assumptions = [...COMMON_OVERRIDABLE_ASSUMPTION_NAMES] as string[];
assumptions.push(this.map[ecosystem]);
if (activity === ACTIVITY.RESTORATION) {
assumptions.push(this.map[ecosystem]);
}
assumptions.push(this.map[activity]);
return assumptions;
}
Expand Down Expand Up @@ -127,4 +126,4 @@ export class NonOverridableModelAssumptions {
}

export type ModelAssumptionsForCalculations = NonOverridableModelAssumptions &
OverridableAssumptions;
OverridableAssumptionsDto;
1 change: 0 additions & 1 deletion api/src/modules/calculations/calculation.engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import {
CostCalculator,
ProjectInput,
Expand Down
8 changes: 3 additions & 5 deletions api/src/modules/calculations/cost.calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { ConservationProjectInput } from '@api/modules/custom-projects/input-fac
import { RestorationProjectInput } from '@api/modules/custom-projects/input-factory/restoration-project.input';
import { BaseSize } from '@shared/entities/base-size.entity';
import { BaseIncrease } from '@shared/entities/base-increase.entity';
import {
OverridableCostInputs,
PROJECT_DEVELOPMENT_TYPE,
} from '@api/modules/custom-projects/dto/project-cost-inputs.dto';
import { RevenueProfitCalculator } from '@api/modules/calculations/revenue-profit.calculator';
import { SequestrationRateCalculator } from '@api/modules/calculations/sequestration-rate.calculator';
import { parseInt, sum } from 'lodash';
Expand All @@ -20,9 +16,11 @@ import {
YearlyBreakdown,
YearlyBreakdownCostName,
} from '@shared/dtos/custom-projects/custom-project-output.dto';
import { OverridableCostInputsDto } from '@api/modules/custom-projects/dto/create-custom-project.dto';
import { PROJECT_DEVELOPMENT_TYPE } from '@shared/dtos/projects/project-development.type';

export type CostPlans = Record<
keyof OverridableCostInputs | string,
keyof OverridableCostInputsDto | string,
CostPlanMap
>;

Expand Down
4 changes: 2 additions & 2 deletions api/src/modules/calculations/data.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
RESTORATION_ACTIVITY_SUBTYPE,
} from '@shared/entities/activity.enum';
import { GetOverridableCostInputs } from '@shared/dtos/custom-projects/get-overridable-cost-inputs.dto';
import { OverridableCostInputs } from '@api/modules/custom-projects/dto/project-cost-inputs.dto';
import { BaseSize } from '@shared/entities/base-size.entity';
import { BaseIncrease } from '@shared/entities/base-increase.entity';
import { AssumptionsRepository } from '@api/modules/calculations/assumptions.repository';
Expand All @@ -19,6 +18,7 @@ import {
RestorationActivityDefaults,
} from '@shared/dtos/custom-projects/activity-types-defaults';
import { Country } from '@shared/entities/country.entity';
import { OverridableCostInputsDto } from '@api/modules/custom-projects/dto/create-custom-project.dto';

/**
* Additional data that is required to perform calculations, which is not overridable by the user. Better naming and clustering of concepts would be great
Expand Down Expand Up @@ -189,7 +189,7 @@ export class DataRepository extends Repository<BaseDataView> {

async getOverridableCostInputs(
dto: GetOverridableCostInputs,
): Promise<OverridableCostInputs> {
): Promise<OverridableCostInputsDto> {
const { countryCode, activity, ecosystem } = dto;
const queryBuilder = this.createQueryBuilder().where({
countryCode,
Expand Down
6 changes: 3 additions & 3 deletions api/src/modules/calculations/sequestration-rate.calculator.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Injectable } from '@nestjs/common';
import { ProjectInput } from '@api/modules/calculations/cost.calculator';
import { ACTIVITY } from '@shared/entities/activity.enum';
import { OverridableAssumptions } from '@api/modules/custom-projects/dto/project-assumptions.dto';
import { NonOverridableModelAssumptions } from '@api/modules/calculations/assumptions.repository';
import { AdditionalBaseData } from '@api/modules/calculations/data.repository';
import { CostPlanMap } from '@shared/dtos/custom-projects/custom-project-output.dto';
import { OverridableAssumptionsDto } from '@api/modules/custom-projects/dto/create-custom-project.dto';

@Injectable()
export class SequestrationRateCalculator {
projectInput: ProjectInput;
activity: ACTIVITY;
defaultProjectLength: number;
projectLength: number;
buffer: OverridableAssumptions['buffer'];
buffer: OverridableAssumptionsDto['buffer'];
plantingSuccessRate: NonOverridableModelAssumptions['plantingSuccessRate'];
tier1SequestrationRate: AdditionalBaseData['tier1SequestrationRate'];
restorationRate: OverridableAssumptions['restorationRate'];
restorationRate: OverridableAssumptionsDto['restorationRate'];
soilOrganicCarbonReleaseLength: NonOverridableModelAssumptions['soilOrganicCarbonReleaseLength'];
constructor(projectInput: ProjectInput) {
this.projectInput = projectInput;
Expand Down
2 changes: 0 additions & 2 deletions api/src/modules/custom-projects/cost-inputs.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ViewColumn } from 'typeorm';

export interface CostInputsDeprecated {
feasibilityAnalysis: number;
conservationPlanningAndAdmin: number;
Expand Down
14 changes: 4 additions & 10 deletions api/src/modules/custom-projects/custom-projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { customProjectContract } from '@shared/contracts/custom-projects.contract';
import { CustomProjectsService } from '@api/modules/custom-projects/custom-projects.service';
import { CreateCustomProjectDto } from '@api/modules/custom-projects/dto/create-custom-project-dto';
import { GetUser } from '@api/decorators/get-user.decorator';
import { User } from '@shared/entities/users/user.entity';
import { AuthGuard } from '@nestjs/passport';
Expand Down Expand Up @@ -73,17 +72,12 @@ export class CustomProjectsController {
);
}

@TsRestHandler(customProjectContract.createCustomProject, {
validateRequestBody: false,
})
async create(
@Body(new ValidationPipe({ enableDebugMessages: true, transform: true }))
dto: CreateCustomProjectDto,
): Promise<ControllerResponse> {
@TsRestHandler(customProjectContract.createCustomProject)
async create(): Promise<ControllerResponse> {
return tsRestHandler(
customProjectContract.createCustomProject,
async () => {
const customProject = await this.customProjects.create(dto as any);
async ({ body }) => {
const customProject = await this.customProjects.create(body);
return {
status: 201,
body: { data: customProject },
Expand Down
10 changes: 6 additions & 4 deletions api/src/modules/custom-projects/custom-projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import {
ServiceUnavailableException,
} from '@nestjs/common';
import { AppBaseService } from '@api/utils/app-base.service';
import { CreateCustomProjectDto } from '@api/modules/custom-projects/dto/create-custom-project-dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { CustomProject } from '@shared/entities/custom-project.entity';
import { CalculationEngine } from '@api/modules/calculations/calculation.engine';
import { CustomProjectFactory } from '@api/modules/custom-projects/input-factory/custom-project.factory';
import { GetOverridableCostInputs } from '@shared/dtos/custom-projects/get-overridable-cost-inputs.dto';
import { DataRepository } from '@api/modules/calculations/data.repository';
import { OverridableCostInputs } from '@api/modules/custom-projects/dto/project-cost-inputs.dto';
import { GetOverridableAssumptionsDTO } from '@shared/dtos/custom-projects/get-overridable-assumptions.dto';
import { AssumptionsRepository } from '@api/modules/calculations/assumptions.repository';
import { User } from '@shared/entities/users/user.entity';
Expand All @@ -23,6 +21,10 @@ import { GetActivityTypesDefaults } from '@shared/dtos/custom-projects/get-activ
import { z } from 'zod';
import { customProjecsQuerySchema } from '@shared/contracts/custom-projects.contract';
import { In } from 'typeorm';
import {
CreateCustomProjectDto,
OverridableCostInputsDto,
} from '@api/modules/custom-projects/dto/create-custom-project.dto';

export type CustomProjectFetchSpecificacion = z.infer<
typeof customProjecsQuerySchema
Expand All @@ -48,7 +50,7 @@ export class CustomProjectsService extends AppBaseService<
super(repo, 'customProject', 'customProjects');
}

async create(dto: CreateCustomProjectDto): Promise<any> {
async create(dto: CreateCustomProjectDto): Promise<CustomProject> {
const { countryCode, ecosystem, activity } = dto;
const {
additionalBaseData,
Expand Down Expand Up @@ -114,7 +116,7 @@ export class CustomProjectsService extends AppBaseService<

async getDefaultCostInputs(
dto: GetOverridableCostInputs,
): Promise<OverridableCostInputs> {
): Promise<OverridableCostInputsDto> {
return this.dataRepository.getOverridableCostInputs(dto);
}

Expand Down

This file was deleted.

75 changes: 0 additions & 75 deletions api/src/modules/custom-projects/dto/create-custom-project-dto.ts

This file was deleted.

14 changes: 14 additions & 0 deletions api/src/modules/custom-projects/dto/create-custom-project.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { z } from 'zod';
import {
AssumptionsSchema,
ConservationCustomProjectSchema,
CreateCustomProjectSchema,
InputCostsSchema,
} from '@shared/schemas/custom-projects/create-custom-project.schema';

export type CreateCustomProjectDto = z.infer<typeof CreateCustomProjectSchema>;
export type ConservationCustomProjectDto = z.infer<
typeof ConservationCustomProjectSchema
>;
export type OverridableCostInputsDto = z.infer<typeof InputCostsSchema>;
export type OverridableAssumptionsDto = z.infer<typeof AssumptionsSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
IsString,
IsOptional,
} from 'class-validator';
import { CreateCustomProjectDto } from './create-custom-project-dto';
import { CustomProjectOutput } from '@shared/dtos/custom-projects/custom-project-output.dto';
import { CreateCustomProjectDto } from '@api/modules/custom-projects/dto/create-custom-project.dto';

export class CustomPrpjectAnnualProjectCashFlowDto {
@IsArray()
Expand Down
Loading
Loading