@@ -32,12 +32,7 @@ public MongoRepository(MongoDbContext dc, IServiceProvider services)
3232
3333 public void Add < TTableInterface > ( object entity )
3434 {
35- if ( entity is Conversation conversation )
36- {
37- _conversations . Add ( conversation ) ;
38- _changedTableNames . Add ( nameof ( Conversation ) ) ;
39- }
40- else if ( entity is Agent agent )
35+ if ( entity is Agent agent )
4136 {
4237 _agents . Add ( agent ) ;
4338 _changedTableNames . Add ( nameof ( Agent ) ) ;
@@ -61,33 +56,7 @@ public int Transaction<TTableInterface>(Action action)
6156
6257 foreach ( var table in _changedTableNames )
6358 {
64- if ( table == nameof ( Conversation ) )
65- {
66- var conversations = _conversations . Select ( x => new ConversationCollection
67- {
68- Id = ! string . IsNullOrEmpty ( x . Id ) ? x . Id : Guid . NewGuid ( ) . ToString ( ) ,
69- AgentId = x . AgentId ,
70- UserId = ! string . IsNullOrEmpty ( x . UserId ) ? x . UserId : string . Empty ,
71- Title = x . Title ,
72- States = x . States ? . ToKeyValueList ( ) ?? new List < StateKeyValue > ( ) ,
73- CreatedTime = x . CreatedTime ,
74- UpdatedTime = x . UpdatedTime
75- } ) . ToList ( ) ;
76-
77- foreach ( var conversation in conversations )
78- {
79- var filter = Builders < ConversationCollection > . Filter . Eq ( x => x . Id , conversation . Id ) ;
80- var update = Builders < ConversationCollection > . Update
81- . Set ( x => x . AgentId , conversation . AgentId )
82- . Set ( x => x . UserId , conversation . UserId )
83- . Set ( x => x . Title , conversation . Title )
84- . Set ( x => x . States , conversation . States )
85- . Set ( x => x . CreatedTime , conversation . CreatedTime )
86- . Set ( x => x . UpdatedTime , conversation . UpdatedTime ) ;
87- _dc . Conversations . UpdateOne ( filter , update , _options ) ;
88- }
89- }
90- else if ( table == nameof ( Agent ) )
59+ if ( table == nameof ( Agent ) )
9160 {
9261 var agents = _agents . Select ( x => new AgentCollection
9362 {
@@ -610,6 +579,7 @@ public void CreateNewConversation(Conversation conversation)
610579 AgentId = conversation . AgentId ,
611580 UserId = ! string . IsNullOrEmpty ( conversation . UserId ) ? conversation . UserId : string . Empty ,
612581 Title = conversation . Title ,
582+ Channel = conversation . Channel ,
613583 Status = conversation . Status ,
614584 States = conversation . States ? . ToKeyValueList ( ) ?? new List < StateKeyValue > ( ) ,
615585 CreatedTime = DateTime . UtcNow ,
@@ -670,6 +640,7 @@ public void AppendConversationDialogs(string conversationId, List<DialogElement>
670640 _dc . ConversationDialogs . UpdateOne ( filterDialog , updateDialog ) ;
671641 _dc . Conversations . UpdateOne ( filterConv , updateConv ) ;
672642 }
643+
673644 public void UpdateConversationTitle ( string conversationId , string title )
674645 {
675646 if ( string . IsNullOrEmpty ( conversationId ) ) return ;
@@ -684,6 +655,7 @@ public void UpdateConversationTitle(string conversationId, string title)
684655
685656 _dc . Conversations . UpdateOne ( filterConv , updateConv ) ;
686657 }
658+
687659 public List < StateKeyValue > GetConversationStates ( string conversationId )
688660 {
689661 var states = new List < StateKeyValue > ( ) ;
@@ -745,6 +717,7 @@ public Conversation GetConversation(string conversationId)
745717 AgentId = conv . AgentId . ToString ( ) ,
746718 UserId = conv . UserId . ToString ( ) ,
747719 Title = conv . Title ,
720+ Channel = conv . Channel ,
748721 Status = conv . Status ,
749722 Dialogs = dialogElements ,
750723 States = new ConversationState ( conv . States ?? new List < StateKeyValue > ( ) ) ,
@@ -753,13 +726,20 @@ public Conversation GetConversation(string conversationId)
753726 } ;
754727 }
755728
756- public List < Conversation > GetConversations ( string userId )
729+ public List < Conversation > GetConversations ( string ? agentId = null , string ? status = null , string ? channel = null , string ? userId = null )
757730 {
758731 var records = new List < Conversation > ( ) ;
759732 if ( string . IsNullOrEmpty ( userId ) ) return records ;
760733
761- var filterByUserId = Builders < ConversationCollection > . Filter . Eq ( x => x . UserId , userId ) ;
762- var conversations = _dc . Conversations . Find ( filterByUserId ) . ToList ( ) ;
734+ var builder = Builders < ConversationCollection > . Filter ;
735+ var filters = new List < FilterDefinition < ConversationCollection > > ( ) ;
736+
737+ if ( ! string . IsNullOrEmpty ( agentId ) ) filters . Add ( builder . Eq ( x => x . AgentId , agentId ) ) ;
738+ if ( ! string . IsNullOrEmpty ( status ) ) filters . Add ( builder . Eq ( x => x . Status , status ) ) ;
739+ if ( ! string . IsNullOrEmpty ( channel ) ) filters . Add ( builder . Eq ( x => x . Channel , channel ) ) ;
740+ if ( ! string . IsNullOrEmpty ( userId ) ) filters . Add ( builder . Eq ( x => x . UserId , userId ) ) ;
741+
742+ var conversations = _dc . Conversations . Find ( builder . And ( filters ) ) . ToList ( ) ;
763743
764744 foreach ( var conv in conversations )
765745 {
@@ -770,6 +750,7 @@ public List<Conversation> GetConversations(string userId)
770750 AgentId = conv . AgentId . ToString ( ) ,
771751 UserId = conv . UserId . ToString ( ) ,
772752 Title = conv . Title ,
753+ Channel = conv . Channel ,
773754 Status = conv . Status ,
774755 CreatedTime = conv . CreatedTime ,
775756 UpdatedTime = conv . UpdatedTime
@@ -791,6 +772,7 @@ public List<Conversation> GetLastConversations()
791772 AgentId = c . AgentId . ToString ( ) ,
792773 UserId = c . UserId . ToString ( ) ,
793774 Title = c . Title ,
775+ Channel = c . Channel ,
794776 Status = c . Status ,
795777 CreatedTime = c . CreatedTime ,
796778 UpdatedTime = c . UpdatedTime
0 commit comments