Skip to content

Commit

Permalink
fix(api): Change Project Maximum total abatement cost and total cost …
Browse files Browse the repository at this point in the history
…in order to align it with the latst busines slogic
  • Loading branch information
alepefe committed Jan 3, 2025
1 parent c1ef711 commit be7c4cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
7 changes: 3 additions & 4 deletions api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ export class ProjectsService extends AppBaseService<
public async findAllProjectsWithMaximums(
query: ProjectFetchSpecificacion,
): Promise<PaginatedProjectsWithMaximums> {
// Elena told us that the maximum values of the abatement_potential and max_total_cost bars is the sum of all values of the filtered results
const qb = this.dataSource
.createQueryBuilder()
.select('SUM(abatement_potential)::BIGINT', 'maxAbatementPotential')
.select('MAX(abatement_potential)', 'maxAbatementPotential')
.from(Project, 'project');

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

const totalsQuery = this.applySearchFiltersToQueryBuilder(qb, query);
Expand Down
19 changes: 12 additions & 7 deletions api/test/integration/projects/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { HttpStatus } from '@nestjs/common';
import { projectsContract } from '@shared/contracts/projects.contract';
import { Country } from '@shared/entities/country.entity';
import { ProjectScorecard } from '@shared/entities/project-scorecard.entity';
import { Project, PROJECT_SIZE_FILTER } from '@shared/entities/projects.entity';
import {
COST_TYPE_SELECTOR,
Project,
PROJECT_SIZE_FILTER,
} from '@shared/entities/projects.entity';

describe('Projects', () => {
let testManager: TestManager;
Expand Down Expand Up @@ -419,15 +423,15 @@ describe('Projects', () => {
await testManager.mocks().createProject({
id: 'e934e9fe-a79c-40a5-8254-8817851764ad',
projectName: 'PROJ_ABC',
totalCost: 100,
totalCostNPV: 50,
capexNPV: 27,
opexNPV: 23,
abatementPotential: 10,
});
await testManager.mocks().createProject({
id: 'e934e9fe-a79c-40a5-8254-8817851764ae',
projectName: 'PROJ_DEF',
totalCost: 200,
totalCostNPV: 100,
capexNPV: 12,
opexNPV: 36,
abatementPotential: 20,
});

Expand All @@ -437,13 +441,14 @@ describe('Projects', () => {
.query({
withMaximums: true,
partialProjectName: 'PROJ',
costRangeSelector: COST_TYPE_SELECTOR.NPV,
});

expect(response.status).toBe(HttpStatus.OK);
expect(response.body.data).toHaveLength(2);
expect(response.body.maximums).toEqual({
maxAbatementPotential: 30,
maxTotalCost: 200,
maxAbatementPotential: 20,
maxTotalCost: 50,
});
});

Expand Down

0 comments on commit be7c4cf

Please sign in to comment.