Skip to content

Commit e5c843d

Browse files
committed
hotfix: fix additional cases and modify service logic
1 parent 04f20f5 commit e5c843d

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

Diff for: src/rotation/pipe/month-validation.pipe.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
33
@Injectable()
44
export class MonthValidationPipe implements PipeTransform {
55
transform(value: any) {
6-
const month = parseInt(value, 10);
7-
if (isNaN(month) || month === null) {
6+
if (value === undefined || value === null) {
87
return 0;
9-
} else if (month < 1 || month > 12) {
8+
} else if (value.match(/^[0-9]+$/) === null) {
9+
throw new BadRequestException(`Invalid month: ${value}`);
10+
} else {
11+
/* valid number format */
12+
}
13+
14+
const month = parseInt(value, 10);
15+
16+
if (month < 1 || month > 12) {
1017
throw new BadRequestException(`Invalid month: ${value}`);
1118
} else {
1219
return value;

Diff for: src/rotation/pipe/year-validation.pipe.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
33
@Injectable()
44
export class YearValidationPipe implements PipeTransform {
55
transform(value: any) {
6-
const year = parseInt(value, 10);
7-
if (isNaN(year) || year === null) {
6+
if (value === undefined || value === null) {
87
return 0;
9-
} else if (year < 2020 || year > 2100) {
8+
} else if (value.match(/^[0-9]+$/) === null) {
9+
throw new BadRequestException(`Invalid year: ${value}`);
10+
} else {
11+
/* valid number format */
12+
}
13+
14+
const year = parseInt(value, 10);
15+
16+
if (year < 2020 || year > 2100) {
1017
throw new BadRequestException(`Invalid year: ${value}`);
1118
} else {
1219
return value;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ export class RotationsController {
157157
@ApiBadRequestResponse({ type: BadRequestExceptionBody })
158158
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
159159
findAllRotation(
160-
@Query('year', new YearValidationPipe()) year: number,
161-
@Query('month', new MonthValidationPipe()) month: number,
160+
@Query('year', new YearValidationPipe()) year: any,
161+
@Query('month', new MonthValidationPipe()) month: any,
162162
): Promise<FindAllRotationDto[]> {
163163
return this.rotationsService.findAllRotation(year, month);
164164
}

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,24 @@ export class RotationsService {
354354
async findAllRotation(year?: number, month?: number): Promise<FindAllRotationDto[]> {
355355
let records: Promise<RotationEntity[]>;
356356

357-
if (year && month) {
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+
358369
records = this.rotationRepository.find({
359370
where: {
360371
year: year,
361372
month: month,
362373
},
363374
});
364-
} else {
365-
records = this.rotationRepository.find();
366375
}
367376

368377
const modifiedRecords = await Promise.all(

0 commit comments

Comments
 (0)