@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
- const content = await this.getTemplate(contentSid);
551
- try {
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
- let approvalsUrl = links.approval_create || links.approvals || links.approvalRequests || links.approval_requests;
554
- if (!approvalsUrl) {
555
- return {
556
- success: false,
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
- try {
49
- twilioContent = await template.save();
50
- const approvalName = `${templateName}_${Date.now()}`.toLowerCase().replace(/[^a-z0-9_]/g, '_');
51
- await provider.submitForApproval(twilioContent.sid, approvalName, 'UTILITY');
52
- } catch (err) {
53
- setupErr = err;
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({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "4.2.9",
3
+ "version": "4.2.11",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",