@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
- // Search through all conversations in the database
317
+ // Prioritize matches on numero/nombre_whatsapp; body search is a last resort.
318
318
  const conversations = await Message.aggregate([
319
- { $match: {
320
- group_id: null,
321
- $or: [
322
- { numero: { $regex: escapedQuery, $options: 'i' } },
323
- { nombre_whatsapp: { $regex: escapedQuery, $options: 'i' } },
324
- { body: { $regex: escapedQuery, $options: 'i' } }
325
- ]
326
- }},
327
- { $project: {
328
- numero: 1,
329
- body: 1,
330
- createdAt: 1,
331
- timestamp: 1,
332
- media: 1,
333
- nombre_whatsapp: 1,
334
- from_me: 1
335
- }},
336
- { $group: {
337
- _id: '$numero',
338
- latestMessage: { $first: '$$ROOT' },
339
- messageCount: { $sum: 1 }
340
- }},
341
- { $sort: { 'latestMessage.createdAt': -1 } },
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "3.3.2",
3
+ "version": "3.3.3",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",