@peopl-health/nexus 6.7.0-dev.21636 → 6.7.1

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.
@@ -44,9 +44,9 @@ const checkAgainAt = (review, at) => getEscalationReview().updateOne(
44
44
  BOOKKEEPING,
45
45
  );
46
46
 
47
- async function close(review, result) {
47
+ async function close(review, result, extra = {}) {
48
48
  const { review: closed } = await closeEscalationReview({ id: String(review._id), closedBy: OPERATOR, expectedAlarmCount: (review.alarms ?? []).length });
49
- await audit(closed, { action: 'followup_result', result });
49
+ await audit(closed, { action: 'followup_result', result, ...extra });
50
50
  if (result === 'answered') await resolveSettledCases(closed, OPERATOR);
51
51
  }
52
52
 
@@ -60,8 +60,8 @@ async function keepUnanswered(review, now) {
60
60
  }
61
61
 
62
62
  async function settleAnswer(review, now) {
63
- const { review: current, reading } = await followUpReadingOf(review);
64
- if (reading === 'settled') return close(current, 'answered');
63
+ const { review: current, reading, basis } = await followUpReadingOf(review, { canFallBackToDecisionReading: true });
64
+ if (reading === 'settled') return close(current, 'answered', { basis });
65
65
  const held = await getEscalationReview().updateOne(
66
66
  { _id: review._id, closedAt: null, status: { $in: FOLLOW_UP_STATUSES }, 'caseFollowUp.reminderId': review.caseFollowUp.reminderId, 'caseFollowUp.unsettledAt': null },
67
67
  { $set: { 'caseFollowUp.unsettledAt': now, 'caseFollowUp.nextCheckAt': null, updatedSinceSeen: true } },
@@ -1631,23 +1631,46 @@ async function closeSettled(review) {
1631
1631
  }
1632
1632
  }
1633
1633
 
1634
- async function followUpReadingOf(snapshot) {
1634
+ // A watched case read settled at its decision stands there until read again; any alarm since the decision, held or not, voids it.
1635
+ function canStandOnDecisionReading(review, sinceMs) {
1636
+ const decisions = review.decisions ?? [];
1637
+ if (decisions.at(-1)?.decision !== 'monitor') return false;
1638
+ return !(review.alarms ?? []).some((alarm) => new Date(alarm.raisedAt).getTime() > sinceMs);
1639
+ }
1640
+
1641
+ async function followUpReadingOf(snapshot, { canFallBackToDecisionReading = false } = {}) {
1635
1642
  const since = (snapshot.decisions ?? []).at(-1)?.createdAt;
1636
1643
  if (!since) return { review: snapshot, reading: null };
1637
1644
  const sinceMs = new Date(since).getTime();
1638
1645
  const { review, known } = await followUpCasesOf(snapshot, sinceMs);
1639
1646
  if (!known.size) return { review, reading: null };
1640
1647
 
1648
+ const previousDecision = (review.decisions ?? []).at(-2)?.createdAt;
1649
+ const previousMs = previousDecision ? new Date(previousDecision).getTime() : -Infinity;
1641
1650
  const latest = new Map();
1642
- (review.updates ?? []).filter((entry) => new Date(entry.at).getTime() > sinceMs)
1651
+ const decidedOn = new Map();
1652
+ [...(review.updates ?? [])]
1643
1653
  .sort((a, b) => new Date(a.at) - new Date(b.at))
1644
- .forEach((entry) => (entry.impressions ?? [])
1645
- .filter((impression) => known.has(impression.caseId) && readingOf(impression))
1646
- .forEach((impression) => latest.set(impression.caseId, readingOf(impression))));
1654
+ .forEach((entry) => {
1655
+ const atMs = new Date(entry.at).getTime();
1656
+ if (atMs <= previousMs) return;
1657
+ const readings = atMs > sinceMs ? latest : decidedOn;
1658
+ (entry.impressions ?? [])
1659
+ .filter((impression) => known.has(impression.caseId) && readingOf(impression))
1660
+ .forEach((impression) => readings.set(impression.caseId, readingOf(impression)));
1661
+ });
1662
+ let basis = 'followUp';
1663
+ if (canFallBackToDecisionReading && canStandOnDecisionReading(review, sinceMs)) {
1664
+ for (const caseId of known) {
1665
+ if (latest.has(caseId) || !SETTLED_TRAJECTORIES.includes(decidedOn.get(caseId))) continue;
1666
+ latest.set(caseId, decidedOn.get(caseId));
1667
+ basis = 'decision';
1668
+ }
1669
+ }
1647
1670
 
1648
1671
  if ([...latest.values()].includes('worsening')) return { review, reading: 'worsening' };
1649
1672
  const settled = [...known].every((caseId) => SETTLED_TRAJECTORIES.includes(latest.get(caseId)));
1650
- return { review, reading: settled ? 'settled' : 'unsettled' };
1673
+ return settled ? { review, reading: 'settled', basis } : { review, reading: 'unsettled' };
1651
1674
  }
1652
1675
 
1653
1676
  async function settleFollowUp(snapshot, { background = true, canClose = true } = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.7.0-dev.21636",
3
+ "version": "6.7.1",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",