@peopl-health/nexus 5.54.0-dev.7969 → 5.54.0-dev.7970

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.
@@ -14,6 +14,7 @@ const SAFETY_NET_SOURCE = 'paused_safety_net';
14
14
  const PATIENT_FACING_ORIGINS = ['whatsapp_platform', 'assistant'];
15
15
  const HUMAN_ORIGINS = ['whatsapp_platform'];
16
16
  const UNDELIVERED_STATUSES = ['failed', 'undelivered'];
17
+ const RECEIVED_STATUSES = ['delivered', 'read'];
17
18
 
18
19
  const anyReplyFilter = (code, since) => ({
19
20
  numero: code,
@@ -30,6 +31,14 @@ const humanReplyFilter = (code, since) => ({
30
31
  origin: { $in: HUMAN_ORIGINS }
31
32
  });
32
33
 
34
+ const settledHumanReplyFilter = (code, since, unconfirmedBefore) => ({
35
+ ...humanReplyFilter(code, since),
36
+ $or: [
37
+ { 'statusInfo.status': { $in: RECEIVED_STATUSES } },
38
+ { createdAt: { $lte: unconfirmedBefore } }
39
+ ]
40
+ });
41
+
33
42
  const CHOICE_LABEL_KEYS = {
34
43
  callback: 'PAUSED_SAFETY_NET_CALLBACK_LABEL'
35
44
  };
@@ -60,6 +69,7 @@ module.exports = {
60
69
  SAFETY_NET_SOURCE,
61
70
  anyReplyFilter,
62
71
  humanReplyFilter,
72
+ settledHumanReplyFilter,
63
73
  CHOICE_LABEL_KEYS,
64
74
  configureSafetyNet,
65
75
  _resetSafetyNetConfig,
@@ -208,7 +208,7 @@ function pledgeWindowText() {
208
208
  if (hours < 24) return `en las próximas ${hours} horas`;
209
209
 
210
210
  const days = Math.ceil(hours / 24);
211
- return days <= 1 ? 'en menos de 24 horas' : `en menos de ${days} días`;
211
+ return days <= 1 ? 'en las próximas 24 horas' : `en los próximos ${days} días`;
212
212
  }
213
213
 
214
214
  function ackFor(choice, outcome) {
@@ -1,4 +1,6 @@
1
- const { CONTACT_WINDOW, anyReplyFilter, getPledgeMs, getSweepIntervalMs, humanReplyFilter } = require('../config/safetyNetConfig');
1
+ const {
2
+ CONTACT_WINDOW, anyReplyFilter, getPledgeMs, getSweepIntervalMs, humanReplyFilter, settledHumanReplyFilter
3
+ } = require('../config/safetyNetConfig');
2
4
  const { createIntervalSweeper } = require('../../utils/intervalSweeper');
3
5
  const { logger } = require('../../utils/logger');
4
6
  const { clampBackToHourWindow, clampToHourWindow } = require('../../utils/scheduleUtils');
@@ -13,12 +15,17 @@ const SWEEP_LIMIT = 200;
13
15
  const BREACH_CATEGORY = 'contact_team_member';
14
16
  const BREACH_QUESTION = 'El paciente sigue esperando el contacto que se le prometió';
15
17
 
18
+ const DELIVERY_GRACE_MS = 10 * 60 * 1000;
19
+
16
20
  async function repliedSince(filter) {
17
21
  const [reply] = await getMessages(filter, { sort: { createdAt: -1 }, limit: 1 });
18
22
  return Boolean(reply);
19
23
  }
20
24
 
21
25
  const humanRepliedSince = (code, since) => repliedSince(humanReplyFilter(code, since));
26
+ const humanReplySettledSince = (code, since) => repliedSince(
27
+ settledHumanReplyFilter(code, since, new Date(Date.now() - DELIVERY_GRACE_MS))
28
+ );
22
29
  const anyoneRepliedSince = (code, since) => repliedSince(anyReplyFilter(code, since));
23
30
 
24
31
  function pledgeDueAt(pledgedAt) {
@@ -60,6 +67,11 @@ const settle = async (id, from, state) => Boolean(
60
67
  await SafetyNetPledge.findOneAndUpdate({ _id: id, state: from }, { $set: { state, resolvedAt: new Date() } }).lean()
61
68
  );
62
69
 
70
+ const returnToPending = (id) => SafetyNetPledge.updateOne(
71
+ { _id: id, state: 'reporting' },
72
+ { $set: { state: 'pending', reportingSince: null } }
73
+ );
74
+
63
75
  const takeReportingLease = async (id) => Boolean(
64
76
  await SafetyNetPledge.findOneAndUpdate(
65
77
  { _id: id, $or: [{ state: 'pending' }, { state: 'reporting', reportingSince: { $lt: new Date(Date.now() - REPORT_LEASE_MS) } }] },
@@ -82,13 +94,18 @@ async function sweepSafetyNetPledges() {
82
94
  try {
83
95
  if (!await takeReportingLease(pledge._id)) continue;
84
96
 
85
- if (await humanRepliedSince(pledge.code, pledge.pledgedAt)) {
97
+ if (await humanReplySettledSince(pledge.code, pledge.pledgedAt)) {
86
98
  await settle(pledge._id, 'reporting', 'met');
87
99
  continue;
88
100
  }
89
101
 
102
+ if (await humanRepliedSince(pledge.code, pledge.pledgedAt)) {
103
+ await returnToPending(pledge._id);
104
+ continue;
105
+ }
106
+
90
107
  if (!await reportBreach(pledge)) {
91
- await SafetyNetPledge.updateOne({ _id: pledge._id, state: 'reporting' }, { $set: { state: 'pending', reportingSince: null } });
108
+ await returnToPending(pledge._id);
92
109
  logger.error('[SafetyNetPledge] Breach was not recorded, pledge returns to pending for the next sweep', { code: pledge.code });
93
110
  continue;
94
111
  }
@@ -115,6 +132,7 @@ module.exports = {
115
132
  stopSafetyNetSweep,
116
133
  anyoneRepliedSince,
117
134
  humanRepliedSince,
135
+ humanReplySettledSince,
118
136
  openPledge,
119
137
  pledgeDueAt,
120
138
  sweepSafetyNetPledges,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.54.0-dev.7969",
3
+ "version": "5.54.0-dev.7970",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",