@peopl-health/nexus 5.57.2 → 5.57.5
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/lib/index.d.ts
CHANGED
|
@@ -188,6 +188,8 @@ declare module '@peopl-health/nexus' {
|
|
|
188
188
|
media: any | null;
|
|
189
189
|
type: 'message' | 'interactive' | 'media';
|
|
190
190
|
raw?: { role?: string; replyRequested?: boolean };
|
|
191
|
+
contactCode?: string;
|
|
192
|
+
contactRole?: 'caregiver' | null;
|
|
191
193
|
}
|
|
192
194
|
|
|
193
195
|
export interface MessageStatusEventData {
|
|
@@ -95,6 +95,7 @@ class MongoStorage {
|
|
|
95
95
|
fromMe: doc.from_me,
|
|
96
96
|
triggeredBy: doc.triggeredBy,
|
|
97
97
|
...(doc.origin === 'system' && doc.raw ? { raw: doc.raw } : {}),
|
|
98
|
+
...(doc.contact_code && { contactCode: doc.contact_code, contactRole: doc.contact_role }),
|
|
98
99
|
status,
|
|
99
100
|
...(frontendId && { frontendId })
|
|
100
101
|
}));
|