feature/IO-3000-Migrate-MSG-to-Sockets - Progress Checkpoint

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-11-19 08:52:07 -08:00
parent 03ae7bb160
commit bed87174d4
7 changed files with 64 additions and 61 deletions

View File

@@ -63,18 +63,28 @@ exports.receive = async (req, res) => {
// Insert new conversation and message
const insertresp = await client.request(queries.RECEIVE_MESSAGE, { msg: newMessage });
const createdConversation = insertresp.insert_conversations.returning[0];
const message = insertresp.insert_messages.returning[0];
// Safely access conversation and message
const createdConversation = insertresp?.insert_messages?.returning?.[0]?.conversation || null;
const message = insertresp?.insert_messages?.returning?.[0] || {
text: "",
image: false,
image_path: []
};
if (!createdConversation) {
throw new Error("Conversation data is missing from the response.");
}
// Emit new conversation event
ioRedis.to(getBodyshopRoom(response.bodyshops[0].id)).emit("new-conversation", {
conversation: createdConversation,
message: {
...message,
text: message.text || "",
image: message.image || false,
image_path: message.image_path || []
}
conversation: {
...createdConversation,
messages_aggregate: {
aggregate: {
count: 1 // Adjust dynamically based on your logic or default to 0
}
}
},
message
});
logger.log("sms-inbound-success", "DEBUG", "api", null, {
@@ -99,6 +109,7 @@ exports.receive = async (req, res) => {
}
} else if (response.bodyshops[0].conversations.length === 1) {
// Add to the existing conversation
// conversation UPDATED
newMessage.conversationid = response.bodyshops[0].conversations[0].id;
} else {
// Duplicate phone error