diff --git a/packages/backend/src/chat/chat.gateway.ts b/packages/backend/src/chat/chat.gateway.ts index 65474edb..0fd6a1f0 100644 --- a/packages/backend/src/chat/chat.gateway.ts +++ b/packages/backend/src/chat/chat.gateway.ts @@ -35,7 +35,7 @@ interface chatResponse { createdAt: Date; } -@WebSocketGateway({ namespace: 'chat' }) +@WebSocketGateway({ namespace: '/api/chat/realtime' }) @UseFilters(WebSocketExceptionFilter) export class ChatGateway implements OnGatewayConnection { @WebSocketServer() diff --git a/packages/backend/src/chat/chat.service.ts b/packages/backend/src/chat/chat.service.ts index c9431a8d..fbb5ac45 100644 --- a/packages/backend/src/chat/chat.service.ts +++ b/packages/backend/src/chat/chat.service.ts @@ -1,8 +1,10 @@ import { BadRequestException, Injectable } from '@nestjs/common'; -import { DataSource, SelectQueryBuilder } from 'typeorm'; +import { WsException } from '@nestjs/websockets'; +import { DataSource, EntityManager, SelectQueryBuilder } from 'typeorm'; import { Chat } from '@/chat/domain/chat.entity'; import { ChatScrollQuery } from '@/chat/dto/chat.request'; import { ChatScrollResponse } from '@/chat/dto/chat.response'; +import { UserStock } from '@/stock/domain/userStock.entity'; export interface ChatMessage { message: string; @@ -23,10 +25,15 @@ export class ChatService { constructor(private readonly dataSource: DataSource) {} async saveChat(userId: number, chatMessage: ChatMessage) { - return this.dataSource.manager.save(Chat, { - user: { id: userId }, - stock: { id: chatMessage.stockId }, - message: chatMessage.message, + return this.dataSource.transaction(async (manager) => { + if (!(await this.hasStock(userId, chatMessage.stockId, manager))) { + throw new WsException('not have stock'); + } + return manager.save(Chat, { + user: { id: userId }, + stock: { id: chatMessage.stockId }, + message: chatMessage.message, + }); }); } @@ -57,6 +64,12 @@ export class ChatService { return queryBuilder.getMany(); } + private hasStock(userId: number, stockId: string, manager: EntityManager) { + return manager.exists(UserStock, { + where: { user: { id: userId }, stock: { id: stockId } }, + }); + } + private validatePageSize(chatScrollQuery: ChatScrollQuery) { const { pageSize } = chatScrollQuery; if (pageSize && pageSize > 100) { diff --git a/packages/backend/src/stock/stock.gateway.ts b/packages/backend/src/stock/stock.gateway.ts index cf98907c..1f4ab32d 100644 --- a/packages/backend/src/stock/stock.gateway.ts +++ b/packages/backend/src/stock/stock.gateway.ts @@ -1,14 +1,14 @@ import { + ConnectedSocket, + MessageBody, + SubscribeMessage, WebSocketGateway, WebSocketServer, - SubscribeMessage, - MessageBody, - ConnectedSocket, } from '@nestjs/websockets'; import { Server, Socket } from 'socket.io'; @WebSocketGateway({ - namespace: '/stock/realtime', + namespace: '/api/stock/realtime', }) export class StockGateway { @WebSocketServer()