Skip to content

Commit

Permalink
fix(api): Return BIGINT for project maximums as numbers are too big a…
Browse files Browse the repository at this point in the history
…nd out of an integer range
  • Loading branch information
alepefe committed Dec 17, 2024
1 parent 1568494 commit f8ae9c2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions api/src/modules/import/services/entity.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,9 @@ 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.opexNPV = row.opex_npv;
project.opex = row.opex;
project.capexNpv = row.capex_npv;
project.capexNPV = row.capex_npv;
project.capex = row.capex;
project.totalCostNPV = row.total_cost_npv;
project.totalCost = row.total_cost;
Expand Down
23 changes: 15 additions & 8 deletions api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,34 @@ 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)::integer', 'maxAbatementPotential')
.select('SUM(abatement_potential)::BIGINT', 'maxAbatementPotential')
.from(Project, 'project');

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

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

const [maximums, { metadata, data }] = await Promise.all([
totalsQuery.getRawOne(),
this.findAllPaginated(query),
]);
const [{ maxAbatementPotential, maxTotalCost }, { metadata, data }] =
await Promise.all([
totalsQuery.getRawOne(),
this.findAllPaginated(query),
]);

// The numbers are too big at the moment.
return {
metadata,
maximums,
maximums: {
maxAbatementPotential: Number(maxAbatementPotential),
maxTotalCost: Number(maxTotalCost),
},
data,
};
}
Expand Down
2 changes: 1 addition & 1 deletion shared/dtos/projects/projects.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export class PaginatedProjectsWithMaximums {
data: Partial<Project>[];
maximums: {
maxAbatementPotential: number;
maxCost: number;
maxTotalCost: number;
};
}
4 changes: 2 additions & 2 deletions shared/entities/projects.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ export class Project extends BaseEntity {
abatementPotential: number;

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

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

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

@Column({ name: "opex", type: "decimal", nullable: true })
opex: number;
Expand Down
4 changes: 2 additions & 2 deletions shared/lib/entity-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export const createProject = async (
totalCost: 100,
costPerTCO2eNPV: 100,
costPerTCO2e: 100,
capexNpv: 100,
capexNPV: 100,
capex: 50,
opexNpv: 100,
opexNPV: 100,
opex: 50,
initialPriceAssumption: "$100",
priceType: PROJECT_PRICE_TYPE.MARKET_PRICE,
Expand Down

0 comments on commit f8ae9c2

Please sign in to comment.