@peopl-health/nexus 3.13.12 → 3.14.0
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.
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
const moment = require('moment-timezone');
|
|
2
2
|
|
|
3
|
-
const runtimeConfig = require('../config/runtimeConfig');
|
|
4
|
-
|
|
5
3
|
const { logger } = require('../utils/logger');
|
|
6
4
|
|
|
7
5
|
const { Message } = require('../models/messageModel.js');
|
|
@@ -95,7 +93,6 @@ const sendMessageController = async (req, res) => {
|
|
|
95
93
|
timeZone: timeZone || null,
|
|
96
94
|
sendTime: sendTime ? moment.tz(sendTime, timeZone) + 2500 : new Date(),
|
|
97
95
|
code: ensureWhatsAppFormat(code),
|
|
98
|
-
author: runtimeConfig.get('USER_DB_MONGO'),
|
|
99
96
|
extraDelay: 0,
|
|
100
97
|
frontendId,
|
|
101
98
|
triggeredBy
|
|
@@ -125,12 +122,11 @@ const sendBulkMessageController = async (req, res) => {
|
|
|
125
122
|
variables = null,
|
|
126
123
|
triggeredBy = null
|
|
127
124
|
} = req.body || {};
|
|
128
|
-
const author = runtimeConfig.get('USER_DB_MONGO');
|
|
129
125
|
const sendMoment = sendTime ? moment.tz(sendTime, timeZone) + 20000 : new Date();
|
|
130
126
|
|
|
131
127
|
try {
|
|
132
128
|
const isFlowTemplate = contentSid && await FlowRouting.findOne({ contentSid }).lean();
|
|
133
|
-
|
|
129
|
+
|
|
134
130
|
let extraDelay = 0;
|
|
135
131
|
const payloads = [];
|
|
136
132
|
for (const recipient of codes) {
|
|
@@ -140,7 +136,6 @@ const sendBulkMessageController = async (req, res) => {
|
|
|
140
136
|
timeZone: timeZone || null,
|
|
141
137
|
sendTime: new Date(sendMoment + extraDelay),
|
|
142
138
|
code: ensureWhatsAppFormat(recipient),
|
|
143
|
-
author,
|
|
144
139
|
extraDelay: extraDelay += Math.floor(Math.random() * 5001) + 5000,
|
|
145
140
|
triggeredBy
|
|
146
141
|
});
|
|
@@ -167,7 +162,6 @@ const sendBulkMessageAirtableController = async (req, res) => {
|
|
|
167
162
|
variables = null,
|
|
168
163
|
triggeredBy = null
|
|
169
164
|
} = req.body || {};
|
|
170
|
-
const author = runtimeConfig.get('USER_DB_MONGO');
|
|
171
165
|
const sendMoment = sendTime ? moment.tz(sendTime, timeZone) + 20000 : new Date();
|
|
172
166
|
|
|
173
167
|
const envVariables = [...message.matchAll(/\[(.*?)\]/g)].map(m => m[1]);
|
|
@@ -199,7 +193,7 @@ const sendBulkMessageAirtableController = async (req, res) => {
|
|
|
199
193
|
: variables,
|
|
200
194
|
timeZone: timeZone || null,
|
|
201
195
|
sendTime: new Date(sendMoment + extraDelay),
|
|
202
|
-
code,
|
|
196
|
+
code,
|
|
203
197
|
triggeredBy
|
|
204
198
|
});
|
|
205
199
|
extraDelay += Math.floor(Math.random() * 5001) + 5000;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const { Monitoreo_ID } = require('../config/airtableConfig');
|
|
2
|
+
|
|
3
|
+
const { logger } = require('../utils/logger');
|
|
4
|
+
|
|
5
|
+
const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
|
|
6
|
+
|
|
7
|
+
const { addLinkedRecord } = require('../services/airtableService');
|
|
8
|
+
|
|
9
|
+
const createPrescriptionController = async (req, res) => {
|
|
10
|
+
const { code, prescription, triggeredBy, creationSource = 'other' } = req.body;
|
|
11
|
+
|
|
12
|
+
if (!code) return res.status(400).json({ success: false, error: 'Code is required' });
|
|
13
|
+
if (!prescription) return res.status(400).json({ success: false, error: 'Prescription is required' });
|
|
14
|
+
if (!triggeredBy) return res.status(400).json({ success: false, error: 'triggeredBy is required' });
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const whatsappId = ensureWhatsAppFormat(code);
|
|
18
|
+
|
|
19
|
+
const record = await addLinkedRecord(
|
|
20
|
+
Monitoreo_ID,
|
|
21
|
+
'patient_prescriptions',
|
|
22
|
+
{
|
|
23
|
+
prescription,
|
|
24
|
+
author_email: triggeredBy,
|
|
25
|
+
creation_source: creationSource,
|
|
26
|
+
created: new Date().toISOString()
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
referenceTable: 'estado_general',
|
|
30
|
+
referenceFilter: `{whatsapp_id}='${whatsappId}'`,
|
|
31
|
+
linkFieldName: 'patient_id'
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
logger.info('[PrescriptionController] Prescription created', { code: whatsappId });
|
|
36
|
+
res.status(201).json({ success: true, message: 'Prescription created', recordId: record?.id || null });
|
|
37
|
+
} catch (error) {
|
|
38
|
+
logger.error('[PrescriptionController] Error creating prescription', { error: error.message, code });
|
|
39
|
+
res.status(500).json({ success: false, error: 'Failed to create prescription' });
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
module.exports = { createPrescriptionController };
|
package/lib/routes/index.js
CHANGED
|
@@ -82,6 +82,10 @@ const dashboardRouteDefinitions = {
|
|
|
82
82
|
'POST /:id': 'updateDashboardControllerById'
|
|
83
83
|
};
|
|
84
84
|
|
|
85
|
+
const prescriptionRouteDefinitions = {
|
|
86
|
+
'POST /create': 'createPrescriptionController'
|
|
87
|
+
};
|
|
88
|
+
|
|
85
89
|
const presetRouteDefinitions = {
|
|
86
90
|
'POST /cache/clear': 'clearPresetCacheController'
|
|
87
91
|
};
|
|
@@ -117,6 +121,7 @@ const templateFlowController = require('../controllers/templateFlowController');
|
|
|
117
121
|
const flowDataExchangeController = require('../controllers/flowDataExchangeController');
|
|
118
122
|
const uploadController = require('../controllers/uploadController');
|
|
119
123
|
const dashboardController = require('../controllers/dashboardController');
|
|
124
|
+
const prescriptionController = require('../controllers/prescriptionController');
|
|
120
125
|
|
|
121
126
|
const builtInControllers = {
|
|
122
127
|
// Assistant controllers
|
|
@@ -195,6 +200,9 @@ const builtInControllers = {
|
|
|
195
200
|
updateAllReviewStatusController: conversationController.updateAllReviewStatusController,
|
|
196
201
|
getReviewStatusController: conversationController.getReviewStatusController,
|
|
197
202
|
|
|
203
|
+
// Prescription controllers
|
|
204
|
+
createPrescriptionController: prescriptionController.createPrescriptionController,
|
|
205
|
+
|
|
198
206
|
// Dashboard controllers
|
|
199
207
|
getDashboardController: dashboardController.getDashboardController,
|
|
200
208
|
getDashboardStatsControllerById: dashboardController.getDashboardStatsControllerById,
|
|
@@ -210,6 +218,7 @@ const setupDefaultRoutes = (app) => {
|
|
|
210
218
|
app.use('/api/patient', createRouter(patientRouteDefinitions, builtInControllers));
|
|
211
219
|
app.use('/api/preset', createRouter(presetRouteDefinitions, builtInControllers));
|
|
212
220
|
app.use('/api/template', createRouter(templateRouteDefinitions, builtInControllers));
|
|
221
|
+
app.use('/api/prescription', createRouter(prescriptionRouteDefinitions, builtInControllers));
|
|
213
222
|
app.use('/api/dashboard', createRouter(dashboardRouteDefinitions, builtInControllers));
|
|
214
223
|
};
|
|
215
224
|
|
|
@@ -221,6 +230,7 @@ module.exports = {
|
|
|
221
230
|
patientRoutes: patientRouteDefinitions,
|
|
222
231
|
presetRoutes: presetRouteDefinitions,
|
|
223
232
|
templateRoutes: templateRouteDefinitions,
|
|
233
|
+
prescriptionRoutes: prescriptionRouteDefinitions,
|
|
224
234
|
dashboardRoutes: dashboardRouteDefinitions,
|
|
225
235
|
|
|
226
236
|
createRouter,
|