@peopl-health/nexus 4.2.9 → 4.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -547,33 +547,27 @@ class TwilioProvider extends MessageProvider {
|
|
|
547
547
|
|
|
548
548
|
async submitForApproval(contentSid, name, category) {
|
|
549
549
|
if (!contentSid) throw new Error('Content SID is required');
|
|
550
|
-
|
|
551
|
-
|
|
550
|
+
|
|
551
|
+
let approvalsUrl = null;
|
|
552
|
+
for (let attempt = 0; attempt < 3 && !approvalsUrl; attempt++) {
|
|
553
|
+
if (attempt > 0) await new Promise(r => setTimeout(r, 300));
|
|
554
|
+
const content = await this.getTemplate(contentSid);
|
|
552
555
|
const links = (content && content.links) || {};
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
contentSid,
|
|
558
|
-
warning: 'Twilio account does not expose an approvals endpoint. Approval must be handled manually in the Console.'
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
const payload = {
|
|
562
|
-
name,
|
|
563
|
-
category,
|
|
564
|
-
friendly_name: name,
|
|
565
|
-
categories: category ? [category] : undefined,
|
|
566
|
-
channel: 'whatsapp'
|
|
567
|
-
};
|
|
568
|
-
const resp = await axios.post(approvalsUrl, payload, { auth: { username: this.accountSid, password: this.authToken } });
|
|
569
|
-
return { success: true, contentSid, approvalRequest: resp.data || null };
|
|
570
|
-
} catch (error) {
|
|
571
|
-
return {
|
|
572
|
-
success: false,
|
|
573
|
-
contentSid,
|
|
574
|
-
warning: `Failed to submit for approval via API: ${error.message}`
|
|
575
|
-
};
|
|
556
|
+
approvalsUrl = links.approval_create || links.approvals || links.approvalRequests || links.approval_requests;
|
|
557
|
+
}
|
|
558
|
+
if (!approvalsUrl) {
|
|
559
|
+
throw new Error(`Approvals URL not available for template ${contentSid}`);
|
|
576
560
|
}
|
|
561
|
+
|
|
562
|
+
const payload = {
|
|
563
|
+
name,
|
|
564
|
+
category,
|
|
565
|
+
friendly_name: name,
|
|
566
|
+
categories: category ? [category] : undefined,
|
|
567
|
+
channel: 'whatsapp'
|
|
568
|
+
};
|
|
569
|
+
const resp = await axios.post(approvalsUrl, payload, { auth: { username: this.accountSid, password: this.authToken } });
|
|
570
|
+
return { success: true, contentSid, approvalRequest: resp.data || null };
|
|
577
571
|
}
|
|
578
572
|
|
|
579
573
|
async deleteTemplate(sid) {
|
|
@@ -6,6 +6,8 @@ const { recordDeliveryAttempt } = require('./deliveryAttemptHelper');
|
|
|
6
6
|
|
|
7
7
|
const getMessaging = () => require('../core/NexusMessaging');
|
|
8
8
|
|
|
9
|
+
const TEMPLATE_BODY_LIMIT = 1024;
|
|
10
|
+
|
|
9
11
|
async function triggerTemplateRecovery(messageDoc, { source = 'reactive', messageSid = null } = {}) {
|
|
10
12
|
try {
|
|
11
13
|
if (!messageDoc?.body || !messageDoc?.numero || !messageDoc?._id) return;
|
|
@@ -45,12 +47,16 @@ async function triggerTemplateRecovery(messageDoc, { source = 'reactive', messag
|
|
|
45
47
|
|
|
46
48
|
let twilioContent;
|
|
47
49
|
let setupErr = null;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
if (messageDoc.body.length > TEMPLATE_BODY_LIMIT) {
|
|
51
|
+
setupErr = new Error(`Body exceeds ${TEMPLATE_BODY_LIMIT}-char template limit (${messageDoc.body.length})`);
|
|
52
|
+
} else {
|
|
53
|
+
try {
|
|
54
|
+
twilioContent = await template.save();
|
|
55
|
+
const approvalName = `${templateName}_${Date.now()}`.toLowerCase().replace(/[^a-z0-9_]/g, '_');
|
|
56
|
+
await provider.submitForApproval(twilioContent.sid, approvalName, 'UTILITY');
|
|
57
|
+
} catch (err) {
|
|
58
|
+
setupErr = err;
|
|
59
|
+
}
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
await recordDeliveryAttempt({
|