chore: nest js best practices - #2800
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
Warning Review the following alerts detected in dependencies. According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR applies a set of NestJS “best practice” updates across the backend: introducing global request validation/transformation, tightening DTO validation, improving Users search query parsing/validation, and refactoring Prisma wiring/cleanup.
Changes:
- Add a global
ValidationPipe(whitelist/transform) and update controller/tests to rely on DTO-based validation and typed params (ParseIntPipe). - Refactor
UsersServicesearch to accept a query DTO, validate sort/filter JSON, and return a typed paginated result with parallelized DB calls. - Introduce a Prisma module pattern and improve Prisma lifecycle cleanup (
pool.end()on shutdown).
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/src/users/users.service.ts | Refactors CRUD/search behavior, adds structured search parsing/validation, and normalizes DTO mapping. |
| backend/src/users/users.service.spec.ts | Updates unit tests for new search/remove behaviors and adds DTO-based query creation helper. |
| backend/src/users/users.controller.ts | Uses DTO-based query binding and ParseIntPipe for :id params. |
| backend/src/users/users.controller.spec.ts | Updates controller tests to use global ValidationPipe and new exception behavior. |
| backend/src/users/dto/user.dto.ts | Adds validation + trimming transforms to user fields. |
| backend/src/users/dto/update-user.dto.ts | Switches update DTO to a swagger PartialType of create DTO. |
| backend/src/users/dto/search-users-query.dto.ts | Adds a dedicated validated query DTO for /users/search. |
| backend/src/prisma.service.ts | Removes custom singleton handling and ensures PG pool is closed on module destroy. |
| backend/src/prisma.service.spec.ts | Updates teardown to call onModuleDestroy() for pool cleanup. |
| backend/src/middleware/req.res.logger.ts | Makes logger field readonly. |
| backend/src/health.controller.ts | Marks injected services as readonly for immutability. |
| backend/src/app.ts | Registers global ValidationPipe and enables implicit conversion. |
| backend/src/app.module.ts | Moves Prisma wiring to PrismaModule and makes ConfigModule global. |
Files not reviewed (1)
- backend/package-lock.json: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| it('given invalid JSON should throw error', async () => { | ||
| const page = 1 | ||
| const limit = 201 | ||
| const sortObject: Prisma.SortOrder = 'asc' | ||
| const sort: any = `[{ "name" "${sortObject}" }]` | ||
| const filter: any = '[{ "name": { "equals": "Peter" } }]' | ||
| try { | ||
| await service.searchUsers(page, limit, sort, filter) | ||
| } catch (e) { | ||
| expect(e).toEqual(new Error('Invalid query parameters')) | ||
| } | ||
| const query = createSearchQuery({ sort: '[{"name" "asc"}]' }) | ||
| await expect(service.searchUsers(query)).rejects.toThrow('Invalid query parameters') | ||
| }) |
| private parseFilters(filter: string): UserSearchFilter[] { | ||
| const parsed: unknown = this.parseJson(filter) | ||
| if (!Array.isArray(parsed)) { | ||
| throw new BadRequestException('Invalid query parameters') | ||
| } | ||
|
|
||
| return parsed.map((item) => { | ||
| if ( | ||
| !this.isRecord(item) || | ||
| typeof item.key !== 'string' || | ||
| typeof item.operation !== 'string' | ||
| ) { | ||
| throw new BadRequestException('Invalid query parameters') | ||
| } | ||
| return this.validateFilter({ | ||
| key: item.key, | ||
| operation: item.operation, | ||
| value: item.value, | ||
| }) | ||
| }) | ||
| } |
| } catch (error) { | ||
| this.throwIfUserNotFound(error, id) | ||
| throw error | ||
| } |
Thanks for the PR!
Deployments, as required, will be available below:
Please create PRs in draft mode. Mark as ready to enable:
After merge, new images are deployed in: