@peopl-health/nexus 5.54.3 → 5.54.5
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.
- package/lib/clinical/config/safetyNetConfig.js +2 -0
- package/lib/clinical/services/pausedSafetyNetService.js +3 -0
- package/lib/clinical/services/supportActionService.js +1 -8
- package/lib/clinical/services/supportCandidateService.js +10 -1
- package/lib/clinical/services/supportInvitability.js +20 -0
- package/lib/controllers/assistantController.js +1 -1
- package/lib/helpers/pausedThreadHelper.js +6 -1
- package/lib/index.d.ts +1 -0
- package/lib/models/threadModel.js +1 -0
- package/lib/services/threadService.js +6 -2
- package/package.json +1 -1
|
@@ -3,6 +3,7 @@ const runtimeConfig = require('../../config/runtimeConfig');
|
|
|
3
3
|
const DEFAULT_OFFER_WINDOW_MS = 24 * 60 * 60 * 1000;
|
|
4
4
|
const DEFAULT_HUMAN_QUIET_MS = 24 * 60 * 60 * 1000;
|
|
5
5
|
const DEFAULT_ACTIVITY_QUIET_MS = 2 * 60 * 60 * 1000;
|
|
6
|
+
const DEFAULT_PAUSE_QUIET_MS = 10 * 60 * 1000;
|
|
6
7
|
const DEFAULT_TAP_WINDOW_MS = 30 * 60 * 1000;
|
|
7
8
|
const DEFAULT_PLEDGE_MS = 8 * 60 * 60 * 1000;
|
|
8
9
|
const DEFAULT_SWEEP_INTERVAL_MS = 5 * 60 * 1000;
|
|
@@ -81,6 +82,7 @@ module.exports = {
|
|
|
81
82
|
getOfferWindowMs: () => duration('offerWindowMs', 'PAUSED_SAFETY_NET_WINDOW_MS', DEFAULT_OFFER_WINDOW_MS),
|
|
82
83
|
getHumanQuietMs: () => duration('humanQuietMs', 'SAFETY_NET_HUMAN_QUIET_MS', DEFAULT_HUMAN_QUIET_MS),
|
|
83
84
|
getActivityQuietMs: () => duration('activityQuietMs', 'SAFETY_NET_ACTIVITY_QUIET_MS', DEFAULT_ACTIVITY_QUIET_MS),
|
|
85
|
+
getPauseQuietMs: () => duration('pauseQuietMs', 'SAFETY_NET_PAUSE_QUIET_MS', DEFAULT_PAUSE_QUIET_MS),
|
|
84
86
|
getTapWindowMs: () => duration('tapWindowMs', 'SAFETY_NET_TAP_WINDOW_MS', DEFAULT_TAP_WINDOW_MS),
|
|
85
87
|
getPledgeMs: () => duration('pledgeMs', 'SAFETY_NET_PLEDGE_MS', DEFAULT_PLEDGE_MS),
|
|
86
88
|
getSweepIntervalMs: () => duration('sweepIntervalMs', 'SAFETY_NET_SWEEP_INTERVAL_MS', DEFAULT_SWEEP_INTERVAL_MS)
|
|
@@ -3,6 +3,7 @@ const {
|
|
|
3
3
|
getActivityQuietMs,
|
|
4
4
|
getHumanQuietMs,
|
|
5
5
|
getOfferWindowMs,
|
|
6
|
+
getPauseQuietMs,
|
|
6
7
|
getSafetyNetContentSid,
|
|
7
8
|
getSafetyNetLabel,
|
|
8
9
|
getSafetyNetPhone,
|
|
@@ -11,6 +12,7 @@ const {
|
|
|
11
12
|
} = require('../config/safetyNetConfig');
|
|
12
13
|
const { logger } = require('../../utils/logger');
|
|
13
14
|
|
|
15
|
+
const { pausedSince } = require('../../helpers/pausedThreadHelper');
|
|
14
16
|
const { acquireSendClaim, completeSendClaim, releaseSendClaim } = require('../../helpers/sendClaimHelper');
|
|
15
17
|
|
|
16
18
|
const { normalizePayload: normalize, registerInteractiveRoute, unregisterInteractiveRoute } = require('../../core/interactiveRouteService');
|
|
@@ -111,6 +113,7 @@ async function offerSafetyNet({ code, reason = PAUSED }) {
|
|
|
111
113
|
try {
|
|
112
114
|
if (!code || !getSafetyNetContentSid() || !getSafetyNetPhone()) return { sent: false, reason: 'not_configured' };
|
|
113
115
|
if (await offerClaims().get(code)) return { sent: false, reason: 'recent_offer' };
|
|
116
|
+
if (await pausedSince(code, new Date(Date.now() - getPauseQuietMs()))) return { sent: false, reason: 'just_paused' };
|
|
114
117
|
if (await humanRepliedSince(code, new Date(Date.now() - getHumanQuietMs()))) return { sent: false, reason: 'human_active' };
|
|
115
118
|
if (await anyoneRepliedSince(code, new Date(Date.now() - getActivityQuietMs()))) return { sent: false, reason: 'conversation_active' };
|
|
116
119
|
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
const { SupportAction } = require('../../shared/dtos/SupportAction');
|
|
2
2
|
const { logger } = require('../../utils/logger');
|
|
3
3
|
const { claimInvitation, recordEngagement, updateSupportOperationalReview } = require('./supportCandidateService');
|
|
4
|
+
const { isSafeGroupCandidate } = require('./supportInvitability');
|
|
4
5
|
|
|
5
6
|
const TEMPLATE_VERSION = 'support-card-v1';
|
|
6
7
|
const URGENT_NUTRITION_TARGET = 'nutrition.urgent_intake';
|
|
7
8
|
|
|
8
|
-
function isSafeGroupCandidate(candidate) {
|
|
9
|
-
if (!candidate.groupEligible) return false;
|
|
10
|
-
return candidate.service === 'nutrition'
|
|
11
|
-
? (candidate.nutritionRisk === 'green' && candidate.disposition === 'group')
|
|
12
|
-
|| (candidate.nutritionRisk === 'yellow' && candidate.disposition === 'individual')
|
|
13
|
-
: candidate.disposition === 'group' && candidate.psychologyRisk === 'green';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
9
|
function isUrgentNutritionCandidate(candidate) {
|
|
17
10
|
return candidate.service === 'nutrition'
|
|
18
11
|
&& candidate.nutritionRisk === 'yellow'
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { v4: uuidv4 } = require('uuid');
|
|
2
2
|
|
|
3
3
|
const { getSupportCandidateModel } = require('../models/supportCandidateModel');
|
|
4
|
+
const { GROUP_INVITABLE } = require('./supportInvitability');
|
|
4
5
|
const { SupportRoutingRecommendation } = require('../../shared/dtos/SupportRoutingRecommendation');
|
|
5
6
|
|
|
6
7
|
const COOLDOWN_MS = 14 * 24 * 60 * 60 * 1000;
|
|
@@ -494,10 +495,16 @@ async function clearCandidateEscalation(candidateId, {
|
|
|
494
495
|
);
|
|
495
496
|
}
|
|
496
497
|
|
|
498
|
+
// The states claimEligibility accepts. 'declined' is absent from both; the rest stay so a
|
|
499
|
+
// specialist can see who was already invited and not invite them twice.
|
|
500
|
+
const INVITABLE_ENGAGEMENT = { 'engagement.state': { $in: ['notInvited', 'invited', 'registered', 'attended'] } };
|
|
501
|
+
|
|
502
|
+
|
|
497
503
|
async function readSupportDashboardCandidates() {
|
|
498
504
|
const rows = await getSupportCandidateModel().find({
|
|
499
505
|
service: { $in: ['nutrition', 'psychology'] },
|
|
500
506
|
$or: [
|
|
507
|
+
...GROUP_INVITABLE.map((clause) => ({ ...INVITABLE_ENGAGEMENT, ...clause })),
|
|
501
508
|
{ disposition: { $in: ['reviewRequired', 'escalated'] } },
|
|
502
509
|
{ service: 'psychology', disposition: 'individual', psychologyRisk: 'orange' },
|
|
503
510
|
{ 'operationalReviews.0': { $exists: true } },
|
|
@@ -505,7 +512,9 @@ async function readSupportDashboardCandidates() {
|
|
|
505
512
|
{ invitationAttempts: { $elemMatch: { status: { $in: ['failed', 'unknown'] } } } },
|
|
506
513
|
],
|
|
507
514
|
})
|
|
508
|
-
|
|
515
|
+
// No `evidence`: reasonCodes carry the "why" as stable snake_case, so the patient's own
|
|
516
|
+
// wording never leaves the clinical store.
|
|
517
|
+
.select('-_id candidateId patientId service disposition groupEligible nutritionRisk psychologyRisk confidence priority engagement.state engagement.lastInvitedAt engagement.cooldownUntil activeInvitationKey escalation operationalReviews operationalReviewOverflow reasonCodes createdAt lastEvaluatedAt invitationAttempts.invitationCycle invitationAttempts.status invitationAttempts.createdAt')
|
|
509
518
|
.limit(1001).maxTimeMS(10000).lean();
|
|
510
519
|
if (rows.length > 1000) throw new Error('Support dashboard candidates incomplete');
|
|
511
520
|
return rows;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* Who may be invited to a collective support session.
|
|
2
|
+
*
|
|
3
|
+
* Lives apart from supportCandidateService and supportActionService because those two are
|
|
4
|
+
* mocked by each other's tests, and a safety rule that can be mocked away is not a rule.
|
|
5
|
+
*
|
|
6
|
+
* A nutrition yellow is invitable at disposition 'individual': routing sends it to an
|
|
7
|
+
* individual consult and keeps it group-eligible at the same time.
|
|
8
|
+
*/
|
|
9
|
+
const GROUP_INVITABLE = Object.freeze([
|
|
10
|
+
Object.freeze({ service: 'nutrition', groupEligible: true, nutritionRisk: 'green', disposition: 'group' }),
|
|
11
|
+
Object.freeze({ service: 'nutrition', groupEligible: true, nutritionRisk: 'yellow', disposition: 'individual' }),
|
|
12
|
+
Object.freeze({ service: 'psychology', groupEligible: true, psychologyRisk: 'green', disposition: 'group' }),
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
function isSafeGroupCandidate(candidate) {
|
|
16
|
+
return GROUP_INVITABLE.some((clause) => Object.entries(clause)
|
|
17
|
+
.every(([field, value]) => candidate[field] === value));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { GROUP_INVITABLE, isSafeGroupCandidate };
|
|
@@ -15,7 +15,7 @@ const stopAssistantController = async (req, res) => {
|
|
|
15
15
|
|
|
16
16
|
const stopped = typeof stop === 'string' ? stop === 'true' : !!stop;
|
|
17
17
|
try {
|
|
18
|
-
await switchThreadStoppedStatus(code, stopped);
|
|
18
|
+
await switchThreadStoppedStatus(code, stopped, { byOperator: true });
|
|
19
19
|
return res.status(200).json({ success: true, message: stopped ? 'Assistant stopped' : 'Assistant resumed' });
|
|
20
20
|
} catch (error) {
|
|
21
21
|
logger.error('[AssistantController] Failed to stop assistant', { error: error.message, code });
|
|
@@ -12,4 +12,9 @@ async function botIsPaused(code, { label = '[pausedThread]' } = {}) {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
async function pausedSince(code, since) {
|
|
16
|
+
const paused = await Thread.findOne({ code, stopped: true, pausedAt: { $gt: since } }, { _id: 1 }).lean();
|
|
17
|
+
return Boolean(paused);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { botIsPaused, pausedSince };
|
package/lib/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ const threadSchema = new mongoose.Schema({
|
|
|
10
10
|
run_id: { type: String, default: null },
|
|
11
11
|
nombre: { type: String, default: null },
|
|
12
12
|
stopped: { type: Boolean, default: false },
|
|
13
|
+
pausedAt: { type: Date, default: null },
|
|
13
14
|
review: { type: Boolean, default: false },
|
|
14
15
|
version: { type: Number, default: null },
|
|
15
16
|
preset_id: { type: String, default: null },
|
|
@@ -34,8 +34,12 @@ const getThreadInfo = async (code) => {
|
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
-
const switchThreadStoppedStatus = async (code, stopped) => {
|
|
38
|
-
|
|
37
|
+
const switchThreadStoppedStatus = async (code, stopped, { byOperator = false } = {}) => {
|
|
38
|
+
if (stopped && byOperator) {
|
|
39
|
+
await Thread.updateOne({ code, stopped: { $ne: true } }, { stopped, pausedAt: new Date() });
|
|
40
|
+
} else {
|
|
41
|
+
await Thread.updateOne({ code }, stopped ? { stopped } : { stopped, pausedAt: null });
|
|
42
|
+
}
|
|
39
43
|
|
|
40
44
|
updateRecordByFilter(Monitoreo_ID, 'message_monitor', `{whatsapp_id} = "${code}"`, { stopped }, code)
|
|
41
45
|
.catch(err => logger.error('[switchThreadStoppedStatus] Failed to update message_monitor', { code, error: err.message }));
|