@peopl-health/nexus 6.0.16 → 6.0.17

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.
@@ -119,6 +119,16 @@ messageSchema.index({ triggeredBy: 1, createdAt: -1 }, { name: 'triggered_by_idx
119
119
 
120
120
  const Message = mongoose.model('Message', messageSchema);
121
121
 
122
+ const INTERNAL_ORIGINS = new Set(['system', 'instruction']);
123
+
124
+ // A stored text message carries a media object of schema defaults, so only a populated one counts as media.
125
+ const threadPreview = (message, at) => ({
126
+ lastMessageAt: at,
127
+ lastMessageBody: message.plainBody || '',
128
+ lastMessageFromMe: !!message.from_me,
129
+ lastMessageMedia: message.media && Object.values(message.media).some((value) => value != null) ? message.media : null,
130
+ });
131
+
122
132
  function getMessageValues(message, content) {
123
133
  return {
124
134
  nombre_whatsapp: message.pushName,
@@ -132,5 +142,7 @@ function getMessageValues(message, content) {
132
142
 
133
143
  module.exports = {
134
144
  Message,
145
+ INTERNAL_ORIGINS,
146
+ threadPreview,
135
147
  getMessageValues,
136
148
  };
@@ -2,15 +2,13 @@ const { Monitoreo_ID } = require('../config/airtableConfig');
2
2
 
3
3
  const { logger } = require('../utils/logger');
4
4
 
5
- const { Message } = require('../models/messageModel');
5
+ const { Message, INTERNAL_ORIGINS, threadPreview } = require('../models/messageModel');
6
6
  const { Thread } = require('../models/threadModel');
7
7
 
8
8
  const { getClinicalContext } = require('../helpers/patientInformationHelper');
9
9
 
10
10
  const { updateRecordByFilter } = require('../services/airtableService');
11
11
 
12
- const INTERNAL_ORIGINS = new Set(['system', 'instruction']);
13
-
14
12
  async function getMessages(query, options = {}) {
15
13
  let cursor = Message.find(query);
16
14
  if (options.select) cursor = cursor.select(options.select);
@@ -61,12 +59,7 @@ async function insertMessage(values) {
61
59
  Thread.findOneAndUpdate(
62
60
  { code: values.numero },
63
61
  {
64
- $set: {
65
- lastMessageAt: new Date(),
66
- lastMessageBody: values.plainBody || '',
67
- lastMessageFromMe: !!values.from_me,
68
- lastMessageMedia: values.media || null
69
- },
62
+ $set: threadPreview(values, new Date()),
70
63
  $inc: {
71
64
  messageCount: 1,
72
65
  ...(!values.from_me ? { unreadCount: 1 } : {})
@@ -2,7 +2,7 @@ const { logger } = require('../utils/logger');
2
2
  const { normalize } = require('../utils/tokenMapUtils');
3
3
  const { mapStringsDeep } = require('../utils/sanitizerUtils');
4
4
 
5
- const { Message } = require('../models/messageModel');
5
+ const { Message, INTERNAL_ORIGINS, threadPreview } = require('../models/messageModel');
6
6
  const { Thread } = require('../models/threadModel');
7
7
  const { getTokenMap } = require('../models/tokenMapModel');
8
8
  const { ScheduledMessage } = require('../models/agendaMessageModel');
@@ -187,6 +187,18 @@ async function moveThreads(source, target, state, migrationId) {
187
187
  await Thread.bulkWrite(rewriteOps(toArchive, (doc) => ({ code: `archived:${source}:${doc._id}`, stopped: true })));
188
188
  }
189
189
  logger.debug('[migratePatientCode] Threads moved', { migrationId, renamed, archived: toArchive.length });
190
+ return renamed;
191
+ }
192
+
193
+ async function countMovedMessages(target, messages) {
194
+ const counted = messages.filter((doc) => !INTERNAL_ORIGINS.has(doc.origin));
195
+ if (!counted.length) return;
196
+ const latest = await Message.findOne({ _id: { $in: counted.map((doc) => doc._id) } }, null, { sort: { createdAt: -1 } }).lean();
197
+ await Thread.updateOne({ code: target }, { $inc: { messageCount: counted.length } });
198
+ await Thread.updateOne(
199
+ { code: target, $or: [{ lastMessageAt: null }, { lastMessageAt: { $lt: latest.createdAt } }] },
200
+ { $set: threadPreview(latest, latest.createdAt) },
201
+ );
190
202
  }
191
203
 
192
204
  async function migratePatientCode({ from, to, asCaregiver = false, apply = false, operator = null } = {}) {
@@ -238,7 +250,8 @@ async function migratePatientCode({ from, to, asCaregiver = false, apply = false
238
250
  await getPatientMemory().bulkWrite(rewriteOps(state.memories, (doc) => ({ ...retokenizedFields(doc, mapping), numero: target })));
239
251
  }
240
252
 
241
- await moveThreads(source, target, state, migrationId);
253
+ const renamed = await moveThreads(source, target, state, migrationId);
254
+ if (!renamed) await countMovedMessages(target, state.messages);
242
255
 
243
256
  if (state.scheduled.length && !asCaregiver) {
244
257
  await ScheduledMessage.bulkWrite(rewriteOps(state.scheduled, (doc) => ({ code: sameFormat(doc.code, target) })));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.0.16",
3
+ "version": "6.0.17",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",