@peopl-health/nexus 6.0.0-dev.16333 → 6.0.0-dev.16334
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.
|
@@ -1300,7 +1300,7 @@ async function alertWithoutEmergencyRow({ reviewId, alarmId = null, urgencyLevel
|
|
|
1300
1300
|
try {
|
|
1301
1301
|
review = await loadReview(reviewIdOf(reviewId));
|
|
1302
1302
|
// Airtable alerts the team only once its emergencies row exists; a team notice that reached no one falls back to one phone.
|
|
1303
|
-
delivery = (teamChannelConfigured() && await alertTeamWithoutEmergencyRow(review, urgencyLevel, alarmId))
|
|
1303
|
+
delivery = (await teamChannelConfigured() && await alertTeamWithoutEmergencyRow(review, urgencyLevel, alarmId))
|
|
1304
1304
|
|| await alertOneWithoutEmergencyRow(review, urgencyLevel, alarmId);
|
|
1305
1305
|
} catch (error) {
|
|
1306
1306
|
delivery = { error: redactedError(error) };
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
2
|
|
|
3
3
|
const runtimeConfig = require('../config/runtimeConfig');
|
|
4
|
+
const { Monitoreo_ID } = require('../config/airtableConfig');
|
|
4
5
|
const { logger } = require('../utils/logger');
|
|
5
6
|
const { PATIENT_CODE, redactDirectIdentifiers } = require('../utils/sanitizerUtils');
|
|
7
|
+
const { withTimeout } = require('../utils/timeoutUtils');
|
|
6
8
|
const { isWithin24HourWindow } = require('../helpers/conversationWindowHelper');
|
|
7
9
|
const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
|
|
8
10
|
|
|
11
|
+
const airtableService = require('./airtableService');
|
|
12
|
+
|
|
9
13
|
const WHAPI_MESSAGES_URL = 'https://gate.whapi.cloud/messages/text';
|
|
10
14
|
const REQUEST_TIMEOUT_MS = 15000;
|
|
11
15
|
const ALERT_TEMPLATE_KEY = 'ESCALATION_TEAM_ALERT_CONTENT_SID';
|
|
12
16
|
const NOTICE_TEMPLATE_KEY = 'ESCALATION_TEAM_NOTICE_CONTENT_SID';
|
|
17
|
+
const ROSTER_TABLE = 'equipo_guardia';
|
|
18
|
+
const ROSTER_FIELDS = ['whatsapp', 'sin_tomar'];
|
|
19
|
+
const ROSTER_TTL_MS = 5 * 60 * 1000;
|
|
20
|
+
const ROSTER_READ_TIMEOUT_MS = 5000;
|
|
13
21
|
|
|
14
22
|
const getMessaging = () => require('../core/NexusMessaging');
|
|
15
23
|
|
|
@@ -17,7 +25,11 @@ const whatsAppCodeOf = (phone) => ensureWhatsAppFormat(String(phone ?? '').repla
|
|
|
17
25
|
|
|
18
26
|
const redacted = (error) => redactDirectIdentifiers(String(error?.message || error || ''));
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
let roster = { members: null, readAt: 0 };
|
|
29
|
+
let reading = null;
|
|
30
|
+
|
|
31
|
+
// Used when the roster is unreadable or has no active phone; it ignores levels.
|
|
32
|
+
function configuredPhones() {
|
|
21
33
|
return String(runtimeConfig.get('ESCALATION_TEAM_ALERT_PHONES', '') ?? '')
|
|
22
34
|
.split(',')
|
|
23
35
|
.map((phone) => phone.trim())
|
|
@@ -26,6 +38,45 @@ function teamPhones() {
|
|
|
26
38
|
.filter((code) => PATIENT_CODE.test(code));
|
|
27
39
|
}
|
|
28
40
|
|
|
41
|
+
async function readRoster() {
|
|
42
|
+
const rows = await airtableService.getRecordByFilter(Monitoreo_ID, ROSTER_TABLE, '{activo}', null, ROSTER_FIELDS);
|
|
43
|
+
if (!Array.isArray(rows)) throw new Error('The on-call roster could not be read');
|
|
44
|
+
return rows
|
|
45
|
+
.map((row) => ({ code: whatsAppCodeOf(row.whatsapp), levels: Array.isArray(row.sin_tomar) ? row.sin_tomar : [] }))
|
|
46
|
+
.filter((member) => PATIENT_CODE.test(member.code));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function refreshRoster(now) {
|
|
50
|
+
try {
|
|
51
|
+
const members = await withTimeout(readRoster(), ROSTER_READ_TIMEOUT_MS, 'on-call roster read');
|
|
52
|
+
roster = { members: members.length ? members : null, readAt: now };
|
|
53
|
+
if (!members.length) logger.warn('[TeamAlert] The on-call roster has no active phone; using ESCALATION_TEAM_ALERT_PHONES');
|
|
54
|
+
} catch (error) {
|
|
55
|
+
roster = { ...roster, readAt: now };
|
|
56
|
+
logger.error('[TeamAlert] The on-call roster could not be read; using the last one read', { error: redacted(error) });
|
|
57
|
+
}
|
|
58
|
+
return roster.members;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The Airtable roster, read at most every 5 min by one read at a time; a failed read keeps the last one and waits as long to retry.
|
|
62
|
+
async function rosterMembers(now = Date.now()) {
|
|
63
|
+
if (roster.readAt && now - roster.readAt < ROSTER_TTL_MS) return roster.members;
|
|
64
|
+
if (!reading) reading = refreshRoster(now).finally(() => { reading = null; });
|
|
65
|
+
return reading;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// With a level, only the members who take that level's untaken alerts; without one, every active member.
|
|
69
|
+
async function teamPhones(level = null) {
|
|
70
|
+
const members = await rosterMembers();
|
|
71
|
+
if (!members) return configuredPhones();
|
|
72
|
+
return members.filter((member) => !level || member.levels.includes(level)).map((member) => member.code);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const _resetRoster = () => {
|
|
76
|
+
roster = { members: null, readAt: 0 };
|
|
77
|
+
reading = null;
|
|
78
|
+
};
|
|
79
|
+
|
|
29
80
|
// Inside the 24-hour window the plain text goes out; outside it, the approved alert template.
|
|
30
81
|
async function sendAlertTo({ code, message, operator, templateKey }) {
|
|
31
82
|
const inWindow = await isWithin24HourWindow(code);
|
|
@@ -42,7 +93,7 @@ async function sendAlertTo({ code, message, operator, templateKey }) {
|
|
|
42
93
|
|
|
43
94
|
const groupConfigured = () => Boolean(runtimeConfig.get('WHAPI_TOKEN') && runtimeConfig.get('ESCALATION_TEAM_GROUP_ID'));
|
|
44
95
|
|
|
45
|
-
const teamChannelConfigured = () => groupConfigured() || teamPhones().length > 0;
|
|
96
|
+
const teamChannelConfigured = async () => groupConfigured() || (await teamPhones()).length > 0;
|
|
46
97
|
|
|
47
98
|
async function postToGroup(body, quoted) {
|
|
48
99
|
const res = await axios({
|
|
@@ -68,13 +119,12 @@ async function sendToGroup(body, quoted = null) {
|
|
|
68
119
|
}
|
|
69
120
|
}
|
|
70
121
|
|
|
71
|
-
// Alerts the
|
|
72
|
-
|
|
73
|
-
async function sendTeamAlert({ message, operator, notice = false, quoted = null }) {
|
|
122
|
+
// Alerts the group and the on-call phones for `level` (all without one); one failed recipient never stops the others and nothing throws.
|
|
123
|
+
async function sendTeamAlert({ message, operator, level = null, notice = false, quoted = null }) {
|
|
74
124
|
const templateKey = notice ? NOTICE_TEMPLATE_KEY : ALERT_TEMPLATE_KEY;
|
|
75
125
|
const group = await sendToGroup(message.body, quoted);
|
|
76
126
|
const phones = [];
|
|
77
|
-
for (const code of teamPhones()) {
|
|
127
|
+
for (const code of await teamPhones(level)) {
|
|
78
128
|
try {
|
|
79
129
|
phones.push({ code, delivered: true, error: null, ...(await sendAlertTo({ code, message, operator, templateKey })) });
|
|
80
130
|
} catch (error) {
|
|
@@ -86,4 +136,4 @@ async function sendTeamAlert({ message, operator, notice = false, quoted = null
|
|
|
86
136
|
return { group, phones, delivered };
|
|
87
137
|
}
|
|
88
138
|
|
|
89
|
-
module.exports = { groupConfigured, sendTeamAlert, teamChannelConfigured, teamPhones };
|
|
139
|
+
module.exports = { _resetRoster, groupConfigured, sendTeamAlert, teamChannelConfigured, teamPhones };
|
|
@@ -163,7 +163,7 @@ async function alertIfUntaken(review, clock, stage, now) {
|
|
|
163
163
|
// Replies to the group message Airtable posted for this alarm, so the case reads as one thread.
|
|
164
164
|
const quoted = await readGroupMessageId(clock.emergencyRecordId, review.patientCode);
|
|
165
165
|
if (!await claimStage(review, clock, stage, now)) return false;
|
|
166
|
-
const result = await sendTeamAlert({ message, operator: OPERATOR, quoted });
|
|
166
|
+
const result = await sendTeamAlert({ message, operator: OPERATOR, level: clock.level, quoted });
|
|
167
167
|
const phonesDelivered = result.phones.filter((phone) => phone.delivered).length;
|
|
168
168
|
await getEscalationReview().updateOne(
|
|
169
169
|
{ _id: review._id, untakenAlerts: { $elemMatch: sameStage(clock, stage) } },
|