Skip to content

Commit

Permalink
feat: set random nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeahxx19 committed Jan 26, 2024
1 parent ac6a21e commit 86fbe34
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
44 changes: 44 additions & 0 deletions src/point/point.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions src/point/point.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
14 changes: 14 additions & 0 deletions src/point/point.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<UserEntity>,
@InjectRepository(PointHistoryEntity)
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ export class UserService {
return users;
}

async createUser() {
async pickRandomNickname() {
const nicknames = Object.values(Nickname).filter((key) => {
return isNaN(Number(key));
});
let rand = Math.floor(Math.random() * 1000);
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,
Expand Down Expand Up @@ -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()) },
Expand Down

0 comments on commit 86fbe34

Please sign in to comment.