Skip to content

Commit

Permalink
Feature :: Fix Purchase Goods + Point & Temp Change
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaho1756 committed Dec 1, 2024
1 parent 742c245 commit d0ef159
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
22 changes: 21 additions & 1 deletion src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions src/goods/goods.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Goods>,
@InjectRepository(User)
private userRepository: Repository<User>,
private readonly userService: UsersService,
private readonly awsGuard: AwsGuard,
) {}

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -31,4 +32,7 @@ export class User {

@Column({ default: '' })
public purchaseQrUrl: string;

@OneToMany(() => Favorite, (favorite) => favorite.user)
favorites: Favorite[];
}
17 changes: 16 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Body,
Controller, Delete,
Get,
Patch,
Patch, Post, Put,
Query,
Req,
UploadedFile,
Expand All @@ -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')
Expand Down Expand Up @@ -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);
}
}
28 changes: 26 additions & 2 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<User | string> {
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);
Expand Down

0 comments on commit d0ef159

Please sign in to comment.