@peopl-health/nexus 3.2.12 → 3.3.1-fix

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.
@@ -0,0 +1,61 @@
1
+ const mongoose = require('mongoose');
2
+ const { logger } = require('../utils/logger');
3
+
4
+ let connection = null;
5
+ const databases = new Map();
6
+ const modelDatabases = {};
7
+
8
+ function setModelDatabase(modelName, dbName) {
9
+ modelDatabases[modelName] = dbName;
10
+ }
11
+
12
+ function setModelDatabases(mapping) {
13
+ Object.assign(modelDatabases, mapping);
14
+ logger.info('[mongoConfig] Model databases configured', { mapping });
15
+ }
16
+
17
+ function getModelDatabase(modelName) {
18
+ return modelDatabases[modelName] || null;
19
+ }
20
+
21
+ async function connect(uri = process.env.MONGODB_URI) {
22
+ if (connection || mongoose.connection.readyState === 1) {
23
+ connection = mongoose.connection;
24
+ return connection;
25
+ }
26
+ if (!uri) throw new Error('MongoDB URI required. Set MONGODB_URI or pass uri parameter.');
27
+ await mongoose.connect(uri);
28
+ connection = mongoose.connection;
29
+ logger.info('MongoDB connected');
30
+ return connection;
31
+ }
32
+
33
+ function getDb(dbName) {
34
+ if (!connection && mongoose.connection.readyState !== 1) throw new Error('MongoDB not connected');
35
+ connection = connection || mongoose.connection;
36
+ if (!dbName) return connection;
37
+ if (!databases.has(dbName)) {
38
+ databases.set(dbName, connection.useDb(dbName, { useCache: true }));
39
+ }
40
+ return databases.get(dbName);
41
+ }
42
+
43
+ function getConnection() {
44
+ return connection || mongoose.connection;
45
+ }
46
+
47
+ async function disconnect() {
48
+ if (mongoose.connection.readyState !== 0) await mongoose.disconnect();
49
+ databases.clear();
50
+ connection = null;
51
+ }
52
+
53
+ module.exports = {
54
+ connect,
55
+ disconnect,
56
+ getDb,
57
+ getConnection,
58
+ setModelDatabase,
59
+ setModelDatabases,
60
+ getModelDatabase
61
+ };
@@ -1,4 +1,4 @@
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');
@@ -50,7 +50,7 @@ const addInteractionController = async (req, res) => {
50
50
  if (!quality || !INTERACTION_QUALITY_VALUES.includes(quality)) {
51
51
  return res.status(400).json({ success: false, error: `Quality must be one of: ${INTERACTION_QUALITY_VALUES.join(', ')}` });
52
52
  }
53
- const interaction = await Interaction.create({ messages, whatsapp_id, voter_username, quality, description });
53
+ const interaction = await getInteraction().create({ messages, whatsapp_id, voter_username, quality, description });
54
54
 
55
55
  logInteractionToAirtable(messages, whatsapp_id, voter_username, description).catch(err =>
56
56
  logger.error('Background Airtable logging failed:', err)
@@ -66,7 +66,7 @@ const addInteractionController = async (req, res) => {
66
66
  const getInteractionsByWhatsappIdController = async (req, res) => {
67
67
  try {
68
68
  const { whatsapp_id } = req.params;
69
- const interactions = await Interaction.find({ whatsapp_id }).populate('messages').sort({ createdAt: -1 });
69
+ const interactions = await getInteraction().find({ whatsapp_id }).populate('messages').sort({ createdAt: -1 });
70
70
  res.status(200).json({ success: true, whatsappId: whatsapp_id, count: interactions.length, interactions });
71
71
  } catch (error) {
72
72
  logger.error('Error fetching interactions:', 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' });
@@ -8,7 +8,7 @@ const { hasPreprocessingHandler, invokePreprocessingHandler } = require('../serv
8
8
  const { ensureThreadExists } = require('../helpers/threadHelper');
9
9
  const { logger } = require('../utils/logger');
10
10
 
11
- const mongoose = require('mongoose');
11
+ const { connect } = require('../config/mongoConfig');
12
12
  const OpenAI = require('openai');
13
13
  const EventEmitter = require('events');
14
14
 
@@ -60,13 +60,8 @@ class NexusMessaging {
60
60
  * @param {string} mongoUri - MongoDB connection string
61
61
  */
62
62
  async initializeMongoDB(mongoUri = process.env.MONGODB_URI) {
63
- if (!mongoUri) {
64
- throw new Error('MongoDB URI not provided. Please set MONGODB_URI environment variable or pass mongoUri parameter.');
65
- }
66
-
67
63
  try {
68
- await mongoose.connect(mongoUri);
69
- this.mongodb = mongoose.connection;
64
+ this.mongodb = await connect(mongoUri);
70
65
  logger.info('MongoDB connected successfully');
71
66
  } catch (error) {
72
67
  logger.error('MongoDB connection failed', { error: error.message });
package/lib/index.js CHANGED
@@ -28,6 +28,7 @@ const {
28
28
  getRequestId
29
29
  } = require('./middleware/requestId');
30
30
  const { logger } = require('./utils/logger');
31
+ const { setModelDatabases, setModelDatabase, getModelDatabase } = require('./config/mongoConfig');
31
32
 
32
33
  /**
33
34
  * Main Nexus class that orchestrates all components
@@ -347,5 +348,9 @@ module.exports = {
347
348
  // Request tracing
348
349
  requestIdMiddleware,
349
350
  getRequestId,
350
- logger
351
+ logger,
352
+ // MongoDB config
353
+ setModelDatabases,
354
+ setModelDatabase,
355
+ getModelDatabase
351
356
  };
@@ -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
  };
@@ -1,5 +1,7 @@
1
1
  const mongoose = require('mongoose');
2
2
  const { INTERACTION_QUALITY_VALUES } = require('../config/interactionConfig');
3
+ const { getDb, getModelDatabase } = require('../config/mongoConfig');
4
+ const { logger } = require('../utils/logger');
3
5
 
4
6
  const interactionSchema = new mongoose.Schema({
5
7
  messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message', required: true }],
@@ -10,9 +12,15 @@ const interactionSchema = new mongoose.Schema({
10
12
  enum: INTERACTION_QUALITY_VALUES,
11
13
  required: true
12
14
  },
13
- description: { type: String, default: null }
15
+ description: { type: String, default: null },
16
+ source: { type: String, default: () => process.env.USER_DB_MONGO }
14
17
  }, { timestamps: true });
15
18
 
16
- const Interaction = mongoose.model('Interaction', interactionSchema);
19
+ const getInteraction = () => {
20
+ const dbName = getModelDatabase('Interaction');
21
+ const db = dbName ? getDb(dbName) : mongoose;
22
+ logger.debug('[Interaction] Using database', { dbName: dbName || 'default' });
23
+ return db.models.Interaction || db.model('Interaction', interactionSchema);
24
+ };
17
25
 
18
- module.exports = { Interaction, interactionSchema };
26
+ module.exports = { getInteraction, interactionSchema };
@@ -1,4 +1,6 @@
1
1
  const mongoose = require('mongoose');
2
+ const { getDb, getModelDatabase } = require('../config/mongoConfig');
3
+ const { logger } = require('../utils/logger');
2
4
 
3
5
  const predictionMetricsSchema = new mongoose.Schema({
4
6
  message_id: { type: String, required: true, index: true },
@@ -9,13 +11,19 @@ const predictionMetricsSchema = new mongoose.Schema({
9
11
  retry_count: { type: Number, default: 1 },
10
12
  completed: { type: Boolean, default: true },
11
13
  error: { type: String, default: null },
12
- timing_breakdown: { type: Object, default: {} }
14
+ timing_breakdown: { type: Object, default: {} },
15
+ source: { type: String, default: () => process.env.USER_DB_MONGO }
13
16
  }, { timestamps: true });
14
17
 
15
18
  predictionMetricsSchema.index({ createdAt: -1 });
16
19
  predictionMetricsSchema.index({ assistant_id: 1, createdAt: -1 });
17
20
  predictionMetricsSchema.index({ numero: 1, createdAt: -1 });
18
21
 
19
- const PredictionMetrics = mongoose.model('PredictionMetrics', predictionMetricsSchema);
22
+ const getPredictionMetrics = () => {
23
+ const dbName = getModelDatabase('PredictionMetrics');
24
+ const db = dbName ? getDb(dbName) : mongoose;
25
+ logger.debug('[PredictionMetrics] Using database', { dbName: dbName || 'default' });
26
+ return db.models.PredictionMetrics || db.model('PredictionMetrics', predictionMetricsSchema);
27
+ };
20
28
 
21
- module.exports = { PredictionMetrics, predictionMetricsSchema };
29
+ module.exports = { getPredictionMetrics, predictionMetricsSchema };
@@ -1,4 +1,6 @@
1
1
  const mongoose = require('mongoose');
2
+ const { getDb, getModelDatabase } = require('../config/mongoConfig');
3
+ const { logger } = require('../utils/logger');
2
4
 
3
5
  const qualityMessageSchema = new mongoose.Schema({
4
6
  message_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Message', required: true, index: true },
@@ -9,11 +11,17 @@ const qualityMessageSchema = new mongoose.Schema({
9
11
  required: true
10
12
  },
11
13
  notes: { type: String, default: null },
12
- context: { type: String, default: null }
14
+ context: { type: String, default: null },
15
+ source: { type: String, default: () => process.env.USER_DB_MONGO }
13
16
  }, { timestamps: true });
14
17
 
15
18
  qualityMessageSchema.index({ message_id: 1, voter_username: 1 }, { unique: true });
16
19
 
17
- const QualityMessage = mongoose.model('QualityMessage', qualityMessageSchema);
20
+ const getQualityMessage = () => {
21
+ const dbName = getModelDatabase('QualityMessage');
22
+ const db = dbName ? getDb(dbName) : mongoose;
23
+ logger.debug('[QualityMessage] Using database', { dbName: dbName || 'default' });
24
+ return db.models.QualityMessage || db.model('QualityMessage', qualityMessageSchema);
25
+ };
18
26
 
19
- module.exports = { QualityMessage, 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 { 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(),
@@ -1,8 +1,7 @@
1
- const mongoose = require('mongoose');
1
+ const { connect: mongoConnect, disconnect: mongoDisconnect, getDb } = require('../config/mongoConfig');
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,21 +25,13 @@ class MongoStorage {
27
25
  createSchemas() {
28
26
  return {
29
27
  Message,
30
- Interaction,
31
28
  Thread
32
29
  };
33
30
  }
34
31
 
35
32
  async connect() {
36
- try {
37
- await mongoose.connect(this.mongoUri, {
38
- useNewUrlParser: true,
39
- useUnifiedTopology: true
40
- });
41
- logger.info('MongoDB connected successfully');
42
- } catch (error) {
43
- throw new Error(`MongoDB connection failed: ${error.message}`);
44
- }
33
+ await mongoConnect(this.mongoUri);
34
+ if (this.dbName) this.db = getDb(this.dbName);
45
35
  }
46
36
 
47
37
  async saveMessage(messageData) {
@@ -222,10 +212,7 @@ class MongoStorage {
222
212
  }
223
213
 
224
214
  async disconnect() {
225
- await mongoose.disconnect();
226
- if (this.client) {
227
- await this.client.close();
228
- }
215
+ await mongoDisconnect();
229
216
  }
230
217
  }
231
218
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "3.2.12",
3
+ "version": "3.3.1-fix",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",