feature/IO-3182-Phone-Number-Consent - Front/Back Start/stop logic complete
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
const { UPDATE_MESSAGE_STATUS, MARK_MESSAGES_AS_READ } = require("../graphql-client/queries");
|
||||
const {
|
||||
UPDATE_MESSAGE_STATUS,
|
||||
MARK_MESSAGES_AS_READ,
|
||||
INSERT_PHONE_NUMBER_OPT_OUT,
|
||||
FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID
|
||||
} = require("../graphql-client/queries");
|
||||
const logger = require("../utils/logger");
|
||||
const { phone } = require("phone");
|
||||
|
||||
/**
|
||||
* Handle the status of an SMS message
|
||||
@@ -9,7 +15,7 @@ const logger = require("../utils/logger");
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
const status = async (req, res) => {
|
||||
const { SmsSid, SmsStatus } = req.body;
|
||||
const { SmsSid, SmsStatus, ErrorCode, To, MessagingServiceSid } = req.body;
|
||||
const {
|
||||
ioRedis,
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom }
|
||||
@@ -21,18 +27,76 @@ const status = async (req, res) => {
|
||||
return res.status(200).json({ message: "Status 'queued' disregarded." });
|
||||
}
|
||||
|
||||
// Handle ErrorCode 21610 (Attempt to send to unsubscribed recipient) first
|
||||
if (ErrorCode === "21610" && To && MessagingServiceSid) {
|
||||
try {
|
||||
// Step 1: Find the bodyshop by MessagingServiceSid
|
||||
const bodyshopResponse = await client.request(FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID, {
|
||||
mssid: MessagingServiceSid,
|
||||
phone: phone(To).phoneNumber // Pass the normalized phone number as required
|
||||
});
|
||||
|
||||
const bodyshop = bodyshopResponse.bodyshops[0];
|
||||
if (!bodyshop) {
|
||||
logger.log("sms-opt-out-error", "ERROR", "api", null, {
|
||||
msid: SmsSid,
|
||||
messagingServiceSid: MessagingServiceSid,
|
||||
to: To,
|
||||
error: "No matching bodyshop found"
|
||||
});
|
||||
} else {
|
||||
// Step 2: Insert into phone_number_opt_out table
|
||||
const now = new Date().toISOString();
|
||||
const optOutInput = {
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_number: phone(To).phoneNumber.replace(/^\+1/, ""), // Normalize phone number (remove +1 for CA numbers)
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
};
|
||||
|
||||
const optOutResponse = await client.request(INSERT_PHONE_NUMBER_OPT_OUT, {
|
||||
optOutInput: [optOutInput]
|
||||
});
|
||||
|
||||
logger.log("sms-opt-out-success", "INFO", null, null, {
|
||||
msid: SmsSid,
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_number: optOutInput.phone_number,
|
||||
affected_rows: optOutResponse.insert_phone_number_opt_out.affected_rows
|
||||
});
|
||||
|
||||
// Store bodyshopid for potential use in WebSocket notification
|
||||
const broadcastRoom = getBodyshopRoom(bodyshop.id);
|
||||
ioRedis.to(broadcastRoom).emit("phone-number-opted-out", {
|
||||
bodyshopid: bodyshop.id,
|
||||
phone_number: optOutInput.phone_number
|
||||
// Note: conversationId is not included yet; will be set after message lookup
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
logger.log("sms-opt-out-error", "ERROR", "api", null, {
|
||||
msid: SmsSid,
|
||||
messagingServiceSid: MessagingServiceSid,
|
||||
to: To,
|
||||
error: error.message,
|
||||
stack: error.stack
|
||||
});
|
||||
// Continue processing to update message status
|
||||
}
|
||||
}
|
||||
|
||||
// Update message status in the database
|
||||
const response = await client.request(UPDATE_MESSAGE_STATUS, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus }
|
||||
});
|
||||
|
||||
const message = response.update_messages.returning[0];
|
||||
const message = response.update_messages?.returning?.[0];
|
||||
|
||||
if (message) {
|
||||
logger.log("sms-status-update", "DEBUG", "api", null, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus }
|
||||
status: SmsStatus
|
||||
});
|
||||
|
||||
// Emit WebSocket event to notify the change in message status
|
||||
@@ -47,20 +111,20 @@ const status = async (req, res) => {
|
||||
type: "status-changed"
|
||||
});
|
||||
} else {
|
||||
logger.log("sms-status-update-warning", "WARN", "api", null, {
|
||||
logger.log("sms-status-update-warning", "WARN", null, null, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus },
|
||||
warning: "No message returned from the database update."
|
||||
status: SmsStatus,
|
||||
warning: "No message found in database for update"
|
||||
});
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
} catch (err) {
|
||||
logger.log("sms-status-update-error", "ERROR", "api", null, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus },
|
||||
stack: error.stack,
|
||||
message: error.message
|
||||
status: SmsStatus,
|
||||
error: err.message,
|
||||
stack: err.stack
|
||||
});
|
||||
res.status(500).json({ error: "Failed to update message status." });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user