Skip to content

Commit

Permalink
Use scorecard view instead of repo
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea committed Dec 2, 2024
1 parent da5615a commit 4f4b9b8
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 184 deletions.
143 changes: 0 additions & 143 deletions api/src/modules/projects/projects-scorecard.repository.ts

This file was deleted.

11 changes: 4 additions & 7 deletions api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -18,7 +17,6 @@ export class ProjectsController {
private readonly projectsService: ProjectsService,
private readonly countryService: CountriesService,
private readonly projectMapRepository: ProjectsMapRepository,
private readonly projectsScorecardRepository: ProjectsScorecardRepository,
) {}

@TsRestHandler(projectsContract.getProjects)
Expand All @@ -41,11 +39,10 @@ export class ProjectsController {
costRangeSelector: query.costRangeSelector,
};

const data =
await this.projectsScorecardRepository.getProjectsScorecard(
filter as unknown as ProjectFilters,
otherFilters,
);
const data = await this.projectsService.getProjectsScorecard(
filter as unknown as ProjectFilters,
otherFilters,
);
return { body: data, status: HttpStatus.OK } as any;
},
);
Expand Down
14 changes: 6 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,13 @@ 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';
@Module({
imports: [TypeOrmModule.forFeature([Project]), CountriesModule],
controllers: [ProjectsController],
providers: [
ProjectsService,
ProjectsMapRepository,
ProjectsScorecardRepository,
imports: [
TypeOrmModule.forFeature([Project, ProjectScorecardView]),
CountriesModule,
],
controllers: [ProjectsController],
providers: [ProjectsService, ProjectsMapRepository],
})
export class ProjectsModule {}
106 changes: 106 additions & 0 deletions api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ 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>;

Expand All @@ -18,10 +23,29 @@ export class ProjectsService extends AppBaseService<
constructor(
@InjectRepository(Project)
public readonly projectRepository: Repository<Project>,
@InjectRepository(ProjectScorecardView)
private readonly projectScorecardRepo: Repository<ProjectScorecardView>,
) {
super(projectRepository, 'project', 'projects');
}

async getProjectsScorecard(
filters?: ProjectFilters,
otherFilters?: OtherProjectFilters,
): Promise<ProjectScorecardView[]> {
const queryBuilder = this.projectScorecardRepo
.createQueryBuilder()
.select();

const scorecards = this.applyScorecardFilters(
queryBuilder,
filters,
otherFilters,
).getRawMany();

return scorecards;
}

async extendFindAllQuery(
query: SelectQueryBuilder<Project>,
fetchSpecification: ProjectFetchSpecificacion,
Expand Down Expand Up @@ -68,4 +92,86 @@ export class ProjectsService extends AppBaseService<

return query;
}

private applyScorecardFilters(
queryBuilder: SelectQueryBuilder<ProjectScorecardView>,
filters: ProjectFilters = {},
otherFilters: OtherProjectFilters = {},
) {
const {
countryCode,
totalCost,
abatementPotential,
activity,
activitySubtype,
ecosystem,
} = filters;
const { costRange, abatementPotentialRange, costRangeSelector } =
otherFilters;
if (countryCode?.length) {
queryBuilder.andWhere('country_code IN (:...countryCodes)', {
countryCodes: countryCode,
});
}
if (totalCost?.length) {
const maxTotalCost = Math.max(...totalCost);
queryBuilder.andWhere('total_cost <= :maxTotalCost', {
maxTotalCost,
});
}
if (abatementPotential?.length) {
const maxAbatementPotential = Math.max(...abatementPotential);
queryBuilder.andWhere('abatement_potential <= :maxAbatementPotential', {
maxAbatementPotential,
});
}
if (activity) {
queryBuilder.andWhere('activity IN (:...activity)', {
activity,
});
}
if (activitySubtype?.length) {
queryBuilder.andWhere('activity_subtype IN (:...activitySubtype)', {
activitySubtype,
});
}

if (ecosystem) {
queryBuilder.andWhere('ecosystem IN (:...ecosystem)', {
ecosystem,
});
}
if (abatementPotentialRange) {
queryBuilder.andWhere(
'abatement_potential >= :minAP AND abatement_potential <= :maxAP',
{
minAP: Math.min(...abatementPotentialRange),
maxAP: Math.max(...abatementPotentialRange),
},
);
}

if (costRange && costRangeSelector) {
let filteredCostColumn: string;
switch (costRangeSelector) {
case 'npv':
filteredCostColumn = 'total_cost_npv';
break;
case 'total':
default:
filteredCostColumn = 'total_cost';
break;
}

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

return queryBuilder;
}
}
26 changes: 0 additions & 26 deletions shared/dtos/projects/projects-scorecard.dto.ts

This file was deleted.

Loading

0 comments on commit 4f4b9b8

Please sign in to comment.