Skip to content

Commit 0e58666

Browse files
authored
Merge pull request #16 from oodd-team/OD-36
유저 신고 기능 수정
2 parents 8e54838 + 67c1db3 commit 0e58666

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/post-report/post-report.controller.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { Body, Controller, Post, Req } from '@nestjs/common';
1+
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
22
import { PostReportService } from './post-report.service';
33
import { CreatePostReportSwagger } from './post-report.swagger';
44
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
55
import { PostReportDto } from './dtos/post-report.dto';
66
import { BaseResponse } from 'src/common/response/dto';
7+
import { AuthGuard } from 'src/auth/guards/jwt.auth.guard';
8+
import { Request } from 'express';
79

810
@ApiBearerAuth('Authorization')
911
@Controller('post-report')
12+
@UseGuards(AuthGuard)
1013
@ApiTags('[서비스] 게시글 신고')
1114
export class PostReportController {
1215
constructor(private readonly postReportService: PostReportService) {}
1316
@Post()
1417
@CreatePostReportSwagger('게시글 신고하기 API')
1518
async createPostReport(
1619
@Body() postReportDto: PostReportDto,
17-
@Req() req: any
20+
@Req() req: Request
1821
): Promise<BaseResponse<string>> {
19-
const requesterId = 1; // 추후 수정
22+
const requesterId = req.user['id'];
2023

2124
await this.postReportService.reportPost({
2225
...postReportDto,

src/post-report/post-report.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
33
import { Repository } from 'typeorm';
44
import { PostReport } from '../common/entities/post-report.entity';
55
import { PostReportDto } from './dtos/post-report.dto';
6-
import { DataNotFoundException } from 'src/common/exception/service.exception';
6+
import { DataNotFoundException, InvalidInputValueException } from 'src/common/exception/service.exception';
77
import { PostService } from '../post/post.service';
88

99
@Injectable()
@@ -29,7 +29,7 @@ export class PostReportService {
2929
});
3030

3131
if (existingReport) {
32-
throw new Error('이미 해당 게시글에 대해 신고하였습니다.');
32+
throw InvalidInputValueException('이미 해당 게시글을 신고하였습니다.');
3333
}
3434

3535
const postReport = this.postReportRepository.create({
+20-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { Body, Controller, Patch, Post, UseGuards } from '@nestjs/common';
1+
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
22
import { UserReportService } from './user-report.service';
33
import { PostUserReportSwagger } from './user-report.swagger';
44
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
55
import { CreateUserReportDto } from './dto/user-report.dto';
66
import { BaseResponse } from 'src/common/response/dto';
7+
import { AuthGuard } from 'src/auth/guards/jwt.auth.guard';
8+
import { Request } from 'express';
9+
import { UnauthorizedException } from 'src/common/exception/service.exception';
710

811
@ApiBearerAuth('Authorization')
912
@Controller('user-report')
13+
@UseGuards(AuthGuard)
1014
@ApiTags('[서비스] 유저 신고')
1115
export class UserReportController {
1216
constructor(private readonly userReportService: UserReportService) {}
@@ -15,12 +19,25 @@ export class UserReportController {
1519
@PostUserReportSwagger('유저 신고하기 API')
1620
async postUserReport(
1721
@Body() createUserReportDto: CreateUserReportDto,
22+
@Req() req: Request
1823
): Promise<BaseResponse<null>> {
19-
const fromUserId = 1; // 나중에 인증 로직 완성되면 유저 인증하자
24+
const fromUserId = req.user['id'];
25+
26+
// jwt 유저와 신고할 유저가 다른 경우
27+
if (fromUserId != createUserReportDto.fromUserId) {
28+
throw UnauthorizedException('신고 권한이 없습니다.');
29+
}
30+
2031
createUserReportDto.fromUserId = fromUserId;
2132

2233
await this.userReportService.createReport(createUserReportDto);
2334

2435
return new BaseResponse<null>(true, 'USER_REPORTED_SUCCESS', null);
2536
}
26-
}
37+
}
38+
39+
/*
40+
TODO
41+
- userReport 중복 생성
42+
- jwt 인증 받은 사람이 아니더라도 신고가 됨 -> 인가
43+
*/

src/user-report/user-report.service.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
33
import { Repository } from 'typeorm';
44
import { UserReport } from '../common/entities/user-report.entity';
55
import { CreateUserReportDto } from '../user-report/dto/user-report.dto';
6-
import { User } from '../common/entities/user.entity';
7-
import { DataNotFoundException } from 'src/common/exception/service.exception';
6+
import { DataNotFoundException, InvalidInputValueException } from 'src/common/exception/service.exception';
87
import { UserService } from 'src/user/user.service';
98

109
@Injectable()
@@ -29,10 +28,25 @@ export class UserReportService {
2928
throw DataNotFoundException('유저를 찾을 수 없습니다.');
3029
}
3130

31+
// 중복 신고 확인
32+
const existingReport = await this.userReportRepository
33+
.createQueryBuilder('report')
34+
.where('report.fromUser = :fromUserId', { fromUserId })
35+
.andWhere('report.toUser = :toUserId', { toUserId })
36+
.andWhere('report.reason = :reason', { reason })
37+
.getOne();
38+
39+
if (existingReport) {
40+
throw InvalidInputValueException('이미 해당 유저를 신고하였습니다.');
41+
}
42+
3243
const userReport = this.userReportRepository.create({
3344
fromUser,
3445
toUser,
3546
reason,
47+
createdAt: new Date(),
48+
updatedAt: new Date(),
49+
status: 'activated',
3650
});
3751

3852
await this.userReportRepository.save(userReport);

0 commit comments

Comments
 (0)