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
|
archived
|
||||||
label
|
label
|
||||||
unreadcnt
|
unreadcnt
|
||||||
|
created_at
|
||||||
job_conversations {
|
job_conversations {
|
||||||
jobid
|
jobid
|
||||||
conversationid
|
conversationid
|
||||||
|
|||||||
@@ -47,7 +47,12 @@ export function* openChatByPhone({ payload }) {
|
|||||||
fetchPolicy: "no-cache" // Ensure the query always gets the latest data
|
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) {
|
if (existingConversation) {
|
||||||
let updatedConversation = existingConversation;
|
let updatedConversation = existingConversation;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID($mssid: String!, $phone: String!) {
|
|||||||
id
|
id
|
||||||
conversations(where: { phone_num: { _eq: $phone } }) {
|
conversations(where: { phone_num: { _eq: $phone } }) {
|
||||||
id
|
id
|
||||||
|
created_at
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`;
|
}`;
|
||||||
|
|||||||
@@ -45,7 +45,12 @@ exports.receive = async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const bodyshop = response.bodyshops[0];
|
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 conversationid;
|
||||||
let newMessage = {
|
let newMessage = {
|
||||||
|
|||||||
Reference in New Issue
Block a user