Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add project filtering by name #72

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('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
Loading