Skip to content

Commit

Permalink
feat(api): Enable partialProjectName search for custom-projects
Browse files Browse the repository at this point in the history
  • Loading branch information
alepefe committed Dec 16, 2024
1 parent 3dfed3f commit fcd33aa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
15 changes: 13 additions & 2 deletions api/src/modules/custom-projects/custom-projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import { EventBus } from '@nestjs/cqrs';
import { SaveCustomProjectEvent } from '@api/modules/custom-projects/events/save-custom-project.event';
import { FetchSpecification } from 'nestjs-base-service';
import { GetActivityTypesDefaults } from '@shared/dtos/custom-projects/get-activity-types-defaults.dto';
import { Country } from '@shared/entities/country.entity';
import { z } from 'zod';
import { customProjecsQuerySchema } from '@shared/contracts/custom-projects.contract';

export type CustomProjectFetchSpecificacion = z.infer<
typeof customProjecsQuerySchema
>;

@Injectable()
export class CustomProjectsService extends AppBaseService<
Expand Down Expand Up @@ -121,7 +126,7 @@ export class CustomProjectsService extends AppBaseService<

async extendFindAllQuery(
query: SelectQueryBuilder<CustomProject>,
fetchSpecification: FetchSpecification,
fetchSpecification: CustomProjectFetchSpecificacion,
info?: { user: User },
): Promise<SelectQueryBuilder<CustomProject>> {
const { user } = info;
Expand All @@ -130,6 +135,12 @@ export class CustomProjectsService extends AppBaseService<
.leftJoinAndSelect('customProject.user', 'user')
.andWhere('user.id = :userId', { userId: user.id });

if (fetchSpecification.partialProjectName) {
query = query.andWhere('project_name ILIKE :projectName', {
projectName: `%${fetchSpecification.partialProjectName}%`,
});
}

return query;
}
}
33 changes: 33 additions & 0 deletions api/test/integration/custom-projects/custom-projects-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,38 @@ describe('Read Custom projects', () => {
responseData.find((cp: CustomProject) => cp.id === customProject2.id),
).toBeDefined();
});

test('An authenticated user should be able to retrieve its custom projects filtering by partialProjectName', async () => {
// Given
const { createCustomProject } = testManager.mocks();
const [customProject1, customProject2] = await Promise.all([
createCustomProject({
id: '2d8fdf38-3295-4970-b194-af503a2a6031',
projectName: 'Should not be found',
user: { id: user.id } as User,
}),
createCustomProject({
id: '2d8fdf38-3295-4970-b194-af503a2a6039',
projectName: 'Seagrass',
user: { id: user.id } as User,
}),
]);

// When
const response = await testManager
.request()
.get(customProjectContract.getCustomProjects.path)
.set('Authorization', `Bearer ${jwtToken}`)
.query({ partialProjectName: 'Sea' })
.send();

// Then
expect(response.status).toBe(200);
const responseData = response.body.data;
expect(responseData.length).toBe(1);
expect(
responseData.find((cp: CustomProject) => cp.id === customProject2.id),
).toBeDefined();
});
});
});
9 changes: 7 additions & 2 deletions shared/contracts/custom-projects.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ import { generateEntityQuerySchema } from "@shared/schemas/query-param.schema";
import { ActivityTypesDefaults } from "@shared/dtos/custom-projects/activity-types-defaults";
import { GetActivityTypesDefaultsSchema } from "@shared/schemas/custom-projects/activity-types-defaults.schema";

export const customProjecsQuerySchema =
generateEntityQuerySchema(CustomProject);
export const customProjecsQuerySchema = generateEntityQuerySchema(
CustomProject,
).merge(
z.object({
partialProjectName: z.string().optional(),
}),
);

const contract = initContract();
export const customProjectContract = contract.router({
Expand Down

0 comments on commit fcd33aa

Please sign in to comment.