Skip to content

Commit

Permalink
Feature/#115 - 주식을 소유한 사람이 채팅방 전체에 메시지를 보낼 수 있다. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjfcnfw3 authored Nov 20, 2024
2 parents 0b467ab + 433cc93 commit 6f6622a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface chatResponse {
createdAt: Date;
}

@WebSocketGateway({ namespace: 'chat' })
@WebSocketGateway({ namespace: '/api/chat/realtime' })
@UseFilters(WebSocketExceptionFilter)
export class ChatGateway implements OnGatewayConnection {
@WebSocketServer()
Expand Down
23 changes: 18 additions & 5 deletions packages/backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
});
});
}

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/stock/stock.gateway.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 6f6622a

Please sign in to comment.