Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dateOnly, username 요청값으로 받아오지 않도록 수정 #13

Merged
merged 2 commits into from
Aug 25, 2023
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/api/users/dto/create-appointment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transform } from 'class-transformer';
import { IsBoolean, IsOptional, IsString } from 'class-validator';
import { IsString } from 'class-validator';

export class CreateAppointmentDto {
@IsString()
Expand Down
7 changes: 0 additions & 7 deletions src/api/users/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { IsBoolean, IsOptional, IsString } from 'class-validator';

Check warning on line 1 in src/api/users/dto/update-user.dto.ts

View workflow job for this annotation

GitHub Actions / CI

'IsBoolean' is defined but never used

Check warning on line 1 in src/api/users/dto/update-user.dto.ts

View workflow job for this annotation

GitHub Actions / CI

'IsOptional' is defined but never used

export class UpdateUserDto {
@IsString()
username: string;

@IsOptional()
@IsBoolean()
dateOnly: boolean;

@IsString({ each: true })
dates: string[];
}
8 changes: 4 additions & 4 deletions src/api/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import {
Patch,
UseGuards,
Param,
Get,
Query,
Request,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

import { UsersService } from './users.service';
import { CreateAppointmentDto } from './dto/create-appointment.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { GetUserNameDto } from './dto/get-user-name.dto';

@Controller('users')
export class UsersController {
Expand All @@ -27,9 +25,11 @@ export class UsersController {
@UseGuards(AuthGuard('jwt'))
@Patch(':roomCode')
update(
@Request() req,
@Param('roomCode') roomCode: string,
@Body() updateUserDto: UpdateUserDto
) {
return this.usersService.update(roomCode, updateUserDto);
const user = req.user;
return this.usersService.update(user, roomCode, updateUserDto);
}
}
34 changes: 15 additions & 19 deletions src/api/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
NotFoundException,
} from '@nestjs/common';
import { hash } from 'bcrypt';
import { Room, User } from '@prisma/client';

Check warning on line 7 in src/api/users/users.service.ts

View workflow job for this annotation

GitHub Actions / CI

'Room' is defined but never used

import { PrismaService } from '@/prisma/prisma.service';

Expand Down Expand Up @@ -49,22 +49,13 @@
badRequestErrors.push('dates must be sorted');
}

let room: Room;
try {
room = await this.roomService.findOne(roomCode);
} catch (e) {
if (e instanceof NotFoundException) {
notFoundErrors.push('Room does not exist');
}
}

const dateOnly = room.dateOnly;
const room = await this.roomService.findOne(roomCode);

/*
dates는 ['2023-07-19 12:30','2023-07-20 13:45']이런 식으로 된 배열이며
공백을 기준으로 날짜와 시간으로 나눕니다.
*/
if (!(await this.isDateOnlyFormatValid(dateOnly, dates))) {
if (!(await this.isDateOnlyFormatValid(room.dateOnly, dates))) {
badRequestErrors.push('Time must be provided when dateOnly is false');
}

Expand Down Expand Up @@ -95,24 +86,29 @@
return token;
}

async update(roomCode: string, updateUserDto: UpdateUserDto) {
async update(
userInfoFromJwt: User,
roomCode: string,
updateUserDto: UpdateUserDto
) {
const badRequestErrors = [];
const notFoundErrors = [];
const { username, dates, dateOnly } = updateUserDto;
const userInfo = await this.prismaService.user.findFirst({
where: { username, roomId: roomCode },
const { dates } = updateUserDto;
const userInfoFromDB = await this.prismaService.user.findFirst({
where: { id: userInfoFromJwt.id, roomId: roomCode },
});

if (!userInfo) {
if (!userInfoFromDB) {
badRequestErrors.push('User does not exist');
}

if (!(await this.roomService.findOne(roomCode))) {
notFoundErrors.push('Room does not exist');
}

const room = await this.roomService.findOne(roomCode);

// 해당 유저가 수정한 가용시간에 대한 유효성을 검사한다.
if (!(await this.isDateOnlyFormatValid(dateOnly, dates))) {
if (!(await this.isDateOnlyFormatValid(room.dateOnly, dates))) {
badRequestErrors.push('Time must be provided when dateOnly is false');
}

Expand All @@ -126,7 +122,7 @@

return this.prismaService.user.update({
where: {
id: userInfo.id,
id: userInfoFromDB.id,
},
data: { enableTimes: dates },
});
Expand Down