feature/IO-3000-Migrate-MSG-to-Sockets - Progress Checkpoint
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user