@peopl-health/nexus 5.57.2 → 5.57.4

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.
@@ -7,8 +7,9 @@ const { ScheduledMessage } = require('../models/agendaMessageModel.js');
7
7
  const { DeliveryAttempt, DELIVERY_ATTEMPT_LANDED_STATUSES } = require('../models/deliveryAttemptModel.js');
8
8
  const { FlowRouting } = require('../models/flowRoutingModel.js');
9
9
 
10
- const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
10
+ const { ensureWhatsAppFormat, stripWhatsAppFormat } = require('../helpers/twilioHelper');
11
11
  const { ensureFlowTokenInVariables } = require('../helpers/templateFlowControllerHelper');
12
+ const { CAREGIVER_ROLE, resolveCaregiver } = require('../helpers/caregiverHelper');
12
13
 
13
14
  const { getRecordByFilter } = require('../services/airtableService');
14
15
  const { getMessages } = require('../services/messageService');
@@ -67,7 +68,8 @@ const sendMessageController = async (req, res) => {
67
68
  variables = null,
68
69
  frontendId = null,
69
70
  triggeredBy = null,
70
- idempotencyKey = null
71
+ idempotencyKey = null,
72
+ contactCode = null
71
73
  } = req.body || {};
72
74
 
73
75
  const formattedCode = ensureWhatsAppFormat(code);
@@ -77,13 +79,26 @@ const sendMessageController = async (req, res) => {
77
79
  }
78
80
 
79
81
  try {
82
+ let contact = {};
83
+ if (contactCode) {
84
+ if (sendTime) {
85
+ return res.status(400).json({ success: false, error: 'contactCode cannot be combined with sendTime' });
86
+ }
87
+ const formattedContact = ensureWhatsAppFormat(contactCode);
88
+ const caregiver = await resolveCaregiver(formattedContact, { strict: true });
89
+ if (!caregiver || stripWhatsAppFormat(caregiver.patientCode) !== stripWhatsAppFormat(formattedCode)) {
90
+ return res.status(400).json({ success: false, error: 'contactCode is not a registered caregiver of this patient' });
91
+ }
92
+ contact = { contactCode: formattedContact, contactRole: CAREGIVER_ROLE };
93
+ }
94
+
80
95
  const finalVariables = contentSid ? await ensureFlowTokenInVariables(contentSid, variables) : variables;
81
96
 
82
97
  if (!sendTime) {
83
98
  const result = await sendMessage({
84
99
  fileUrl, body: message, fileType, contentSid, hidePreview,
85
100
  variables: finalVariables, code: formattedCode,
86
- frontendId, triggeredBy, idempotencyKey
101
+ frontendId, triggeredBy, idempotencyKey, ...contact
87
102
  });
88
103
  return res.status(200).json({
89
104
  success: result?.success ?? true,
@@ -58,8 +58,14 @@ async function lookupCaregiver(code) {
58
58
  return { patientCode: ensureWhatsAppFormat(row.whatsapp_id), caregiverName: caregiver.name };
59
59
  }
60
60
 
61
- async function resolveCaregiver(code) {
61
+ async function resolveCaregiver(code, { strict = false } = {}) {
62
62
  if (!code) return null;
63
+ if (strict) {
64
+ const found = await lookupCaregiver(code);
65
+ caregiverCache.set(code, found);
66
+ return found;
67
+ }
68
+
63
69
  const cached = caregiverCache.get(code);
64
70
  if (cached !== undefined) return cached;
65
71
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.57.2",
3
+ "version": "5.57.4",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",