@peopl-health/nexus 5.57.2 → 5.58.0-dev.10158

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.
@@ -1,5 +1,3 @@
1
- const { escapeFormulaValue } = require('./airtableFormulaHelper');
2
-
3
1
  const CLOSED_REQUEST_STATUSES = [
4
2
  'resuelto',
5
3
  'no resuelto',
@@ -9,18 +7,21 @@ const CLOSED_REQUEST_STATUSES = [
9
7
  'alucinación',
10
8
  'revisado',
11
9
  ];
10
+ const CLOSED_CASE_STATUSES = ['Resuelto', 'Fallecido'];
11
+ const CLOSED_OPERATIONS_STATUS = 'cerrado';
12
12
  const ATTENTION_REQUEST_STATUS = 'sin atender';
13
- const ATTENTION_CASE_WINDOW_DAYS = 60;
13
+ const ATTENTION_OPERATIONS_STATUS = 'pendiente';
14
+ const ATTENTION_CASE_WINDOW_DAYS = 14;
14
15
  const ATTENTION_CASE_WINDOW_MS = ATTENTION_CASE_WINDOW_DAYS * 24 * 60 * 60 * 1000;
15
16
  const URGENT_REQUEST_URGENCY = 'urgent';
16
17
  const REQUEST_URGENCIES = ['routine', 'soon', 'urgent'];
17
18
  const URGENT_CASE_TYPES = ['Urgencia', 'Emergencia'];
18
- const TASK_PROGRAM_SOURCE = 'auna';
19
-
20
- const programSourceFilter = (field) => `FIND('${escapeFormulaValue(TASK_PROGRAM_SOURCE)}', ARRAYJOIN({${field}}))`;
21
19
 
22
20
  const isRequestOpen = (status) => !CLOSED_REQUEST_STATUSES.includes(status);
23
21
 
22
+ const isCaseOpen = (status, operationsStatus) => operationsStatus !== CLOSED_OPERATIONS_STATUS
23
+ && !CLOSED_CASE_STATUSES.includes(status);
24
+
24
25
  const isWithinAttentionWindow = (createdAt) => {
25
26
  const created = Date.parse(createdAt || '');
26
27
  return Number.isFinite(created) && Date.now() - created <= ATTENTION_CASE_WINDOW_MS;
@@ -28,9 +29,9 @@ const isWithinAttentionWindow = (createdAt) => {
28
29
 
29
30
  const needsRequestAttention = (status) => status === ATTENTION_REQUEST_STATUS;
30
31
 
31
- const isCaseOpen = (resolvedTime, createdAt) => !resolvedTime && isWithinAttentionWindow(createdAt);
32
-
33
- const needsCaseAttention = isCaseOpen;
32
+ const needsCaseAttention = (status, operationsStatus, createdAt) => operationsStatus === ATTENTION_OPERATIONS_STATUS
33
+ && !CLOSED_CASE_STATUSES.includes(status)
34
+ && isWithinAttentionWindow(createdAt);
34
35
 
35
36
  const isRequestUrgent = (urgency) => urgency === URGENT_REQUEST_URGENCY;
36
37
 
@@ -39,6 +40,7 @@ const isCaseUrgent = (type) => URGENT_CASE_TYPES.includes(type);
39
40
  module.exports = {
40
41
  ATTENTION_CASE_WINDOW_DAYS,
41
42
  CLOSED_REQUEST_STATUSES,
43
+ ATTENTION_OPERATIONS_STATUS,
42
44
  ATTENTION_REQUEST_STATUS,
43
45
  REQUEST_URGENCIES,
44
46
  isCaseOpen,
@@ -47,5 +49,4 @@ module.exports = {
47
49
  isRequestUrgent,
48
50
  needsCaseAttention,
49
51
  needsRequestAttention,
50
- programSourceFilter,
51
52
  };
@@ -1,4 +1,4 @@
1
- const { Historial_Clinico_ID } = require('../config/airtableConfig');
1
+ const { Historial_Clinico_ID, Symptoms_ID } = require('../config/airtableConfig');
2
2
 
3
3
  const { logger } = require('../utils/logger');
4
4
 
@@ -17,7 +17,7 @@ const toMap = (arr, keyFn, valFn) => arr.reduce((m, item) => {
17
17
 
18
18
  const PENDING_TASK_FILTERS = ['pending-tasks', 'pending-cases', 'pending-requests'];
19
19
 
20
- const buildThreadFilter = (filter, { taskCodes }) => {
20
+ const buildThreadFilter = (filter, { triageIds, taskCodes }) => {
21
21
  const base = { lastMessageAt: { $ne: null } };
22
22
  switch (filter) {
23
23
  case 'unread':
@@ -28,6 +28,8 @@ const buildThreadFilter = (filter, { taskCodes }) => {
28
28
  return { ...base, lastMessageAt: { $gt: new Date(Date.now() - 86400000) } };
29
29
  case 'no-response':
30
30
  return { ...base, lastMessageFromMe: false };
31
+ case 'triage-last-month':
32
+ return { ...base, code: { $in: triageIds } };
31
33
  case 'pending-tasks':
32
34
  return { ...base, code: { $in: taskCodes.all } };
33
35
  case 'pending-cases':
@@ -41,6 +43,12 @@ const buildThreadFilter = (filter, { taskCodes }) => {
41
43
  };
42
44
 
43
45
  const fetchConversationData = async (filter, skip, limit) => {
46
+ let triageIds = [];
47
+ if (filter === 'triage-last-month') {
48
+ const triageRecords = await getRecordByFilter(Symptoms_ID, 'estado_general', 'TRUE()', 'complete_triage_last_month', ['whatsapp_id']);
49
+ triageIds = (triageRecords || []).map(r => r.whatsapp_id).filter(Boolean);
50
+ }
51
+
44
52
  const openTaskMap = PENDING_TASK_FILTERS.includes(filter)
45
53
  ? await getOpenTaskSummary()
46
54
  : await getOpenTaskSummary().catch((error) => {
@@ -54,7 +62,7 @@ const fetchConversationData = async (filter, skip, limit) => {
54
62
  requests: taskEntries.filter(([, entry]) => entry.requests > 0).map(([code]) => code),
55
63
  };
56
64
 
57
- const threadFilter = buildThreadFilter(filter, { taskCodes });
65
+ const threadFilter = buildThreadFilter(filter, { triageIds, taskCodes });
58
66
 
59
67
  logger.info('[fetchConversationData] Applying filter', { filter });
60
68
  const startTime = Date.now();
@@ -12,7 +12,6 @@ const {
12
12
  isRequestUrgent,
13
13
  needsCaseAttention,
14
14
  needsRequestAttention,
15
- programSourceFilter,
16
15
  } = require('../helpers/patientTaskRulesHelper');
17
16
 
18
17
  const { getRecordByFilter, updateRecordByFilter } = require('../services/airtableService');
@@ -98,9 +97,8 @@ const toCase = (row) => ({
98
97
  followUp: row.seguimiento ?? null,
99
98
  note: row.observations ?? null,
100
99
  recommendation: row.recomendation_final ?? null,
101
- resolvedAt: row.resolved_time ?? null,
102
- isOpen: isCaseOpen(row.resolved_time, row.created || row._createdTime),
103
- needsAttention: needsCaseAttention(row.resolved_time, row.created || row._createdTime),
100
+ isOpen: isCaseOpen(row.status, row.status_operaciones),
101
+ needsAttention: needsCaseAttention(row.status, row.status_operaciones, row.created || row._createdTime),
104
102
  isUrgent: isCaseUrgent(row.type),
105
103
  });
106
104
 
@@ -108,7 +106,7 @@ const TASK_KINDS = {
108
106
  requests: {
109
107
  table: 'unresolved_questions',
110
108
  fields: ['question_id', 'created_time', 'question', 'details', 'category', 'urgency', 'status', 'Observaciones', 'doctor_office'],
111
- scope: `AND(${fieldEquals('message_tag', 'request')}, ${programSourceFilter('source (from customer_id)')})`,
109
+ scope: fieldEquals('message_tag', 'request'),
112
110
  toTask: toRequest,
113
111
  writable: {
114
112
  status: { field: 'status', options: REQUEST_STATUSES },
@@ -118,8 +116,8 @@ const TASK_KINDS = {
118
116
  },
119
117
  cases: {
120
118
  table: 'patient_cases',
121
- fields: ['case_id', 'created', 'resolved_time', 'description', 'grade', 'type', 'status', 'status_operaciones', 'seguimiento', 'observations', 'recomendation_final'],
122
- scope: programSourceFilter('source'),
119
+ fields: ['case_id', 'created', 'description', 'grade', 'type', 'status', 'status_operaciones', 'seguimiento', 'observations', 'recomendation_final'],
120
+ scope: null,
123
121
  toTask: toCase,
124
122
  writable: {
125
123
  status: { field: 'status', options: CASE_STATUSES },
@@ -138,7 +136,8 @@ const validWhatsappId = (code) => {
138
136
 
139
137
  const taskFilter = (kind, whatsappId) => {
140
138
  const guard = `FIND('${escapeFormulaValue(whatsappId)},', ARRAYJOIN({whatsapp_id}, ',') & ',')`;
141
- return `AND(${guard}, ${TASK_KINDS[kind].scope})`;
139
+ const { scope } = TASK_KINDS[kind];
140
+ return scope ? `AND(${guard}, ${scope})` : guard;
142
141
  };
143
142
 
144
143
  const byOpenThenNewest = (a, b) => (b.isOpen - a.isOpen) || (b.createdAt || '').localeCompare(a.createdAt || '');
@@ -5,12 +5,12 @@ const { MapCache } = require('../utils/MapCache');
5
5
  const { fieldEquals } = require('../helpers/airtableFormulaHelper');
6
6
  const {
7
7
  ATTENTION_CASE_WINDOW_DAYS,
8
+ ATTENTION_OPERATIONS_STATUS,
8
9
  ATTENTION_REQUEST_STATUS,
9
10
  isCaseUrgent,
10
11
  isRequestUrgent,
11
12
  needsCaseAttention,
12
13
  needsRequestAttention,
13
- programSourceFilter,
14
14
  } = require('../helpers/patientTaskRulesHelper');
15
15
 
16
16
  const { getRecordByFilter } = require('../services/airtableService');
@@ -18,9 +18,9 @@ const { getRecordByFilter } = require('../services/airtableService');
18
18
  const CACHE_KEY = 'open-tasks';
19
19
  const CACHE_TTL_MS = 60000;
20
20
  const REQUEST_FIELDS = ['whatsapp_id', 'status', 'urgency'];
21
- const CASE_FIELDS = ['whatsapp_id', 'resolved_time', 'type', 'created'];
22
- const REQUEST_FILTER = `AND(${fieldEquals('message_tag', 'request')}, ${fieldEquals('status', ATTENTION_REQUEST_STATUS)}, ${programSourceFilter('source (from customer_id)')})`;
23
- const CASE_FILTER = `AND({resolved_time} = BLANK(), OR(IS_AFTER({created}, DATEADD(NOW(), -${ATTENTION_CASE_WINDOW_DAYS}, 'days')), {created} = BLANK()), ${programSourceFilter('source')})`;
21
+ const CASE_FIELDS = ['whatsapp_id', 'status', 'status_operaciones', 'type', 'created'];
22
+ const REQUEST_FILTER = `AND(${fieldEquals('message_tag', 'request')}, ${fieldEquals('status', ATTENTION_REQUEST_STATUS)})`;
23
+ const CASE_FILTER = `AND(${fieldEquals('status_operaciones', ATTENTION_OPERATIONS_STATUS)}, OR(IS_AFTER({created}, DATEADD(NOW(), -${ATTENTION_CASE_WINDOW_DAYS}, 'days')), {created} = BLANK()))`;
24
24
 
25
25
  const cache = new MapCache({ maxSize: 1, ttl: CACHE_TTL_MS });
26
26
  let inFlight = null;
@@ -51,7 +51,7 @@ async function loadSummary() {
51
51
 
52
52
  for (const row of caseRows || []) {
53
53
  const createdAt = row.created || row._createdTime || null;
54
- if (!needsCaseAttention(row.resolved_time, createdAt)) continue;
54
+ if (!needsCaseAttention(row.status, row.status_operaciones, createdAt)) continue;
55
55
  for (const code of codesOf(row)) addTask(summary, code, 'cases', isCaseUrgent(row.type));
56
56
  }
57
57
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.57.2",
3
+ "version": "5.58.0-dev.10158",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",