-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/chats business entity #110
base: development
Are you sure you want to change the base?
Changes from 1 commit
b21e56b
3579716
ee7c761
41bd68d
82edbca
185dbf6
49657ca
d0cdafa
3b2ab25
0183b3b
14087f0
73fded0
2e14c7a
235905f
5beb219
38bc45d
9f7ef5e
1e9af4c
ef92513
fb7a238
95e6c49
28ad4f4
34815a0
4b59952
55aabe5
c9fe5fd
632637a
9b25b61
9c7a943
b69869b
5d3bf1d
6cd68db
0432685
bb7f530
019c57d
cd4b278
7be02f2
5bfc846
51caeb0
1c247a5
24bf152
33d9d51
c5d77c7
6c6cca3
21b1828
6082b4e
a5a0d9f
4c87ebd
e6847c2
a56d7f8
a371b3e
271fe3f
90f1de2
aea6ff6
bfdb894
fd90134
5f99c13
ce9f525
175ad4b
8d0e9f3
73ed128
eca4093
7b551da
665757f
e04a429
5e531ad
3f88f72
5febb88
2354ac0
c27de25
da60ff8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { Injectable, Scope } from '@nestjs/common'; | ||
import { ChatsRepository } from '../../datalake/chats/chats.repository'; | ||
import { MessagesRepository } from '../../datalake/messages/messages.repository'; | ||
import { Chat } from '../../datalake/chats/schemas/chat.schema'; | ||
import { Message } from '../../datalake/messages/schemas/messages.schema'; | ||
import { Types } from 'mongoose'; | ||
|
||
@Injectable({ scope: Scope.TRANSIENT }) | ||
export class ChatEntity { | ||
private metadata: any[]; | ||
private messages: any[][]; | ||
private metadata: Chat[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь лучше использовать интерфейсы - так мы не привязаны к Мангусту. |
||
private messages: Message[][]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А почему массив массивов? В отдельном чате только один набор сообщений :) |
||
|
||
constructor( | ||
private readonly chatsRepository: ChatsRepository, | ||
|
@@ -14,4 +17,48 @@ export class ChatEntity { | |
this.metadata = []; | ||
this.messages = []; | ||
} | ||
|
||
async createChat(metadata: Partial<Chat>, messages: Partial<Message>[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. При создании чата - может и не быть ни одного ещё сообщения. Если они есть - их надо сохранить в их репу :) |
||
const chat = await this.chatsRepository.create(metadata); | ||
this.metadata.push(chat); | ||
this.messages.push(messages as Message[]); | ||
return this; | ||
} | ||
|
||
async findChatByParams(params: Record<string, any>) { | ||
const chats = await this.chatsRepository.find(params); | ||
this.metadata = chats; | ||
return this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А если там уже есть сообщения? Их тоже надо найти :) |
||
} | ||
|
||
async findConflictingChats(params: Record<string, any>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const chats = await this.chatsRepository.find(params); | ||
this.metadata = chats; | ||
return this; | ||
} | ||
|
||
async addMessage(chatId: string, message: Partial<Message>) { | ||
const newMessage = await this.messagesRepository.create({ ...message, chatId }); | ||
const chatIndex = this.metadata.findIndex((chat) => chat._id.toString() === chatId); | ||
if (chatIndex !== -1) { | ||
if (!this.messages[chatIndex]) { | ||
this.messages[chatIndex] = []; | ||
} | ||
this.messages[chatIndex].push(newMessage); | ||
} | ||
return this; | ||
} | ||
|
||
async closeChat(chatId: string): Promise<ChatEntity> { | ||
const objectId = new Types.ObjectId(chatId); | ||
const chats = await this.chatsRepository.find({ _id: objectId }) as Chat[]; | ||
if (chats.length > 0) { | ||
await this.chatsRepository['model'].updateOne({ _id: objectId }, { isActive: false }).exec(); | ||
const chatIndex = this.metadata.findIndex((c) => c._id.toString() === chatId); | ||
if (chatIndex !== -1) { | ||
this.metadata[chatIndex].isActive = false; | ||
} | ||
} | ||
return this; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А точно не
REQUEST
? Можешь обосновать выбор?(Это не предложение поменять, тут надо подумать)