diff --git a/src/chat/chat.gateway.ts b/src/chat/chat.gateway.ts index 3a48847..5e0b606 100644 --- a/src/chat/chat.gateway.ts +++ b/src/chat/chat.gateway.ts @@ -158,12 +158,19 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { } const pointMap = { - 1: 50, + 1: 0, 2: 100, 3: 150, 4: 200, }; + const tempMap = { + 1: -2, + 2: 0, + 3: 1, + 4: 2, + }; + const pointToAdd = pointMap[payload.point]; if (pointToAdd === undefined) { @@ -173,11 +180,24 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { return; } + const tempToAdd = tempMap[payload.point]; + + if (tempToAdd === undefined) { + client.emit('error', { + message: '유효하지 않은 포인트 값입니다.', + }); + return; + } + if (chat.writer) { await this.usersService.updatePoint( chat.writer.id.toString(), pointToAdd, ); + await this.usersService.updateTemp( + chat.writer.id.toString(), + tempToAdd, + ); } chat.isEnded = true; diff --git a/src/goods/goods.service.ts b/src/goods/goods.service.ts index 0fd7668..70be568 100644 --- a/src/goods/goods.service.ts +++ b/src/goods/goods.service.ts @@ -13,14 +13,14 @@ import { User } from '../users/user.entity'; import * as path from 'node:path'; import { AwsService } from '../aws/aws.service'; import { AwsGuard } from '../aws/aws.guard'; +import { UsersService } from '../users/users.service'; @Injectable() export class GoodsService { constructor( @InjectRepository(Goods) private goodsRepository: Repository, - @InjectRepository(User) - private userRepository: Repository, + private readonly userService: UsersService, private readonly awsGuard: AwsGuard, ) {} @@ -50,20 +50,18 @@ export class GoodsService { if (!goods) { throw new NotFoundException('상품을 찾을 수 없습니다.'); } - const user = await this.userRepository.findOne({ - where: { id: Number(userId) }, - }); + const user = await this.userService.findOne(userId, false, true); if (!user) { throw new NotFoundException('사용자를 찾을 수 없습니다.'); } if (user.point < goods.price) { throw new UnauthorizedException('포인트가 부족합니다.'); } - - user.point -= goods.price; goods.purchaseCount += 1; - await this.userRepository.save(user); + await this.userService.updatePoint(userId, -goods.price); + + // await return this.goodsRepository.save(goods); } catch (error) { console.log(error); diff --git a/src/users/user.entity.ts b/src/users/user.entity.ts index aaed095..9874b9e 100644 --- a/src/users/user.entity.ts +++ b/src/users/user.entity.ts @@ -1,4 +1,5 @@ -import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm'; +import { Column, Entity, OneToMany, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm'; +import { Favorite } from '../favorite/favorite.entity'; @Entity() export class User { @@ -31,4 +32,7 @@ export class User { @Column({ default: '' }) public purchaseQrUrl: string; + + @OneToMany(() => Favorite, (favorite) => favorite.user) + favorites: Favorite[]; } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 1649401..266246c 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -2,7 +2,7 @@ import { Body, Controller, Delete, Get, - Patch, + Patch, Post, Put, Query, Req, UploadedFile, @@ -17,6 +17,7 @@ import { LostStuffService } from '../lostStuff/lostStuff.service'; import { FileInterceptor } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; import { v4 as uuidv4 } from 'uuid'; +import { Point } from 'typeorm'; @ApiTags('User') @Controller('users') @@ -146,4 +147,18 @@ export class UsersController { myLostStuff: lostStuff.filter((item) => item.createUser.id === user.id), }; } + + @UseGuards(AuthGuard) + @Post('/point') + async setPoint(@Body() { point }, @Req() request) { + console.log(point); + return this.usersService.updatePoint(request.user.id, point); + } + + @UseGuards(AuthGuard) + @Post('/tep') + async setTemp(@Body() { point }, @Req() request) { + console.log(point); + return this.usersService.updateTemp(request.user.id, point); + } } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index e1a9d15..870902a 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -101,10 +101,34 @@ export class UsersService { ); } - const sumPoint = point + user.point; + let sumPoint = point + user.point; - await this.usersRepository.update(id, { point: sumPoint }); + if (sumPoint < 0) { + sumPoint = 0; + } + + user.point = sumPoint; + await this.usersRepository.save(user); + + return this.findOne(id, false, true); + } + + async updateTemp(id: string, temp: number): Promise { + const user = await this.findOne(id, false, true); + if (!user) { + throw new HttpException( + '해당 유저가 존재하지 않습니다.', + HttpStatus.BAD_REQUEST, + ); + } + + let sumTemp = temp + user.temperature; + + if (sumTemp < 0) { + sumTemp = 0; + } + user.temperature = sumTemp; await this.usersRepository.save(user); return this.findOne(id, false, true);