Skip to content

Commit

Permalink
Merge pull request #269 from boostcampwm2023/BE-fixQuery-#268
Browse files Browse the repository at this point in the history
[BE/#268] findRoomList 쿼리 수정
  • Loading branch information
koomin1227 authored Dec 3, 2023
2 parents c72911e + 531dbfd commit 8935d53
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export class ChatService {
constructor(
@InjectRepository(ChatRoomEntity)
private chatRoomRepository: Repository<ChatRoomEntity>,
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
@InjectRepository(ChatEntity)
private chatRepository: Repository<ChatEntity>,
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
private fcmHandler: FcmHandler,

) {}

async saveMessage(message: ChatDto) {
Expand Down Expand Up @@ -48,38 +47,39 @@ export class ChatService {
'chat_room.id',
'chat_room.post_id',
'chat.message',
'chat.create_date',
])
.where('chat_room.user = :userId', {
userId: userId,
})
.orWhere('chat_room.writer = :userId', {
userId: userId,
})
.select([
'chat_room.user',
'chat_room.writer',
'chat_room.id',
'chat_room.post_id',
'chat.message',
])
.innerJoin('chat', 'chat', 'chat_room.id = chat.chat_room')
.orderBy('chat.id', 'DESC')
.limit(1)
.addSelect(['user.w.user_hash', 'user.w.profile_img'])
.addSelect(['user.w.user_hash', 'user.w.profile_img', 'user.w.nickname'])
.leftJoin('user', 'user.w', 'user.w.user_hash = chat_room.writer')
.addSelect(['user.u.user_hash', 'user.u.profile_img'])
.addSelect(['user.u.user_hash', 'user.u.profile_img', 'user.u.nickname'])
.leftJoin('user', 'user.u', 'user.u.user_hash = chat_room.user')
.addSelect(['post.thumbnail', 'post.title'])
.leftJoin('post', 'post', 'post.id = chat_room.post_id')
.getRawMany();

return rooms.reduce((acc, cur) => {
acc.push({
room_id: cur.chat_room_id,
post_id: cur.chat_room_post_id,
post_title: cur.post_title,
post_thumbnail: cur.post_thumbnail,
user: cur['user.w_user_hash'],
user_profile_img: cur['user.w_profile_img'],
user_nickname: cur['user.w_nickname'],
writer: cur['user.u_user_hash'],
writer_profile_img: cur['user.u_profile_img'],
writer_nickname: cur['user.u_nickname'],
last_chat: cur.chat_message,
last_chat_date: cur.chat_create_date,
});
return acc;
}, []);
Expand Down

0 comments on commit 8935d53

Please sign in to comment.