@peopl-health/nexus 5.71.24 → 5.71.25

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,9 +1,11 @@
1
1
  const { foldAccents } = require('../../utils/formatUtils');
2
2
 
3
3
  const MAX_ALIAS_WORDS = 4;
4
- const PART_SEPARATORS = /\s*(?:\+|±|,|;|\/|\(|\)|\by\b|\bcon\b)\s*/;
4
+ const PART_SEPARATORS = /\s*(?:\+|±|,|;|\/|\(|\)|\by\b|\bcon\b|\bm[aá]s\b|\bplus\b)\s*/i;
5
5
  // a dose or a route left between separators ("100 MG / 20 ML en 500 ML glucosado", "IV/SC") names no drug
6
6
  const DOSE_OR_ROUTE = /^(\d+|iv|sc|im|vo|po|ev|mg|ml|mcg|ui)(\s|$)/;
7
+ // beside a drug, only a dose, a route, or how it is formulated or scheduled says nothing about another drug
8
+ const DOSE_OR_ROUTE_WORD = /^(\d+(mg|ml|mcg|g|ui)?|iv|sc|im|vo|po|ev|mg|ml|mcg|ui|g)$/;
7
9
 
8
10
  // one key for a brand, a Spanish and an English spelling: ciclofosfamida and cyclophosphamide meet
9
11
  const drugKey = (name) => foldAccents(String(name ?? '')).toLowerCase()
@@ -14,6 +16,9 @@ const drugKey = (name) => foldAccents(String(name ?? '')).toLowerCase()
14
16
  .replace(/\s+/g, ' ')
15
17
  .trim();
16
18
 
19
+ const FORM_OR_SCHEDULE = new Set(['monodroga', 'semanal', 'liposomal', 'pegilada', 'lar', 'depot', 'pfs', 'jeringa'].map(drugKey));
20
+ const describesDose = (word) => DOSE_OR_ROUTE_WORD.test(word) || FORM_OR_SCHEDULE.has(word);
21
+
17
22
  const lines = (text) => String(text ?? '').split(/[\n,;]+/).map((line) => line.trim()).filter(Boolean);
18
23
 
19
24
  function buildDrugIdentity(catalog) {
@@ -34,24 +39,26 @@ function buildDrugIdentity(catalog) {
34
39
 
35
40
  function genericsInPart(words, identity) {
36
41
  const found = new Set();
42
+ let stray = false;
37
43
  for (let at = 0; at < words.length;) {
38
44
  let span = Math.min(MAX_ALIAS_WORDS, words.length - at);
39
45
  for (; span > 0 && !identity.aliases.has(words.slice(at, at + span).join(' ')); span -= 1);
40
46
  if (span) found.add(identity.aliases.get(words.slice(at, at + span).join(' ')));
47
+ else if (!describesDose(words[at])) stray = true;
41
48
  at += span || 1;
42
49
  }
43
- return found;
50
+ return { found, stray };
44
51
  }
45
52
 
46
- // every drug the text names; unresolved when some part of it names none the catalog knows
53
+ // every drug the text names; unresolved when some part names none the catalog knows, or names one beside an unknown word
47
54
  function drugsNamed(text, identity) {
48
55
  const whole = identity.aliases.get(drugKey(text));
49
56
  if (whole) return { drugs: [identity.generics.get(whole)], unresolved: false };
50
57
  const keys = new Set();
51
58
  let unresolved = false;
52
59
  for (const part of String(text ?? '').split(PART_SEPARATORS).map(drugKey).filter(Boolean)) {
53
- const found = genericsInPart(part.split(' '), identity);
54
- if (!found.size && !DOSE_OR_ROUTE.test(part)) unresolved = true;
60
+ const { found, stray } = genericsInPart(part.split(' '), identity);
61
+ if (found.size ? stray : !DOSE_OR_ROUTE.test(part)) unresolved = true;
55
62
  found.forEach((key) => keys.add(key));
56
63
  }
57
64
  return { drugs: [...keys].map((key) => identity.generics.get(key)), unresolved: unresolved || !keys.size };
@@ -6,27 +6,38 @@ const { Quimio_Ops_ID } = require('../../config/airtableConfig');
6
6
  const { MATCH_ALL } = require('../../helpers/airtableFormulaHelper');
7
7
  const { readContextFacts, listContextFactPatients } = require('../../fhir');
8
8
  const { mapConcurrent } = require('../../utils/concurrencyUtils');
9
- const { mexicoDay } = require('../../utils/dateUtils');
9
+ const { daysBetween, mexicoDay } = require('../../utils/dateUtils');
10
10
  const { maskSensitiveValue } = require('../../utils/sanitizerUtils');
11
11
  const { logger } = require('../../utils/logger');
12
12
 
13
13
  const CATALOG_TABLE = 'catalogo_farmacos';
14
14
  const CATALOG_FIELDS = ['farmaco', 'variantes', 'generico', 'grupo'];
15
- const ACTIVE_WINDOW_DAYS = 100;
15
+ // the oncology sign-off of 2026-10-14 sets these windows and lists
16
+ const LAST_APPLICATION_DAYS = 60;
17
+ const REGIMEN_MIN_DAYS = 21;
18
+ // a checkpoint inhibitor is often given every 6 weeks, or held for an immune toxicity
19
+ const CHECKPOINT_REGIMEN_MIN_DAYS = 84;
16
20
  const INJECTABLE_VARIABLE = 'injectable_onco_summary';
17
- const CURRENT_STATUSES = new Set([null, 'taking', 'unknown', 'on-hold']);
21
+ const CURRENT_STATUSES = new Set([null, 'taking', 'unknown']);
18
22
  const PROPOSED_STATUS = 'stopped';
19
23
  // the unit's catalog files support care and premedication apart; only its own antineoplastics are closed
20
24
  const CLOSABLE_GROUP = 'Quimioterapia';
21
- // in that group but oral, a hormone depot, or given outside the unit: pending oncology sign-off
25
+ // in that group but oral, a hormone depot, a rescue or protector, or given outside the unit
22
26
  const NEVER_CLOSED = new Set([
23
- 'capecitabina', 'darolutamida', 'degarelix', 'fulvestrant', 'goserelina', 'leuprorelina', 'lanreotida',
24
- 'octreotida', 'bacilo de Calmette-Guérin', 'ácido folínico',
27
+ 'capecitabina', 'darolutamida', 'degarelix', 'fulvestrant', 'goserelina', 'leuprorelina', 'triptorelina',
28
+ 'histrelina', 'buserelina', 'lanreotida', 'octreotida', 'pasireotida', 'bacilo de Calmette-Guérin', 'ácido folínico',
29
+ 'levofolinato', 'mesna', 'dexrazoxano',
30
+ ].map(drugKey));
31
+ const CHECKPOINT_INHIBITORS = new Set([
32
+ 'pembrolizumab', 'nivolumab', 'atezolizumab', 'durvalumab', 'avelumab', 'cemiplimab', 'dostarlimab',
33
+ 'tislelizumab', 'toripalimab', 'retifanlimab', 'sintilimab', 'camrelizumab', 'serplulimab', 'cosibelimab',
34
+ 'ipilimumab', 'tremelimumab',
25
35
  ].map(drugKey));
26
36
  const DEFAULT_PATIENT_CONCURRENCY = 10;
27
37
 
28
38
  // a co-formulation such as pertuzumab/trastuzumab is each of its drugs
29
39
  const keysOf = (drug) => drug.generic.split('/').map(drugKey);
40
+ const isCheckpoint = (key) => CHECKPOINT_INHIBITORS.has(key);
30
41
  const closable = (drug) => drug.group === CLOSABLE_GROUP && !NEVER_CLOSED.has(drugKey(drug.generic));
31
42
  // the day as stated: a date-only report is stored at UTC midnight, which Mexico time would move to the day before
32
43
  const dayOf = (row) => {
@@ -39,13 +50,15 @@ async function loadDrugIdentity() {
39
50
  return catalog?.length ? buildDrugIdentity(catalog) : null;
40
51
  }
41
52
 
42
- function skipReason(row, { chemo, identity, regimen }) {
53
+ function skipReason(row, { chemo, identity, regimen, regimenDays, replacedInClass }) {
43
54
  const named = drugsNamed(row.concept ?? row.value, identity);
44
55
  if (named.unresolved) return 'unresolved';
45
56
  if (named.drugs.flatMap(keysOf).some((key) => regimen.has(key))) return 'in-regimen';
46
57
  if (!named.drugs.every(closable)) return 'never-closed';
47
58
  const since = dayOf(row);
48
59
  if (!since || since >= chemo.inicioEsquema.fecha) return 'not-before-regimen';
60
+ const checkpoint = named.drugs.flatMap(keysOf).some(isCheckpoint);
61
+ if (checkpoint && !replacedInClass && regimenDays < CHECKPOINT_REGIMEN_MIN_DAYS) return 'checkpoint-too-recent';
49
62
  return null;
50
63
  }
51
64
 
@@ -62,10 +75,15 @@ function proposeClosures({ chemo, facts, identity, today = mexicoDay(new Date())
62
75
  const none = (reason) => ({ closures: [], skipped: { [reason]: 1 } });
63
76
  if (chemo === undefined) return none('chemo-unreadable');
64
77
  if (!chemo?.inicioEsquema) return none('no-regimen-start');
65
- const status = chemoStatus(chemo, today, ACTIVE_WINDOW_DAYS);
78
+ const status = chemoStatus(chemo, today, LAST_APPLICATION_DAYS);
66
79
  if (!status.active) return none('inactive');
67
80
  // a scheduled session keeps a regimen active, but an old last application is no evidence of what it is now
68
- if (status.days > ACTIVE_WINDOW_DAYS) return none('stale-regimen');
81
+ if (status.days > LAST_APPLICATION_DAYS) return none('stale-regimen');
82
+ // a drug given on day 8 or every three weeks may not have come up yet in a younger regimen
83
+ const regimenDays = daysBetween(chemo.inicioEsquema.fecha, today);
84
+ if (regimenDays < REGIMEN_MIN_DAYS) return none('regimen-too-recent');
85
+ // without a previous regimen the start is only the first recorded session, so a second one must show the regimen
86
+ if (!chemo.esquemaPrevio && !(chemo.ultimaAplicacion.fecha > chemo.inicioEsquema.fecha)) return none('single-session-regimen');
69
87
  const named = chemo.esquema.map((entry) => drugsNamed(entry.farmaco, identity));
70
88
  // a drug of the regimen the catalog cannot name could be the very drug of a slot
71
89
  if (named.some((entry) => entry.unresolved)) return none('unresolved-regimen');
@@ -73,11 +91,12 @@ function proposeClosures({ chemo, facts, identity, today = mexicoDay(new Date())
73
91
  // a depot or support visit is no evidence the antineoplastic line changed
74
92
  if (!regimenDrugs.some(closable)) return none('no-closable-regimen');
75
93
  const regimen = new Set(regimenDrugs.flatMap(keysOf));
94
+ const replacedInClass = [...regimen].some(isCheckpoint);
76
95
  const closures = [];
77
96
  const skipped = {};
78
97
  for (const row of facts) {
79
98
  if (row.variableId !== INJECTABLE_VARIABLE || !CURRENT_STATUSES.has(row.status ?? null)) continue;
80
- const reason = skipReason(row, { chemo, identity, regimen });
99
+ const reason = skipReason(row, { chemo, identity, regimen, regimenDays, replacedInClass });
81
100
  if (reason) {
82
101
  skipped[reason] = (skipped[reason] ?? 0) + 1;
83
102
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.71.24",
3
+ "version": "5.71.25",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",