@peopl-health/nexus 6.7.0-dev.21501 → 6.7.0-dev.21502
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.
|
@@ -12,7 +12,7 @@ const { duration } = require('./untakenAlertService');
|
|
|
12
12
|
const { positiveSetting, responseDeadlinePolicy, teamOpenAt } = require('./reviewDeadlineService');
|
|
13
13
|
|
|
14
14
|
const MINUTE_MS = 60 * 1000;
|
|
15
|
-
const LOOKBACK_MS =
|
|
15
|
+
const LOOKBACK_MS = 48 * 60 * MINUTE_MS;
|
|
16
16
|
const DEFAULT_MINUTES = { 3: 60, 2: 120 };
|
|
17
17
|
const MINUTES_KEYS = { 3: 'ESCALATION_UNREVIEWED_MINUTES_G3', 2: 'ESCALATION_UNREVIEWED_MINUTES_G2' };
|
|
18
18
|
const noticed = new WriteClaimStore({ namespace: 'unreviewedCaseNotice', ttlMs: 14 * 24 * 60 * MINUTE_MS });
|
|
@@ -37,14 +37,15 @@ async function reviewedCaseIds(recordIds) {
|
|
|
37
37
|
|
|
38
38
|
async function noticePatient(patientCode, cases, now) {
|
|
39
39
|
const { patient, doctor, identity, link } = await teamAlertIdentity({ _id: null, patientCode });
|
|
40
|
-
if (identity.testPatient) return
|
|
40
|
+
if (identity.testPatient) return 'skipped';
|
|
41
41
|
const grade = Math.max(...cases.map(gradeOf));
|
|
42
42
|
const oldest = new Date(Math.min(...cases.map((patientCase) => new Date(patientCase.createdAt).getTime())));
|
|
43
|
-
const header = `📋 Caso G${grade} sin revisión clínica · creado hace ${duration(Math.max(1, Math.round((now - oldest) / MINUTE_MS)))}`;
|
|
43
|
+
const header = `📋 ${cases.length > 1 ? 'Casos' : 'Caso'} G${grade} sin revisión clínica · creado hace ${duration(Math.max(1, Math.round((now - oldest) / MINUTE_MS)))}`;
|
|
44
44
|
const action = 'Qué hacer: abrir la revisión clínica o poner su estado en Pendientes';
|
|
45
45
|
const sent = await sendGroupNotice([header, `${patient} (${doctor})`, ...cases.map(caseLabel), action, link].join('\n'), null, [header, action].join('\n'));
|
|
46
|
-
if (
|
|
47
|
-
|
|
46
|
+
if (sent.delivered) return 'sent';
|
|
47
|
+
logger.error('[UnreviewedCase] The notice did not reach the group', { error: sent.error });
|
|
48
|
+
return 'failed';
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
async function sweepUnreviewedCases({ now = new Date() } = {}) {
|
|
@@ -52,7 +53,7 @@ async function sweepUnreviewedCases({ now = new Date() } = {}) {
|
|
|
52
53
|
const policy = responseDeadlinePolicy();
|
|
53
54
|
if (teamOpenAt(now, policy).getTime() !== now.getTime()) return 0;
|
|
54
55
|
const due = (await readUnreviewedCaseCandidates(new Date(now.getTime() - LOOKBACK_MS)))
|
|
55
|
-
.filter((patientCase) => patientCase.patientCode && isDue(patientCase, now, policy));
|
|
56
|
+
.filter((patientCase) => patientCase.isOpen && patientCase.patientCode && isDue(patientCase, now, policy));
|
|
56
57
|
if (!due.length) return 0;
|
|
57
58
|
const reviewed = await reviewedCaseIds(due.map((patientCase) => patientCase.recordId));
|
|
58
59
|
const byPatient = new Map();
|
|
@@ -63,11 +64,14 @@ async function sweepUnreviewedCases({ now = new Date() } = {}) {
|
|
|
63
64
|
}
|
|
64
65
|
let sent = 0;
|
|
65
66
|
for (const [patientCode, cases] of byPatient) {
|
|
67
|
+
let outcome = 'failed';
|
|
66
68
|
try {
|
|
67
|
-
|
|
69
|
+
outcome = await noticePatient(patientCode, cases, now);
|
|
68
70
|
} catch (error) {
|
|
69
71
|
logger.error('[UnreviewedCase] The notice failed', { error: errorOf(error) });
|
|
70
72
|
}
|
|
73
|
+
if (outcome === 'sent') sent += 1;
|
|
74
|
+
if (outcome === 'failed') await Promise.all(cases.map((patientCase) => noticed.release(patientCase.recordId)));
|
|
71
75
|
}
|
|
72
76
|
return sent;
|
|
73
77
|
}
|