feature/IO-3000-messaging-sockets-migrations2 -
- testing and edge cases Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -16,23 +16,21 @@ exports.receive = async (req, res) => {
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom }
|
||||
} = req;
|
||||
|
||||
logger.log("sms-inbound", "DEBUG", "api", null, {
|
||||
const loggerData = {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
image: !!req.body.MediaUrl0,
|
||||
image_path: generateMediaArray(req.body)
|
||||
});
|
||||
};
|
||||
|
||||
logger.log("sms-inbound", "DEBUG", "api", null, loggerData);
|
||||
|
||||
if (!req.body || !req.body.MessagingServiceSid || !req.body.SmsMessageSid) {
|
||||
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),
|
||||
...loggerData,
|
||||
type: "malformed-request"
|
||||
});
|
||||
res.status(400).json({ success: false, error: "Malformed Request" });
|
||||
return;
|
||||
return res.status(400).json({ success: false, error: "Malformed Request" });
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -41,6 +39,14 @@ exports.receive = async (req, res) => {
|
||||
phone: phone(req.body.From).phoneNumber
|
||||
});
|
||||
|
||||
if (!response.bodyshops[0]) {
|
||||
return res.status(400).json({ success: false, error: "No matching bodyshop" });
|
||||
}
|
||||
|
||||
const bodyshop = response.bodyshops[0];
|
||||
const isNewConversation = bodyshop.conversations.length === 0;
|
||||
const isDuplicate = bodyshop.conversations.length > 1;
|
||||
|
||||
let newMessage = {
|
||||
msid: req.body.SmsMessageSid,
|
||||
text: req.body.Body,
|
||||
@@ -48,140 +54,105 @@ exports.receive = async (req, res) => {
|
||||
image_path: generateMediaArray(req.body)
|
||||
};
|
||||
|
||||
if (response.bodyshops[0]) {
|
||||
const bodyshop = response.bodyshops[0];
|
||||
if (bodyshop.conversations.length === 0) {
|
||||
newMessage.conversation = {
|
||||
data: {
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_num: phone(req.body.From).phoneNumber,
|
||||
archived: false
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const insertresp = await client.request(queries.RECEIVE_MESSAGE, { msg: newMessage });
|
||||
const createdConversation = insertresp?.insert_messages?.returning?.[0]?.conversation || null;
|
||||
const message = insertresp?.insert_messages?.returning?.[0];
|
||||
|
||||
if (!createdConversation) {
|
||||
throw new Error("Conversation data is missing from the response.");
|
||||
}
|
||||
|
||||
const broadcastRoom = getBodyshopRoom(createdConversation.bodyshop.id);
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: message.conversation.bodyshop.id,
|
||||
conversationId: message.conversation.id
|
||||
});
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("new-message-summary", {
|
||||
isoutbound: false,
|
||||
existingConversation: false,
|
||||
newConversation: createdConversation,
|
||||
conversationId: createdConversation.id,
|
||||
updated_at: message.updated_at,
|
||||
msid: message.sid,
|
||||
summary: true
|
||||
});
|
||||
|
||||
ioRedis.to(conversationRoom).emit("new-message-detailed", {
|
||||
newMessage: message,
|
||||
isoutbound: false,
|
||||
newConversation: createdConversation,
|
||||
existingConversation: false,
|
||||
conversationId: createdConversation.id,
|
||||
summary: false
|
||||
});
|
||||
|
||||
logger.log("sms-inbound-success", "DEBUG", "api", null, {
|
||||
newMessage,
|
||||
createdConversation
|
||||
});
|
||||
|
||||
res.status(200).send("");
|
||||
return;
|
||||
} catch (e) {
|
||||
handleError(req, e, res, "RECEIVE_MESSAGE");
|
||||
return;
|
||||
}
|
||||
} else if (bodyshop.conversations.length === 1) {
|
||||
newMessage.conversationid = bodyshop.conversations[0].id;
|
||||
} else {
|
||||
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,
|
||||
type: "duplicate-phone"
|
||||
});
|
||||
res.status(400).json({ success: false, error: "Duplicate phone number" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const insertresp = await client.request(queries.INSERT_MESSAGE, {
|
||||
msg: newMessage,
|
||||
conversationid: newMessage.conversationid
|
||||
});
|
||||
|
||||
const message = insertresp.insert_messages.returning[0];
|
||||
const data = {
|
||||
type: "messaging-inbound",
|
||||
conversationid: message.conversationid || "",
|
||||
text: message.text || "",
|
||||
messageid: message.id || "",
|
||||
phone_num: message.conversation.phone_num || ""
|
||||
};
|
||||
|
||||
const fcmresp = await admin.messaging().send({
|
||||
topic: `${message.conversation.bodyshop.imexshopid}-messaging`,
|
||||
notification: {
|
||||
title: InstanceManager({
|
||||
imex: `ImEX Online Message - ${data.phone_num}`,
|
||||
rome: `Rome Online Message - ${data.phone_num}`,
|
||||
promanager: `ProManager Message - ${data.phone_num}`
|
||||
}),
|
||||
body: message.image_path ? `Image ${message.text}` : message.text
|
||||
},
|
||||
data
|
||||
});
|
||||
|
||||
logger.log("sms-inbound-success", "DEBUG", "api", null, {
|
||||
newMessage,
|
||||
fcmresp
|
||||
});
|
||||
|
||||
const broadcastRoom = getBodyshopRoom(message.conversation.bodyshop.id);
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: message.conversation.bodyshop.id,
|
||||
conversationId: message.conversation.id
|
||||
});
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("new-message-summary", {
|
||||
isoutbound: false,
|
||||
existingConversation: true,
|
||||
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: message.conversationid,
|
||||
summary: false
|
||||
});
|
||||
|
||||
res.status(200).send("");
|
||||
} catch (e) {
|
||||
handleError(req, e, res, "INSERT_MESSAGE");
|
||||
}
|
||||
if (isDuplicate) {
|
||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||
...loggerData,
|
||||
messagingServiceSid: req.body.MessagingServiceSid,
|
||||
type: "duplicate-phone"
|
||||
});
|
||||
return res.status(400).json({ success: false, error: "Duplicate phone number" });
|
||||
}
|
||||
|
||||
if (isNewConversation) {
|
||||
newMessage.conversation = {
|
||||
data: {
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_num: phone(req.body.From).phoneNumber,
|
||||
archived: false
|
||||
}
|
||||
};
|
||||
} else {
|
||||
const existingConversation = bodyshop.conversations[0];
|
||||
|
||||
// Update the conversation to unarchive it
|
||||
if (existingConversation.archived) {
|
||||
await client.request(queries.UNARCHIVE_CONVERSATION, {
|
||||
id: existingConversation.id,
|
||||
archived: false
|
||||
});
|
||||
}
|
||||
|
||||
newMessage.conversationid = existingConversation.id;
|
||||
}
|
||||
|
||||
const query = isNewConversation ? queries.RECEIVE_MESSAGE : queries.INSERT_MESSAGE;
|
||||
const variables = isNewConversation
|
||||
? { msg: newMessage }
|
||||
: { msg: newMessage, conversationid: newMessage.conversationid };
|
||||
|
||||
const insertresp = await client.request(query, variables);
|
||||
const message = insertresp?.insert_messages?.returning?.[0];
|
||||
const conversation = message?.conversation || null;
|
||||
|
||||
if (!conversation) {
|
||||
throw new Error("Conversation data is missing from the response.");
|
||||
}
|
||||
|
||||
const broadcastRoom = getBodyshopRoom(conversation.bodyshop.id);
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: conversation.bodyshop.id,
|
||||
conversationId: conversation.id
|
||||
});
|
||||
|
||||
const commonPayload = {
|
||||
isoutbound: false,
|
||||
conversationId: conversation.id,
|
||||
updated_at: message.updated_at,
|
||||
msid: message.sid
|
||||
};
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("new-message-summary", {
|
||||
...commonPayload,
|
||||
existingConversation: !isNewConversation,
|
||||
newConversation: isNewConversation ? conversation : null,
|
||||
summary: true
|
||||
});
|
||||
|
||||
ioRedis.to(conversationRoom).emit("new-message-detailed", {
|
||||
newMessage: message,
|
||||
...commonPayload,
|
||||
newConversation: isNewConversation ? conversation : null,
|
||||
existingConversation: !isNewConversation,
|
||||
summary: false
|
||||
});
|
||||
|
||||
const fcmresp = await admin.messaging().send({
|
||||
topic: `${message.conversation.bodyshop.imexshopid}-messaging`,
|
||||
notification: {
|
||||
title: InstanceManager({
|
||||
imex: `ImEX Online Message - ${message.conversation.phone_num}`,
|
||||
rome: `Rome Online Message - ${message.conversation.phone_num}`,
|
||||
promanager: `ProManager Message - ${message.conversation.phone_num}`
|
||||
}),
|
||||
body: message.image_path ? `Image ${message.text}` : message.text
|
||||
},
|
||||
data: {
|
||||
type: "messaging-inbound",
|
||||
conversationid: message.conversationid || "",
|
||||
text: message.text || "",
|
||||
messageid: message.id || "",
|
||||
phone_num: message.conversation.phone_num || ""
|
||||
}
|
||||
});
|
||||
|
||||
logger.log("sms-inbound-success", "DEBUG", "api", null, {
|
||||
newMessage,
|
||||
fcmresp
|
||||
});
|
||||
|
||||
res.status(200).send("");
|
||||
} catch (e) {
|
||||
handleError(req, e, res, "FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID");
|
||||
handleError(req, e, res, "RECEIVE_MESSAGE");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user