feature/IO-3182-Phone-Number-Consent - Checkpoint
This commit is contained in:
@@ -3,8 +3,7 @@ const {
|
||||
FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID,
|
||||
UNARCHIVE_CONVERSATION,
|
||||
CREATE_CONVERSATION,
|
||||
INSERT_MESSAGE,
|
||||
SET_PHONE_NUMBER_CONSENT
|
||||
INSERT_MESSAGE
|
||||
} = require("../graphql-client/queries");
|
||||
const { phone } = require("phone");
|
||||
const { admin } = require("../firebase/firebase-handler");
|
||||
@@ -17,11 +16,11 @@ const InstanceManager = require("../utils/instanceMgr").default;
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
const receive = async (req, res) => {
|
||||
console.dir(req.body);
|
||||
const {
|
||||
logger,
|
||||
ioRedis,
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom },
|
||||
sessionUtils: { getBodyshopFromRedis }
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom }
|
||||
} = req;
|
||||
|
||||
const loggerData = {
|
||||
@@ -54,35 +53,6 @@ const receive = async (req, res) => {
|
||||
|
||||
const bodyshop = response.bodyshops[0];
|
||||
|
||||
// Step 2: Check enforce_sms_consent
|
||||
const bodyShopData = await getBodyshopFromRedis(bodyshopid);
|
||||
const enforceConsent = bodyShopData?.enforce_sms_consent ?? false;
|
||||
|
||||
// Step 3: Handle consent only if enforce_sms_consent is true
|
||||
if (enforceConsent) {
|
||||
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 4: Process conversation
|
||||
const sortedConversations = bodyshop.conversations.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
|
||||
const existingConversation = sortedConversations.length
|
||||
|
||||
Reference in New Issue
Block a user