Skip to content

Commit d54934a

Browse files
authored
Merge pull request #26 from oodd-team/OD-35
유저 약관 동의 기능
2 parents 3896bf7 + 5ec8b64 commit d54934a

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

src/common/entities/user.entity.ts

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export class User extends BaseEntity {
5050
@Column('datetime')
5151
joinedAt!: Date; // joinedAt는 datetime 타입
5252

53+
@ApiProperty({
54+
description: '이용약관 동의 시각',
55+
example: '2021-08-01 00:00:00',
56+
})
57+
@Column('datetime')
58+
privacyTermAcceptedAt!: Date;
59+
5360
//one to many 관계 설정
5461

5562
@OneToMany(() => Post, (post) => post.user)

src/user/user.controller.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Get,
55
Param,
66
Patch,
7+
Post,
78
Req,
89
UseGuards,
910
} from '@nestjs/common';
@@ -67,9 +68,21 @@ export class UserController {
6768
});
6869
}
6970

70-
@Patch()
71+
@Post(':userId')
72+
@UseGuards(AuthGuard)
7173
@PatchUserTermsSwagger('이용약관 동의 API')
72-
patchUserTerms() {
73-
// return this.userService.getHello();
74+
async patchUserTerms(
75+
@Req() req: Request,
76+
@Param('userId') userId: number,
77+
): Promise<BaseResponse<any>> {
78+
if (!(await this.userService.getUserById(userId)))
79+
throw DataNotFoundException('유저가 존재하지 않습니다.');
80+
if (req.user.id !== Number(userId)) {
81+
throw UnauthorizedException('권한이 없습니다.');
82+
}
83+
84+
const updatedUser = await this.userService.patchUserTerms(userId);
85+
86+
return new BaseResponse(true, 'Success', updatedUser.privacyTermAcceptedAt);
7487
}
7588
}

src/user/user.service.ts

+14
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,18 @@ export class UserService {
7979
throw InternalServerException(error.message);
8080
}
8181
}
82+
83+
async patchUserTerms(id: number): Promise<User> {
84+
const user = await this.userRepository.findOne({
85+
where: { id: id, status: 'activated' },
86+
});
87+
88+
try {
89+
user.privacyTermAcceptedAt = new Date();
90+
await this.userRepository.save(user);
91+
return user;
92+
} catch (error) {
93+
throw InternalServerException(error.message);
94+
}
95+
}
8296
}

src/user/user.swagger.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ApiBadRequestResponse,
3+
ApiCreatedResponse,
34
ApiInternalServerErrorResponse,
45
ApiNotFoundResponse,
56
ApiOperation,
@@ -45,5 +46,14 @@ export function PatchUserSwagger(apiSummary: string) {
4546

4647
// 이용약관 동의 API Swagger
4748
export function PatchUserTermsSwagger(apiSummary: string) {
48-
return ApiOperation({ summary: apiSummary });
49+
return BaseSwaggerDecorator(
50+
{ summary: apiSummary },
51+
[],
52+
[
53+
ApiCreatedResponse({ description: '이용약관 동의 성공' }),
54+
ApiBadRequestResponse({ description: 'Bad Request' }),
55+
ApiNotFoundResponse({ description: '해당 유저가 존재하지 않습니다.' }),
56+
ApiInternalServerErrorResponse({ description: 'Internal Server Error' }),
57+
],
58+
);
4959
}

0 commit comments

Comments
 (0)