@peopl-health/nexus 1.4.0 → 1.4.2

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.
@@ -313,12 +313,13 @@ const sendBulkMessageAirtableController = async (req, res) => {
313
313
 
314
314
  const getLastInteractionController = async (req, res) => {
315
315
  const { code } = req.query;
316
+ const normalizedCode = normalizeCode(code);
316
317
 
317
318
  try {
318
319
  const lastMessage = await Message.findOne({
319
320
  $or: [
320
- { numero: code },
321
- { group_id: code }
321
+ { numero: normalizedCode },
322
+ { group_id: normalizedCode }
322
323
  ]
323
324
  }).sort({ createdAt: -1 }).exec();
324
325
 
@@ -1,6 +1,7 @@
1
1
  const mongoose = require('mongoose');
2
2
  const moment = require('moment-timezone');
3
3
 
4
+
4
5
  const messageSchema = new mongoose.Schema({
5
6
  nombre_whatsapp: { type: String, required: true },
6
7
  numero: { type: String, required: true },
@@ -51,13 +52,10 @@ messageSchema.pre('save', function (next) {
51
52
 
52
53
  const Message = mongoose.model('Message', messageSchema);
53
54
 
55
+
54
56
  async function insertMessage(values) {
55
57
  try {
56
- const skipNumbers = [
57
- '5215592261426@s.whatsapp.net',
58
- '5215547411345@s.whatsapp.net',
59
- '51985959446@s.whatsapp.net'
60
- ];
58
+ const skipNumbers = ['5215592261426@s.whatsapp.net', '5215547411345@s.whatsapp.net', '51985959446@s.whatsapp.net'];
61
59
  const messageData = {
62
60
  nombre_whatsapp: values.nombre_whatsapp,
63
61
  numero: values.numero,
@@ -70,9 +68,7 @@ async function insertMessage(values) {
70
68
  reply_id: values.reply_id,
71
69
  from_me: values.from_me,
72
70
  processed: skipNumbers.includes(values.numero),
73
- media: values.media ? values.media : null,
74
- content_sid: values.content_sid || null,
75
- template_variables: values.template_variables || null
71
+ media: values.media ? values.media : null
76
72
  };
77
73
 
78
74
  await Message.findOneAndUpdate(
@@ -88,14 +84,16 @@ async function insertMessage(values) {
88
84
  }
89
85
  }
90
86
 
87
+
91
88
  function formatTimestamp(unixTimestamp) {
92
89
  const date = new Date(unixTimestamp * 1000);
93
90
  return date.toLocaleString('sv-MX', {
94
91
  timeZone: 'America/Mexico_City',
95
92
  hour12: false,
96
- }).replace(' ', 'T').slice(0, 19);
93
+ }).replace(' ', 'T').slice(0, 19);
97
94
  }
98
95
 
96
+
99
97
  function getMessageValues(message, content, reply, is_media) {
100
98
  const nombre_whatsapp = message.pushName;
101
99
  const numero = message.key.participant || message.key.remoteJid;
@@ -124,7 +122,7 @@ function getMessageValues(message, content, reply, is_media) {
124
122
  async function getContactDisplayName(contactNumber) {
125
123
  try {
126
124
  const latestMessage = await Message.findOne({ numero: contactNumber })
127
- .sort({ timestamp: -1 })
125
+ .sort({ createdAt: -1 })
128
126
  .select('nombre_whatsapp');
129
127
 
130
128
  if (latestMessage && latestMessage.nombre_whatsapp && latestMessage.nombre_whatsapp.trim() !== '') {
@@ -144,4 +142,4 @@ module.exports = {
144
142
  getMessageValues,
145
143
  formatTimestamp,
146
144
  getContactDisplayName
147
- };
145
+ };
@@ -58,7 +58,7 @@ const fetchConversationData = async (filter, skip, limit) => {
58
58
  }},
59
59
  { $group: {
60
60
  _id: '$numero',
61
- latestMessage: { $first: '$$ROOT' },
61
+ latestMessage: { $last: '$$ROOT' },
62
62
  messageCount: { $sum: 1 }
63
63
  }},
64
64
  { $sort: { 'latestMessage.createdAt': -1 } }
@@ -71,9 +71,33 @@ class MongoStorage {
71
71
  }
72
72
  }
73
73
 
74
+ normalizeNumero(numero) {
75
+ if (!numero || typeof numero !== 'string') return numero;
76
+
77
+ const trimmed = numero.trim();
78
+ if (trimmed.startsWith('whatsapp:')) {
79
+ return trimmed;
80
+ }
81
+
82
+ if (trimmed.includes('@')) {
83
+ return trimmed;
84
+ }
85
+
86
+ if (trimmed.startsWith('+')) {
87
+ return `whatsapp:${trimmed}`;
88
+ }
89
+
90
+ if (/^\d+$/.test(trimmed)) {
91
+ return `whatsapp:+${trimmed}`;
92
+ }
93
+
94
+ return trimmed;
95
+ }
96
+
74
97
  buildLegacyMessageValues(messageData = {}) {
75
98
  const numero = messageData.to || messageData.code || messageData.numero || messageData.from;
76
- const normalizedNumero = typeof numero === 'string' ? numero : '';
99
+ const rawNumero = typeof numero === 'string' ? numero : '';
100
+ const normalizedNumero = this.normalizeNumero(rawNumero);
77
101
  const isGroup = normalizedNumero.includes('@g.us');
78
102
  const isMedia = messageData.isMedia === true || (messageData.fileType && messageData.fileType !== 'text');
79
103
  const now = new Date();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "publishConfig": {
6
6
  "access": "public"