From 148de80d68dc45933152be4710ad118c955b5269 Mon Sep 17 00:00:00 2001 From: Vladimir Buravlev Date: Wed, 27 Nov 2024 13:02:38 +0100 Subject: [PATCH] VM-1506: update last message in conversation when delete --- .../Services/MessageService.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/VirtoCommerce.CommunicationModule.Data/Services/MessageService.cs b/src/VirtoCommerce.CommunicationModule.Data/Services/MessageService.cs index 6e11d12..7f9835b 100644 --- a/src/VirtoCommerce.CommunicationModule.Data/Services/MessageService.cs +++ b/src/VirtoCommerce.CommunicationModule.Data/Services/MessageService.cs @@ -127,6 +127,9 @@ public virtual async Task UpdateMessage(Message message) public virtual async Task DeleteMessage(IList messageIds, bool withReplies) { + var messages = await _messageCrudService.GetAsync(messageIds); + var conversationIds = messages.Select(x => x.ConversationId).Distinct().ToList(); + if (withReplies) { var idsToDelete = await GetChildMessageIdsRecursively(messageIds); @@ -147,6 +150,19 @@ public virtual async Task DeleteMessage(IList messageIds, bool withRepli await _messageCrudService.DeleteAsync(messageIds); } + + var conversations = await _conversationCrudService.GetAsync(conversationIds); + foreach (var conversation in conversations) + { + if (conversation != null && messageIds.Contains(conversation.LastMessageId)) + { + var newLastMessage = (await GetMessagesByConversation(conversation.Id)).OrderByDescending(x => x.CreatedDate).FirstOrDefault(); + conversation.LastMessageId = newLastMessage?.Id; + conversation.LastMessageTimestamp = newLastMessage?.CreatedDate ?? DateTime.MinValue; + } + } + + await _conversationCrudService.SaveChangesAsync(conversations); } public virtual async Task SetMessageReadStatus(string messageId, string recipientId, bool notRead = false)