@peopl-health/nexus 5.56.0-dev.8546 → 5.56.0-dev.8568
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.
|
@@ -7,7 +7,7 @@ const runtimeConfig = require('../../config/runtimeConfig');
|
|
|
7
7
|
const airtableService = require('../../services/airtableService');
|
|
8
8
|
const { logger } = require('../../utils/logger');
|
|
9
9
|
const { fieldEquals, recordIdIn } = require('../../helpers/airtableFormulaHelper');
|
|
10
|
-
const { CLOSED_REQUEST_STATUSES } = require('../../helpers/patientTaskRulesHelper');
|
|
10
|
+
const { ATTENTION_REQUEST_STATUS, CLOSED_REQUEST_STATUSES } = require('../../helpers/patientTaskRulesHelper');
|
|
11
11
|
const { phoneDigits } = require('../../helpers/whatsappHelper');
|
|
12
12
|
const { WriteClaimStore } = require('./writeClaimStore');
|
|
13
13
|
|
|
@@ -98,11 +98,12 @@ async function findIntakePatientRecordId(code) {
|
|
|
98
98
|
async function writeUnresolvedRequest({ code, trace, question, details, resolutionAttempted = false, forDoctorOffice = false, category = null, urgency = null, replacesClosedTask = false }) {
|
|
99
99
|
const fingerprint = createHash('sha1').update(`${code}\n${category || ''}\n${question || ''}`).digest('hex');
|
|
100
100
|
let recordId = null;
|
|
101
|
+
const duplicate = !replacesClosedTask && await recentUnresolvedCache.get(code) === fingerprint;
|
|
101
102
|
const written = await auditedWrite({
|
|
102
103
|
trace,
|
|
103
104
|
code,
|
|
104
105
|
table: 'unresolved_questions',
|
|
105
|
-
skip:
|
|
106
|
+
skip: duplicate ? 'duplicate' : null,
|
|
106
107
|
write: async () => {
|
|
107
108
|
const patientId = await findMonitoreoPatientRecordId(code);
|
|
108
109
|
const fields = {
|
|
@@ -121,7 +122,7 @@ async function writeUnresolvedRequest({ code, trace, question, details, resoluti
|
|
|
121
122
|
},
|
|
122
123
|
});
|
|
123
124
|
if (written) await recentUnresolvedCache.set(code, fingerprint);
|
|
124
|
-
return { written, recordId };
|
|
125
|
+
return { written, recordId, duplicate };
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
async function recordUnresolvedRequest(request) {
|
|
@@ -134,7 +135,7 @@ function conversationLink(code) {
|
|
|
134
135
|
return base && digits ? `${base}/c/${digits}` : null;
|
|
135
136
|
}
|
|
136
137
|
|
|
137
|
-
function
|
|
138
|
+
function formatOpsAlert(code, { label, summary }) {
|
|
138
139
|
const digits = phoneDigits(code);
|
|
139
140
|
const link = conversationLink(code);
|
|
140
141
|
return `${label}${digits ? ` (+${digits})` : ''}: ${summary}.${link ? ` Revisar: ${link}` : ''}`;
|
|
@@ -160,6 +161,19 @@ async function addToOpenAlert(code, open, question) {
|
|
|
160
161
|
return { handled: !written || matched, written: written && matched };
|
|
161
162
|
}
|
|
162
163
|
|
|
164
|
+
async function isTaskAttended(recordId) {
|
|
165
|
+
if (!RECORD_ID_RX.test(recordId || '')) return null;
|
|
166
|
+
try {
|
|
167
|
+
const [task] = await airtableService.getRecordByFilter(Monitoreo_ID, 'unresolved_questions', recordIdIn([recordId]), null, ['status']);
|
|
168
|
+
if (!task) return true;
|
|
169
|
+
const status = String(task.status || '').trim();
|
|
170
|
+
return status !== '' && status !== ATTENTION_REQUEST_STATUS;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
logger.warn('[clinicalAirtableService] Could not read whether the ops task was attended', { recordId, error: error.message });
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
163
177
|
async function recordPausedThreadMessage({ code, text, alert = OPS_ALERTS.paused }) {
|
|
164
178
|
const question = text || 'Envió un archivo sin texto.';
|
|
165
179
|
const open = await recentPausedAlertCache.get(code);
|
|
@@ -174,7 +188,7 @@ async function recordPausedThreadMessage({ code, text, alert = OPS_ALERTS.paused
|
|
|
174
188
|
code,
|
|
175
189
|
trace: null,
|
|
176
190
|
question,
|
|
177
|
-
details:
|
|
191
|
+
details: formatOpsAlert(code, alert),
|
|
178
192
|
category: 'contact_team_member',
|
|
179
193
|
urgency: 'soon',
|
|
180
194
|
replacesClosedTask,
|
|
@@ -558,7 +572,10 @@ async function recordClusterRecommendationsMirror({ code, triageRecordId, step1
|
|
|
558
572
|
|
|
559
573
|
module.exports = {
|
|
560
574
|
OPS_ALERTS,
|
|
575
|
+
formatOpsAlert,
|
|
576
|
+
isTaskAttended,
|
|
561
577
|
recordUnresolvedRequest,
|
|
578
|
+
writeUnresolvedRequest,
|
|
562
579
|
recordPausedThreadMessage,
|
|
563
580
|
recordEmergency,
|
|
564
581
|
recordClinicalIntake,
|
|
@@ -10,11 +10,25 @@ const { SafetyNetPledge } = require('../../models/safetyNetPledgeModel');
|
|
|
10
10
|
|
|
11
11
|
const { getMessages } = require('../../services/messageService');
|
|
12
12
|
|
|
13
|
-
const { recordUnresolvedRequest } = require('./clinicalAirtableService');
|
|
13
|
+
const { formatOpsAlert, isTaskAttended, recordUnresolvedRequest } = require('./clinicalAirtableService');
|
|
14
14
|
|
|
15
15
|
const SWEEP_LIMIT = 200;
|
|
16
16
|
const BREACH_CATEGORY = 'contact_team_member';
|
|
17
17
|
const BREACH_QUESTION = 'El paciente sigue esperando el contacto que se le prometió';
|
|
18
|
+
const ONBOARDING_BREACH_QUESTION = 'El contacto sigue esperando la llamada de bienvenida';
|
|
19
|
+
|
|
20
|
+
const BREACHES = {
|
|
21
|
+
safety_net: {
|
|
22
|
+
question: BREACH_QUESTION,
|
|
23
|
+
label: 'Contacto prometido sin cumplir',
|
|
24
|
+
summary: (waitedMinutes) => `pidió que lo contactaran y lleva ${waitedMinutes} minutos sin respuesta`,
|
|
25
|
+
},
|
|
26
|
+
onboarding: {
|
|
27
|
+
question: ONBOARDING_BREACH_QUESTION,
|
|
28
|
+
label: 'Llamada de bienvenida sin hacer',
|
|
29
|
+
summary: (waitedMinutes) => `pidió la llamada de bienvenida y lleva ${waitedMinutes} minutos sin que nadie lo contacte`,
|
|
30
|
+
},
|
|
31
|
+
};
|
|
18
32
|
|
|
19
33
|
const DELIVERY_GRACE_MS = 10 * 60 * 1000;
|
|
20
34
|
const HOUR_MS = 60 * 60 * 1000;
|
|
@@ -40,21 +54,28 @@ function pledgeDueAt(pledgedAt) {
|
|
|
40
54
|
return { dueAt: addContactHours(pledgedAt, DEFAULT_PLEDGE_MS / HOUR_MS, CONTACT_WINDOW) };
|
|
41
55
|
}
|
|
42
56
|
|
|
43
|
-
async function openPledge({ code }) {
|
|
57
|
+
async function openPledge({ code, kind = 'safety_net', taskRecordId = null }) {
|
|
44
58
|
const pledgedAt = new Date();
|
|
45
59
|
const { dueAt } = pledgeDueAt(pledgedAt);
|
|
46
|
-
const pledge = await SafetyNetPledge.create({
|
|
47
|
-
|
|
60
|
+
const pledge = await SafetyNetPledge.create({
|
|
61
|
+
code,
|
|
62
|
+
pledgedAt,
|
|
63
|
+
dueAt,
|
|
64
|
+
...(kind === 'safety_net' ? {} : { kind }),
|
|
65
|
+
...(taskRecordId ? { taskRecordId } : {}),
|
|
66
|
+
});
|
|
67
|
+
logger.info('[SafetyNetPledge] Contact pledged', { code, kind, dueAt });
|
|
48
68
|
return { pledgeId: pledge._id, dueAt };
|
|
49
69
|
}
|
|
50
70
|
|
|
51
71
|
async function reportBreach(pledge) {
|
|
52
72
|
const waitedMinutes = Math.round((Date.now() - new Date(pledge.pledgedAt).getTime()) / 60000);
|
|
73
|
+
const breach = BREACHES[pledge.kind] || BREACHES.safety_net;
|
|
53
74
|
return recordUnresolvedRequest({
|
|
54
75
|
code: pledge.code,
|
|
55
76
|
trace: null,
|
|
56
|
-
question:
|
|
57
|
-
details:
|
|
77
|
+
question: breach.question,
|
|
78
|
+
details: formatOpsAlert(pledge.code, { label: breach.label, summary: breach.summary(waitedMinutes) }),
|
|
58
79
|
category: BREACH_CATEGORY,
|
|
59
80
|
urgency: 'urgent'
|
|
60
81
|
});
|
|
@@ -83,7 +104,7 @@ async function sweepSafetyNetPledges() {
|
|
|
83
104
|
state: { $in: ['pending', 'reporting'] },
|
|
84
105
|
dueAt: { $lte: new Date() },
|
|
85
106
|
})
|
|
86
|
-
.select({ _id: 1, code: 1, pledgedAt: 1 })
|
|
107
|
+
.select({ _id: 1, code: 1, kind: 1, taskRecordId: 1, pledgedAt: 1 })
|
|
87
108
|
.limit(SWEEP_LIMIT)
|
|
88
109
|
.lean();
|
|
89
110
|
|
|
@@ -98,6 +119,18 @@ async function sweepSafetyNetPledges() {
|
|
|
98
119
|
continue;
|
|
99
120
|
}
|
|
100
121
|
|
|
122
|
+
if (pledge.kind === 'onboarding' && pledge.taskRecordId) {
|
|
123
|
+
const attended = await isTaskAttended(pledge.taskRecordId);
|
|
124
|
+
if (attended) {
|
|
125
|
+
await settle(pledge._id, 'reporting', 'met');
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (attended === null) {
|
|
129
|
+
await returnToPending(pledge._id);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
101
134
|
if (await humanRepliedSince(pledge.code, pledge.pledgedAt)) {
|
|
102
135
|
await returnToPending(pledge._id);
|
|
103
136
|
continue;
|
|
@@ -135,5 +168,6 @@ module.exports = {
|
|
|
135
168
|
openPledge,
|
|
136
169
|
pledgeDueAt,
|
|
137
170
|
sweepSafetyNetPledges,
|
|
138
|
-
BREACH_QUESTION
|
|
171
|
+
BREACH_QUESTION,
|
|
172
|
+
ONBOARDING_BREACH_QUESTION
|
|
139
173
|
};
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
|
|
3
3
|
const PLEDGE_STATES = ['pending', 'reporting', 'met', 'breached'];
|
|
4
|
+
const PLEDGE_KINDS = ['safety_net', 'onboarding'];
|
|
4
5
|
|
|
5
6
|
const safetyNetPledgeSchema = new mongoose.Schema({
|
|
6
7
|
code: { type: String, required: true },
|
|
8
|
+
kind: { type: String, enum: PLEDGE_KINDS, default: 'safety_net' },
|
|
9
|
+
taskRecordId: { type: String, default: null },
|
|
7
10
|
pledgedAt: { type: Date, required: true },
|
|
8
11
|
dueAt: { type: Date, required: true },
|
|
9
12
|
state: { type: String, enum: PLEDGE_STATES, default: 'pending' },
|
|
@@ -16,4 +19,4 @@ safetyNetPledgeSchema.index({ state: 1, dueAt: 1 }, { name: 'safety_net_pledge_d
|
|
|
16
19
|
|
|
17
20
|
const SafetyNetPledge = mongoose.model('SafetyNetPledge', safetyNetPledgeSchema);
|
|
18
21
|
|
|
19
|
-
module.exports = { SafetyNetPledge, PLEDGE_STATES };
|
|
22
|
+
module.exports = { SafetyNetPledge, PLEDGE_STATES, PLEDGE_KINDS };
|