@peopl-health/nexus 5.57.0 → 5.57.1
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.
- package/lib/clinical/memory/DefaultMemoryManager.js +4 -1
- package/lib/core/NexusMessaging.js +7 -1
- package/lib/core/PhiProcessor.js +6 -0
- package/lib/helpers/caregiverHelper.js +41 -12
- package/lib/index.d.ts +2 -0
- package/lib/models/messageModel.js +1 -0
- package/lib/storage/MongoStorage.js +2 -1
- package/package.json +1 -1
|
@@ -174,7 +174,10 @@ class DefaultMemoryManager extends MemoryManager {
|
|
|
174
174
|
|
|
175
175
|
_speakerPrefix(msg, role) {
|
|
176
176
|
if (this._isInlineOperatorTurn(msg, role)) return `${OPERATOR_TURN_PREFIX} `;
|
|
177
|
-
if (msg.origin === 'patient' && msg.contact_role === 'caregiver')
|
|
177
|
+
if (msg.origin === 'patient' && msg.contact_role === 'caregiver') {
|
|
178
|
+
const label = String(msg.contact_label || '').replace(/[[\]\n]/g, '').trim();
|
|
179
|
+
return label ? `${CAREGIVER_TURN_PREFIX.slice(0, -1)}: ${label}] ` : `${CAREGIVER_TURN_PREFIX} `;
|
|
180
|
+
}
|
|
178
181
|
if (msg.origin !== 'whatsapp_platform') return '';
|
|
179
182
|
// The send API records the authenticated sender; without one the send was automated.
|
|
180
183
|
return msg.triggeredBy ? `${TEAM_TURN_PREFIX} ` : `${PLATFORM_TURN_PREFIX} `;
|
|
@@ -16,7 +16,7 @@ const { Thread } = require('../models/threadModel');
|
|
|
16
16
|
|
|
17
17
|
const { storeRunMetrics } = require('../helpers/metricsHelper');
|
|
18
18
|
const { isWithin24HourWindow } = require('../helpers/conversationWindowHelper');
|
|
19
|
-
const { remapCaregiverSender, replyContactFor } = require('../helpers/caregiverHelper');
|
|
19
|
+
const { maskCaregiverLabel, remapCaregiverSender, replyContactFor } = require('../helpers/caregiverHelper');
|
|
20
20
|
const { triggerTemplateRecovery } = require('../helpers/templateRecoveryHelper');
|
|
21
21
|
const { prepareMessageMedia } = require('../helpers/mediaHelper');
|
|
22
22
|
const { acquireSendClaim, completeSendClaim, releaseSendClaim } = require('../helpers/sendClaimHelper');
|
|
@@ -568,6 +568,12 @@ class NexusMessaging {
|
|
|
568
568
|
|
|
569
569
|
async processIncomingMessage(messageData, { onSaved = null } = {}) {
|
|
570
570
|
await remapCaregiverSender(messageData);
|
|
571
|
+
if (messageData.contactName) {
|
|
572
|
+
messageData.contactLabel = await maskCaregiverLabel(messageData.contactName, messageData.code, this.phiProcessor).catch((error) => {
|
|
573
|
+
logger.error('[processIncomingMessage] Caregiver name could not be masked; the model sees the role only', { error: error.message });
|
|
574
|
+
return null;
|
|
575
|
+
});
|
|
576
|
+
}
|
|
571
577
|
const chatId = messageData.code;
|
|
572
578
|
|
|
573
579
|
if (chatId) {
|
package/lib/core/PhiProcessor.js
CHANGED
|
@@ -181,6 +181,12 @@ class PhiProcessor {
|
|
|
181
181
|
return encoded;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
+
async encodeKnownName(name, numero) {
|
|
185
|
+
if (!this._phiEnabled) return name;
|
|
186
|
+
if (!name || !numero) return null;
|
|
187
|
+
return (await tokenMapService.encode(numero, name, 'PERSONA')) || null;
|
|
188
|
+
}
|
|
189
|
+
|
|
184
190
|
async inData(messageData, saveFn, handler, { onSaved } = {}) {
|
|
185
191
|
const decodedBody = messageData.body;
|
|
186
192
|
const encodedBody = await this.encodeBody(decodedBody, messageData.code);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { Historial_Clinico_ID } = require('../config/airtableConfig');
|
|
2
2
|
|
|
3
3
|
const { logger } = require('../utils/logger');
|
|
4
4
|
const { MapCache } = require('../utils/MapCache');
|
|
@@ -14,10 +14,23 @@ const CAREGIVER_ROLE = 'caregiver';
|
|
|
14
14
|
|
|
15
15
|
const caregiverCache = new MapCache({ maxSize: 1000, ttl: 5 * 60 * 1000 });
|
|
16
16
|
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
const PHONE_RX = /\+?\d[\d\s-]{6,}\d/;
|
|
18
|
+
|
|
19
|
+
function listedCaregivers(value) {
|
|
20
|
+
return String(value || '')
|
|
21
|
+
.split(/[,;\n]/)
|
|
22
|
+
.map((entry) => {
|
|
23
|
+
const phone = entry.match(PHONE_RX);
|
|
24
|
+
if (!phone) return null;
|
|
25
|
+
const name = entry
|
|
26
|
+
.replace(phone[0], '')
|
|
27
|
+
.replace(/whatsapp:/gi, '')
|
|
28
|
+
.replace(/\s+/g, ' ')
|
|
29
|
+
.replace(/^[\s:–-]+|[\s:–-]+$/g, '');
|
|
30
|
+
return { digits: phone[0].replace(/\D/g, ''), name: name || null };
|
|
31
|
+
})
|
|
32
|
+
.filter(Boolean);
|
|
33
|
+
}
|
|
21
34
|
|
|
22
35
|
async function lookupCaregiver(code) {
|
|
23
36
|
const digits = stripWhatsAppFormat(code);
|
|
@@ -25,20 +38,24 @@ async function lookupCaregiver(code) {
|
|
|
25
38
|
|
|
26
39
|
const safeCode = escapeFormulaValue(code);
|
|
27
40
|
const records = await getRecordByFilter(
|
|
28
|
-
|
|
41
|
+
Historial_Clinico_ID,
|
|
29
42
|
'estado_general',
|
|
30
|
-
`OR({whatsapp_id}='${safeCode}', FIND('${digits}', {${CAREGIVERS_FIELD}}))`,
|
|
43
|
+
`OR({whatsapp_id}='${safeCode}', FIND('${digits}', SUBSTITUTE(SUBSTITUTE({${CAREGIVERS_FIELD}}, ' ', ''), '-', '')))`,
|
|
31
44
|
);
|
|
32
45
|
if (!records?.length) return null;
|
|
33
46
|
if (records.some((row) => row.whatsapp_id === code)) return null;
|
|
34
47
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
const matches = records
|
|
49
|
+
.filter((row) => row.whatsapp_id)
|
|
50
|
+
.map((row) => ({ row, caregiver: listedCaregivers(row[CAREGIVERS_FIELD]).find((entry) => entry.digits === digits) }))
|
|
51
|
+
.filter(({ caregiver }) => caregiver);
|
|
52
|
+
if (matches.length > 1) {
|
|
53
|
+
logger.warn('[caregiverHelper] number listed as caregiver of several patients; not remapping', { count: matches.length });
|
|
38
54
|
return null;
|
|
39
55
|
}
|
|
40
|
-
if (!
|
|
41
|
-
|
|
56
|
+
if (!matches.length) return null;
|
|
57
|
+
const [{ row, caregiver }] = matches;
|
|
58
|
+
return { patientCode: ensureWhatsAppFormat(row.whatsapp_id), caregiverName: caregiver.name };
|
|
42
59
|
}
|
|
43
60
|
|
|
44
61
|
async function resolveCaregiver(code) {
|
|
@@ -62,6 +79,8 @@ async function remapCaregiverSender(messageData) {
|
|
|
62
79
|
if (!caregiver) return messageData;
|
|
63
80
|
messageData.contactCode = messageData.code;
|
|
64
81
|
messageData.contactRole = CAREGIVER_ROLE;
|
|
82
|
+
messageData.contactName = caregiver.caregiverName || null;
|
|
83
|
+
if (caregiver.caregiverName) messageData.pushName = caregiver.caregiverName;
|
|
65
84
|
messageData.code = caregiver.patientCode;
|
|
66
85
|
messageData.from = caregiver.patientCode;
|
|
67
86
|
return messageData;
|
|
@@ -82,6 +101,14 @@ async function replyContactFor(code) {
|
|
|
82
101
|
}
|
|
83
102
|
}
|
|
84
103
|
|
|
104
|
+
async function maskCaregiverLabel(name, code, phiProcessor) {
|
|
105
|
+
if (!name) return null;
|
|
106
|
+
const relation = (name.match(/\([^)]*\)/g) || []).join(' ');
|
|
107
|
+
const person = name.replace(/\([^)]*\)/g, '').replace(/\s+/g, ' ').trim();
|
|
108
|
+
const token = person ? await phiProcessor.encodeKnownName(person, code) : null;
|
|
109
|
+
return [token, relation].filter(Boolean).join(' ') || null;
|
|
110
|
+
}
|
|
111
|
+
|
|
85
112
|
function contactOf(message) {
|
|
86
113
|
return message?.contact_code ? { contactCode: message.contact_code, contactRole: message.contact_role } : {};
|
|
87
114
|
}
|
|
@@ -94,6 +121,8 @@ module.exports = {
|
|
|
94
121
|
CAREGIVERS_FIELD,
|
|
95
122
|
CAREGIVER_ROLE,
|
|
96
123
|
contactOf,
|
|
124
|
+
listedCaregivers,
|
|
125
|
+
maskCaregiverLabel,
|
|
97
126
|
remapCaregiverSender,
|
|
98
127
|
replyContactFor,
|
|
99
128
|
resolveCaregiver,
|
package/lib/index.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ const messageSchema = new mongoose.Schema({
|
|
|
28
28
|
},
|
|
29
29
|
contact_code: { type: String, default: null },
|
|
30
30
|
contact_role: { type: String, enum: [null, 'caregiver'], default: null },
|
|
31
|
+
contact_label: { type: String, default: null },
|
|
31
32
|
tools_executed: {
|
|
32
33
|
type: [{
|
|
33
34
|
tool_name: { type: String, required: true },
|
|
@@ -131,7 +131,8 @@ class MongoStorage {
|
|
|
131
131
|
triggeredBy: messageData.triggeredBy || null,
|
|
132
132
|
...(messageData.contactCode && {
|
|
133
133
|
contact_code: ensureWhatsAppFormat(messageData.contactCode),
|
|
134
|
-
contact_role: messageData.contactRole || null
|
|
134
|
+
contact_role: messageData.contactRole || null,
|
|
135
|
+
...(messageData.contactLabel && { contact_label: messageData.contactLabel })
|
|
135
136
|
})
|
|
136
137
|
};
|
|
137
138
|
return values;
|