Skip to content

Commit d8a2554

Browse files
authored
Merge pull request #88 from Together42/hotfix-rotation
hotfix: fix month/year validation
2 parents 9fa1b50 + 2981c6c commit d8a2554

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
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/pipe/month-validation.pipe.ts

-12
This file was deleted.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +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';
45+
import { FindRotationQueryDto } from './dto/find-rotation-query.dto';
4646

4747
@Controller('rotations')
4848
@ApiTags('rotations')
@@ -156,9 +156,9 @@ export class RotationsController {
156156
@ApiBadRequestResponse({ type: BadRequestExceptionBody })
157157
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
158158
findAllRotation(
159-
@Query('year') year: number,
160-
@Query('month', new MonthValidationPipe()) month: number,
159+
@Query() findRotationQueryDto: FindRotationQueryDto,
161160
): Promise<FindAllRotationDto[]> {
161+
const { year, month } = findRotationQueryDto;
162162
return this.rotationsService.findAllRotation(year, month);
163163
}
164164

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

+15-17
Original file line numberDiff line numberDiff line change
@@ -352,25 +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-
where: {
360-
year: year,
361-
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,
362364
},
363-
});
364-
} else {
365-
records = this.rotationRepository.find();
366-
}
365+
},
366+
});
367367

368-
const modifiedRecords = await Promise.all(
369-
(await records).map(async (record) => {
370-
const userRecord = await this.userService.findOneById(record.userId);
371-
return { ...record, intraId: userRecord.nickname };
372-
}),
373-
);
368+
const modifiedRecords = records.map((records) => {
369+
const { user, ...other } = records;
370+
return { ...other, intraId: user.nickname };
371+
});
374372

375373
return modifiedRecords;
376374
}

0 commit comments

Comments
 (0)