Skip to content

Commit

Permalink
Add project filtering by name
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea authored and alexeh committed Nov 5, 2024
1 parent 9c453eb commit b25c0b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
16 changes: 15 additions & 1 deletion api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Injectable } from '@nestjs/common';
import { AppBaseService } from '@api/utils/app-base.service';
import { Project } from '@shared/entities/projects.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { FetchSpecification } from 'nestjs-base-service';

@Injectable()
export class ProjectsService extends AppBaseService<
Expand All @@ -17,4 +18,17 @@ export class ProjectsService extends AppBaseService<
) {
super(projectRepository, 'project', 'projects');
}

async extendFindAllQuery(
query: SelectQueryBuilder<Project>,
fetchSpecification: FetchSpecification,
): Promise<SelectQueryBuilder<Project>> {
// Filter by project name
if (fetchSpecification?.filter?.projectName) {
query = query.andWhere('project_name ILIKE :projectName', {
projectName: `%${fetchSpecification.filter.projectName}%`,
});
}
return query;
}
}
25 changes: 25 additions & 0 deletions api/test/integration/projects/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ describe('Projects', () => {
.map((project) => project.projectName),
);
});

test.only('Should return a list of projects filtered by project name', async () => {
const projects: Project[] = [];
projects.push(
await testManager
.mocks()
.createProject({ projectName: 'PROJECT_NAME_ABC' }),
await testManager
.mocks()
.createProject({ projectName: 'PROJECT_NAME_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(['PROJECT_NAME_ABC']);
});
});

describe('Filters for Projects', () => {
Expand Down

0 comments on commit b25c0b5

Please sign in to comment.