@peopl-health/nexus 6.8.0 → 6.9.0-dev.21842
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.
|
@@ -10,6 +10,8 @@ const { DeliveryAttempt, DELIVERY_ATTEMPT_LANDED_STATUSES } = require('../models
|
|
|
10
10
|
const { FlowRouting } = require('../models/flowRoutingModel.js');
|
|
11
11
|
|
|
12
12
|
const { ensureWhatsAppFormat, isArchivedPatientCode } = require('../helpers/twilioHelper');
|
|
13
|
+
const { recipientPhone } = require('../helpers/recipientHelper');
|
|
14
|
+
const { lastDigits } = require('../helpers/outboundAllowlistHelper');
|
|
13
15
|
const { ensureFlowTokenInVariables } = require('../helpers/templateFlowControllerHelper');
|
|
14
16
|
const { CAREGIVER_ROLE, registeredCaregiverCode } = require('../helpers/caregiverHelper');
|
|
15
17
|
|
|
@@ -91,6 +93,10 @@ const sendMessageController = async (req, res) => {
|
|
|
91
93
|
if (!ensureWhatsAppFormat(code)) {
|
|
92
94
|
return res.status(400).json({ success: false, error: 'code is required' });
|
|
93
95
|
}
|
|
96
|
+
const recipient = recipientPhone(code);
|
|
97
|
+
if (!recipient) {
|
|
98
|
+
return res.status(400).json({ success: false, error: 'code is not a valid phone number' });
|
|
99
|
+
}
|
|
94
100
|
|
|
95
101
|
if (!message && !fileUrl && !contentSid) {
|
|
96
102
|
return res.status(400).json({ success: false, error: 'message, fileUrl or contentSid is required' });
|
|
@@ -99,7 +105,7 @@ const sendMessageController = async (req, res) => {
|
|
|
99
105
|
if (!_validateExpiresAt(res, expiresAt)) return;
|
|
100
106
|
|
|
101
107
|
try {
|
|
102
|
-
const formattedCode = await resolveThreadCode(ensureWhatsAppFormat(
|
|
108
|
+
const formattedCode = await resolveThreadCode(ensureWhatsAppFormat(recipient));
|
|
103
109
|
let contact = {};
|
|
104
110
|
if (contactCode) {
|
|
105
111
|
if (sendTime) {
|
|
@@ -189,7 +195,12 @@ const sendBulkMessageController = async (req, res) => {
|
|
|
189
195
|
|
|
190
196
|
let extraDelay = 0;
|
|
191
197
|
const staggered = [];
|
|
192
|
-
for (const
|
|
198
|
+
for (const raw of codes) {
|
|
199
|
+
const recipient = recipientPhone(raw);
|
|
200
|
+
if (!recipient) {
|
|
201
|
+
logger.warn('[sendBulkMessageController] Skipping a recipient that is not a phone number', { recipient: lastDigits(String(raw)) });
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
193
204
|
staggered.push({
|
|
194
205
|
offset: extraDelay,
|
|
195
206
|
payload: {
|
|
@@ -208,7 +219,7 @@ const sendBulkMessageController = async (req, res) => {
|
|
|
208
219
|
const sendMoment = sendTime ? moment.tz(sendTime, timeZone) + 20000 : Date.now();
|
|
209
220
|
const payloads = staggered.map(({ offset, payload }) => ({ ...payload, sendTime: new Date(sendMoment + offset) }));
|
|
210
221
|
|
|
211
|
-
await _sendBulkMessages(payloads, res, `Sent bulk of ${
|
|
222
|
+
await _sendBulkMessages(payloads, res, `Sent bulk of ${payloads.length} messages`);
|
|
212
223
|
} catch (err) {
|
|
213
224
|
logger.error('Error sending bulk messages:', { error: err.message });
|
|
214
225
|
res.status(500).json({ success: false, error: err.message });
|
|
@@ -252,7 +263,12 @@ const sendBulkMessageAirtableController = async (req, res) => {
|
|
|
252
263
|
|
|
253
264
|
let code = Array.isArray(row[columnPhone]) ? row[columnPhone][0] : row[columnPhone];
|
|
254
265
|
if (!code || isArchivedPatientCode(code)) continue;
|
|
255
|
-
|
|
266
|
+
const phone = recipientPhone(code);
|
|
267
|
+
if (!phone) {
|
|
268
|
+
logger.warn('[sendBulkMessageAirtableController] Skipping a row whose phone is not a phone number', { phone: lastDigits(String(code)) });
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
code = ensureWhatsAppFormat(phone);
|
|
256
272
|
if (sentPhones.has(code)) continue;
|
|
257
273
|
sentPhones.add(code);
|
|
258
274
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const { WHATSAPP_PATIENT_CODE } = require('../utils/sanitizerUtils');
|
|
2
|
+
|
|
3
|
+
// Without its country code: 10 digits is Mexico (an old 044/045 prefix dropped; never 51, Peru's code), 9 starting with 9 a Peruvian mobile.
|
|
4
|
+
const withCountryCode = (digits) => {
|
|
5
|
+
const national = digits.replace(/^04[45](?=\d{10}$)/, '');
|
|
6
|
+
if (/^(?!51)[2-9]\d{9}$/.test(national)) return `+52${national}`;
|
|
7
|
+
if (/^9\d{8}$/.test(digits)) return `+51${digits}`;
|
|
8
|
+
return digits;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// «+(811) 618-4774»: a plus typed before a parenthesised area code carries no country code.
|
|
12
|
+
const writtenPhone = (phone) => {
|
|
13
|
+
const raw = String(phone ?? '');
|
|
14
|
+
const written = raw.replace(/[^\d+]/g, '');
|
|
15
|
+
if (/^\s*\+\s*\(/.test(raw)) return withCountryCode(written.slice(1));
|
|
16
|
+
return written.startsWith('+') ? written : withCountryCode(written);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const WHATSAPP_JID = /^(?:whatsapp:)?\d[\d:-]{4,}@(s\.whatsapp\.net|g\.us|c\.us|lid)$/i;
|
|
20
|
+
// Without a «+», a Mexican mobile has at most 13 digits (521…); more means several numbers in one field.
|
|
21
|
+
const MAX_DIGITS_WITHOUT_PLUS = 13;
|
|
22
|
+
|
|
23
|
+
const recipientPhone = (raw) => {
|
|
24
|
+
const text = typeof raw === 'number' ? String(raw) : raw;
|
|
25
|
+
if (typeof text !== 'string') return null;
|
|
26
|
+
const trimmed = text.trim();
|
|
27
|
+
if (WHATSAPP_JID.test(trimmed)) return trimmed;
|
|
28
|
+
const phone = trimmed.replace(/^whatsapp:/i, '');
|
|
29
|
+
if (!phone || /[^\d\s()+.-]/.test(phone)) return null;
|
|
30
|
+
if (!phone.includes('+') && phone.replace(/\D/g, '').length > MAX_DIGITS_WITHOUT_PLUS) return null;
|
|
31
|
+
const e164 = `+${writtenPhone(phone).replace(/^\+/, '')}`;
|
|
32
|
+
return WHATSAPP_PATIENT_CODE.test(`whatsapp:${e164}`) ? e164 : null;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = { recipientPhone, writtenPhone };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { v4: uuidv4 } = require('uuid');
|
|
2
|
+
const { writtenPhone } = require('./recipientHelper');
|
|
2
3
|
|
|
3
4
|
function convertTwilioToInternalFormat(twilioMessage) {
|
|
4
5
|
const from = twilioMessage.From || '';
|
|
@@ -47,19 +48,8 @@ const ensureWhatsAppFormat = (code) => {
|
|
|
47
48
|
return trimmed;
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
// Without its country code: 10 digits is Mexico (an old 044/045 prefix dropped; never 51, Peru's code), 9 starting with 9 a Peruvian mobile.
|
|
51
|
-
const withCountryCode = (digits) => {
|
|
52
|
-
const national = digits.replace(/^04[45](?=\d{10}$)/, '');
|
|
53
|
-
if (/^(?!51)[2-9]\d{9}$/.test(national)) return `+52${national}`;
|
|
54
|
-
if (/^9\d{8}$/.test(digits)) return `+51${digits}`;
|
|
55
|
-
return digits;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
51
|
// A phone as typed in Airtable («55 1234 5678», «987 654 321», «+51 987…») as a WhatsApp code.
|
|
59
|
-
const whatsAppCodeFromPhone = (phone) =>
|
|
60
|
-
const written = String(phone ?? '').replace(/[^\d+]/g, '');
|
|
61
|
-
return ensureWhatsAppFormat(written.startsWith('+') ? written : withCountryCode(written));
|
|
62
|
-
};
|
|
52
|
+
const whatsAppCodeFromPhone = (phone) => ensureWhatsAppFormat(writtenPhone(phone));
|
|
63
53
|
|
|
64
54
|
const stripWhatsAppFormat = (code) => {
|
|
65
55
|
const raw = String(code).trim();
|