From b25c0b529173ef15cd0cb99655dd93642cf0af38 Mon Sep 17 00:00:00 2001 From: Catalin Oancea Date: Mon, 4 Nov 2024 18:29:03 +0200 Subject: [PATCH] Add project filtering by name --- api/src/modules/projects/projects.service.ts | 16 +++++++++++- .../integration/projects/projects.spec.ts | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/api/src/modules/projects/projects.service.ts b/api/src/modules/projects/projects.service.ts index 06b09955..aa214f5e 100644 --- a/api/src/modules/projects/projects.service.ts +++ b/api/src/modules/projects/projects.service.ts @@ -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< @@ -17,4 +18,17 @@ export class ProjectsService extends AppBaseService< ) { super(projectRepository, 'project', 'projects'); } + + async extendFindAllQuery( + query: SelectQueryBuilder, + fetchSpecification: FetchSpecification, + ): Promise> { + // Filter by project name + if (fetchSpecification?.filter?.projectName) { + query = query.andWhere('project_name ILIKE :projectName', { + projectName: `%${fetchSpecification.filter.projectName}%`, + }); + } + return query; + } } diff --git a/api/test/integration/projects/projects.spec.ts b/api/test/integration/projects/projects.spec.ts index bae15e1f..27fdcb65 100644 --- a/api/test/integration/projects/projects.spec.ts +++ b/api/test/integration/projects/projects.spec.ts @@ -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', () => {