-
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 13 commits
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Injectable, Scope } from '@nestjs/common'; | ||
import { ChatsRepository } from '../../datalake/chats/chats.repository'; | ||
import { MessagesRepository } from '../../datalake/messages/messages.repository'; | ||
import { ChatInterface } from '../../common/types/chat.types'; | ||
import { MessageInterface } from '../../common/types/chats.types'; | ||
import { Types } from 'mongoose'; | ||
|
||
@Injectable({ scope: Scope.TRANSIENT }) | ||
export class ChatEntity { | ||
private metadata: ChatInterface[]; | ||
private messages: { [chatId: string]: MessageInterface[] }; | ||
|
||
constructor( | ||
private readonly chatsRepository: ChatsRepository, | ||
private readonly messagesRepository: MessagesRepository | ||
) { | ||
this.metadata = []; | ||
this.messages = {}; | ||
} | ||
|
||
async createChat( | ||
metadata: Partial<ChatInterface>, | ||
messages: Partial<MessageInterface>[] | ||
): Promise<ChatEntity> { | ||
const chat = await this.chatsRepository.create(metadata as any) as ChatInterface; | ||
this.metadata.push(chat); | ||
if (messages.length > 0) { | ||
const savedMessages = await Promise.all( | ||
messages.map((message) => | ||
this.messagesRepository.create({ ...message, chatId: chat._id } as any) | ||
) | ||
); | ||
this.messages[chat._id.toString()] = savedMessages as MessageInterface[]; | ||
} else { | ||
this.messages[chat._id.toString()] = []; | ||
} | ||
return this; | ||
} | ||
|
||
async findChatByParams(params: Partial<ChatInterface>): Promise<ChatEntity> { | ||
const chats = await this.chatsRepository.find(params) as ChatInterface[]; | ||
this.metadata = chats; | ||
this.messages = {}; // Сброс сообщений при новом поиске | ||
for (const chat of chats) { | ||
const chatMessages = await this.messagesRepository.find({ chatId: chat._id }) as MessageInterface[]; | ||
this.messages[chat._id.toString()] = chatMessages; | ||
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. В одном экземпляре - данные только одного чата |
||
} | ||
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: Partial<ChatInterface>): Promise<ChatEntity> { | ||
// Поиск чатов по типу | ||
const primaryChats = await this.findChatByParams(params); | ||
const conflictingChats: ChatInterface[] = []; | ||
this.metadata = primaryChats.metadata; | ||
this.messages = primaryChats.messages; | ||
// Поиск конфликтных чатов, которые указывают друг на друга | ||
for (const primaryChat of this.metadata) { | ||
const conflictChats = await this.chatsRepository.find({ | ||
taskId: primaryChat.taskId, | ||
ownerId: { $ne: primaryChat.ownerId }, | ||
}) as ChatInterface[]; | ||
if (conflictChats.length > 0) { | ||
conflictingChats.push(...conflictChats); | ||
} | ||
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. Привет! Кажется тут потеряны сообщения админа 🙈 Вот этот интерфейс:
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.
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.
По В любом случае сейчас для конфликта метод 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.
Это должно делаться по
Но они будут в разных элементах кортежа
С идентификаторами там чуть-чуть поменяется, вспомни начало прошлого QA. |
||
} | ||
// Объединение найденных чатов и сообщений | ||
for (const conflictChat of conflictingChats) { | ||
const conflictMessages = await this.messagesRepository.find({ chatId: conflictChat._id }) as MessageInterface[]; | ||
this.metadata.push(conflictChat); | ||
this.messages[conflictChat._id.toString()] = conflictMessages; | ||
} | ||
return this; | ||
} | ||
|
||
async addMessage(chatId: string, message: Partial<MessageInterface>): Promise<ChatEntity> { | ||
const newMessage = await this.messagesRepository.create({ | ||
...message, | ||
chatId: new Types.ObjectId(chatId), | ||
} as any); | ||
if (!this.messages[chatId]) { | ||
this.messages[chatId] = []; | ||
} | ||
this.messages[chatId].push(newMessage as MessageInterface); | ||
return this; | ||
} | ||
|
||
async closeChat(chatId: string): Promise<ChatEntity> { | ||
const objectId = new Types.ObjectId(chatId); | ||
const chats = await this.chatsRepository.find({ _id: objectId }) as ChatInterface[]; | ||
if (chats.length > 0) { | ||
await this.chatsRepository['model'].updateOne({ _id: objectId }, { isOpen: false }).exec(); | ||
const chatIndex = this.metadata.findIndex((c) => c._id.toString() === chatId); | ||
if (chatIndex !== -1) { | ||
this.metadata[chatIndex].isOpen = 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
? Можешь обосновать выбор?(Это не предложение поменять, тут надо подумать)