@peopl-health/nexus 2.2.4 → 2.2.6
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.
|
@@ -34,10 +34,10 @@ const addInsAssistantController = async (req, res) => {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
const addMsgAssistantController = async (req, res) => {
|
|
37
|
-
const { code, messages, reply = false } = req.body;
|
|
37
|
+
const { code, messages, role = 'user', reply = false } = req.body;
|
|
38
38
|
|
|
39
39
|
try {
|
|
40
|
-
const ans = await addMsgAssistant(code, messages, reply);
|
|
40
|
+
const ans = await addMsgAssistant(code, messages, role, reply);
|
|
41
41
|
if (ans) await sendMessage({code, body: ans, fileType: 'text'});
|
|
42
42
|
return res.status(200).send({ message: 'Add message to the assistant' });
|
|
43
43
|
} catch (error) {
|
|
@@ -3,6 +3,8 @@ const { fetchConversationData, processConversations } = require('../services/con
|
|
|
3
3
|
const { sendMessage } = require('../core/NexusMessaging');
|
|
4
4
|
const { Thread } = require('../models/threadModel');
|
|
5
5
|
const llmConfig = require('../config/llmConfig');
|
|
6
|
+
const { Historial_Clinico_ID } = require('../config/airtableConfig');
|
|
7
|
+
const { getRecordByFilter } = require('../services/airtableService');
|
|
6
8
|
|
|
7
9
|
const Message = mongoose.models.Message;
|
|
8
10
|
|
|
@@ -301,6 +303,9 @@ const searchConversationsController = async (req, res) => {
|
|
|
301
303
|
});
|
|
302
304
|
}
|
|
303
305
|
|
|
306
|
+
const maxLimit = 100;
|
|
307
|
+
const parsedLimit = Math.min(parseInt(limit) || 50, maxLimit);
|
|
308
|
+
|
|
304
309
|
console.log(`Searching conversations for query: "${query}"`);
|
|
305
310
|
const searchStartTime = Date.now();
|
|
306
311
|
|
|
@@ -332,18 +337,61 @@ const searchConversationsController = async (req, res) => {
|
|
|
332
337
|
messageCount: { $sum: 1 }
|
|
333
338
|
}},
|
|
334
339
|
{ $sort: { 'latestMessage.createdAt': -1 } },
|
|
335
|
-
{ $limit:
|
|
340
|
+
{ $limit: parsedLimit }
|
|
336
341
|
]);
|
|
337
342
|
|
|
338
343
|
const searchTime = Date.now() - searchStartTime;
|
|
339
344
|
console.log(`Search completed in ${searchTime}ms, found ${conversations.length} conversations`);
|
|
340
345
|
|
|
346
|
+
// Fetch names from Airtable and WhatsApp (same logic as fetchConversationData)
|
|
347
|
+
const phoneNumbers = conversations.map(conv => conv._id).filter(Boolean);
|
|
348
|
+
let airtableNameMap = {};
|
|
349
|
+
let airtablePatientIdMap = {};
|
|
350
|
+
|
|
351
|
+
if (phoneNumbers.length > 0) {
|
|
352
|
+
try {
|
|
353
|
+
// Batch phone numbers to avoid Airtable URL length limit (16,000 chars)
|
|
354
|
+
const BATCH_SIZE = 50;
|
|
355
|
+
const batches = [];
|
|
356
|
+
for (let i = 0; i < phoneNumbers.length; i += BATCH_SIZE) {
|
|
357
|
+
batches.push(phoneNumbers.slice(i, i + BATCH_SIZE));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const batchPromises = batches.map(batch => {
|
|
361
|
+
const formula = 'OR(' +
|
|
362
|
+
batch.map(p => `{whatsapp_id} = "${p}"`).join(', ') +
|
|
363
|
+
')';
|
|
364
|
+
return getRecordByFilter(Historial_Clinico_ID, 'estado_general', formula).catch(error => {
|
|
365
|
+
console.error('Error fetching Airtable batch for search:', error);
|
|
366
|
+
return [];
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const batchResults = await Promise.all(batchPromises);
|
|
371
|
+
const patientTable = batchResults.flat();
|
|
372
|
+
|
|
373
|
+
// Create both maps in a single iteration
|
|
374
|
+
patientTable.forEach(patient => {
|
|
375
|
+
if (patient && patient.whatsapp_id) {
|
|
376
|
+
airtableNameMap[patient.whatsapp_id] = patient.name;
|
|
377
|
+
if (patient.patient_id) {
|
|
378
|
+
airtablePatientIdMap[patient.whatsapp_id] = patient.patient_id;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
console.log(`Found ${Object.keys(airtableNameMap).length} names in Airtable for search results (${batches.length} batches)`);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
console.error('Error fetching names from Airtable for search, falling back to nombre_whatsapp:', error);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
341
388
|
// Process conversations for response
|
|
342
389
|
const processedConversations = conversations.map(conv => {
|
|
343
390
|
if (!conv || !conv.latestMessage) {
|
|
344
391
|
return {
|
|
345
392
|
phoneNumber: conv?._id || 'unknown',
|
|
346
393
|
name: 'Unknown',
|
|
394
|
+
patientId: airtablePatientIdMap[conv?._id] || null,
|
|
347
395
|
lastMessage: '',
|
|
348
396
|
lastMessageTime: new Date(),
|
|
349
397
|
messageCount: 0,
|
|
@@ -375,7 +423,8 @@ const searchConversationsController = async (req, res) => {
|
|
|
375
423
|
|
|
376
424
|
return {
|
|
377
425
|
phoneNumber: conv._id,
|
|
378
|
-
name: conv?.latestMessage?.nombre_whatsapp || 'Unknown',
|
|
426
|
+
name: airtableNameMap[conv._id] || conv?.latestMessage?.nombre_whatsapp || 'Unknown',
|
|
427
|
+
patientId: airtablePatientIdMap[conv._id] || null,
|
|
379
428
|
lastMessage: conv?.latestMessage?.body || '',
|
|
380
429
|
lastMessageTime: conv?.latestMessage?.createdAt || conv?.latestMessage?.timestamp || new Date(),
|
|
381
430
|
messageCount: conv.messageCount || 0,
|