-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use scorecard view instead of repo (#129)
- Loading branch information
1 parent
3ed8832
commit 7e47e50
Showing
9 changed files
with
286 additions
and
208 deletions.
There are no files selected for viewing
143 changes: 0 additions & 143 deletions
143
api/src/modules/projects/projects-scorecard.repository.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.