Skip to content
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
2 changes: 1 addition & 1 deletion src/modules/response/controllers/response.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ResponseController {
@ApiOkResponse({ description: 'Responses fetched successfully' })
public async findAll(
@Req() request: Request,
@Body() body: { page?: number; limit?: number; sortBy?: string; sortOrder?: 'ASC' | 'DESC' },
@Body() body: { page?: number; limit?: number; sortBy?: string; sortOrder?: 'ASC' | 'DESC'; contextIds?: string[] },
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Using an inline type for the request body bypasses the ValidationPipe. This means properties like page, limit, and the new contextIds are not validated for their type or format, which could lead to unexpected errors.

It's recommended to create a dedicated Data Transfer Object (DTO) class for the request body. This allows you to leverage class-validator decorators to enforce type and format constraints.

For example, you could create a ListResponsesDto class:

import { IsOptional, IsInt, IsString, IsIn, IsArray, IsUUID, Min } from 'class-validator';
import { Type } from 'class-transformer';

export class ListResponsesDto {
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  page?: number;

  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  limit?: number;

  @IsOptional()
  @IsString()
  sortBy?: string;

  @IsOptional()
  @IsString()
  @IsIn(['ASC', 'DESC'])
  sortOrder?: 'ASC' | 'DESC';

  @IsOptional()
  @IsArray()
  @IsUUID('all', { each: true })
  contextIds?: string[];
}

You can then use this DTO in the controller (@Body() body: ListResponsesDto) and in the service (pagination: ListResponsesDto) for better type safety and validation.

@Res() response: Response,
@GetTenantId() tenantId: string,
@Param('surveyId', ParseUUIDPipe) surveyId: string,
Expand Down
10 changes: 7 additions & 3 deletions src/modules/response/services/response.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
HttpStatus,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { Request, Response } from 'express';
import {
SurveyResponse,
Expand Down Expand Up @@ -160,7 +160,7 @@ export class ResponseService {
request: Request,
tenantId: string,
surveyId: string,
pagination: { page?: number; limit?: number; sortBy?: string; sortOrder?: 'ASC' | 'DESC' },
pagination: { page?: number; limit?: number; sortBy?: string; sortOrder?: 'ASC' | 'DESC'; contextIds?: string[] },
response: Response,
) {
const apiId = APIID.RESPONSE_LIST;
Expand All @@ -172,7 +172,11 @@ export class ResponseService {
const skip = (page - 1) * limit;

const [responses, total] = await this.responseRepo.findAndCount({
where: { tenantId, surveyId },
where: {
tenantId,
surveyId,
...(pagination.contextIds?.length ? { contextId: In(pagination.contextIds) } : {}),
},
order: { [sortBy]: sortOrder },
skip,
take: limit,
Expand Down