feature/IO-3182-Phone-Number-Consent - Checkpoint
This commit is contained in:
@@ -3,7 +3,8 @@ const {
|
||||
FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID,
|
||||
UNARCHIVE_CONVERSATION,
|
||||
CREATE_CONVERSATION,
|
||||
INSERT_MESSAGE
|
||||
INSERT_MESSAGE,
|
||||
SET_PHONE_NUMBER_CONSENT
|
||||
} = require("../graphql-client/queries");
|
||||
const { phone } = require("phone");
|
||||
const { admin } = require("../firebase/firebase-handler");
|
||||
@@ -91,7 +92,30 @@ const receive = async (req, res) => {
|
||||
|
||||
const bodyshop = response.bodyshops[0];
|
||||
|
||||
// Sort conversations by `updated_at` (or `created_at`) and pick the last one
|
||||
// Step 2: Handle consent
|
||||
const normalizedPhone = phone(req.body.From, "CA").phoneNumber.replace(/^\+1/, "");
|
||||
const isStop = req.body.Body.toUpperCase().includes("STOP");
|
||||
const consentStatus = isStop ? false : true;
|
||||
const reason = isStop ? "Customer texted STOP" : "Inbound message received";
|
||||
|
||||
const consentResponse = await client.request(SET_PHONE_NUMBER_CONSENT, {
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_number: normalizedPhone,
|
||||
consent_status: consentStatus,
|
||||
reason,
|
||||
changed_by: "system"
|
||||
});
|
||||
|
||||
// Emit WebSocket event for consent change
|
||||
const broadcastRoom = getBodyshopRoom(bodyshop.id);
|
||||
ioRedis.to(broadcastRoom).emit("consent-changed", {
|
||||
bodyshopId: bodyshop.id,
|
||||
phone_number: normalizedPhone,
|
||||
consent_status: consentStatus,
|
||||
reason
|
||||
});
|
||||
|
||||
// Step 3: Process conversation
|
||||
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]
|
||||
@@ -104,14 +128,11 @@ const receive = async (req, res) => {
|
||||
image: !!req.body.MediaUrl0,
|
||||
image_path: generateMediaArray(req.body),
|
||||
isoutbound: false,
|
||||
userid: null // Add additional fields as necessary
|
||||
userid: null
|
||||
};
|
||||
|
||||
if (existingConversation) {
|
||||
// Use the existing conversation
|
||||
conversationid = existingConversation.id;
|
||||
|
||||
// Unarchive the conversation if necessary
|
||||
if (existingConversation.archived) {
|
||||
await client.request(UNARCHIVE_CONVERSATION, {
|
||||
id: conversationid,
|
||||
@@ -119,11 +140,10 @@ const receive = async (req, res) => {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Create a new conversation
|
||||
const newConversationResponse = await client.request(CREATE_CONVERSATION, {
|
||||
conversation: {
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_num: phone(req.body.From).phoneNumber,
|
||||
phone_num: normalizedPhone,
|
||||
archived: false
|
||||
}
|
||||
});
|
||||
@@ -131,13 +151,12 @@ const receive = async (req, res) => {
|
||||
conversationid = createdConversation.id;
|
||||
}
|
||||
|
||||
// Ensure `conversationid` is added to the message
|
||||
newMessage.conversationid = conversationid;
|
||||
|
||||
// Step 3: Insert the message into the conversation
|
||||
// Step 4: Insert the message
|
||||
const insertresp = await client.request(INSERT_MESSAGE, {
|
||||
msg: newMessage,
|
||||
conversationid: conversationid
|
||||
conversationid
|
||||
});
|
||||
|
||||
const message = insertresp?.insert_messages?.returning?.[0];
|
||||
@@ -147,8 +166,7 @@ const receive = async (req, res) => {
|
||||
throw new Error("Conversation data is missing from the response.");
|
||||
}
|
||||
|
||||
// Step 4: Notify clients through Redis
|
||||
const broadcastRoom = getBodyshopRoom(conversation.bodyshop.id);
|
||||
// Step 5: Notify clients
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: conversation.bodyshop.id,
|
||||
conversationId: conversation.id
|
||||
@@ -176,7 +194,7 @@ const receive = async (req, res) => {
|
||||
summary: false
|
||||
});
|
||||
|
||||
// Step 5: Send FCM notification
|
||||
// Step 6: Send FCM notification
|
||||
const fcmresp = await admin.messaging().send({
|
||||
topic: `${message.conversation.bodyshop.imexshopid}-messaging`,
|
||||
notification: {
|
||||
|
||||
Reference in New Issue
Block a user