Skip to content

Commit

Permalink
Add partial search param
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea committed Nov 8, 2024
1 parent fe8c7cb commit 1d27dc6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
9 changes: 4 additions & 5 deletions api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ export class ProjectsService extends AppBaseService<
fetchSpecification: ProjectFetchSpecificacion,
): Promise<SelectQueryBuilder<Project>> {
// Filter by project name
if (fetchSpecification.filter?.projectName) {
query = query.andWhere('project_name ILIKE ANY (:projectNames)', {
projectNames: fetchSpecification.filter.projectName.map(
(term) => `%${term}%`,
),
if (fetchSpecification.partialProjectName) {
query = query.andWhere('project_name ILIKE :projectName', {
projectName: `%${fetchSpecification.partialProjectName}%`,
});
}

Expand Down Expand Up @@ -67,6 +65,7 @@ export class ProjectsService extends AppBaseService<
},
);
}

return query;
}
}
32 changes: 15 additions & 17 deletions api/test/integration/projects/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,21 @@ describe('Projects', () => {
).toEqual(['15', '25']);
});

// test('Should return a list of projects filtered by project name', async () => {
// await testManager.mocks().createProject({ projectName: 'PROJ_ABC' });
// await testManager.mocks().createProject({ projectName: 'PROJ_DEF' });

// const response = await testManager
// .request()
// .get(projectsContract.getProjects.path)
// .query({
// filter: {
// projectName: ['ABC'],
// },
// });
// expect(response.body.data).toHaveLength(1);
// expect(
// response.body.data.map((project: Project) => project.projectName),
// ).toEqual(['PROJ_ABC']);
// });
test('Should return a list of projects filtered by project name', async () => {
await testManager.mocks().createProject({ projectName: 'PROJ_ABC' });
await testManager.mocks().createProject({ projectName: 'PROJ_DEF' });

const response = await testManager
.request()
.get(projectsContract.getProjects.path)
.query({
partialProjectName: 'ABC',
});
expect(response.body.data).toHaveLength(1);
expect(
response.body.data.map((project: Project) => project.projectName),
).toEqual(['PROJ_ABC']);
});
});

describe('Filters for Projects', () => {
Expand Down
1 change: 1 addition & 0 deletions shared/contracts/projects.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const otherFilters = z.object({
costRange: z.coerce.number().array().optional(),
abatementPotentialRange: z.coerce.number().array().optional(),
costRangeSelector: z.enum(["total", "npv"]).optional(),
partialProjectName: z.string().optional(),
});
export const projectsQuerySchema = generateEntityQuerySchema(Project);
export const getProjectsQuerySchema = projectsQuerySchema.merge(otherFilters);
Expand Down

0 comments on commit 1d27dc6

Please sign in to comment.