Skip to content

chore: nest js best practices - #2800

Closed
mishraomp wants to merge 2 commits into
mainfrom
chore/nestjs-best-practices
Closed

chore: nest js best practices#2800
mishraomp wants to merge 2 commits into
mainfrom
chore/nestjs-best-practices

Conversation

@mishraomp

@mishraomp mishraomp commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

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:

Copilot AI review requested due to automatic review settings July 31, 2026 16:48
@mishraomp
mishraomp requested review from DerekRoberts and removed request for Copilot July 31, 2026 16:48
Copilot AI review requested due to automatic review settings July 31, 2026 16:52
@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedclass-transformer@​0.5.11001009982100
Addedclass-validator@​0.14.410010010084100

View full report

@socket-security

Copy link
Copy Markdown

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.

Action Severity Alert  (click "▶" to expand/collapse)
Warn High
Obfuscated code: npm validator is 90.0% likely obfuscated

Confidence: 0.90

Location: Package overview

From: backend/package-lock.jsonnpm/class-validator@0.14.4npm/validator@13.15.35

ℹ Read more on: This package | This alert | What is obfuscated code?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support@socket.dev.

Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/validator@13.15.35. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

View full report

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UsersService search 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.

Comment on lines 173 to 176
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')
})
Comment thread backend/src/app.ts
Comment on lines +162 to +182
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,
})
})
}
Comment on lines +79 to 82
} catch (error) {
this.throwIfUserNotFound(error, id)
throw error
}
@mishraomp mishraomp closed this Jul 31, 2026
@github-project-automation github-project-automation Bot moved this from New to Done in DevOps (NR) Jul 31, 2026
@mishraomp
mishraomp deleted the chore/nestjs-best-practices branch July 31, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants