@peopl-health/nexus 5.57.7 → 5.57.9
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.
|
@@ -40,8 +40,10 @@ const getOfficeMessagesController = async (req, res) => {
|
|
|
40
40
|
|
|
41
41
|
const sendOfficeMessageController = async (req, res) => {
|
|
42
42
|
try {
|
|
43
|
-
const { triggeredBy, text, clientMessageId } = req.body || {};
|
|
44
|
-
const delivery = await sendOfficeMessage({
|
|
43
|
+
const { triggeredBy, text, clientMessageId, fileUrl, fileName } = req.body || {};
|
|
44
|
+
const delivery = await sendOfficeMessage({
|
|
45
|
+
requestedBy: triggeredBy, code: req.params.code, text, clientMessageId, fileUrl, fileName,
|
|
46
|
+
});
|
|
45
47
|
res.status(201).json({ success: true, ...delivery });
|
|
46
48
|
} catch (error) {
|
|
47
49
|
respondWithError(res, error, 'sending office message', { code: redactDirectIdentifiers(String(req.params?.code || '')) });
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { Estado_General_ID, Monitoreo_ID } = require('../config/airtableConfig');
|
|
2
|
+
const runtimeConfig = require('../config/runtimeConfig');
|
|
2
3
|
|
|
3
4
|
const { httpError, requiredText } = require('../utils/httpErrorUtils');
|
|
4
5
|
const { inlineOf } = require('../utils/coercionUtils');
|
|
@@ -7,6 +8,7 @@ const { logger } = require('../utils/logger');
|
|
|
7
8
|
const { PATIENT_CODE, redactDirectIdentifiers } = require('../utils/sanitizerUtils');
|
|
8
9
|
const { escapeFormulaValue, fieldEquals } = require('../helpers/airtableFormulaHelper');
|
|
9
10
|
const { isWithin24HourWindow } = require('../helpers/conversationWindowHelper');
|
|
11
|
+
const { prepareMessageMedia } = require('../helpers/mediaHelper');
|
|
10
12
|
const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
|
|
11
13
|
const { sendMessage } = require('../core/NexusMessaging');
|
|
12
14
|
|
|
@@ -14,20 +16,23 @@ const { getRecordByFilter } = require('./airtableService');
|
|
|
14
16
|
const { getMessages } = require('./messageService');
|
|
15
17
|
const { getPatientInformation } = require('./patientService');
|
|
16
18
|
|
|
17
|
-
const ASSISTANT_FIELDS = ['email', 'doctor_match', 'office_label', 'template_content_sid'];
|
|
18
19
|
const PATIENT_FIELDS = ['whatsapp_id', 'name', 'doctor'];
|
|
19
20
|
const HIDDEN_ORIGINS = ['system', 'instruction'];
|
|
20
21
|
const DEFAULT_MESSAGE_LIMIT = 30;
|
|
21
22
|
const MAX_MESSAGE_LIMIT = 50;
|
|
22
23
|
const MAX_TEXT_LENGTH = 1500;
|
|
23
24
|
const MAX_TEMPLATE_TEXT_LENGTH = 800;
|
|
25
|
+
const DOCUMENT_TEMPLATE_DEFAULT_TEXT = 'Le compartimos un documento.';
|
|
26
|
+
const DOCUMENT_FILE_TYPE = 'document';
|
|
24
27
|
|
|
25
28
|
async function getOfficeAssistant(email) {
|
|
26
29
|
const normalized = String(email ?? '').trim().toLowerCase();
|
|
27
30
|
if (!normalized) return null;
|
|
28
31
|
|
|
29
32
|
const formula = `AND(LOWER({email}) = '${escapeFormulaValue(normalized)}', {active})`;
|
|
30
|
-
|
|
33
|
+
// No field list: Airtable rejects a listed column that does not exist yet, which would take the whole office view down
|
|
34
|
+
// until document_template_content_sid is added.
|
|
35
|
+
const [assistant] = await getRecordByFilter(Monitoreo_ID, 'office_assistants', formula, null, undefined, true) || [];
|
|
31
36
|
const doctor = inlineOf(assistant?.doctor_match);
|
|
32
37
|
const label = inlineOf(assistant?.office_label);
|
|
33
38
|
if (!doctor || !label) return null;
|
|
@@ -37,6 +42,7 @@ async function getOfficeAssistant(email) {
|
|
|
37
42
|
doctor,
|
|
38
43
|
label,
|
|
39
44
|
templateContentSid: inlineOf(assistant.template_content_sid) || null,
|
|
45
|
+
documentTemplateContentSid: inlineOf(assistant.document_template_content_sid) || null,
|
|
40
46
|
};
|
|
41
47
|
}
|
|
42
48
|
|
|
@@ -100,34 +106,78 @@ async function readOfficeMessages({ requestedBy, code, limit }) {
|
|
|
100
106
|
};
|
|
101
107
|
}
|
|
102
108
|
|
|
103
|
-
function
|
|
109
|
+
function officeDocumentUrl(fileUrl) {
|
|
110
|
+
if (!fileUrl) return null;
|
|
111
|
+
const bucketName = runtimeConfig.get('AWS_S3_BUCKET_NAME');
|
|
112
|
+
let url = null;
|
|
113
|
+
try {
|
|
114
|
+
url = new URL(String(fileUrl));
|
|
115
|
+
} catch {
|
|
116
|
+
url = null;
|
|
117
|
+
}
|
|
118
|
+
// The approved document template pins the S3 host and takes the rest of the URL as a variable. A bucket name
|
|
119
|
+
// with dots (whatsapp.prod) is presigned path-style, so the bucket is then the first path segment.
|
|
120
|
+
const bucket = escapeRegExp(bucketName ?? '');
|
|
121
|
+
const isVirtualHosted = new RegExp(`^${bucket}\\.s3(\\.[a-z0-9-]+)?\\.amazonaws\\.com$`).test(url?.hostname ?? '');
|
|
122
|
+
const isPathStyle = /^s3(\.[a-z0-9-]+)?\.amazonaws\.com$/.test(url?.hostname ?? '')
|
|
123
|
+
&& url.pathname.startsWith(`/${bucketName}/`);
|
|
124
|
+
if (!bucketName || url?.protocol !== 'https:' || !(isVirtualHosted || isPathStyle)) {
|
|
125
|
+
throw httpError(400, 'The document must be uploaded before it is sent');
|
|
126
|
+
}
|
|
127
|
+
return url;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Twilio needs the file type in a template's media URL, and uploads are stored without an
|
|
131
|
+
// extension, so the template gets the copy prepareMessageMedia stores with one.
|
|
132
|
+
async function templateDocumentOf(document, patientCode) {
|
|
133
|
+
const { mediaUrl } = await prepareMessageMedia({
|
|
134
|
+
code: patientCode, fileUrl: document.url.href, fileType: DOCUMENT_FILE_TYPE, fileName: document.name,
|
|
135
|
+
});
|
|
136
|
+
// prepareMessageMedia answers with the original URL when the copy fails, and that one has no file type.
|
|
137
|
+
if (!mediaUrl || mediaUrl === document.url.href) throw httpError(502, 'The document could not be prepared for the template');
|
|
138
|
+
return { ...document, url: officeDocumentUrl(mediaUrl) };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function officeMessage({ assistant, patientCode, text, inWindow, document }) {
|
|
142
|
+
const attachment = document ? { fileUrl: document.url.href, fileType: DOCUMENT_FILE_TYPE, fileName: document.name } : {};
|
|
104
143
|
if (inWindow) {
|
|
105
144
|
if (text.length > MAX_TEXT_LENGTH) throw httpError(422, `The message exceeds ${MAX_TEXT_LENGTH} characters`);
|
|
106
|
-
return { code: patientCode, body: `${assistant.label}: ${text}` };
|
|
145
|
+
return { code: patientCode, body: text ? `${assistant.label}: ${text}` : assistant.label, ...attachment };
|
|
107
146
|
}
|
|
108
147
|
|
|
109
|
-
const templateText = text.replace(/\s+/g, ' ').trim();
|
|
148
|
+
const templateText = text.replace(/\s+/g, ' ').trim() || DOCUMENT_TEMPLATE_DEFAULT_TEXT;
|
|
110
149
|
if (templateText.length > MAX_TEMPLATE_TEXT_LENGTH) {
|
|
111
150
|
throw httpError(422, `Outside the 24-hour window the message is sent as a template and cannot exceed ${MAX_TEMPLATE_TEXT_LENGTH} characters`);
|
|
112
151
|
}
|
|
113
|
-
|
|
152
|
+
const contentSid = document ? assistant.documentTemplateContentSid : assistant.templateContentSid;
|
|
153
|
+
if (!contentSid) {
|
|
154
|
+
const template = document ? 'office document template' : 'office template';
|
|
155
|
+
throw httpError(503, `The ${template} is not configured; the patient is outside the 24-hour window`);
|
|
156
|
+
}
|
|
114
157
|
return {
|
|
115
158
|
code: patientCode,
|
|
116
159
|
body: `${assistant.label}: ${templateText}`,
|
|
117
|
-
contentSid
|
|
118
|
-
variables:
|
|
160
|
+
contentSid,
|
|
161
|
+
variables: document
|
|
162
|
+
? { 1: templateText, 2: `${document.url.pathname.slice(1)}${document.url.search}` }
|
|
163
|
+
: { 1: templateText },
|
|
164
|
+
...attachment,
|
|
119
165
|
};
|
|
120
166
|
}
|
|
121
167
|
|
|
122
|
-
async function sendOfficeMessage({ requestedBy, code, text, clientMessageId }) {
|
|
168
|
+
async function sendOfficeMessage({ requestedBy, code, text, clientMessageId, fileUrl, fileName }) {
|
|
123
169
|
const assistant = await requireOfficeAssistant(requestedBy);
|
|
124
170
|
const patientCode = await officePatientCode(assistant, code);
|
|
125
|
-
const
|
|
171
|
+
const url = officeDocumentUrl(fileUrl);
|
|
172
|
+
const document = url ? { url, name: fileName } : null;
|
|
173
|
+
const messageText = document ? String(text ?? '').trim() : requiredText(text, 'text');
|
|
126
174
|
const messageKey = requiredText(clientMessageId, 'clientMessageId');
|
|
127
175
|
|
|
128
176
|
const inWindow = await isWithin24HourWindow(patientCode);
|
|
177
|
+
const isDocumentTemplate = Boolean(document && !inWindow && assistant.documentTemplateContentSid);
|
|
178
|
+
const sentDocument = isDocumentTemplate ? await templateDocumentOf(document, patientCode) : document;
|
|
129
179
|
const message = {
|
|
130
|
-
...officeMessage({ assistant, patientCode, text: messageText, inWindow }),
|
|
180
|
+
...officeMessage({ assistant, patientCode, text: messageText, inWindow, document: sentDocument }),
|
|
131
181
|
triggeredBy: assistant.email,
|
|
132
182
|
idempotencyKey: `office:${patientCode}:${messageKey}`,
|
|
133
183
|
};
|
|
@@ -143,6 +193,7 @@ async function sendOfficeMessage({ requestedBy, code, text, clientMessageId }) {
|
|
|
143
193
|
assistant: assistant.email,
|
|
144
194
|
code: redactDirectIdentifiers(patientCode),
|
|
145
195
|
transport,
|
|
196
|
+
hasDocument: Boolean(document),
|
|
146
197
|
duplicate: result?.duplicate === true,
|
|
147
198
|
});
|
|
148
199
|
return { transport, messageId: result?.messageId || null, duplicate: result?.duplicate === true };
|