@peopl-health/nexus 5.77.0-dev.16506 → 5.77.0
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.
|
@@ -24,4 +24,4 @@ contactGateDecisionSchema.index({ createdAt: 1 }, { name: 'contact_gate_ttl_idx'
|
|
|
24
24
|
|
|
25
25
|
const ContactGateDecision = mongoose.model('ContactGateDecision', contactGateDecisionSchema);
|
|
26
26
|
|
|
27
|
-
module.exports = { ContactGateDecision
|
|
27
|
+
module.exports = { ContactGateDecision };
|
|
@@ -2,17 +2,18 @@ const moment = require('moment-timezone');
|
|
|
2
2
|
|
|
3
3
|
const runtimeConfig = require('../config/runtimeConfig');
|
|
4
4
|
const { logger } = require('../utils/logger');
|
|
5
|
+
const { withTimeout } = require('../utils/timeoutUtils');
|
|
6
|
+
const { isWithinHourWindow } = require('../utils/scheduleUtils');
|
|
5
7
|
|
|
6
8
|
const { ContactGateDecision } = require('../models/contactGateDecisionModel');
|
|
7
9
|
const { isStaffSend } = require('../helpers/staffSendHelper');
|
|
10
|
+
const { CONTACT_WINDOW } = require('../clinical/config/safetyNetConfig');
|
|
8
11
|
const { getMessages } = require('./messageService');
|
|
9
12
|
|
|
10
13
|
const CATEGORIES = ['clinical', 'triage', 'sessions', 'followup', 'invitations', 'unclassified'];
|
|
11
14
|
const EXEMPT_CATEGORIES = ['clinical', 'triage', 'sessions'];
|
|
12
15
|
const DAILY_CAP = 2;
|
|
13
16
|
const WEEKLY_CAP = 6;
|
|
14
|
-
const CONTACT_TIMEZONE = 'America/Mexico_City';
|
|
15
|
-
const CONTACT_HOURS = { start: 8, end: 21 };
|
|
16
17
|
const REPLY_WINDOW_MS = 30 * 60 * 1000;
|
|
17
18
|
const MODES = ['shadow', 'enforce'];
|
|
18
19
|
const NOT_COUNTED_STATUSES = ['blocked', 'failed'];
|
|
@@ -78,7 +79,7 @@ async function classify(messageData, now) {
|
|
|
78
79
|
const isCounted = ({ kind, category }) => kind === 'proactive' && !EXEMPT_CATEGORIES.includes(category);
|
|
79
80
|
|
|
80
81
|
async function priorCounts(code, now, ownId = null) {
|
|
81
|
-
const local = moment(now).tz(
|
|
82
|
+
const local = moment(now).tz(CONTACT_WINDOW.timeZone);
|
|
82
83
|
const counted = { code, kind: 'proactive', category: { $nin: EXEMPT_CATEGORIES }, status: { $nin: NOT_COUNTED_STATUSES } };
|
|
83
84
|
if (ownId) counted._id = { $ne: ownId };
|
|
84
85
|
const [dayCount, weekCount] = await Promise.all([
|
|
@@ -90,8 +91,7 @@ async function priorCounts(code, now, ownId = null) {
|
|
|
90
91
|
|
|
91
92
|
function verdict(classified, { dayCount, weekCount }, now) {
|
|
92
93
|
if (!isCounted(classified)) return { wouldAllow: true, reason: null };
|
|
93
|
-
|
|
94
|
-
if (hour < CONTACT_HOURS.start || hour >= CONTACT_HOURS.end) return { wouldAllow: false, reason: 'outside_hours' };
|
|
94
|
+
if (!isWithinHourWindow(now, CONTACT_WINDOW)) return { wouldAllow: false, reason: 'outside_hours' };
|
|
95
95
|
if (dayCount >= DAILY_CAP) return { wouldAllow: false, reason: 'daily_cap' };
|
|
96
96
|
if (weekCount >= WEEKLY_CAP) return { wouldAllow: false, reason: 'weekly_cap' };
|
|
97
97
|
return { wouldAllow: true, reason: null };
|
|
@@ -110,15 +110,6 @@ function decisionRow(messageData, classified, triggeredBy, now) {
|
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
function withTimeout(promise) {
|
|
114
|
-
let timer;
|
|
115
|
-
const timeout = new Promise((resolve, reject) => {
|
|
116
|
-
timer = setTimeout(() => reject(new Error('contact gate check timed out')), CHECK_TIMEOUT_MS);
|
|
117
|
-
if (typeof timer.unref === 'function') timer.unref();
|
|
118
|
-
});
|
|
119
|
-
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
113
|
// The reservation is written before counting, so of two concurrent sends at the last slot at most one goes out.
|
|
123
114
|
async function reserve(messageData, enforced, now) {
|
|
124
115
|
const triggeredBy = await triggerOf(messageData);
|
|
@@ -147,7 +138,7 @@ async function reserveSend(messageData, now = new Date()) {
|
|
|
147
138
|
if (!enforced.length || !code) return null;
|
|
148
139
|
const pending = reserve(messageData, enforced, now);
|
|
149
140
|
// A check that finishes after the send went out frees its slot; that send is recorded when it leaves.
|
|
150
|
-
return await withTimeout(pending).catch(error => {
|
|
141
|
+
return await withTimeout(pending, CHECK_TIMEOUT_MS, 'contact gate check').catch(error => {
|
|
151
142
|
pending.then(late => releaseSend(late?.decisionId), () => {});
|
|
152
143
|
throw error;
|
|
153
144
|
});
|
|
@@ -157,7 +148,6 @@ async function reserveSend(messageData, now = new Date()) {
|
|
|
157
148
|
}
|
|
158
149
|
}
|
|
159
150
|
|
|
160
|
-
// A reservation whose send never left frees its slot.
|
|
161
151
|
async function releaseSend(decisionId) {
|
|
162
152
|
if (!decisionId) return;
|
|
163
153
|
try {
|