Skip to content

Commit

Permalink
Bug/#346 - 좋아요 순 스크롤 시 다른 방 데이터 조회 문제 해결 (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjfcnfw3 authored Dec 3, 2024
2 parents d98bf6c + 3af586c commit 981f190
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ export class ChatService {

async scrollChat(chatScrollQuery: ChatScrollQuery, userId?: number) {
this.validatePageSize(chatScrollQuery);
await this.validateLastedChatId(chatScrollQuery);
const result = await this.findChatScroll(chatScrollQuery, userId);
return await this.toScrollResponse(result, chatScrollQuery.pageSize);
}

async scrollChatByLike(chatScrollQuery: ChatScrollQuery, userId?: number) {
this.validatePageSize(chatScrollQuery);
await this.validateLastedChatId(chatScrollQuery);
const result = await this.findChatScrollOrderByLike(
chatScrollQuery,
userId,
Expand All @@ -64,6 +66,18 @@ export class ChatService {
return queryBuilder.getMany();
}

private async validateLastedChatId(chatScrollQuery: ChatScrollQuery) {
const { latestChatId, stockId } = chatScrollQuery;
if (!latestChatId) return;
const lastChat = await this.dataSource.manager.findOne(Chat, {
where: { id: latestChatId },
relations: ['stock'],
});
if (!lastChat || stockId !== lastChat.stock.id) {
throw new BadRequestException('lasted chat not in this room');
}
}

private hasStock(userId: number, stockId: string, manager: EntityManager) {
return manager.exists(UserStock, {
where: { user: { id: userId }, stock: { id: stockId } },
Expand Down Expand Up @@ -152,8 +166,8 @@ export class ChatService {
});
if (chat) {
queryBuilder.andWhere(
'chat.likeCount < :likeCount or' +
' (chat.likeCount = :likeCount and chat.id < :latestChatId)',
'(chat.likeCount < :likeCount or' +
' (chat.likeCount = :likeCount and chat.id < :latestChatId))',
{
likeCount: chat.likeCount,
latestChatId,
Expand Down

0 comments on commit 981f190

Please sign in to comment.