feature/IO-3000-messaging-sockets-migrations2 - Base cleanup
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -11,7 +11,6 @@ const logger = require("../utils/logger");
|
||||
const InstanceManager = require("../utils/instanceMgr").default;
|
||||
|
||||
exports.receive = async (req, res) => {
|
||||
// Perform request validation
|
||||
const {
|
||||
ioRedis,
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom }
|
||||
@@ -50,20 +49,17 @@ exports.receive = async (req, res) => {
|
||||
};
|
||||
|
||||
if (response.bodyshops[0]) {
|
||||
if (response.bodyshops[0].conversations.length === 0) {
|
||||
// No conversation found, create one
|
||||
const bodyshop = response.bodyshops[0];
|
||||
if (bodyshop.conversations.length === 0) {
|
||||
newMessage.conversation = {
|
||||
data: {
|
||||
bodyshopid: response.bodyshops[0].id,
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_num: phone(req.body.From).phoneNumber
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
// Insert new conversation and message
|
||||
const insertresp = await client.request(queries.RECEIVE_MESSAGE, { msg: newMessage });
|
||||
|
||||
// Safely access conversation and message
|
||||
const createdConversation = insertresp?.insert_messages?.returning?.[0]?.conversation || null;
|
||||
const message = insertresp?.insert_messages?.returning?.[0];
|
||||
|
||||
@@ -71,12 +67,12 @@ exports.receive = async (req, res) => {
|
||||
throw new Error("Conversation data is missing from the response.");
|
||||
}
|
||||
|
||||
const broadcastRoom = getBodyshopRoom(r2.insert_messages.returning[0].conversation.bodyshop.id);
|
||||
const broadcastRoom = getBodyshopRoom(createdConversation.bodyshop.id);
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: message.conversation.bodyshop.id,
|
||||
conversationId: message.conversation.id
|
||||
});
|
||||
// Broadcast new message to the conversation room
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("new-message-summary", {
|
||||
isoutbound: false,
|
||||
existingConversation: false,
|
||||
@@ -86,6 +82,7 @@ exports.receive = async (req, res) => {
|
||||
msid: message.sid,
|
||||
summary: true
|
||||
});
|
||||
|
||||
ioRedis.to(conversationRoom).emit("new-message-detailed", {
|
||||
newMessage: message,
|
||||
isoutbound: false,
|
||||
@@ -103,24 +100,12 @@ exports.receive = async (req, res) => {
|
||||
res.status(200).send("");
|
||||
return;
|
||||
} catch (e) {
|
||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
image: !!req.body.MediaUrl0,
|
||||
image_path: generateMediaArray(req.body),
|
||||
messagingServiceSid: req.body.MessagingServiceSid,
|
||||
error: e
|
||||
});
|
||||
|
||||
res.status(500).json(e);
|
||||
handleError(req, e, res, "RECEIVE_MESSAGE");
|
||||
return;
|
||||
}
|
||||
} else if (response.bodyshops[0].conversations.length === 1) {
|
||||
// Add to the existing conversation
|
||||
// conversation UPDATED
|
||||
newMessage.conversationid = response.bodyshops[0].conversations[0].id;
|
||||
} else if (bodyshop.conversations.length === 1) {
|
||||
newMessage.conversationid = bodyshop.conversations[0].id;
|
||||
} else {
|
||||
// Duplicate phone error
|
||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
@@ -134,7 +119,6 @@ exports.receive = async (req, res) => {
|
||||
}
|
||||
|
||||
try {
|
||||
// Insert message into an existing conversation
|
||||
const insertresp = await client.request(queries.INSERT_MESSAGE, {
|
||||
msg: newMessage,
|
||||
conversationid: newMessage.conversationid
|
||||
@@ -167,52 +151,36 @@ exports.receive = async (req, res) => {
|
||||
fcmresp
|
||||
});
|
||||
|
||||
const broadcastRoom = getBodyshopRoom(r2.insert_messages.returning[0].conversation.bodyshop.id);
|
||||
const broadcastRoom = getBodyshopRoom(message.conversation.bodyshop.id);
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: message.conversation.bodyshop.id,
|
||||
conversationId: message.conversation.id
|
||||
});
|
||||
// Broadcast new message to the conversation room
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("new-message-summary", {
|
||||
isoutbound: false,
|
||||
existingConversation: true,
|
||||
conversationId: conversationid,
|
||||
conversationId: message.conversationid,
|
||||
updated_at: message.updated_at,
|
||||
msid: message.sid,
|
||||
summary: true
|
||||
});
|
||||
|
||||
ioRedis.to(conversationRoom).emit("new-message-detailed", {
|
||||
newMessage: message,
|
||||
isoutbound: false,
|
||||
existingConversation: true,
|
||||
conversationId: conversationid,
|
||||
conversationId: message.conversationid,
|
||||
summary: false
|
||||
});
|
||||
|
||||
res.status(200).send("");
|
||||
} catch (e) {
|
||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
image: !!req.body.MediaUrl0,
|
||||
image_path: generateMediaArray(req.body),
|
||||
messagingServiceSid: req.body.MessagingServiceSid,
|
||||
error: e
|
||||
});
|
||||
|
||||
res.status(500).json(e);
|
||||
handleError(req, e, res, "INSERT_MESSAGE");
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
image: !!req.body.MediaUrl0,
|
||||
image_path: generateMediaArray(req.body),
|
||||
messagingServiceSid: req.body.MessagingServiceSid,
|
||||
error: e
|
||||
});
|
||||
res.status(500).json(e);
|
||||
handleError(req, e, res, "FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -228,3 +196,17 @@ const generateMediaArray = (body) => {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const handleError = (req, error, res, context) => {
|
||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
image: !!req.body.MediaUrl0,
|
||||
image_path: generateMediaArray(req.body),
|
||||
messagingServiceSid: req.body.MessagingServiceSid,
|
||||
context,
|
||||
error
|
||||
});
|
||||
|
||||
res.status(500).json({ error: error.message || "Internal Server Error" });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user