@peopl-health/nexus 5.72.9 → 5.72.11

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.
@@ -301,7 +301,7 @@ function buildSymptomChangeBlock(symptomsChange = []) {
301
301
  .map((entry) => {
302
302
  const absent = ABSENT_CHANGE_RX.exec(entry);
303
303
  if (!absent) return `- ${entry}`;
304
- return `- ${entry} → el paciente marcó «Nunca tuve este síntoma» para "${absent[1]}". Si este triaje le da grado a ese síntoma, la respuesta se contradice y vale el grado`;
304
+ return `- ${entry} → el paciente marcó «Nunca tuve este síntoma» para "${absent[1]}". Si este triaje le da grado a ese síntoma, la respuesta se contradice: vale el grado (no lo bajes ni lo descartes), pero en el resumen interno escribe «respuesta contradictoria: marcó Nunca tuve y el cuestionario le da grado N», pide caracterización y que lo primero sea confirmar si lo tuvo estos días. Al paciente no se lo presentes como un hecho («reportas náusea»): dile que en el cuestionario lo marcó y pídele que te confirme si lo tuvo`;
305
305
  })
306
306
  .join('\n');
307
307
  }
@@ -15,6 +15,8 @@ const ATTENTION_CASE_WINDOW_MS = ATTENTION_CASE_WINDOW_DAYS * 24 * 60 * 60 * 100
15
15
  const URGENT_REQUEST_URGENCY = 'urgent';
16
16
  const REQUEST_URGENCIES = ['routine', 'soon', 'urgent'];
17
17
  const URGENT_CASE_TYPES = ['Urgencia', 'Emergencia'];
18
+ // The operations statuses that close a case in Airtable, including the legacy lowercase one.
19
+ const CLOSING_CASE_OPERATIONS_STATUSES = new Set(['Cerrado', 'No requiere', 'cerrado']);
18
20
  const TASK_PROGRAM_SOURCE = 'auna';
19
21
 
20
22
  const programSourceFilter = (field) => `FIND('${escapeFormulaValue(TASK_PROGRAM_SOURCE)}', ARRAYJOIN({${field}}))`;
@@ -28,9 +30,13 @@ const isWithinAttentionWindow = (createdAt) => {
28
30
 
29
31
  const needsRequestAttention = (status) => status === ATTENTION_REQUEST_STATUS;
30
32
 
31
- const isCaseOpen = (resolvedTime, createdAt) => !resolvedTime && isWithinAttentionWindow(createdAt);
33
+ // A case stays open until it is closed: a closed_time, or a closing operations status not yet stamped.
34
+ const isCaseOpen = (closedTime, createdAt, operationsStatus) => !closedTime
35
+ && !CLOSING_CASE_OPERATIONS_STATUSES.has(operationsStatus)
36
+ && isWithinAttentionWindow(createdAt);
32
37
 
33
- const needsCaseAttention = isCaseOpen;
38
+ // A case needs attention until it is attended, which stamps its resolved_time.
39
+ const needsCaseAttention = (resolvedTime, createdAt) => !resolvedTime && isWithinAttentionWindow(createdAt);
34
40
 
35
41
  const isRequestUrgent = (urgency) => urgency === URGENT_REQUEST_URGENCY;
36
42
 
@@ -39,6 +45,7 @@ const isCaseUrgent = (type) => URGENT_CASE_TYPES.includes(type);
39
45
  module.exports = {
40
46
  ATTENTION_CASE_WINDOW_DAYS,
41
47
  CLOSED_REQUEST_STATUSES,
48
+ CLOSING_CASE_OPERATIONS_STATUSES,
42
49
  ATTENTION_REQUEST_STATUS,
43
50
  REQUEST_URGENCIES,
44
51
  isCaseOpen,
@@ -2,6 +2,7 @@ const runtimeConfig = require('../config/runtimeConfig');
2
2
  const { logger } = require('../utils/logger');
3
3
  const { isShadowRun } = require('../utils/shadowScope');
4
4
  const { caseIdsOf, getEscalationReview, onCaseWrite, supersededCaseIds } = require('../models/escalationReviewModel');
5
+ const { CLOSING_CASE_OPERATIONS_STATUSES } = require('../helpers/patientTaskRulesHelper');
5
6
  const { storeEscalationCase } = require('../fhir/services/escalationService');
6
7
  const { caseStatusOf } = require('../fhir/constants/escalationCaseStatus');
7
8
  const { effectiveUrgencyOf } = require('./reviewDeadlineService');
@@ -17,13 +18,11 @@ function conditionIdsOf(review) {
17
18
  return [...new Set([...(review.followUpCases ?? []), ...impressed].filter((id) => typeof id === 'string' && id))];
18
19
  }
19
20
 
20
- // The labels that close a case in Airtable, including the legacy lowercase one.
21
- const CLOSING_LABELS = new Set(['Cerrado', 'No requiere', 'cerrado']);
22
21
  const CLOSING_TASK_STATUSES = new Set(['completed', 'cancelled']);
23
22
  const TO_REVIEW = caseStatusOf('open');
24
23
 
25
24
  // A closed case, by ops by hand or by a review, is left as it was closed.
26
- const isClosed = (patientCase) => CLOSING_LABELS.has(patientCase?.operationsStatus);
25
+ const isClosed = (patientCase) => CLOSING_CASE_OPERATIONS_STATUSES.has(patientCase?.operationsStatus);
27
26
 
28
27
  // A review is of the whole patient: it covers every case the patient raised before it closed that is still open.
29
28
  // A closed review takes none once the patient has a newer review, which covers them instead.
@@ -74,7 +73,7 @@ async function mirrorToCases(review, cases) {
74
73
  .map((recordId) => ({ recordId, caseStatus: !closing || closing.has(recordId) ? reviewStatus : TO_REVIEW }))
75
74
  .filter(({ recordId, caseStatus }) => mirrored[recordId] !== caseStatus.businessStatus)
76
75
  .filter(({ recordId }) => !isClosed(byId.get(recordId)))
77
- // A case resolved meanwhile that the review does not close is left resolved, not sent back to be reviewed.
76
+ // A case closed meanwhile that the review does not close is left closed; an attended one goes back to be reviewed.
78
77
  .filter(({ recordId }) => !closing || closing.has(recordId) || byId.get(recordId)?.isOpen !== false);
79
78
  if (!pending.length) return;
80
79
  const settled = {};
@@ -93,7 +93,8 @@ const toCase = (row) => ({
93
93
  note: row.observations ?? null,
94
94
  recommendation: row.recomendation_final ?? null,
95
95
  resolvedAt: row.resolved_time ?? null,
96
- isOpen: isCaseOpen(row.resolved_time, row.created || row._createdTime),
96
+ closedAt: row.closed_time ?? null,
97
+ isOpen: isCaseOpen(row.closed_time, row.created || row._createdTime, row.status_operaciones),
97
98
  needsAttention: needsCaseAttention(row.resolved_time, row.created || row._createdTime),
98
99
  isUrgent: isCaseUrgent(row.type),
99
100
  });
@@ -112,7 +113,7 @@ const TASK_KINDS = {
112
113
  },
113
114
  cases: {
114
115
  table: 'patient_cases',
115
- fields: ['case_id', 'created', 'resolved_time', 'description', 'grade', 'type', 'status', 'status_operaciones', 'seguimiento', 'observations', 'recomendation_final'],
116
+ fields: ['case_id', 'created', 'resolved_time', 'closed_time', 'description', 'grade', 'type', 'status', 'status_operaciones', 'seguimiento', 'observations', 'recomendation_final'],
116
117
  scope: programSourceFilter('source'),
117
118
  toTask: toCase,
118
119
  writable: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.72.9",
3
+ "version": "5.72.11",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",