@peopl-health/nexus 5.77.0-dev.16340 → 5.77.0-dev.16439
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.
|
@@ -880,6 +880,13 @@ function issuePaths(issues) {
|
|
|
880
880
|
return [...new Set(issues.map((issue) => String(issue.path || '').replace(/\[[^\]]*\]/g, '[]')).filter(Boolean))];
|
|
881
881
|
}
|
|
882
882
|
|
|
883
|
+
// Zod issue paths in dotted form with indices collapsed; keys are the DTO's camelCase, not the model's snake_case.
|
|
884
|
+
function dtoIssues(err) {
|
|
885
|
+
const issues = Array.isArray(err?.issues) ? err.issues : [];
|
|
886
|
+
const paths = issues.map((issue) => ({ path: arr(issue.path).map((key) => (typeof key === 'number' ? '[]' : `.${String(key)}`)).join('').replace(/^\./, '') }));
|
|
887
|
+
return paths.some((issue) => issue.path) ? paths : null;
|
|
888
|
+
}
|
|
889
|
+
|
|
883
890
|
function truncateToCardLimit(elements, repairs) {
|
|
884
891
|
const excess = elements.length - MAX_TRIAGE_CARDS;
|
|
885
892
|
if (excess <= 0) return 0;
|
|
@@ -992,7 +999,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
992
999
|
plan = buildPlan(takeaways, elements);
|
|
993
1000
|
} catch (err) {
|
|
994
1001
|
lastIssues = [{ path: 'plan', problem: err?.message || 'DTO inválido' }];
|
|
995
|
-
if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_dto_invalid'); continue; }
|
|
1002
|
+
if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_dto_invalid', dtoIssues(err)); continue; }
|
|
996
1003
|
return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_invalid', hardRejectIssues: lastIssues } });
|
|
997
1004
|
}
|
|
998
1005
|
return outcome({
|
|
@@ -26,7 +26,6 @@ const { isStaffSend } = require('../helpers/staffSendHelper');
|
|
|
26
26
|
|
|
27
27
|
const { createMessagingProvider } = require('../adapters/registry');
|
|
28
28
|
const { ensureThreadExists } = require('../services/threadService');
|
|
29
|
-
const { observeSend } = require('../services/contactGateService');
|
|
30
29
|
|
|
31
30
|
const { hasPreprocessingHandler, runPreprocessingHandler } = require('../services/preprocessingService');
|
|
32
31
|
const { getMessages, updateMessage } = require('../services/messageService');
|
|
@@ -244,7 +243,6 @@ class NexusMessaging {
|
|
|
244
243
|
}
|
|
245
244
|
|
|
246
245
|
if (messageData.code) ensureThreadExists(messageData.code);
|
|
247
|
-
if (result?.success === true) observeSend(messageData);
|
|
248
246
|
|
|
249
247
|
return result;
|
|
250
248
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -942,13 +942,6 @@ declare module '@peopl-health/nexus' {
|
|
|
942
942
|
provider: (context: { now: Date }) => SupportDashboardSnapshot | Promise<SupportDashboardSnapshot>
|
|
943
943
|
): void;
|
|
944
944
|
|
|
945
|
-
export type ContactCategory = 'clinical' | 'triage' | 'sessions' | 'followup' | 'invitations' | 'unclassified';
|
|
946
|
-
|
|
947
|
-
export function registerContactCategories(categories: {
|
|
948
|
-
contentSids?: Record<string, ContactCategory>;
|
|
949
|
-
triggeredBy?: Record<string, ContactCategory>;
|
|
950
|
-
}): void;
|
|
951
|
-
|
|
952
945
|
export const supportCandidateService: {
|
|
953
946
|
updateSupportOperationalReview(request: {
|
|
954
947
|
patientId: string;
|
package/lib/index.js
CHANGED
|
@@ -54,7 +54,6 @@ const { registerTool, hasTool, getToolSchemas, clearRegistry } = require('./serv
|
|
|
54
54
|
const { MapCache } = require('./utils/MapCache');
|
|
55
55
|
const supportCandidateService = require('./clinical/services/supportCandidateService');
|
|
56
56
|
const { registerDashboardProvider } = require('./services/dashboard-provider-registry');
|
|
57
|
-
const { registerContactCategories } = require('./services/contactGateService');
|
|
58
57
|
const supportActionService = require('./clinical/services/supportActionService');
|
|
59
58
|
const supportLightService = require('./clinical/services/supportLightService');
|
|
60
59
|
const supportInvitability = require('./clinical/services/supportInvitability');
|
|
@@ -396,7 +395,6 @@ module.exports = {
|
|
|
396
395
|
MapCache,
|
|
397
396
|
supportCandidateService,
|
|
398
397
|
registerDashboardProvider,
|
|
399
|
-
registerContactCategories,
|
|
400
398
|
supportActionService,
|
|
401
399
|
supportLightService,
|
|
402
400
|
supportInvitability,
|
package/package.json
CHANGED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const mongoose = require('mongoose');
|
|
2
|
-
|
|
3
|
-
const CONTACT_GATE_TTL_SECONDS = 90 * 24 * 60 * 60;
|
|
4
|
-
|
|
5
|
-
const contactGateDecisionSchema = new mongoose.Schema({
|
|
6
|
-
code: { type: String, required: true },
|
|
7
|
-
contactCode: { type: String, default: null },
|
|
8
|
-
kind: { type: String, enum: ['operator', 'reply', 'proactive'], required: true },
|
|
9
|
-
category: { type: String, default: null },
|
|
10
|
-
mode: { type: String, enum: ['shadow'], required: true },
|
|
11
|
-
wouldAllow: { type: Boolean, required: true },
|
|
12
|
-
reason: { type: String, default: null },
|
|
13
|
-
dayCount: { type: Number, default: 0 },
|
|
14
|
-
weekCount: { type: Number, default: 0 },
|
|
15
|
-
sendKind: { type: String, default: null },
|
|
16
|
-
triggeredBy: { type: String, default: null },
|
|
17
|
-
contentSid: { type: String, default: null },
|
|
18
|
-
createdAt: { type: Date, default: Date.now }
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
contactGateDecisionSchema.index({ code: 1, kind: 1, createdAt: -1 }, { name: 'contact_gate_code_kind_idx' });
|
|
22
|
-
contactGateDecisionSchema.index({ createdAt: 1 }, { name: 'contact_gate_ttl_idx', expireAfterSeconds: CONTACT_GATE_TTL_SECONDS });
|
|
23
|
-
|
|
24
|
-
const ContactGateDecision = mongoose.model('ContactGateDecision', contactGateDecisionSchema);
|
|
25
|
-
|
|
26
|
-
module.exports = { ContactGateDecision, CONTACT_GATE_TTL_SECONDS };
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
const moment = require('moment-timezone');
|
|
2
|
-
|
|
3
|
-
const runtimeConfig = require('../config/runtimeConfig');
|
|
4
|
-
const { logger } = require('../utils/logger');
|
|
5
|
-
|
|
6
|
-
const { ContactGateDecision } = require('../models/contactGateDecisionModel');
|
|
7
|
-
const { isStaffSend } = require('../helpers/staffSendHelper');
|
|
8
|
-
const { getMessages } = require('./messageService');
|
|
9
|
-
|
|
10
|
-
const CATEGORIES = ['clinical', 'triage', 'sessions', 'followup', 'invitations', 'unclassified'];
|
|
11
|
-
const EXEMPT_CATEGORIES = ['clinical'];
|
|
12
|
-
const DAILY_CAP = 2;
|
|
13
|
-
const WEEKLY_CAP = 6;
|
|
14
|
-
const CONTACT_TIMEZONE = 'America/Mexico_City';
|
|
15
|
-
const CONTACT_HOURS = { start: 8, end: 21 };
|
|
16
|
-
const REPLY_WINDOW_MS = 30 * 60 * 1000;
|
|
17
|
-
const NEXUS_TRIGGER_CATEGORIES = {
|
|
18
|
-
paused_safety_net: 'clinical',
|
|
19
|
-
seguimiento: 'followup',
|
|
20
|
-
followup: 'followup',
|
|
21
|
-
send_reminder: 'followup'
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const registered = { contentSids: {}, triggeredBy: {} };
|
|
25
|
-
|
|
26
|
-
function registerContactCategories({ contentSids = {}, triggeredBy = {} } = {}) {
|
|
27
|
-
const unknown = [...Object.values(contentSids), ...Object.values(triggeredBy)].find(value => !CATEGORIES.includes(value));
|
|
28
|
-
if (unknown !== undefined) throw new Error(`Unknown contact category: ${unknown}`);
|
|
29
|
-
Object.assign(registered.contentSids, contentSids);
|
|
30
|
-
Object.assign(registered.triggeredBy, triggeredBy);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function contactGateMode() {
|
|
34
|
-
return runtimeConfig.get('CONTACT_GATE_MODE') === 'shadow' ? 'shadow' : 'off';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function triggerSource(triggeredBy) {
|
|
38
|
-
const trigger = String(triggeredBy || '');
|
|
39
|
-
return trigger.startsWith('followup_') ? 'followup' : trigger.split(':')[0];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function categoryOf({ contentSid, triggeredBy }) {
|
|
43
|
-
const source = triggerSource(triggeredBy);
|
|
44
|
-
return registered.contentSids[contentSid] || registered.triggeredBy[source] || NEXUS_TRIGGER_CATEGORIES[source] || 'unclassified';
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// A released or recovery send carries no trigger of its own; it inherits the one of the message it delivers.
|
|
48
|
-
async function triggerOf({ triggeredBy, parentMessageId }) {
|
|
49
|
-
if (triggeredBy || !parentMessageId) return triggeredBy || null;
|
|
50
|
-
const [parent] = await getMessages({ _id: parentMessageId }, { select: 'triggeredBy', limit: 1 });
|
|
51
|
-
return parent?.triggeredBy || null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async function classify(messageData, now) {
|
|
55
|
-
if (isStaffSend(messageData.triggeredBy)) return { kind: 'operator', category: null };
|
|
56
|
-
const [inbound] = await getMessages(
|
|
57
|
-
{
|
|
58
|
-
numero: messageData.code,
|
|
59
|
-
contact_code: messageData.contactCode || null,
|
|
60
|
-
from_me: false,
|
|
61
|
-
createdAt: { $gte: new Date(now.getTime() - REPLY_WINDOW_MS) }
|
|
62
|
-
},
|
|
63
|
-
{ select: '_id', limit: 1 }
|
|
64
|
-
);
|
|
65
|
-
return { kind: inbound ? 'reply' : 'proactive', category: categoryOf(messageData) };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const isCounted = ({ kind, category }) => kind === 'proactive' && !EXEMPT_CATEGORIES.includes(category);
|
|
69
|
-
|
|
70
|
-
async function priorCounts(code, now) {
|
|
71
|
-
const local = moment(now).tz(CONTACT_TIMEZONE);
|
|
72
|
-
const counted = { code, kind: 'proactive', category: { $nin: EXEMPT_CATEGORIES } };
|
|
73
|
-
const [dayCount, weekCount] = await Promise.all([
|
|
74
|
-
ContactGateDecision.countDocuments({ ...counted, createdAt: { $gte: local.clone().startOf('day').toDate() } }),
|
|
75
|
-
ContactGateDecision.countDocuments({ ...counted, createdAt: { $gte: local.clone().startOf('isoWeek').toDate() } })
|
|
76
|
-
]);
|
|
77
|
-
return { dayCount, weekCount };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function verdict(classified, { dayCount, weekCount }, now) {
|
|
81
|
-
if (!isCounted(classified)) return { wouldAllow: true, reason: null };
|
|
82
|
-
const hour = moment(now).tz(CONTACT_TIMEZONE).hour();
|
|
83
|
-
if (hour < CONTACT_HOURS.start || hour >= CONTACT_HOURS.end) return { wouldAllow: false, reason: 'outside_hours' };
|
|
84
|
-
if (dayCount >= DAILY_CAP) return { wouldAllow: false, reason: 'daily_cap' };
|
|
85
|
-
if (weekCount >= WEEKLY_CAP) return { wouldAllow: false, reason: 'weekly_cap' };
|
|
86
|
-
return { wouldAllow: true, reason: null };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Shadow mode only records what the gate would have decided about a send that already went out; it never blocks one.
|
|
90
|
-
async function observeSend(messageData, now = new Date()) {
|
|
91
|
-
const code = messageData?.code;
|
|
92
|
-
try {
|
|
93
|
-
if (contactGateMode() !== 'shadow' || !code) return null;
|
|
94
|
-
const triggeredBy = await triggerOf(messageData);
|
|
95
|
-
const classified = await classify({ ...messageData, triggeredBy }, now);
|
|
96
|
-
const counts = isCounted(classified) ? await priorCounts(code, now) : { dayCount: 0, weekCount: 0 };
|
|
97
|
-
return await ContactGateDecision.create({
|
|
98
|
-
code,
|
|
99
|
-
...classified,
|
|
100
|
-
mode: 'shadow',
|
|
101
|
-
...verdict(classified, counts, now),
|
|
102
|
-
...counts,
|
|
103
|
-
contactCode: messageData.contactCode || null,
|
|
104
|
-
sendKind: messageData.kind || null,
|
|
105
|
-
triggeredBy,
|
|
106
|
-
contentSid: messageData.contentSid || null,
|
|
107
|
-
createdAt: now
|
|
108
|
-
});
|
|
109
|
-
} catch (error) {
|
|
110
|
-
logger.warn('[contactGate] Shadow decision was not recorded', { code, error: error.message });
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
module.exports = { registerContactCategories, observeSend };
|