@peopl-health/nexus 3.16.0 → 3.16.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.
|
@@ -15,6 +15,7 @@ const { ScheduledMessage } = require('../models/agendaMessageModel');
|
|
|
15
15
|
|
|
16
16
|
const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
|
|
17
17
|
const { uploadMediaToS3, getFileExtension } = require('../helpers/mediaHelper');
|
|
18
|
+
const { recordDeliveryAttempt } = require('../helpers/deliveryAttemptHelper');
|
|
18
19
|
|
|
19
20
|
const { MessageProvider } = require('../adapters/MessageProvider');
|
|
20
21
|
const { TwilioMessageAdapter } = require('./messageAdapters/TwilioMessageAdapter');
|
|
@@ -140,18 +141,21 @@ class TwilioProvider extends MessageProvider {
|
|
|
140
141
|
};
|
|
141
142
|
|
|
142
143
|
let result;
|
|
144
|
+
const kind = messageParams.contentSid ? 'template' : 'freeform';
|
|
143
145
|
const chunks = messageParams.body?.length > 1600 && !messageParams.mediaUrl && !messageParams.contentSid
|
|
144
146
|
? this._splitMessageAtWordBoundaries(messageParams.body) : null;
|
|
145
|
-
|
|
147
|
+
|
|
146
148
|
if (chunks) {
|
|
147
149
|
for (let i = 0; i < chunks.length; i++) {
|
|
148
150
|
result = await this.twilioClient.messages.create({ ...messageParams, body: chunks[i] });
|
|
149
151
|
await saveMessage(chunks[i], result);
|
|
152
|
+
await recordDeliveryAttempt({ messageData, twilioResult: result, kind });
|
|
150
153
|
if (i < chunks.length - 1) await new Promise(r => setTimeout(r, 100));
|
|
151
154
|
}
|
|
152
155
|
} else {
|
|
153
156
|
result = await this.twilioClient.messages.create(messageParams);
|
|
154
157
|
await saveMessage(messageParams.body || messageData.body, result);
|
|
158
|
+
await recordDeliveryAttempt({ messageData, twilioResult: result, kind });
|
|
155
159
|
}
|
|
156
160
|
|
|
157
161
|
return {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const { logger } = require('../utils/logger');
|
|
2
|
+
|
|
3
|
+
const { Message } = require('../models/messageModel');
|
|
4
|
+
const { DeliveryAttempt } = require('../models/deliveryAttemptModel');
|
|
5
|
+
|
|
6
|
+
async function recordDeliveryAttempt({ messageData, twilioResult, kind }) {
|
|
7
|
+
if (messageData?._skipStorage) return null;
|
|
8
|
+
if (!twilioResult?.sid) return null;
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const msgDoc = await Message.findOne({ message_id: twilioResult.sid }, '_id').lean();
|
|
12
|
+
if (!msgDoc) {
|
|
13
|
+
logger.warn('[deliveryAttemptHelper] Message not found for SID; skipping attempt record', {
|
|
14
|
+
twilioSid: twilioResult.sid
|
|
15
|
+
});
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const status = twilioResult.status?.toLowerCase() || 'sent';
|
|
20
|
+
|
|
21
|
+
const attempt = await DeliveryAttempt.create({
|
|
22
|
+
messageId: msgDoc._id,
|
|
23
|
+
kind,
|
|
24
|
+
twilioSid: twilioResult.sid,
|
|
25
|
+
contentSid: messageData?.contentSid || null,
|
|
26
|
+
status,
|
|
27
|
+
completedAt: new Date()
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
await Message.updateOne(
|
|
31
|
+
{ _id: msgDoc._id },
|
|
32
|
+
{ $set: { 'statusInfo.latestDeliveryStatus': status } }
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return attempt;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
logger.error('[deliveryAttemptHelper] Failed to record delivery attempt', {
|
|
38
|
+
twilioSid: twilioResult?.sid,
|
|
39
|
+
kind,
|
|
40
|
+
error: error.message
|
|
41
|
+
});
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = { recordDeliveryAttempt };
|
|
@@ -13,6 +13,7 @@ async function updateMessageStatus(messageSid, status, errorCode = null, errorMe
|
|
|
13
13
|
try {
|
|
14
14
|
const updateData = {
|
|
15
15
|
'statusInfo.status': status,
|
|
16
|
+
'statusInfo.latestDeliveryStatus': status,
|
|
16
17
|
'statusInfo.updatedAt': new Date(),
|
|
17
18
|
...(errorCode && { 'statusInfo.errorCode': errorCode }),
|
|
18
19
|
...(errorMessage && { 'statusInfo.errorMessage': errorMessage })
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
|
|
3
|
-
const KIND_VALUES = ['freeform', 'recovery_template'];
|
|
3
|
+
const KIND_VALUES = ['freeform', 'template', 'recovery_template'];
|
|
4
4
|
const STATUS_VALUES = [null, 'queued', 'sending', 'sent', 'delivered', 'undelivered', 'failed', 'read'];
|
|
5
5
|
|
|
6
6
|
const deliveryAttemptSchema = new mongoose.Schema({
|
|
@@ -112,6 +112,7 @@ messageSchema.index({ nombre_whatsapp: 1, createdAt: -1 }, { name: 'nombre_whats
|
|
|
112
112
|
messageSchema.index({ createdAt: -1 }, { name: 'global_sort_idx' });
|
|
113
113
|
|
|
114
114
|
messageSchema.index({ 'statusInfo.recoveryMessageId': 1 }, { name: 'recovery_message_id_idx', sparse: true });
|
|
115
|
+
messageSchema.index({ message_id: 1 }, { name: 'message_id_idx', sparse: true });
|
|
115
116
|
|
|
116
117
|
messageSchema.index({ triggeredBy: 1, createdAt: -1 }, { name: 'triggered_by_idx', sparse: true });
|
|
117
118
|
|