feature/IO-3000-messaging-sockets-migration2 - Take last conversation if more than one exists.
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -96,6 +96,7 @@ export const CONVERSATION_ID_BY_PHONE = gql`
|
||||
archived
|
||||
label
|
||||
unreadcnt
|
||||
created_at
|
||||
job_conversations {
|
||||
jobid
|
||||
conversationid
|
||||
|
||||
@@ -47,7 +47,12 @@ export function* openChatByPhone({ payload }) {
|
||||
fetchPolicy: "no-cache" // Ensure the query always gets the latest data
|
||||
});
|
||||
|
||||
const existingConversation = conversations?.find((c) => c.phone_num === p.number);
|
||||
// Sort conversations by `updated_at` or `created_at` and pick the last one for the given phone number
|
||||
const sortedConversations = conversations
|
||||
?.filter((c) => c.phone_num === p.number) // Filter to match the phone number
|
||||
.sort((a, b) => new Date(a.created_at) - new Date(b.created_at)); // Sort by `updated_at`
|
||||
|
||||
const existingConversation = sortedConversations?.[sortedConversations.length - 1] || null;
|
||||
|
||||
if (existingConversation) {
|
||||
let updatedConversation = existingConversation;
|
||||
|
||||
Reference in New Issue
Block a user