@peopl-health/nexus 5.71.34 → 5.71.36

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.
@@ -5,6 +5,7 @@ const { getRecordByFilter } = require('../../services/airtableService');
5
5
  const { Quimio_Ops_ID } = require('../../config/airtableConfig');
6
6
  const { MATCH_ALL } = require('../../helpers/airtableFormulaHelper');
7
7
  const { readContextFacts, listContextFactPatients, storeContextFacts } = require('../../fhir');
8
+ const { compareClaims } = require('../../fhir/helpers/contextArbitrationHelper');
8
9
  const { mapConcurrent } = require('../../utils/concurrencyUtils');
9
10
  const { daysBetween, mexicoDay } = require('../../utils/dateUtils');
10
11
  const { maskSensitiveValue } = require('../../utils/sanitizerUtils');
@@ -41,6 +42,7 @@ const DEFAULT_PATIENT_CONCURRENCY = 10;
41
42
  // a co-formulation such as pertuzumab/trastuzumab is each of its drugs
42
43
  const keysOf = (drug) => drug.generic.split('/').map(drugKey);
43
44
  const isCheckpoint = (key) => CHECKPOINT_INHIBITORS.has(key);
45
+ const endInstant = (day) => `${day}T00:00:00.000Z`;
44
46
  const closable = (drug) => drug.group === CLOSABLE_GROUP && !NEVER_CLOSED.has(drugKey(drug.generic));
45
47
  // the day as stated: a date-only report is stored at UTC midnight, which Mexico time would move to the day before
46
48
  const dayOf = (row) => {
@@ -49,7 +51,7 @@ const dayOf = (row) => {
49
51
  };
50
52
 
51
53
  async function loadDrugIdentity() {
52
- const catalog = await getRecordByFilter(Quimio_Ops_ID, CATALOG_TABLE, MATCH_ALL, null, CATALOG_FIELDS);
54
+ const catalog = await getRecordByFilter(Quimio_Ops_ID, CATALOG_TABLE, MATCH_ALL, null, CATALOG_FIELDS).catch(() => null);
53
55
  return catalog?.length ? buildDrugIdentity(catalog) : null;
54
56
  }
55
57
 
@@ -103,8 +105,12 @@ function proposeClosures({ chemo, facts, identity, today = mexicoDay(new Date())
103
105
  for (const row of facts) {
104
106
  if (row.variableId !== INJECTABLE_VARIABLE || !CURRENT_STATUSES.has(row.status ?? null)) continue;
105
107
  const reason = skipReason(row, { chemo, identity, regimen, regimenDays, replacedInClass });
106
- if (reason) {
107
- skipped[reason] = (skipped[reason] ?? 0) + 1;
108
+ const end = reason ? null : endOf(row, chemo, identity);
109
+ // a closure that loses arbitration to the slot would change nothing and be rewritten every pass
110
+ const claim = end && { source: CLOSURE_SOURCE, trust: CLOSURE_TRUST, validTime: endInstant(end.endDate) };
111
+ const skip = reason || (compareClaims(claim, row, INJECTABLE_VARIABLE, new Date().toISOString()) > 0 ? null : 'outranked');
112
+ if (skip) {
113
+ skipped[skip] = (skipped[skip] ?? 0) + 1;
108
114
  continue;
109
115
  }
110
116
  closures.push({
@@ -114,7 +120,7 @@ function proposeClosures({ chemo, facts, identity, today = mexicoDay(new Date())
114
120
  status: row.status ?? null,
115
121
  since: dayOf(row),
116
122
  proposedStatus: PROPOSED_STATUS,
117
- ...endOf(row, chemo, identity),
123
+ ...end,
118
124
  evidence: {
119
125
  ultimaAplicacion: chemo.ultimaAplicacion,
120
126
  esquema: chemo.esquema.map((entry) => entry.farmaco),
@@ -134,7 +140,7 @@ const closureOf = (patientId, closure, recordTime) => ({
134
140
  status: closure.proposedStatus,
135
141
  source: CLOSURE_SOURCE,
136
142
  trust: CLOSURE_TRUST,
137
- validTime: `${closure.endDate}T00:00:00.000Z`,
143
+ validTime: endInstant(closure.endDate),
138
144
  recordTime,
139
145
  });
140
146
 
@@ -152,7 +158,12 @@ async function closeStoppedRegimens({ dryRun = true, patientScope = null, today
152
158
  if (!patientScope && !dryRun) return { ...empty, ok: false, reason: 'patient-scope-required' };
153
159
  const identity = await loadDrugIdentity();
154
160
  if (!identity) return { ...empty, ok: false, reason: 'drug-catalog-unreadable' };
155
- const scoped = inScope(await listContextFactPatients(), patientScope);
161
+ const listed = await listContextFactPatients().catch((error) => {
162
+ logger.error('[regimenCloser] Patients unreadable', { error: error.message });
163
+ return null;
164
+ });
165
+ if (!listed) return { ...empty, ok: false, reason: 'patients-unreadable' };
166
+ const scoped = inScope(listed, patientScope);
156
167
  if (!scoped) return { ...empty, ok: false, reason: 'patient-scope-must-return-boolean' };
157
168
  const patients = [];
158
169
  const skipped = {};
@@ -3,12 +3,17 @@ const { Config_ID } = require('../../config/airtableConfig');
3
3
  const { getRecordByFilter } = require('../../services/airtableService');
4
4
 
5
5
  const { safeParse, safeParseArray } = require('../../utils/jsonUtils');
6
+ const { logger } = require('../../utils/logger');
6
7
 
7
8
  const FHIR_CONFIG_TABLE = 'fhir';
9
+ // Every stored resource key, extension url and code system is built on these. They are identity, not
10
+ // configuration: an edited base would fork the corpus into duplicates and orphan every stored extension.
11
+ const BASES = {
12
+ IDENT_BASE: 'https://peopl.health/fhir/identifier',
13
+ EXT_BASE: 'https://peopl.health/fhir/StructureDefinition',
14
+ CODESYSTEM_BASE: 'https://peopl.health/fhir/CodeSystem',
15
+ };
8
16
  const REQUIRED_KEYS = [
9
- 'IDENT_BASE',
10
- 'EXT_BASE',
11
- 'CODESYSTEM_BASE',
12
17
  'SYSTEMS',
13
18
  'SUSPECTED_GRADE_DISPLAYS',
14
19
  'QUESTIONNAIRE_CANONICAL_URL',
@@ -18,9 +23,6 @@ const REQUIRED_KEYS = [
18
23
  ];
19
24
 
20
25
  const state = {
21
- identBase: null,
22
- extBase: null,
23
- codesystemBase: null,
24
26
  systems: null,
25
27
  suspectedGradeDisplays: null,
26
28
  questionnaireCanonicalUrl: null,
@@ -43,9 +45,11 @@ async function initialize() {
43
45
  const suspectedGradeDisplays = safeParseArray(byKey.SUSPECTED_GRADE_DISPLAYS, null);
44
46
  if (!suspectedGradeDisplays) throw new Error('fhir config SUSPECTED_GRADE_DISPLAYS is not a JSON array');
45
47
 
46
- state.identBase = byKey.IDENT_BASE;
47
- state.extBase = byKey.EXT_BASE;
48
- state.codesystemBase = byKey.CODESYSTEM_BASE;
48
+ for (const [key, value] of Object.entries(BASES)) {
49
+ if (byKey[key] && byKey[key] !== value) {
50
+ logger.error('[fhirConfig] Config base value ignored: FHIR identity bases are fixed in code', { key, configured: byKey[key], used: value });
51
+ }
52
+ }
49
53
  state.systems = systems;
50
54
  state.suspectedGradeDisplays = suspectedGradeDisplays;
51
55
  state.questionnaireCanonicalUrl = byKey.QUESTIONNAIRE_CANONICAL_URL;
@@ -60,9 +64,9 @@ function assertLoaded() {
60
64
 
61
65
  module.exports = {
62
66
  initialize,
63
- getIdentBase: () => { assertLoaded(); return state.identBase; },
64
- getExtBase: () => { assertLoaded(); return state.extBase; },
65
- getCodesystemBase: () => { assertLoaded(); return state.codesystemBase; },
67
+ getIdentBase: () => BASES.IDENT_BASE,
68
+ getExtBase: () => BASES.EXT_BASE,
69
+ getCodesystemBase: () => BASES.CODESYSTEM_BASE,
66
70
  getSystems: () => { assertLoaded(); return state.systems; },
67
71
  getSuspectedGradeDisplays: () => { assertLoaded(); return state.suspectedGradeDisplays; },
68
72
  getQuestionnaireCanonicalUrl: () => { assertLoaded(); return state.questionnaireCanonicalUrl; },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.71.34",
3
+ "version": "5.71.36",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",