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

@@ -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 = {