Skip to content

Commit 0a394c3

Browse files
committed
fix(채팅):채팅 조회시 발송자 id RDB측 id로 변경
1 parent 3562eee commit 0a394c3

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/chat/chat.service.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { ChatRepository } from './chat.repository';
44
import { User } from '../user/schema/user.schema';
55
import { ChatRoom } from '../room/entity/room.schema';
66
import { Chat } from './schema/chat.schema';
7-
import { Types } from 'mongoose';
7+
import { Model, Types } from 'mongoose';
8+
import { InjectModel } from '@nestjs/mongoose';
89

910
@Injectable()
1011
export class ChatService {
1112
constructor(
1213
private gateway: ChatGateway,
1314
private repository: ChatRepository,
15+
@InjectModel(Chat.name) protected model: Model<Chat>,
1416
) {}
1517

1618
public async sendChat(chat: Partial<Chat>) {
@@ -22,9 +24,20 @@ export class ChatService {
2224
}
2325

2426
public async searchChat(roomId: string, lastAccessedAt?: Date) {
25-
return await this.repository.find({
26-
room: new Types.ObjectId(roomId),
27-
createdAt: { $gte: lastAccessedAt ? lastAccessedAt : 0 },
28-
});
27+
return this.model
28+
.find(
29+
{
30+
room: new Types.ObjectId(roomId),
31+
createdAt: { $gte: lastAccessedAt ? lastAccessedAt : 0 },
32+
},
33+
{
34+
room: true,
35+
_id: true,
36+
content: true,
37+
createdAt: true,
38+
sender: true,
39+
},
40+
)
41+
.populate('sender');
2942
}
3043
}

src/chat/schema/chat.schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ChatRoom, ChatRoomSchema } from '../../room/entity/room.schema';
44
import { User, UserSchema } from '../../user/schema/user.schema';
55
import { now, Types } from 'mongoose';
66
import { ApiProperty } from '@nestjs/swagger';
7+
import * as inspector from 'inspector';
78
export class Message {
89
@ApiProperty()
910
message: string;
@@ -16,7 +17,7 @@ export class Chat extends BaseSchema {
1617
required: true,
1718
ref: User.name,
1819
})
19-
sender: Types.ObjectId;
20+
sender: User;
2021
@Prop({ type: Message, required: true })
2122
content: Message;
2223
}

src/room/dto/chat.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ChatDto {
1818
if (!chat) return;
1919
this.id = chat._id.toString();
2020
this.room = { id: chat.room.toString() };
21-
this.sender = { id: chat.sender.toString() };
21+
this.sender = { id: chat.sender.id };
2222
this.content = chat.content;
2323
this.createdAt = Math.floor(chat.createdAt.getTime() / 1000);
2424
}

src/room/room.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class RoomService {
107107
const createdChat = await this.chatService.sendChat({
108108
content: { message: chat.message },
109109
room: room._id,
110-
sender: sender._id,
110+
sender: sender,
111111
});
112112
const users = await this.userService.findByRoomId(room._id);
113113
await this.repository.findOneAndUpdate(

0 commit comments

Comments
 (0)