@peopl-health/nexus 5.57.0-dev.9639 → 5.57.0-dev.9678

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.
@@ -85,6 +85,29 @@ class WriteClaimStore {
85
85
  }
86
86
  }
87
87
 
88
+ async has(key) {
89
+ if (!this._durable()) return this.memory.get(key) !== undefined ? true : null;
90
+ try {
91
+ const claim = await WriteClaim.findOne({ key: this._key(key), expiresAt: { $gt: new Date() } }).lean();
92
+ return Boolean(claim);
93
+ } catch (error) {
94
+ logger.warn('[writeClaimStore] Claim presence could not be read', { namespace: this.namespace, error: error.message });
95
+ return null;
96
+ }
97
+ }
98
+
99
+ async countSince(since, { value = undefined } = {}) {
100
+ if (!this._durable()) return null;
101
+ const query = { key: new RegExp(`^${this.namespace}:`), createdAt: { $gte: since } };
102
+ if (value !== undefined) query.value = value;
103
+ try {
104
+ return await WriteClaim.countDocuments(query);
105
+ } catch (error) {
106
+ logger.warn('[writeClaimStore] Claims could not be counted', { namespace: this.namespace, error: error.message });
107
+ return null;
108
+ }
109
+ }
110
+
88
111
  async release(key) {
89
112
  this.memory.delete(key);
90
113
  if (!this._durable()) return;
@@ -20,7 +20,9 @@ const REPLAY_SETTLE_MS = 5 * 60 * 1000;
20
20
  const DEFAULT_SWEEP_INTERVAL_MS = 5 * 60 * 1000;
21
21
  const COLD_MIN_AGE_MS = 4 * 60 * 60 * 1000;
22
22
  const COLD_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
23
- const COLD_TICKETS_PER_PASS = 25;
23
+ const COLD_TICKETS_PER_DAY = 40;
24
+ const COLD_TICKET_WINDOW_MS = 24 * 60 * 60 * 1000;
25
+ const COLD_TICKET_TTL_MS = 7 * 24 * 60 * 60 * 1000;
24
26
  // The cold band asks the same question of thirty days instead of one hour, so its page has
25
27
  // to be wide enough that answered conversations at the head cannot mask the rest.
26
28
  const COLD_SCAN_LIMIT = 1000;
@@ -28,6 +30,7 @@ const PATIENT_CONTACT_WINDOW = { timeZone: 'America/Monterrey', startHour: 8, en
28
30
 
29
31
  const recoveryClaims = new WriteClaimStore({ namespace: 'inbound-recovery', ttlMs: CLAIM_TTL_MS, maxSize: 500 });
30
32
  const ticketClaims = new WriteClaimStore({ namespace: 'inbound-recovery-ticket', ttlMs: CLAIM_TTL_MS, maxSize: 500 });
33
+ const coldTicketClaims = new WriteClaimStore({ namespace: 'inbound-recovery-cold-ticket', ttlMs: COLD_TICKET_TTL_MS, maxSize: 2000 });
31
34
 
32
35
  const FAILED_SEND = { 'statusInfo.status': 'failed', message_id: null };
33
36
  const UNANSWERED_QUESTION = 'El paciente escribió y no recibió respuesta.';
@@ -191,12 +194,18 @@ async function sweepUnansweredInbound({
191
194
  }
192
195
  }
193
196
 
197
+ // Claimed before filing, like the replay branch above: a ticket filed without its marker is a
198
+ // ticket the next pass files again.
199
+ const ticket = await ticketGate.acquire(key, 'alerted');
200
+ if (ticket !== 'acquired') { skipped += 1; continue; }
201
+
194
202
  logger.error('[inboundRecovery] patient was left unanswered', {
195
203
  code, ageMs, recoveryLive: canRecover, reachable,
196
204
  });
197
205
  if (await fileTicket(code, alert, ageMs)) {
198
206
  alerted += 1;
199
207
  } else {
208
+ await ticketGate.release(key);
200
209
  await claims.release(key);
201
210
  }
202
211
  }
@@ -212,6 +221,7 @@ async function sweepColdUnansweredInbound({
212
221
  isProcessing = () => false,
213
222
  isPaused = threadIsOwnedByAHuman,
214
223
  ticketGate = ticketClaims,
224
+ coldGate = coldTicketClaims,
215
225
  alert = recordUnresolvedRequest,
216
226
  } = {}) {
217
227
  if (mode === 'off') return { waiting: 0, alerted: 0 };
@@ -251,13 +261,22 @@ async function sweepColdUnansweredInbound({
251
261
 
252
262
  if (!waiting.length) return { waiting: 0, alerted: 0 };
253
263
 
264
+ const spent = () => coldGate.countSince(new Date(now.getTime() - COLD_TICKET_WINDOW_MS));
265
+ if ((await spent() ?? COLD_TICKETS_PER_DAY) >= COLD_TICKETS_PER_DAY) {
266
+ logger.error('[inboundRecovery] the day\'s cold tickets are spent; the rest wait for tomorrow', {
267
+ waiting: waiting.length, perDay: COLD_TICKETS_PER_DAY,
268
+ });
269
+ return { waiting: waiting.length, alerted: 0 };
270
+ }
271
+
254
272
  let alerted = 0;
255
273
  for (const { code, inbound } of waiting) {
256
- if (alerted >= COLD_TICKETS_PER_PASS) break;
274
+ if ((await spent() ?? COLD_TICKETS_PER_DAY) >= COLD_TICKETS_PER_DAY) break;
257
275
  const key = String(inbound._id);
258
- if (await ticketGate.acquire(key, 'alerted') !== 'acquired') continue;
276
+ if (await ticketGate.has(key) !== false) continue;
277
+ if (await coldGate.acquire(key, 'cold') !== 'acquired') continue;
259
278
  if (await fileTicket(code, alert, now.getTime() - new Date(inbound.createdAt).getTime())) alerted += 1;
260
- else await ticketGate.release(key);
279
+ else await coldGate.release(key);
261
280
  }
262
281
 
263
282
  logger.error('[inboundRecovery] filed for patients nobody had answered', { waiting: waiting.length, alerted });
@@ -310,8 +329,10 @@ module.exports = {
310
329
  threadIsOwnedByAHuman,
311
330
  _claims: recoveryClaims,
312
331
  _ticketClaims: ticketClaims,
332
+ _coldTicketClaims: coldTicketClaims,
313
333
  MIN_AGE_MS,
314
334
  MAX_AGE_MS,
335
+ COLD_TICKETS_PER_DAY,
315
336
  MAX_CANDIDATES,
316
337
  MAX_RECOVERIES_PER_PASS,
317
338
  SCAN_LIMIT,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.57.0-dev.9639",
3
+ "version": "5.57.0-dev.9678",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",