@peopl-health/nexus 3.3.2 → 3.3.3
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.
|
@@ -314,31 +314,48 @@ const searchConversationsController = async (req, res) => {
|
|
|
314
314
|
|
|
315
315
|
const escapedQuery = query.replace(/\+/g, '\\+');
|
|
316
316
|
|
|
317
|
-
//
|
|
317
|
+
// Prioritize matches on numero/nombre_whatsapp; body search is a last resort.
|
|
318
318
|
const conversations = await Message.aggregate([
|
|
319
|
-
{
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
319
|
+
{
|
|
320
|
+
$facet: {
|
|
321
|
+
primary: [
|
|
322
|
+
{ $match: {
|
|
323
|
+
group_id: null,
|
|
324
|
+
$or: [
|
|
325
|
+
{ numero: { $regex: escapedQuery, $options: 'i' } },
|
|
326
|
+
{ nombre_whatsapp: { $regex: escapedQuery, $options: 'i' } }
|
|
327
|
+
]
|
|
328
|
+
}},
|
|
329
|
+
{ $sort: { createdAt: -1, timestamp: -1 } },
|
|
330
|
+
{ $group: {
|
|
331
|
+
_id: '$numero',
|
|
332
|
+
latestMessage: { $first: '$$ROOT' },
|
|
333
|
+
messageCount: { $sum: 1 }
|
|
334
|
+
}},
|
|
335
|
+
{ $addFields: { priority: 1 } }
|
|
336
|
+
],
|
|
337
|
+
secondary: [
|
|
338
|
+
{ $match: {
|
|
339
|
+
group_id: null,
|
|
340
|
+
body: { $regex: escapedQuery, $options: 'i' }
|
|
341
|
+
}},
|
|
342
|
+
{ $sort: { createdAt: -1, timestamp: -1 } },
|
|
343
|
+
{ $group: {
|
|
344
|
+
_id: '$numero',
|
|
345
|
+
latestMessage: { $first: '$$ROOT' },
|
|
346
|
+
messageCount: { $sum: 1 }
|
|
347
|
+
}},
|
|
348
|
+
{ $addFields: { priority: 2 } }
|
|
349
|
+
]
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
{ $project: { merged: { $concatArrays: ['$primary', '$secondary'] } } },
|
|
353
|
+
{ $unwind: '$merged' },
|
|
354
|
+
{ $replaceRoot: { newRoot: '$merged' } },
|
|
355
|
+
{ $sort: { priority: 1, 'latestMessage.createdAt': -1 } },
|
|
356
|
+
// Deduplicate numbers keeping highest-priority (name/number) result first
|
|
357
|
+
{ $group: { _id: '$_id', doc: { $first: '$$ROOT' } } },
|
|
358
|
+
{ $replaceRoot: { newRoot: '$doc' } },
|
|
342
359
|
{ $limit: parsedLimit }
|
|
343
360
|
]);
|
|
344
361
|
|
|
@@ -741,4 +758,4 @@ module.exports = {
|
|
|
741
758
|
searchConversationsController,
|
|
742
759
|
sendTemplateToNewNumberController,
|
|
743
760
|
getOpenAIThreadMessagesController
|
|
744
|
-
};
|
|
761
|
+
};
|