mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-04-11 18:08:16 +00:00
feat: add worklist engine with prioritized missed calls, follow-ups, and leads
This commit is contained in:
139
src/worklist/worklist.service.ts
Normal file
139
src/worklist/worklist.service.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { PlatformGraphqlService } from '../platform/platform-graphql.service';
|
||||
|
||||
export type WorklistResponse = {
|
||||
missedCalls: any[];
|
||||
followUps: any[];
|
||||
marketingLeads: any[];
|
||||
totalPending: number;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class WorklistService {
|
||||
private readonly logger = new Logger(WorklistService.name);
|
||||
|
||||
constructor(private readonly platform: PlatformGraphqlService) {}
|
||||
|
||||
async getWorklist(agentName: string, authHeader: string): Promise<WorklistResponse> {
|
||||
const [missedCalls, followUps, marketingLeads] = await Promise.all([
|
||||
this.getMissedCallsWithToken(agentName, authHeader),
|
||||
this.getPendingFollowUpsWithToken(agentName, authHeader),
|
||||
this.getAssignedLeadsWithToken(agentName, authHeader),
|
||||
]);
|
||||
|
||||
return {
|
||||
missedCalls,
|
||||
followUps,
|
||||
marketingLeads,
|
||||
totalPending: missedCalls.length + followUps.length + marketingLeads.length,
|
||||
};
|
||||
}
|
||||
|
||||
private async getAssignedLeadsWithToken(agentName: string, authHeader: string): Promise<any[]> {
|
||||
try {
|
||||
const data = await this.platform.queryWithAuth<{
|
||||
leads: { edges: { node: any }[] };
|
||||
}>(
|
||||
`query GetAssignedLeads($filter: LeadFilterInput, $first: Int, $orderBy: [LeadOrderByInput]) {
|
||||
leads(filter: $filter, first: $first, orderBy: $orderBy) {
|
||||
edges {
|
||||
node {
|
||||
id createdAt
|
||||
contactName { firstName lastName }
|
||||
contactPhone { number callingCode }
|
||||
contactEmail { address }
|
||||
leadSource leadStatus interestedService
|
||||
assignedAgent campaignId adId
|
||||
contactAttempts spamScore isSpam
|
||||
aiSummary aiSuggestedAction
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
{
|
||||
filter: { assignedAgent: { eq: agentName } },
|
||||
first: 20,
|
||||
orderBy: [{ createdAt: 'AscNullsLast' }],
|
||||
},
|
||||
authHeader,
|
||||
);
|
||||
|
||||
return data.leads.edges.map((e) => e.node);
|
||||
} catch (err) {
|
||||
this.logger.warn(`Failed to fetch assigned leads: ${err}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private async getPendingFollowUpsWithToken(agentName: string, authHeader: string): Promise<any[]> {
|
||||
try {
|
||||
const data = await this.platform.queryWithAuth<{
|
||||
followUps: { edges: { node: any }[] };
|
||||
}>(
|
||||
`query GetPendingFollowUps($filter: FollowUpFilterInput, $first: Int) {
|
||||
followUps(filter: $filter, first: $first) {
|
||||
edges {
|
||||
node {
|
||||
id createdAt
|
||||
followUpType followUpStatus
|
||||
scheduledAt completedAt
|
||||
priority assignedAgent
|
||||
patientId callId
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
{
|
||||
filter: {
|
||||
assignedAgent: { eq: agentName },
|
||||
followUpStatus: { in: ['PENDING', 'OVERDUE'] },
|
||||
},
|
||||
first: 20,
|
||||
},
|
||||
authHeader,
|
||||
);
|
||||
|
||||
return data.followUps.edges.map((e) => e.node);
|
||||
} catch (err) {
|
||||
this.logger.warn(`Failed to fetch follow-ups: ${err}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private async getMissedCallsWithToken(agentName: string, authHeader: string): Promise<any[]> {
|
||||
try {
|
||||
const data = await this.platform.queryWithAuth<{
|
||||
calls: { edges: { node: any }[] };
|
||||
}>(
|
||||
`query GetMissedCalls($filter: CallFilterInput, $first: Int, $orderBy: [CallOrderByInput]) {
|
||||
calls(filter: $filter, first: $first, orderBy: $orderBy) {
|
||||
edges {
|
||||
node {
|
||||
id createdAt
|
||||
callDirection callStatus
|
||||
callerNumber { number callingCode }
|
||||
agentName startedAt endedAt
|
||||
durationSeconds disposition
|
||||
callNotes leadId
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
{
|
||||
filter: {
|
||||
callStatus: { eq: 'MISSED' },
|
||||
agentName: { eq: agentName },
|
||||
},
|
||||
first: 20,
|
||||
orderBy: [{ createdAt: 'AscNullsLast' }],
|
||||
},
|
||||
authHeader,
|
||||
);
|
||||
|
||||
return data.calls.edges.map((e) => e.node);
|
||||
} catch (err) {
|
||||
this.logger.warn(`Failed to fetch missed calls: ${err}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user