@peopl-health/nexus 3.8.19 → 3.8.21
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.
|
@@ -2,6 +2,7 @@ const { Dashboard_ID } = require('../config/airtableConfig');
|
|
|
2
2
|
|
|
3
3
|
const { logger } = require('../utils/logger');
|
|
4
4
|
const { safeParse } = require('../utils/jsonUtils');
|
|
5
|
+
const { resolveMessage } = require('../utils/templateUtils');
|
|
5
6
|
|
|
6
7
|
const { getRecordByFilter } = require('../services/airtableService');
|
|
7
8
|
|
|
@@ -57,8 +58,12 @@ async function fetchDetailsFromAirtable(boxes) {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
function attachPreview(box, records) {
|
|
61
|
+
const { template, empty_message, ...rest } = box.metadata || {};
|
|
60
62
|
if (records.length > 0) {
|
|
61
|
-
box.metadata = { ...
|
|
63
|
+
box.metadata = { ...rest, preview: records[0] };
|
|
64
|
+
box.metadata.preview = resolveMessage(template, box);
|
|
65
|
+
} else {
|
|
66
|
+
box.metadata = { ...rest, preview: empty_message };
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
|
|
@@ -7,7 +7,7 @@ const { Message } = require('../models/messageModel');
|
|
|
7
7
|
|
|
8
8
|
const { handle24HourWindowError } = require('../helpers/templateRecoveryHelper');
|
|
9
9
|
|
|
10
|
-
const { addLinkedRecord } = require('../services/airtableService');
|
|
10
|
+
const { addLinkedRecord, updateRecordByFilter } = require('../services/airtableService');
|
|
11
11
|
|
|
12
12
|
let _eventBus = null;
|
|
13
13
|
|
|
@@ -34,23 +34,32 @@ async function updateMessageStatus(messageSid, status, errorCode = null, errorMe
|
|
|
34
34
|
logger.warn('[MessageStatus] Message not found', { messageSid });
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
if (updated
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
37
|
+
if (updated) {
|
|
38
|
+
if (status === 'failed' || status === 'undelivered') {
|
|
39
|
+
addLinkedRecord(
|
|
40
|
+
Monitoreo_ID,
|
|
41
|
+
'undelivered_messages',
|
|
42
|
+
{
|
|
43
|
+
whatsapp_id: updated.numero,
|
|
44
|
+
error_code: errorCode || null,
|
|
45
|
+
status,
|
|
46
|
+
date: updated.timestamp,
|
|
47
|
+
message_id: updated.message_id
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
referenceTable: 'message_monitor',
|
|
51
|
+
referenceFilter: `{whatsapp_id}="${updated.numero}"`,
|
|
52
|
+
linkFieldName: 'message_monitor'
|
|
53
|
+
}
|
|
54
|
+
).catch(err => logger.error('[MessageStatus] Failed to create undelivered_messages record', { messageSid, error: err.message }));
|
|
55
|
+
} else {
|
|
56
|
+
updateRecordByFilter(
|
|
57
|
+
Monitoreo_ID,
|
|
58
|
+
'undelivered_messages',
|
|
59
|
+
`{message_id}="${updated.message_id}"`,
|
|
60
|
+
{ status }
|
|
61
|
+
).catch(err => logger.error('[MessageStatus] Failed to update undelivered_messages record', { messageSid, error: err.message }));
|
|
62
|
+
}
|
|
54
63
|
}
|
|
55
64
|
|
|
56
65
|
return updated;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function resolvePath(obj, path) {
|
|
2
|
+
return path.split('.').reduce((acc, key) => acc?.[key], obj);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function resolveMessage(template, context) {
|
|
6
|
+
if (!template || typeof template !== 'string') return template;
|
|
7
|
+
return template.replace(/\{\{([^}]+)\}\}/g, (_, expr) => {
|
|
8
|
+
expr = expr.trim();
|
|
9
|
+
const arithMatch = expr.match(/^([\w.]+)\s*([-+*/])\s*(\d+)$/);
|
|
10
|
+
if (arithMatch) {
|
|
11
|
+
const val = Number(resolvePath(context, arithMatch[1]));
|
|
12
|
+
const n = Number(arithMatch[3]);
|
|
13
|
+
const ops = { '-': val - n, '+': val + n, '*': val * n, '/': val / n };
|
|
14
|
+
return ops[arithMatch[2]] ?? '';
|
|
15
|
+
}
|
|
16
|
+
const resolved = resolvePath(context, expr);
|
|
17
|
+
return resolved !== undefined && resolved !== null ? resolved : '';
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = { resolveMessage };
|