diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index a0ffa6f..67e4803 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -40,7 +40,16 @@ export class ChatService { chat.chat_room = message.room_id; chat.is_read = is_read; chat.count = message.count; - await this.chatRepository.save(chat); + const lastChat = await this.chatRepository.save(chat); + + await this.chatRoomRepository.update( + { + id: message.room_id, + }, + { + last_chat_id: lastChat.id, + }, + ); } async createRoom( @@ -79,24 +88,12 @@ export class ChatService { async findRoomList(userId: string) { const chatListInfo = { all_read: true, chat_list: [] }; - const subquery = this.chatRepository - .createQueryBuilder('chat') - .select('chat.id', 'id') - .addSelect('chat.chat_room', 'chat_room') - .addSelect('chat.message', 'message') - .addSelect('chat.create_date', 'create_date') - .addSelect('chat.is_read', 'is_read') - .addSelect('chat.sender', 'sender') - .where( - 'chat.id IN (SELECT MAX(chat.id) FROM chat GROUP BY chat.chat_room)', - ); - const rooms = await this.chatRoomRepository .createQueryBuilder('chat_room') .innerJoin( - '(' + subquery.getQuery() + ')', + 'chat_room.lastChat', 'chat_info', - 'chat_room.id = chat_info.chat_room', + 'chat_room.lastChat = chat_info.id', ) .innerJoin( 'chat_room.writerUser', @@ -162,24 +159,12 @@ export class ChatService { } async unreadChat(userId: string) { - const subquery = this.chatRepository - .createQueryBuilder('chat') - .select('chat.id', 'id') - .addSelect('chat.chat_room', 'chat_room') - .addSelect('chat.message', 'message') - .addSelect('chat.create_date', 'create_date') - .addSelect('chat.is_read', 'is_read') - .addSelect('chat.sender', 'sender') - .where( - 'chat.id IN (SELECT MAX(chat.id) FROM chat GROUP BY chat.chat_room)', - ); - const rooms = await this.chatRoomRepository .createQueryBuilder('chat_room') .innerJoin( - '(' + subquery.getQuery() + ')', + 'chat_room.lastChat', 'chat_info', - 'chat_room.id = chat_info.chat_room', + 'chat_room.lastChat = chat_info.id', ) .innerJoin( 'chat_room.writerUser', diff --git a/BE/src/entities/chatRoom.entity.ts b/BE/src/entities/chatRoom.entity.ts index 4a37ffe..f9bc493 100644 --- a/BE/src/entities/chatRoom.entity.ts +++ b/BE/src/entities/chatRoom.entity.ts @@ -8,6 +8,7 @@ import { ManyToOne, OneToMany, PrimaryGeneratedColumn, + OneToOne, } from 'typeorm'; import { PostEntity } from './post.entity'; import { ChatEntity } from './chat.entity'; @@ -42,6 +43,9 @@ export class ChatRoomEntity { @DeleteDateColumn() delete_date: Date; + @Column() + last_chat_id: number; + @Column({ default: false }) writer_left: boolean; @@ -51,6 +55,10 @@ export class ChatRoomEntity { @OneToMany(() => ChatEntity, (chat) => chat.chatRoom) chats: ChatEntity[]; + @OneToOne(() => ChatEntity, (chat) => chat.id) + @JoinColumn({ name: 'last_chat_id' }) + lastChat: ChatEntity; + @ManyToOne(() => PostEntity, (post) => post.id) @JoinColumn({ name: 'post_id' }) post: PostEntity;