@peopl-health/nexus 5.74.0-dev.15344 → 5.74.0-dev.15345
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,6 +1,7 @@
|
|
|
1
1
|
const moment = require('moment-timezone');
|
|
2
2
|
|
|
3
3
|
const { getMessages } = require('../../services/messageService');
|
|
4
|
+
const { checkInsPausedUntil } = require('../../services/threadService');
|
|
4
5
|
const { DEFAULT_TIMEZONE } = require('../../utils/scheduleUtils');
|
|
5
6
|
const { readActiveNets, escalationUrgencyOf } = require('./contingencyDispatchService');
|
|
6
7
|
const { readPendingReminderForPatient } = require('./clinicalAirtableService');
|
|
@@ -73,6 +74,7 @@ async function planCheckInDue({ code, reminderId, type, question = '', firstName
|
|
|
73
74
|
if (MANDATORY_URGENCIES.has(urgency)) {
|
|
74
75
|
return { mode: 'mandatory', planId, instruction: mandatoryInstruction(firstName, netQuestion) };
|
|
75
76
|
}
|
|
77
|
+
if (await checkInsPausedUntil(code, now)) return { mode: 'stale', reason: 'patient_paused', planId };
|
|
76
78
|
checkIn = {
|
|
77
79
|
type,
|
|
78
80
|
question: netQuestion,
|
|
@@ -86,6 +88,7 @@ async function planCheckInDue({ code, reminderId, type, question = '', firstName
|
|
|
86
88
|
|
|
87
89
|
const plan = await readPendingReminderForPatient({ code, reminderId });
|
|
88
90
|
if (!plan) return { mode: 'stale', reason: 'plan_not_pending' };
|
|
91
|
+
if (await checkInsPausedUntil(code, now)) return { mode: 'stale', reason: 'patient_paused' };
|
|
89
92
|
checkIn = { type, question: plan.message || question, concern: plan.windowReasoning, awaiting: null, armedAt: null, urgency: null };
|
|
90
93
|
return { mode: 'decide', planId: null, brief: await briefFor(code, now, checkIn) };
|
|
91
94
|
}
|
|
@@ -2,12 +2,13 @@ const moment = require('moment-timezone');
|
|
|
2
2
|
|
|
3
3
|
const { isShadowRun } = require('../helpers/shadowHelper');
|
|
4
4
|
const { dispatchReminder } = require('../services/reminderDispatchService');
|
|
5
|
-
const { recordReminder, readPendingFollowUpsForPatient, cancelFollowUp } = require('../services/clinicalAirtableService');
|
|
5
|
+
const { recordReminder, readPendingFollowUpsForPatient, cancelFollowUp, cancelPendingFollowUps } = require('../services/clinicalAirtableService');
|
|
6
6
|
const { isWithinTriageProtectionWindow } = require('../services/outreachSweepService');
|
|
7
7
|
const { UTILITY_BASES, sanitizeVariable } = require('../services/outreachCarrierService');
|
|
8
8
|
const { logger } = require('../../utils/logger');
|
|
9
9
|
const { DEFAULT_TIMEZONE, isWithinHourWindow } = require('../../utils/scheduleUtils');
|
|
10
10
|
const { botIsPaused } = require('../../helpers/pausedThreadHelper');
|
|
11
|
+
const { setCheckInsPausedUntil, checkInsPausedUntil } = require('../../services/threadService');
|
|
11
12
|
|
|
12
13
|
const definition = {
|
|
13
14
|
name: 'schedulePatientReminder',
|
|
@@ -31,8 +32,8 @@ const definition = {
|
|
|
31
32
|
},
|
|
32
33
|
reminder_action_type: {
|
|
33
34
|
type: 'string',
|
|
34
|
-
enum: ['create', 'update', 'cancel'],
|
|
35
|
-
description: '\'create\' = new reminder. \'update\' = modify an existing reminder (change date/time/description) — pass its id in `reminder_id`. \'cancel\' = retire a pending seguimiento that the conversation already answered or made unnecessary — pass its id in `reminder_id`; the date and description are ignored.',
|
|
35
|
+
enum: ['create', 'update', 'cancel', 'pause_checkins'],
|
|
36
|
+
description: '\'create\' = new reminder. \'update\' = modify an existing reminder (change date/time/description) — pass its id in `reminder_id`. \'cancel\' = retire a pending seguimiento that the conversation already answered or made unnecessary — pass its id in `reminder_id`; the date and description are ignored. \'pause_checkins\' = the patient asked to receive fewer messages: no seguimiento or contingency check-in reaches the patient until `reminder_date` (at most 30 days ahead; 14 days if the patient gave no date), pending seguimientos are retired, and the patient can still write at any time; put the request in `reminder_description`. stat/asap escalation check-ins and the patient\'s own medication, appointment or exam reminders are not paused.',
|
|
36
37
|
},
|
|
37
38
|
reminder_id: {
|
|
38
39
|
type: ['string', 'null'],
|
|
@@ -53,6 +54,8 @@ const REMINDER_TYPES = definition.parameters.properties.reminder_type.enum;
|
|
|
53
54
|
const ACTION_TYPES = definition.parameters.properties.reminder_action_type.enum;
|
|
54
55
|
|
|
55
56
|
const MAX_FOLLOW_UP_LEAD_HOURS = 48;
|
|
57
|
+
const DEFAULT_PAUSE_DAYS = 14;
|
|
58
|
+
const MAX_PAUSE_DAYS = 30;
|
|
56
59
|
const ROUTINE_OUTREACH_START_HOUR = 11;
|
|
57
60
|
const ROUTINE_OUTREACH_END_HOUR = 21;
|
|
58
61
|
const PAUSED_ERROR = 'thread_paused — do not schedule a seguimiento in this turn.';
|
|
@@ -84,6 +87,7 @@ async function handler(args = {}, context = {}) {
|
|
|
84
87
|
|
|
85
88
|
const requestedAction = (args?.reminder_action_type || '').toLowerCase();
|
|
86
89
|
if (requestedAction === 'cancel') return await cancelSeguimiento(args, runtime);
|
|
90
|
+
if (requestedAction === 'pause_checkins') return await pauseCheckIns(args, runtime);
|
|
87
91
|
|
|
88
92
|
const reminderDate = (args?.reminder_date || '').trim();
|
|
89
93
|
const description = sanitizeVariable(args?.reminder_description || '');
|
|
@@ -135,6 +139,12 @@ async function handler(args = {}, context = {}) {
|
|
|
135
139
|
if (isRoutineOutreach && !isWithinHourWindow(fireMoment.toDate(), { timeZone, startHour: ROUTINE_OUTREACH_START_HOUR, endHour: ROUTINE_OUTREACH_END_HOUR })) {
|
|
136
140
|
return JSON.stringify({ success: false, error: `routine outreach must go out between ${ROUTINE_OUTREACH_START_HOUR}:00 and ${ROUTINE_OUTREACH_END_HOUR}:00 patient time; choose a time in that range.`, data: {} });
|
|
137
141
|
}
|
|
142
|
+
if (isRoutineOutreach && runtime.patientCode) {
|
|
143
|
+
const pausedUntil = await checkInsPausedUntil(runtime.patientCode);
|
|
144
|
+
if (pausedUntil && fireMoment.toDate() < pausedUntil) {
|
|
145
|
+
return JSON.stringify({ success: false, error: 'checkins_paused — the patient asked for fewer messages; do not schedule a seguimiento before the pause ends.', data: { paused_until: moment(pausedUntil).tz(timeZone).format('YYYY-MM-DDTHH:mm') } });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
138
148
|
if (isRoutineOutreach && actionType === 'create' && runtime.patientCode) {
|
|
139
149
|
if (await botIsPaused(runtime.patientCode, { label: '[schedulePatientReminder]' })) {
|
|
140
150
|
return JSON.stringify({ success: false, error: PAUSED_ERROR, data: {} });
|
|
@@ -226,6 +236,30 @@ async function handler(args = {}, context = {}) {
|
|
|
226
236
|
}
|
|
227
237
|
}
|
|
228
238
|
|
|
239
|
+
async function pauseCheckIns(args, runtime) {
|
|
240
|
+
const { patientCode, trace } = runtime;
|
|
241
|
+
const now = moment.tz(DEFAULT_TIMEZONE);
|
|
242
|
+
const rawDate = String(args?.reminder_date || '').trim();
|
|
243
|
+
const until = rawDate
|
|
244
|
+
? moment.tz(rawDate, ['YYYY-MM-DDTHH:mm:ss', 'YYYY-MM-DDTHH:mm', 'YYYY-MM-DD'], true, DEFAULT_TIMEZONE)
|
|
245
|
+
: now.clone().add(DEFAULT_PAUSE_DAYS, 'days');
|
|
246
|
+
if (!until.isValid() || !until.isAfter(now)) {
|
|
247
|
+
return JSON.stringify({ success: false, error: 'pause_checkins reminder_date must be a future patient-local date (YYYY-MM-DDTHH:mm), or empty for 14 days.', data: {} });
|
|
248
|
+
}
|
|
249
|
+
if (until.isAfter(now.clone().add(MAX_PAUSE_DAYS, 'days'))) {
|
|
250
|
+
return JSON.stringify({ success: false, error: `pause_checkins reminder_date must be at most ${MAX_PAUSE_DAYS} days ahead.`, data: {} });
|
|
251
|
+
}
|
|
252
|
+
if (patientCode && !isShadowRun()) {
|
|
253
|
+
await setCheckInsPausedUntil(patientCode, until.toDate());
|
|
254
|
+
if (!await cancelPendingFollowUps({ code: patientCode, trace, reason: 'patient_paused' })) {
|
|
255
|
+
logger.warn('[schedulePatientReminder] pending seguimientos were not retired on pause', { patientCode });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const existing = Array.isArray(trace.signals?.reminderIntents) ? trace.signals.reminderIntents : [];
|
|
259
|
+
trace.setSignals({ reminderIntents: [...existing, { reminderId: null, reminderType: 'seguimiento', actionType: 'pause_checkins', pausedUntil: until.toISOString(), channel: 'whatsapp' }] });
|
|
260
|
+
return JSON.stringify({ success: true, data: { action_type: 'pause_checkins', paused_until: until.format('YYYY-MM-DDTHH:mm'), time_zone: DEFAULT_TIMEZONE } });
|
|
261
|
+
}
|
|
262
|
+
|
|
229
263
|
async function cancelSeguimiento(args, runtime) {
|
|
230
264
|
const reminderId = typeof args?.reminder_id === 'string' ? args.reminder_id.trim() : '';
|
|
231
265
|
if (!reminderId) {
|
|
@@ -1133,6 +1133,7 @@ class NexusMessaging {
|
|
|
1133
1133
|
plan = await planCheckInDue({ code, reminderId, type, question, firstName });
|
|
1134
1134
|
if (plan.mode === 'stale') {
|
|
1135
1135
|
await settleCheckInRow({ code, reminderId, reason: plan.reason });
|
|
1136
|
+
await this._fireCheckInNet(code, plan.planId, reminderId);
|
|
1136
1137
|
await completeSendClaim(claim.claimId);
|
|
1137
1138
|
return { decision: 'skipped', holdReason: plan.reason, sent: false };
|
|
1138
1139
|
}
|
|
@@ -11,6 +11,7 @@ const threadSchema = new mongoose.Schema({
|
|
|
11
11
|
nombre: { type: String, default: null },
|
|
12
12
|
stopped: { type: Boolean, default: false },
|
|
13
13
|
outreachOptOut: { type: Boolean, default: false },
|
|
14
|
+
checkInsPausedUntil: { type: Date, default: null },
|
|
14
15
|
pausedAt: { type: Date, default: null },
|
|
15
16
|
deceasedAt: { type: Date, default: null },
|
|
16
17
|
programStatusBeforeDeath: { type: String, default: null },
|
|
@@ -51,6 +51,13 @@ const switchThreadStoppedStatus = async (code, stopped, { byOperator = false } =
|
|
|
51
51
|
|
|
52
52
|
const setThreadDoNotAlert = async (code, doNotAlert, through = null) => Thread.updateMany({ code }, { $set: { doNotAlertThrough: doNotAlert ? (through || new Date()) : null } });
|
|
53
53
|
|
|
54
|
+
const setCheckInsPausedUntil = async (code, until) => Thread.updateMany({ code }, { $set: { checkInsPausedUntil: until } });
|
|
55
|
+
|
|
56
|
+
const checkInsPausedUntil = async (code, now = new Date()) => {
|
|
57
|
+
const thread = await Thread.findOne({ code, checkInsPausedUntil: { $gt: now } }).select('checkInsPausedUntil').lean();
|
|
58
|
+
return thread?.checkInsPausedUntil || null;
|
|
59
|
+
};
|
|
60
|
+
|
|
54
61
|
const setThreadPromptId = async (code, promptId) => {
|
|
55
62
|
const updateFields = {};
|
|
56
63
|
Thread.setAssistantId(updateFields, promptId);
|
|
@@ -110,5 +117,7 @@ module.exports = {
|
|
|
110
117
|
ensureThreadExists,
|
|
111
118
|
switchThreadStoppedStatus,
|
|
112
119
|
setThreadDoNotAlert,
|
|
113
|
-
setThreadPromptId
|
|
120
|
+
setThreadPromptId,
|
|
121
|
+
setCheckInsPausedUntil,
|
|
122
|
+
checkInsPausedUntil
|
|
114
123
|
};
|