Skip to content

Commit

Permalink
fix(채팅):채팅 조회시 발송자 id RDB측 id로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jsween5723 committed Dec 16, 2023
1 parent 3562eee commit 0a394c3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { ChatRepository } from './chat.repository';
import { User } from '../user/schema/user.schema';
import { ChatRoom } from '../room/entity/room.schema';
import { Chat } from './schema/chat.schema';
import { Types } from 'mongoose';
import { Model, Types } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';

@Injectable()
export class ChatService {
constructor(
private gateway: ChatGateway,
private repository: ChatRepository,
@InjectModel(Chat.name) protected model: Model<Chat>,
) {}

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

public async searchChat(roomId: string, lastAccessedAt?: Date) {
return await this.repository.find({
room: new Types.ObjectId(roomId),
createdAt: { $gte: lastAccessedAt ? lastAccessedAt : 0 },
});
return this.model
.find(
{
room: new Types.ObjectId(roomId),
createdAt: { $gte: lastAccessedAt ? lastAccessedAt : 0 },
},
{
room: true,
_id: true,
content: true,
createdAt: true,
sender: true,
},
)
.populate('sender');
}
}
3 changes: 2 additions & 1 deletion src/chat/schema/chat.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChatRoom, ChatRoomSchema } from '../../room/entity/room.schema';
import { User, UserSchema } from '../../user/schema/user.schema';
import { now, Types } from 'mongoose';
import { ApiProperty } from '@nestjs/swagger';
import * as inspector from 'inspector';
export class Message {
@ApiProperty()
message: string;
Expand All @@ -16,7 +17,7 @@ export class Chat extends BaseSchema {
required: true,
ref: User.name,
})
sender: Types.ObjectId;
sender: User;
@Prop({ type: Message, required: true })
content: Message;
}
Expand Down
2 changes: 1 addition & 1 deletion src/room/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ChatDto {
if (!chat) return;
this.id = chat._id.toString();
this.room = { id: chat.room.toString() };
this.sender = { id: chat.sender.toString() };
this.sender = { id: chat.sender.id };
this.content = chat.content;
this.createdAt = Math.floor(chat.createdAt.getTime() / 1000);
}
Expand Down
2 changes: 1 addition & 1 deletion src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class RoomService {
const createdChat = await this.chatService.sendChat({
content: { message: chat.message },
room: room._id,
sender: sender._id,
sender: sender,
});
const users = await this.userService.findByRoomId(room._id);
await this.repository.findOneAndUpdate(
Expand Down

0 comments on commit 0a394c3

Please sign in to comment.