Skip to content

Commit 2996a7a

Browse files
committed
유저 신고 jwt 추가
1 parent d8d67cb commit 2996a7a

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed
+22-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,27 @@ 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+
//console.log("fromUserId is ~~~~~~~~~~~~~", fromUserId);
26+
//console.log("createUserReportDto.fromUserId is ~~~~~~~~~~~~~", createUserReportDto.fromUserId);
27+
28+
// jwt 유저와 신고할 유저가 다른 경우
29+
if (fromUserId != createUserReportDto.fromUserId) {
30+
throw UnauthorizedException('신고 권한이 없습니다.');
31+
}
32+
2033
createUserReportDto.fromUserId = fromUserId;
2134

2235
await this.userReportService.createReport(createUserReportDto);
2336

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

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

+10-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,6 +28,15 @@ export class UserReportService {
2928
throw DataNotFoundException('유저를 찾을 수 없습니다.');
3029
}
3130

31+
// 중복 신고 확인
32+
const existingReport = await this.userReportRepository.findOne({
33+
where: { fromUser: { id: fromUserId }, toUser: { id: toUserId } },
34+
});
35+
36+
if (existingReport) {
37+
throw InvalidInputValueException('이미 해당 유저를 신고하였습니다.');
38+
}
39+
3240
const userReport = this.userReportRepository.create({
3341
fromUser,
3442
toUser,

0 commit comments

Comments
 (0)