@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.
- package/lib/controllers/conversationController.js +42 -25
- package/lib/controllers/interactionController.js +12 -7
- package/lib/controllers/qualityMessageController.js +5 -5
- package/lib/models/index.js +6 -4
- package/lib/models/interactionModel.js +3 -1
- package/lib/models/predictionMetricsModel.js +1 -1
- package/lib/models/qualityMessageModel.js +1 -1
- package/lib/services/assistantServiceCore.js +2 -2
- package/lib/storage/MongoStorage.js +0 -3
- package/package.json +1 -1
|
@@ -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
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
const {
|
|
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
|
|
54
|
-
|
|
55
|
-
|
|
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 {
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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' });
|
package/lib/models/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
const { Message, getMessageValues, formatTimestamp } = require('./messageModel');
|
|
2
2
|
const { Thread } = require('./threadModel');
|
|
3
|
-
const {
|
|
4
|
-
const {
|
|
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
|
-
|
|
10
|
-
|
|
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
|
|
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
|
|
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
|
|
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 {
|
|
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
|
|
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
|
}
|