@peopl-health/nexus 3.3.1 → 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
+ };
@@ -1,11 +1,11 @@
1
- const { Interaction } = require('../models/interactionModel');
1
+ const { getInteraction } = require('../models/interactionModel');
2
2
  const { Message } = require('../models/messageModel');
3
3
  const { INTERACTION_QUALITY_VALUES } = require('../config/interactionConfig');
4
4
  const { addRecord, getRecordByFilter } = require('../services/airtableService');
5
5
  const { Logging_ID } = require('../config/airtableConfig');
6
6
  const { logger } = require('../utils/logger');
7
7
 
8
- async function logInteractionToAirtable(messageIds, whatsapp_id, voter_username, description) {
8
+ async function logInteractionToAirtable(messageIds, whatsapp_id, voter_username, description, type, medical_note) {
9
9
  try {
10
10
  const messageObjects = await Message.find({ _id: { $in: messageIds } }).sort({ createdAt: -1 });
11
11
 
@@ -29,7 +29,9 @@ async function logInteractionToAirtable(messageIds, whatsapp_id, voter_username,
29
29
  patient_id: patientId ? [patientId] : undefined,
30
30
  reporter: voter_username,
31
31
  conversation,
32
- description
32
+ description,
33
+ type: type && type.length ? type : undefined,
34
+ medical_note: medical_note || undefined
33
35
  };
34
36
 
35
37
  await addRecord(Logging_ID, 'interactions', airtableData);
@@ -41,7 +43,7 @@ async function logInteractionToAirtable(messageIds, whatsapp_id, voter_username,
41
43
 
42
44
  const addInteractionController = async (req, res) => {
43
45
  try {
44
- const { messages, whatsapp_id, voter_username, quality, description } = req.body;
46
+ const { messages, whatsapp_id, voter_username, quality, description, type, medical_note } = req.body;
45
47
  if (!messages || !Array.isArray(messages) || messages.length === 0) {
46
48
  return res.status(400).json({ success: false, error: 'Messages array is required and must not be empty' });
47
49
  }
@@ -50,9 +52,11 @@ const addInteractionController = async (req, res) => {
50
52
  if (!quality || !INTERACTION_QUALITY_VALUES.includes(quality)) {
51
53
  return res.status(400).json({ success: false, error: `Quality must be one of: ${INTERACTION_QUALITY_VALUES.join(', ')}` });
52
54
  }
53
- const interaction = await Interaction.create({ messages, whatsapp_id, voter_username, quality, description });
54
-
55
- logInteractionToAirtable(messages, whatsapp_id, voter_username, description).catch(err =>
55
+ const typeArray = Array.isArray(type) ? type : type ? [type] : undefined;
56
+ const Interaction = getInteraction();
57
+ const interaction = await Interaction.create({ messages, whatsapp_id, voter_username, quality, description, type: typeArray, medical_note });
58
+
59
+ logInteractionToAirtable(messages, whatsapp_id, voter_username, description, typeArray, medical_note).catch(err =>
56
60
  logger.error('Background Airtable logging failed:', err)
57
61
  );
58
62
 
@@ -66,6 +70,7 @@ const addInteractionController = async (req, res) => {
66
70
  const getInteractionsByWhatsappIdController = async (req, res) => {
67
71
  try {
68
72
  const { whatsapp_id } = req.params;
73
+ const Interaction = getInteraction();
69
74
  const interactions = await Interaction.find({ whatsapp_id }).populate('messages').sort({ createdAt: -1 });
70
75
  res.status(200).json({ success: true, whatsappId: whatsapp_id, count: interactions.length, interactions });
71
76
  } catch (error) {
@@ -1,4 +1,4 @@
1
- const { QualityMessage } = require('../models/qualityMessageModel');
1
+ const { getQualityMessage } = require('../models/qualityMessageModel');
2
2
  const { Message } = require('../models/messageModel');
3
3
  const { logger } = require('../utils/logger');
4
4
 
@@ -15,7 +15,7 @@ const addQualityVoteController = async (req, res) => {
15
15
  const message = await Message.findById(message_id);
16
16
  if (!message) return res.status(404).json({ success: false, error: 'Message not found' });
17
17
 
18
- const qualityVote = await QualityMessage.findOneAndUpdate(
18
+ const qualityVote = await getQualityMessage().findOneAndUpdate(
19
19
  { message_id, voter_username },
20
20
  { quality, notes, context },
21
21
  { upsert: true, new: true }
@@ -31,7 +31,7 @@ const addQualityVoteController = async (req, res) => {
31
31
  const getQualityVotesByMessageController = async (req, res) => {
32
32
  try {
33
33
  const { message_id } = req.params;
34
- const votes = await QualityMessage.find({ message_id }).sort({ createdAt: -1 });
34
+ const votes = await getQualityMessage().find({ message_id }).sort({ createdAt: -1 });
35
35
 
36
36
  const summary = {
37
37
  total: votes.length,
@@ -50,7 +50,7 @@ const getQualityVotesByMessageController = async (req, res) => {
50
50
  const getQualityVotesByVoterController = async (req, res) => {
51
51
  try {
52
52
  const { voter_username } = req.params;
53
- const votes = await QualityMessage.find({ voter_username }).populate('message_id').sort({ createdAt: -1 });
53
+ const votes = await getQualityMessage().find({ voter_username }).populate('message_id').sort({ createdAt: -1 });
54
54
  res.status(200).json({ success: true, voterUsername: voter_username, count: votes.length, votes });
55
55
  } catch (error) {
56
56
  logger.error('Error fetching voter quality votes:', error);
@@ -61,7 +61,7 @@ const getQualityVotesByVoterController = async (req, res) => {
61
61
  const getQualityVoteByMessageAndVoterController = async (req, res) => {
62
62
  try {
63
63
  const { message_id, voter_username } = req.params;
64
- const vote = await QualityMessage.findOne({ message_id, voter_username }).populate('message_id');
64
+ const vote = await getQualityMessage().findOne({ message_id, voter_username }).populate('message_id');
65
65
 
66
66
  if (!vote) {
67
67
  return res.status(404).json({ success: false, error: 'Vote not found' });
@@ -1,13 +1,15 @@
1
1
  const { Message, getMessageValues, formatTimestamp } = require('./messageModel');
2
2
  const { Thread } = require('./threadModel');
3
- const { Interaction } = require('./interactionModel');
4
- const { QualityMessage } = require('./qualityMessageModel');
3
+ const { getInteraction } = require('./interactionModel');
4
+ const { getQualityMessage } = require('./qualityMessageModel');
5
+ const { getPredictionMetrics } = require('./predictionMetricsModel');
5
6
 
6
7
  module.exports = {
7
8
  Message,
8
9
  Thread,
9
- Interaction,
10
- QualityMessage,
10
+ getInteraction,
11
+ getQualityMessage,
12
+ getPredictionMetrics,
11
13
  getMessageValues,
12
14
  formatTimestamp
13
15
  };
@@ -13,6 +13,8 @@ const interactionSchema = new mongoose.Schema({
13
13
  required: true
14
14
  },
15
15
  description: { type: String, default: null },
16
+ medical_note: { type: String, default: null },
17
+ type: { type: [String], default: [] },
16
18
  source: { type: String, default: () => process.env.USER_DB_MONGO }
17
19
  }, { timestamps: true });
18
20
 
@@ -23,4 +25,4 @@ const getInteraction = () => {
23
25
  return db.models.Interaction || db.model('Interaction', interactionSchema);
24
26
  };
25
27
 
26
- module.exports = { getInteraction, interactionSchema, get Interaction() { return getInteraction(); } };
28
+ module.exports = { getInteraction, interactionSchema };
@@ -26,4 +26,4 @@ const getPredictionMetrics = () => {
26
26
  return db.models.PredictionMetrics || db.model('PredictionMetrics', predictionMetricsSchema);
27
27
  };
28
28
 
29
- module.exports = { getPredictionMetrics, predictionMetricsSchema, get PredictionMetrics() { return getPredictionMetrics(); } };
29
+ module.exports = { getPredictionMetrics, predictionMetricsSchema };
@@ -24,4 +24,4 @@ const getQualityMessage = () => {
24
24
  return db.models.QualityMessage || db.model('QualityMessage', qualityMessageSchema);
25
25
  };
26
26
 
27
- module.exports = { getQualityMessage, qualityMessageSchema, get QualityMessage() { return getQualityMessage(); } };
27
+ module.exports = { getQualityMessage, qualityMessageSchema };
@@ -3,7 +3,7 @@ const runtimeConfig = require('../config/runtimeConfig');
3
3
  const { createProvider } = require('../providers/createProvider');
4
4
 
5
5
  const { Thread } = require('../models/threadModel.js');
6
- const { PredictionMetrics } = require('../models/predictionMetricsModel');
6
+ const { getPredictionMetrics } = require('../models/predictionMetricsModel');
7
7
  const { insertMessage } = require('../models/messageModel');
8
8
  const { Historial_Clinico_ID } = require('../config/airtableConfig');
9
9
 
@@ -267,7 +267,7 @@ const replyAssistantCore = async (code, message_ = null, thread_ = null, runOpti
267
267
  has_breakdown: !!timings.process_messages_breakdown
268
268
  });
269
269
 
270
- await PredictionMetrics.create({
270
+ await getPredictionMetrics().create({
271
271
  message_id: `${code}-${Date.now()}`,
272
272
  numero: code,
273
273
  assistant_id: finalThread.getAssistantId(),
@@ -2,7 +2,6 @@ const { connect: mongoConnect, disconnect: mongoDisconnect, getDb } = require('.
2
2
  const runtimeConfig = require('../config/runtimeConfig');
3
3
 
4
4
  const { Message, insertMessage } = require('../models/messageModel');
5
- const { Interaction } = require('../models/interactionModel');
6
5
  const { Thread } = require('../models/threadModel');
7
6
 
8
7
  const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
@@ -18,7 +17,6 @@ class MongoStorage {
18
17
  this.dbName = config.dbName;
19
18
  this.collections = config.collections || {
20
19
  messages: 'messages',
21
- interactions: 'interactions',
22
20
  threads: 'threads'
23
21
  };
24
22
  this.schemas = this.createSchemas();
@@ -27,7 +25,6 @@ class MongoStorage {
27
25
  createSchemas() {
28
26
  return {
29
27
  Message,
30
- Interaction,
31
28
  Thread
32
29
  };
33
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "3.3.1",
3
+ "version": "3.3.3",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",