Skip to content

Commit

Permalink
Use scorecard view instead of repo (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea authored Dec 5, 2024
1 parent 3ed8832 commit 7e47e50
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 208 deletions.
143 changes: 0 additions & 143 deletions api/src/modules/projects/projects-scorecard.repository.ts

This file was deleted.

75 changes: 75 additions & 0 deletions api/src/modules/projects/projects-scorecard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Injectable } from '@nestjs/common';
import { AppBaseService } from '@api/utils/app-base.service';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { z } from 'zod';
import { getProjectsQuerySchema } from '@shared/contracts/projects.contract';
import { ProjectScorecardView } from '@shared/entities/project-scorecard.view';
import {
OtherProjectFilters,
ProjectFilters,
} from '@shared/dtos/projects/projects-map.dto';

export type ProjectFetchSpecificacion = z.infer<typeof getProjectsQuerySchema>;

@Injectable()
export class ProjectsScorecardService extends AppBaseService<
ProjectScorecardView,
unknown,
unknown,
unknown
> {
constructor(
@InjectRepository(ProjectScorecardView)
private readonly projectScorecardRepo: Repository<ProjectScorecardView>,
) {
super(projectScorecardRepo, 'project_scorecard', 'project_scorecards');
}

async extendFindAllQuery(
query: SelectQueryBuilder<ProjectScorecardView>,
fetchSpecification: ProjectFetchSpecificacion,
): Promise<SelectQueryBuilder<ProjectScorecardView>> {
// Filter by project name
if (fetchSpecification.partialProjectName) {
query = query.andWhere('project_name ILIKE :projectName', {
projectName: `%${fetchSpecification.partialProjectName}%`,
});
}

// Filter by abatement potential
if (fetchSpecification.abatementPotentialRange) {
query = query.andWhere(
'abatement_potential >= :minAP AND abatement_potential <= :maxAP',
{
minAP: Math.min(...fetchSpecification.abatementPotentialRange),
maxAP: Math.max(...fetchSpecification.abatementPotentialRange),
},
);
}

// Filter by cost (total or NPV)
if (fetchSpecification.costRange && fetchSpecification.costRangeSelector) {
let filteredCostColumn: string;
switch (fetchSpecification.costRangeSelector) {
case 'npv':
filteredCostColumn = 'total_cost_npv';
break;
case 'total':
default:
filteredCostColumn = 'total_cost';
break;
}

query = query.andWhere(
`${filteredCostColumn} >= :minCost AND ${filteredCostColumn} <= :maxCost`,
{
minCost: Math.min(...fetchSpecification.costRange),
maxCost: Math.max(...fetchSpecification.costRange),
},
);
}

return query;
}
}
18 changes: 4 additions & 14 deletions api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProjectsScorecardService } from './projects-scorecard.service';
import { Controller, HttpStatus } from '@nestjs/common';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
Expand All @@ -6,7 +7,6 @@ import { ProjectsService } from '@api/modules/projects/projects.service';
import { CountriesService } from '@api/modules/countries/countries.service';
import { CountryWithNoGeometry } from '@shared/entities/country.entity';
import { ProjectsMapRepository } from '@api/modules/projects/projects-map.repository';
import { ProjectsScorecardRepository } from '@api/modules/projects/projects-scorecard.repository';
import {
OtherProjectFilters,
ProjectFilters,
Expand All @@ -17,8 +17,8 @@ export class ProjectsController {
constructor(
private readonly projectsService: ProjectsService,
private readonly countryService: CountriesService,
private readonly projectsScorecardService: ProjectsScorecardService,
private readonly projectMapRepository: ProjectsMapRepository,
private readonly projectsScorecardRepository: ProjectsScorecardRepository,
) {}

@TsRestHandler(projectsContract.getProjects)
Expand All @@ -34,19 +34,9 @@ export class ProjectsController {
return tsRestHandler(
projectsContract.getProjectsScorecard,
async ({ query }) => {
const { filter } = query;
const otherFilters: OtherProjectFilters = {
costRange: query.costRange,
abatementPotentialRange: query.abatementPotentialRange,
costRangeSelector: query.costRangeSelector,
};

const data =
await this.projectsScorecardRepository.getProjectsScorecard(
filter as unknown as ProjectFilters,
otherFilters,
);
return { body: data, status: HttpStatus.OK } as any;
await this.projectsScorecardService.findAllPaginated(query);
return { body: data, status: HttpStatus.OK };
},
);
}
Expand Down
15 changes: 7 additions & 8 deletions api/src/modules/projects/projects.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { ProjectsController } from './projects.controller';
import { ProjectsService } from './projects.service';
import { CountriesModule } from '@api/modules/countries/countries.module';
import { ProjectsMapRepository } from '@api/modules/projects/projects-map.repository';
import { ProjectsScorecardRepository } from '@api/modules/projects/projects-scorecard.repository';

import { ProjectScorecardView } from '@shared/entities/project-scorecard.view';
import { ProjectsScorecardService } from './projects-scorecard.service';
@Module({
imports: [TypeOrmModule.forFeature([Project]), CountriesModule],
controllers: [ProjectsController],
providers: [
ProjectsService,
ProjectsMapRepository,
ProjectsScorecardRepository,
imports: [
TypeOrmModule.forFeature([Project, ProjectScorecardView]),
CountriesModule,
],
controllers: [ProjectsController],
providers: [ProjectsService, ProjectsMapRepository, ProjectsScorecardService],
})
export class ProjectsModule {}
Loading

0 comments on commit 7e47e50

Please sign in to comment.