Skip to content

Commit 966ddd1

Browse files
authored
Merge pull request #89 from Together42/hotfix-rotation-dto
proposal: dto를 사용하여 query string 유효성 검사 하기
2 parents e5c843d + fd4d845 commit 966ddd1

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

Diff for: src/rotation/dto/find-rotation-query.dto.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsNumber, IsOptional } from 'class-validator';
2+
import { Transform } from 'class-transformer';
3+
import { IsNumber, IsOptional, Max, Min } from 'class-validator';
34

45
export class FindRotationQueryDto {
6+
@Transform((params) => +params.value)
57
@ApiProperty({
8+
required: false,
69
example: 1,
710
description: '로테이션 정보를 찾는 기준이 되는 월. 기본값은 다음 달 월입니다.',
811
})
912
@IsOptional()
1013
@IsNumber()
14+
@Min(1)
15+
@Max(12)
1116
month?: number;
1217

18+
@Transform((params) => +params.value)
1319
@ApiProperty({
20+
required: false,
1421
example: 2024,
1522
description: '로테이션 정보를 찾는 기준이 되는 연도. 기본값은 다음 달 연도입니다.',
1623
})
1724
@IsOptional()
1825
@IsNumber()
26+
@Min(2020)
27+
@Max(2100)
1928
year?: number;
2029
}

Diff for: src/rotation/rotations.controller.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ import {
4141
} from 'src/common/dto/error-response.dto';
4242
import { FindTodayRotationDto } from './dto/find-today-rotation.dto';
4343
import { FindRegistrationDto } from './dto/find-registration.dto';
44-
import { MonthValidationPipe } from './pipe/month-validation.pipe';
4544
import { FindAllRotationDto } from './dto/find-all-rotation.dto';
46-
import { YearValidationPipe } from './pipe/year-validation.pipe';
45+
import { FindRotationQueryDto } from './dto/find-rotation-query.dto';
4746

4847
@Controller('rotations')
4948
@ApiTags('rotations')
@@ -157,9 +156,9 @@ export class RotationsController {
157156
@ApiBadRequestResponse({ type: BadRequestExceptionBody })
158157
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
159158
findAllRotation(
160-
@Query('year', new YearValidationPipe()) year: any,
161-
@Query('month', new MonthValidationPipe()) month: any,
159+
@Query() findRotationQueryDto: FindRotationQueryDto,
162160
): Promise<FindAllRotationDto[]> {
161+
const { year, month } = findRotationQueryDto;
163162
return this.rotationsService.findAllRotation(year, month);
164163
}
165164

Diff for: src/rotation/rotations.service.ts

+15-26
Original file line numberDiff line numberDiff line change
@@ -352,34 +352,23 @@ export class RotationsService {
352352
* 만약 parameter로 month와 year가 들어오면, 해당 스코프에 맞는 레코드를 반환.
353353
*/
354354
async findAllRotation(year?: number, month?: number): Promise<FindAllRotationDto[]> {
355-
let records: Promise<RotationEntity[]>;
356-
357-
if (!year && !month) {
358-
records = this.rotationRepository.find();
359-
} else {
360-
const currentDate = new Date();
361-
if (!year) {
362-
year = currentDate.getFullYear();
363-
} else if (!month) {
364-
month = currentDate.getMonth() + 1;
365-
} else {
366-
/* both inputs are exists */
367-
}
368-
369-
records = this.rotationRepository.find({
370-
where: {
371-
year: year,
372-
month: month,
355+
const records = await this.rotationRepository.find({
356+
where: {
357+
year,
358+
month,
359+
},
360+
relations: ['user'],
361+
select: {
362+
user: {
363+
nickname: true,
373364
},
374-
});
375-
}
365+
},
366+
});
376367

377-
const modifiedRecords = await Promise.all(
378-
(await records).map(async (record) => {
379-
const userRecord = await this.userService.findOneById(record.userId);
380-
return { ...record, intraId: userRecord.nickname };
381-
}),
382-
);
368+
const modifiedRecords = records.map((records) => {
369+
const { user, ...other } = records;
370+
return { ...other, intraId: user.nickname };
371+
});
383372

384373
return modifiedRecords;
385374
}

0 commit comments

Comments
 (0)