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:
Dave Richer
2024-11-26 10:29:57 -08:00
parent 5f1daffb3e
commit 63397769d2
4 changed files with 14 additions and 2 deletions

View File

@@ -96,6 +96,7 @@ export const CONVERSATION_ID_BY_PHONE = gql`
archived
label
unreadcnt
created_at
job_conversations {
jobid
conversationid

View File

@@ -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;

View File

@@ -4,6 +4,7 @@ query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID($mssid: String!, $phone: String!) {
id
conversations(where: { phone_num: { _eq: $phone } }) {
id
created_at
}
}
}`;

View File

@@ -45,7 +45,12 @@ exports.receive = async (req, res) => {
}
const bodyshop = response.bodyshops[0];
const existingConversation = bodyshop.conversations[0]; // Expect only one conversation per phone number per bodyshop
// Sort conversations by `updated_at` (or `created_at`) and pick the last one
const sortedConversations = bodyshop.conversations.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
const existingConversation = sortedConversations.length
? sortedConversations[sortedConversations.length - 1]
: null;
let conversationid;
let newMessage = {