Skip to content

Commit

Permalink
Merge pull request #78 from Together42/rotation-dto
Browse files Browse the repository at this point in the history
change: change partial entities to specific dtos
  • Loading branch information
seo-wo authored Jan 26, 2024
2 parents e81b30c + dc5c856 commit 50986fd
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 23 deletions.
68 changes: 68 additions & 0 deletions src/rotation/dto/find-all-rotation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsInt, IsDate, IsOptional } from 'class-validator';

export class FindAllRotationDto {
@ApiProperty({
example: 1,
description: '사용자 ID',
})
@IsInt()
userId: number;

@ApiProperty({
example: 1,
description: '정보를 업데이트한 유저의 ID',
})
@IsInt()
updateUserId: number;

@ApiProperty({
example: 2021,
description: '사서 일정의 연도',
})
@IsInt()
year: number;

@ApiProperty({
example: 1,
description: '사서 일정의 월',
})
@IsInt()
month: number;

@ApiProperty({
example: 1,
description: '사서 일정의 일',
})
@IsInt()
day: number;

@ApiProperty({
example: 1,
description: '사서 일정의 생성 시간',
})
@IsDate()
createdAt: Date;

@ApiProperty({
example: 1,
description: '사서 일정의 업데이트 시간',
})
@IsOptional()
@IsDate()
updatedAt: Date;

@ApiProperty({
example: 1,
description: '사서 일정의 삭제 시간',
})
@IsOptional()
@IsDate()
deletedAt: Date;

@ApiProperty({
example: 1,
description: '해당 사서 일정에 참여하는 사서의 닉네임',
})
intraId: string;
}
35 changes: 35 additions & 0 deletions src/rotation/dto/find-registration.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber } from 'class-validator';

export class FindRegistrationDto {
@ApiProperty({
example: 2024,
description: '유저 본인의 다음 달 로테이션 날짜의 연도',
})
@IsNotEmpty()
@IsNumber()
year: number;

@ApiProperty({
example: 1,
description: '유저 본인의 다음 달 로테이션 날짜의 달',
})
@IsNotEmpty()
@IsNumber()
month: number;

@ApiProperty({
example: 1,
description: '유저 본인이 선택한 다음 달 로테이션 불가능 일자',
})
@IsNotEmpty()
attendLimit: JSON;

@ApiProperty({
example: 1,
description: '유저 본인의 인트라 ID(닉네임)',
})
@IsNotEmpty()
intraId: string;

}
17 changes: 17 additions & 0 deletions src/rotation/dto/find-today-rotation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator';

export class FindTodayRotationDto {
@ApiProperty({
example: 1,
description: '오늘 사서 업무를 맡은 사서의 닉네임입니다.',
})
@IsNotEmpty()
nickname: string;

@ApiProperty({
example: 2024,
description: '오늘 사서 업무를 맡은 사서의 슬랙 ID입니다.',
})
slackMemberId: string;
}
16 changes: 10 additions & 6 deletions src/rotation/rotations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import {
InternalServerExceptionBody,
NotFoundExceptionBody,
} from 'src/common/dto/error-response.dto';
import { FindTodayRotationDto } from './dto/find-today-rotation.dto';
import { FindRegistrationDto } from './dto/find-registration.dto';
import { FindAllRotationDto } from './dto/find-all-rotation.dto';
import { MonthValidationPipe } from './pipe/month-validation.pipe';

@Controller('rotations')
Expand All @@ -60,10 +63,10 @@ export class RotationsController {
description: '당일 사서 조회를 위한 API. 구글 시트에서 사용 예정. 누구나 사용할 수 있습니다.',
})
@ApiOkResponse({
type: [RotationEntity],
type: [FindTodayRotationDto],
})
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
findTodayRotation(): Promise<Partial<RotationEntity>[]> {
findTodayRotation(): Promise<FindTodayRotationDto[]> {
return this.rotationsService.findTodayRotation();
}

Expand All @@ -85,7 +88,7 @@ export class RotationsController {
})
@ApiUnauthorizedResponse({ type: UnauthorizedException })
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
async findOwnRegistration(@GetUser() user: UserEntity): Promise<Partial<RotationAttendeeEntity>> {
async findOwnRegistration(@GetUser() user: UserEntity): Promise<FindRegistrationDto> {
return await this.rotationsService.findRegistration(user.id);
}

Expand Down Expand Up @@ -155,9 +158,10 @@ export class RotationsController {
@ApiBadRequestResponse({ type: BadRequestExceptionBody })
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
findAllRotation(
@Query('year') year: number,
@Query('month', new MonthValidationPipe()) month: number,
): Promise<Partial<RotationEntity>[]> {
@Query(ValidationPipe)
findRotationQueryDto: FindRotationQueryDto,
): Promise<FindAllRotationDto[]> {
const { month, year } = findRotationQueryDto
return this.rotationsService.findAllRotation(year, month);
}

Expand Down
44 changes: 27 additions & 17 deletions src/rotation/rotations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { RotationAttendeeRepository } from './repository/rotation-attendees.repo
import { DayObject, RotationAttendeeInfo } from './utils/types';
import { HolidayService } from 'src/holiday/holiday.service';
import { createRotation } from './utils/rotation';
import { FindTodayRotationDto } from './dto/find-today-rotation.dto';
import { FindRegistrationDto } from './dto/find-registration.dto';
import { FindAllRotationDto } from './dto/find-all-rotation.dto';

function getRotationCronTime() {
if (process.env.NODE_ENV === 'production') {
Expand Down Expand Up @@ -184,7 +187,7 @@ export class RotationsService {
* 구글 API에서 당일 사서를 가져오는데 사용되는 서비스
* 당일 사서이기 때문에, 만약 데이터가 두 개 이상 나온다면 오류 로그를 찍는다.
*/
async findTodayRotation(): Promise<Partial<RotationEntity>[]> {
async findTodayRotation(): Promise<FindTodayRotationDto[]> {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
Expand All @@ -207,7 +210,10 @@ export class RotationsService {
},
});

return records.map((record) => record.user);
return records.map((record) => ({
nickname: record.user.nickname,
slackMemberId: record.user.slackMemberId,
}));
} catch (error: any) {
this.logger.error(error);
throw error;
Expand All @@ -224,7 +230,7 @@ export class RotationsService {
* [20231219 수정] - 만약 records가 빈 객체인 경우,
* attendLimit이 빈 배열인 객체를 반환한다.
*/
async findRegistration(userId: number): Promise<Partial<RotationAttendeeEntity>> {
async findRegistration(userId: number): Promise<FindRegistrationDto> {
const { year, month } = getNextYearAndMonth();

try {
Expand All @@ -242,18 +248,22 @@ export class RotationsService {
}

const intraIdRecord = await this.userService.findOneById(userId);
const modifiedRecord = {};
let modifiedRecord: FindRegistrationDto;

if (records.length == 0) {
modifiedRecord['year'] = year;
modifiedRecord['month'] = month;
modifiedRecord['attendLimit'] = [];
modifiedRecord['intraId'] = intraIdRecord.nickname;
modifiedRecord = {
year: year,
month: month,
attendLimit: JSON.parse(JSON.stringify([])),
intraId: intraIdRecord.nickname,
};
} else {
modifiedRecord['year'] = records[0].year;
modifiedRecord['month'] = records[0].month;
modifiedRecord['attendLimit'] = records[0].attendLimit;
modifiedRecord['intraId'] = intraIdRecord.nickname;
modifiedRecord = {
year: records[0].year,
month: records[0].month,
attendLimit: records[0].attendLimit,
intraId: intraIdRecord.nickname,
};
}

return modifiedRecord;
Expand Down Expand Up @@ -382,23 +392,23 @@ export class RotationsService {
* 기본적으로는 모든 로테이션을 반환.
* 만약 parameter로 month와 year가 들어오면, 해당 스코프에 맞는 레코드를 반환.
*/
async findAllRotation(year?: number, month?: number): Promise<Partial<RotationEntity>[]> {
async findAllRotation(year?: number, month?: number): Promise<FindAllRotationDto[]> {
try {
let records: Promise<Partial<RotationEntity>[]>;
let records: RotationEntity[];

if (year && month) {
records = this.rotationRepository.find({
records = await this.rotationRepository.find({
where: {
year: year,
month: month,
},
});
} else {
records = this.rotationRepository.find();
records = await this.rotationRepository.find();
}

const modifiedRecords = await Promise.all(
(await records).map(async (record) => {
(records).map(async (record) => {
const userRecord = await this.userService.findOneById(record.userId);
return { ...record, intraId: userRecord.nickname };
}),
Expand Down

0 comments on commit 50986fd

Please sign in to comment.