diff --git a/src/chat/chat.module.ts b/src/chat/chat.module.ts index aed7071..456f25c 100644 --- a/src/chat/chat.module.ts +++ b/src/chat/chat.module.ts @@ -22,7 +22,6 @@ import { NotificationEntity } from 'src/entities'; UserModule, ReportModule, FcmModule, - ReportModule, MongooseModule.forFeature([ { name: Chatting.name, schema: ChattingSchema }, { name: Room.name, schema: RoomSchema }, diff --git a/src/point/point.controller.ts b/src/point/point.controller.ts index 94385d6..7a64f19 100644 --- a/src/point/point.controller.ts +++ b/src/point/point.controller.ts @@ -111,6 +111,50 @@ export class PointController { return res.send(false); } + @UseGuards(AuthGuard('access')) + @ApiCreatedResponse({ + description: '포인트 차감 성공 시', + schema: { + example: { point: 10 }, + properties: { + point: { + type: 'number', + description: '수정된 포인트 값', + }, + }, + }, + }) + @ApiResponse({ + description: '포인트 차감 실패 시', + schema: { + example: false, + type: 'boolean', + }, + }) + @ApiHeader({ + name: 'authorization', + required: true, + example: 'Bearer asdas.asdasd.asd', + }) + @ApiOperation({ + summary: '닉네임 랜덤', + description: '닉네임 랜덤 돌리기 포인트 차감 가능 여부 판단', + }) + @Get('/nickname') + async getRandomNickname(@Req() req: Request, @Res() res: Response) { + const { id } = req.user as JwtPayload; + try { + const updatedPoint = await this.pointService.checkNicknamePoint(id); + if (updatedPoint) { + return res.json({ point: updatedPoint.point }); + } + return res.send(false); + } catch (err) { + console.log(err); + return err; + } + } + @UseGuards(AuthGuard('access')) @ApiHeader({ name: 'authorization', diff --git a/src/point/point.module.ts b/src/point/point.module.ts index 1be6646..fda7bbe 100644 --- a/src/point/point.module.ts +++ b/src/point/point.module.ts @@ -5,12 +5,14 @@ import { PointController } from './point.controller'; import { PointService } from './point.service'; import { ChatModule } from 'src/chat/chat.module'; import { ReportModule } from 'src/report/report.module'; +import { UserModule } from 'src/user/user.module'; @Module({ imports: [ TypeOrmModule.forFeature([UserEntity, PointHistoryEntity]), ChatModule, ReportModule, + UserModule, ], controllers: [PointController], providers: [PointService], diff --git a/src/point/point.service.ts b/src/point/point.service.ts index ae3d849..3f8342f 100644 --- a/src/point/point.service.ts +++ b/src/point/point.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { ChatService } from 'src/chat/chat.service'; +import { UserService } from 'src/user/user.service'; import { PointHistoryDto } from 'src/dtos/point.dto'; import { PointHistoryEntity, UserEntity } from 'src/entities'; import { Repository } from 'typeorm'; @@ -9,6 +10,7 @@ import { Repository } from 'typeorm'; export class PointService { constructor( private readonly chatService: ChatService, + private readonly userService: UserService, @InjectRepository(UserEntity) private readonly userRepository: Repository, @InjectRepository(PointHistoryEntity) @@ -87,6 +89,18 @@ export class PointService { return false; } + async checkNicknamePoint(userId: number) { + const point = await this.updatePoint(userId, -10); + if (point) { + const nickname = await this.userService.pickRandomNickname(); + await this.userService.updateUser(userId, 'userNickname', nickname); + await this.userService.updateUserSocket(userId, 'userNickname', nickname); + const history = `닉네임 랜덤 돌리기를 하고 10p가 사용 되었습니다`; + this.recordPointHistory(userId, -10, history); + } + return point; + } + async giveBlurtingPoint(userId: number) { const updatedPoint = await this.updatePoint(userId, 10); if (updatedPoint) { diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 0a0423c..5cd5470 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -51,7 +51,7 @@ export class UserService { return users; } - async createUser() { + async pickRandomNickname() { const nicknames = Object.values(Nickname).filter((key) => { return isNaN(Number(key)); }); @@ -59,6 +59,11 @@ export class UserService { const index = rand % nicknames.length; rand = Math.floor(Math.random() * 1000); const nickname = nicknames[index].toString() + rand.toString(); + return nickname; + } + + async createUser() { + const nickname = await this.pickRandomNickname(); const user = this.userRepository.create({ userNickname: nickname, point: 0, @@ -157,6 +162,12 @@ export class UserService { ); } + async updateUserSocket(userId: number, field: string, value: string) { + const user = await this.socketUserModel.findOne({ userId: userId }); + user[field] = value; + await user.save(); + } + async findUserByPhone(phone: string) { const user = await this.userRepository.findOne({ where: { phoneNumber: phone, email: Not(IsNull()) },