@peopl-health/nexus 5.57.0-dev.8830 → 5.57.0-dev.8852

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.
@@ -520,8 +520,8 @@ async function clearCandidateEscalation(candidateId, {
520
520
  // specialist can see who was already invited and not invite them twice.
521
521
  const INVITABLE_ENGAGEMENT = { 'engagement.state': { $in: ['notInvited', 'invited', 'registered', 'attended'] } };
522
522
 
523
- async function readSupportDashboardCandidates() {
524
- const rows = await getSupportCandidateModel().find({
523
+ function dashboardFilter() {
524
+ return {
525
525
  service: { $in: ['nutrition', 'psychology'] },
526
526
  $or: [
527
527
  ...GROUP_INVITABLE.map((clause) => ({ ...INVITABLE_ENGAGEMENT, ...clause })),
@@ -531,19 +531,64 @@ async function readSupportDashboardCandidates() {
531
531
  { operationalReviewOverflow: true },
532
532
  { invitationAttempts: { $elemMatch: { status: { $in: ['failed', 'unknown'] } } } },
533
533
  ],
534
- })
535
- // No `evidence`: reasonCodes carry the "why" as stable snake_case, so the patient's own
536
- // wording never leaves the clinical store.
537
- .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')
538
- .limit(1001).maxTimeMS(10000).lean();
539
- if (rows.length > 1000) throw new Error('Support dashboard candidates incomplete');
540
- return rows;
534
+ };
535
+ }
536
+
537
+ const DASHBOARD_READ_LIMIT = 2000;
538
+
539
+ // No `evidence`: reasonCodes carry the "why", so the patient's own wording stays in the store.
540
+ const DASHBOARD_FIELDS = Object.freeze([
541
+ 'candidateId', 'patientId', 'service', 'disposition', 'groupEligible', 'nutritionRisk',
542
+ 'psychologyRisk', 'confidence', 'priority', 'engagement.state', 'engagement.lastInvitedAt',
543
+ 'engagement.cooldownUntil', 'activeInvitationKey', 'escalation', 'operationalReviews',
544
+ 'operationalReviewOverflow', 'reasonCodes', 'createdAt', 'lastEvaluatedAt',
545
+ 'invitationAttempts.invitationCycle', 'invitationAttempts.status', 'invitationAttempts.createdAt',
546
+ ]);
547
+
548
+ const DASHBOARD_PROJECTION = Object.freeze({
549
+ _id: 0,
550
+ ...Object.fromEntries(DASHBOARD_FIELDS.map((field) => [field, 1])),
551
+ });
552
+
553
+ const DASHBOARD_RANK = {
554
+ $switch: {
555
+ branches: [
556
+ { case: { $eq: ['$disposition', 'escalated'] }, then: 0 },
557
+ { case: { $eq: ['$disposition', 'reviewRequired'] }, then: 1 },
558
+ { case: { $gt: [{ $size: { $ifNull: ['$operationalReviews', []] } }, 0] }, then: 2 },
559
+ { case: { $eq: ['$operationalReviewOverflow', true] }, then: 2 },
560
+ ],
561
+ default: 3,
562
+ },
563
+ };
564
+
565
+ const boundedLimit = (limit) => {
566
+ const requested = Number.isInteger(limit) && limit > 0 ? limit : DASHBOARD_READ_LIMIT;
567
+ return Math.min(requested, DASHBOARD_READ_LIMIT);
568
+ };
569
+
570
+ async function readSupportDashboardCandidates({ limit = DASHBOARD_READ_LIMIT } = {}) {
571
+ return getSupportCandidateModel().aggregate([
572
+ { $match: dashboardFilter() },
573
+ { $project: { ...DASHBOARD_PROJECTION, dashboardRank: DASHBOARD_RANK } },
574
+ { $sort: { dashboardRank: 1, lastEvaluatedAt: -1, candidateId: 1 } },
575
+ { $limit: boundedLimit(limit) },
576
+ { $unset: 'dashboardRank' },
577
+ ]).option({ maxTimeMS: 10000, allowDiskUse: true });
578
+ }
579
+
580
+ async function readSupportDashboardCandidate(candidateId) {
581
+ if (!candidateId) return null;
582
+ return getSupportCandidateModel().findOne({ ...dashboardFilter(), candidateId })
583
+ .select(DASHBOARD_PROJECTION).maxTimeMS(10000).lean();
541
584
  }
542
585
 
543
586
  module.exports = {
544
587
  recordSentInvitationResponse,
545
588
  updateSupportOperationalReview,
546
589
  readSupportDashboardCandidates,
590
+ readSupportDashboardCandidate,
591
+ DASHBOARD_READ_LIMIT,
547
592
  approveCandidate,
548
593
  candidatePatch,
549
594
  claimInvitation,
package/lib/index.d.ts CHANGED
@@ -835,7 +835,9 @@ declare module '@peopl-health/nexus' {
835
835
  noClinicalDecision?: boolean;
836
836
  now?: Date;
837
837
  }, options?: { Model?: unknown }): Promise<SupportCandidateRecord | null>;
838
- readSupportDashboardCandidates(): Promise<SupportDashboardCandidateRecord[]>;
838
+ readSupportDashboardCandidates(options?: { limit?: number }): Promise<SupportDashboardCandidateRecord[]>;
839
+ readSupportDashboardCandidate(candidateId: string): Promise<SupportDashboardCandidateRecord | null>;
840
+ DASHBOARD_READ_LIMIT: number;
839
841
  approveCandidate(candidateId: string, options: {
840
842
  reviewedRevision: number;
841
843
  reviewedRisk: 'green' | 'yellow';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.57.0-dev.8830",
3
+ "version": "5.57.0-dev.8852",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",