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

feat: Add capex and opex columns to the project entity and truncate d… #172

Merged
merged 1 commit into from
Dec 17, 2024
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
4 changes: 4 additions & 0 deletions api/src/modules/import/dtos/excel-projects.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export type ExcelProjects = {
project_size_ha: number;
project_size_filter: string;
abatement_potential: number;
capex_npv: number;
capex: number;
opex_npv: number;
opex: number;
total_cost_npv: number;
total_cost: number;
cost_per_tco2e_npv: number;
Expand Down
38 changes: 37 additions & 1 deletion api/src/modules/import/import.repostiory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,42 @@ export class ImportRepository {
baseIncrease: BaseIncrease[];
modelAssumptions: ModelAssumptions[];
}) {
return this.dataSource.transaction(async (manager) => {
return this.dataSource.transaction('READ COMMITTED', async (manager) => {
// DATA WIPE STARTS
await manager.clear(Project);
await manager.clear(ProjectSize);
await manager.clear(FeasibilityAnalysis);
await manager.clear(ConservationPlanningAndAdmin);
await manager.clear(DataCollectionAndFieldCosts);
await manager.clear(CommunityRepresentation);
await manager.clear(BlueCarbonProjectPlanning);
await manager.clear(CarbonRights);
await manager.clear(FinancingCost);
await manager.clear(ValidationCost);
await manager.clear(MonitoringCost);
await manager.clear(Maintenance);
await manager.clear(CommunityBenefitSharingFund);
await manager.clear(BaselineReassessment);
await manager.clear(MRV);
await manager.clear(LongTermProjectOperating);
await manager.clear(CarbonStandardFees);
await manager.clear(CommunityCashFlow);
await manager.clear(ImplementationLaborCost);

// Carbon inputs ingestion
await manager.clear(EcosystemExtent);
await manager.clear(EcosystemLoss);
await manager.clear(RestorableLand);
await manager.clear(SequestrationRate);
await manager.clear(EmissionFactors);

// Other tables ingestion
await manager.clear(BaseSize);
await manager.clear(BaseIncrease);
await manager.clear(ModelAssumptions);
// DATA WIPE ENDS

// CREATION STARTS
await manager.save(importData.projects);

// Cost inputs ingestion
Expand Down Expand Up @@ -102,6 +137,7 @@ export class ImportRepository {
await manager.save(importData.baseSize);
await manager.save(importData.baseIncrease);
await manager.save(importData.modelAssumptions);
// CREATION ENDS
});
}
}
4 changes: 4 additions & 0 deletions api/src/modules/import/services/entity.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,10 @@ export class EntityPreprocessor {
project.projectSize = row.project_size_ha;
project.projectSizeFilter = row.project_size_filter;
project.abatementPotential = row.abatement_potential;
project.opexNpv = row.opex_npv;
project.opex = row.opex;
project.capexNpv = row.capex_npv;
project.capex = row.capex;
project.totalCostNPV = row.total_cost_npv;
project.totalCost = row.total_cost;
// TODO: This has dissapeared from the excel file and it is required for filtering, setting a fake value for now
Expand Down
13 changes: 10 additions & 3 deletions api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { AppBaseService } from '@api/utils/app-base.service';
import { Project } from '@shared/entities/projects.entity';
import { COST_TYPE_SELECTOR, Project } from '@shared/entities/projects.entity';
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { DataSource, Repository, SelectQueryBuilder } from 'typeorm';
import { z } from 'zod';
Expand Down Expand Up @@ -30,9 +30,16 @@ export class ProjectsService extends AppBaseService<
): Promise<PaginatedProjectsWithMaximums> {
const qb = this.dataSource
.createQueryBuilder()
.select('MAX(abatement_potential)::integer', 'maxAbatementPotential')
.addSelect('MAX(total_cost + total_cost_npv)::integer', 'maxTotalCost')
.select('SUM(abatement_potential)::integer', 'maxAbatementPotential')
.from(Project, 'project');

const { costRangeSelector } = query;
if (costRangeSelector == COST_TYPE_SELECTOR.NPV) {
qb.addSelect('SUM(capex_npv + opex_npv)::integer', 'maxTotalCost');
} else {
qb.addSelect('SUM(capex + opex)::integer', 'maxTotalCost');
}

const totalsQuery = this.applySearchFiltersToQueryBuilder(qb, query);

const [maximums, { metadata, data }] = await Promise.all([
Expand Down
12 changes: 6 additions & 6 deletions api/test/integration/projects/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,15 @@ describe('Projects', () => {
await testManager.mocks().createProject({
id: 'e934e9fe-a79c-40a5-8254-8817851764ad',
projectName: 'PROJ_ABC',
totalCost: 100,
totalCostNPV: 50,
opex: 100,
capex: 100,
abatementPotential: 10,
});
await testManager.mocks().createProject({
id: 'e934e9fe-a79c-40a5-8254-8817851764ae',
projectName: 'PROJ_DEF',
totalCost: 200,
totalCostNPV: 100,
opex: 100,
capex: 100,
abatementPotential: 20,
});

Expand All @@ -351,8 +351,8 @@ describe('Projects', () => {
expect(response.status).toBe(HttpStatus.OK);
expect(response.body.data).toHaveLength(2);
expect(response.body.maximums).toEqual({
maxAbatementPotential: 20,
maxTotalCost: 300,
maxAbatementPotential: 30,
maxTotalCost: 400,
});
});

Expand Down
12 changes: 12 additions & 0 deletions shared/entities/projects.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ export class Project extends BaseEntity {
@Column({ name: "abatement_potential", type: "decimal", nullable: true })
abatementPotential: number;

@Column({ name: "capex_npv", type: "decimal", nullable: true })
capexNpv: number;

@Column({ name: "capex", type: "decimal", nullable: true })
capex: number;

@Column({ name: "opex_npv", type: "decimal", nullable: true })
opexNpv: number;

@Column({ name: "opex", type: "decimal", nullable: true })
opex: number;

@Column({ name: "total_cost_npv", type: "decimal", nullable: true })
totalCostNPV: number;

Expand Down
4 changes: 4 additions & 0 deletions shared/lib/entity-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export const createProject = async (
totalCost: 100,
costPerTCO2eNPV: 100,
costPerTCO2e: 100,
capexNpv: 100,
capex: 50,
opexNpv: 100,
opex: 50,
initialPriceAssumption: "$100",
priceType: PROJECT_PRICE_TYPE.MARKET_PRICE,
};
Expand Down
Loading