mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-05-18 20:08:19 +00:00
Compare commits
5 Commits
77b3e917db
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 34eae1c19a | |||
|
|
1dd8413297 | ||
|
|
7d8424b446 | ||
|
|
55b8680923 | ||
|
|
973614749b |
@@ -89,34 +89,19 @@ export class CallerContextService {
|
||||
|
||||
private async build(leadId: string, patientId: string, auth: string): Promise<CallerContext | null> {
|
||||
try {
|
||||
// Step 1: Fetch lead first to get the authoritative patientId
|
||||
const leadData = await this.platform.queryWithAuth<any>(
|
||||
`{ lead(filter: { id: { eq: "${leadId}" } }) {
|
||||
id contactName { firstName lastName }
|
||||
contactPhone { primaryPhoneNumber }
|
||||
source status interestedService
|
||||
aiSummary contactAttempts lastContacted
|
||||
utmCampaign patientId
|
||||
} }`,
|
||||
undefined, auth,
|
||||
);
|
||||
|
||||
const lead = leadData?.lead;
|
||||
if (!lead) return null;
|
||||
|
||||
// Use Lead's patientId as authoritative source — the input
|
||||
// param may be empty if caller resolution just linked them.
|
||||
const resolvedPatientId = patientId || lead.patientId || '';
|
||||
this.logger.log(`[CALLER-CTX] Resolved patientId=${resolvedPatientId} (input=${patientId}, lead=${lead.patientId ?? '∅'})`);
|
||||
|
||||
const firstName = lead.contactName?.firstName ?? '';
|
||||
const lastName = lead.contactName?.lastName ?? '';
|
||||
|
||||
// Step 2: Fetch appointments, calls, activities in parallel
|
||||
// using the resolved patientId from the Lead record.
|
||||
const [appointmentsData, callsData, activitiesData] = await Promise.all([
|
||||
resolvedPatientId ? this.platform.queryWithAuth<any>(
|
||||
`{ appointments(first: 10, filter: { patientId: { eq: "${resolvedPatientId}" } }, orderBy: [{ scheduledAt: DescNullsLast }]) { edges { node {
|
||||
const [leadData, appointmentsData, callsData, activitiesData] = await Promise.all([
|
||||
this.platform.queryWithAuth<any>(
|
||||
`{ lead(filter: { id: { eq: "${leadId}" } }) {
|
||||
id contactName { firstName lastName }
|
||||
contactPhone { primaryPhoneNumber }
|
||||
source status interestedService
|
||||
aiSummary contactAttempts lastContacted
|
||||
utmCampaign patientId
|
||||
} }`,
|
||||
undefined, auth,
|
||||
),
|
||||
patientId ? this.platform.queryWithAuth<any>(
|
||||
`{ appointments(first: 10, filter: { patientId: { eq: "${patientId}" } }, orderBy: [{ scheduledAt: DescNullsLast }]) { edges { node {
|
||||
scheduledAt status doctorName department reasonForVisit
|
||||
} } } }`,
|
||||
undefined, auth,
|
||||
@@ -135,6 +120,12 @@ export class CallerContextService {
|
||||
),
|
||||
]);
|
||||
|
||||
const lead = leadData?.lead;
|
||||
if (!lead) return null;
|
||||
|
||||
const firstName = lead.contactName?.firstName ?? '';
|
||||
const lastName = lead.contactName?.lastName ?? '';
|
||||
|
||||
const appointments = (appointmentsData?.appointments?.edges ?? []).map((e: any) => e.node);
|
||||
const calls = (callsData?.calls?.edges ?? []).map((e: any) => ({
|
||||
startedAt: e.node.startedAt,
|
||||
@@ -157,7 +148,7 @@ export class CallerContextService {
|
||||
|
||||
return {
|
||||
leadId,
|
||||
patientId: resolvedPatientId,
|
||||
patientId: patientId || lead.patientId || '',
|
||||
name: `${firstName} ${lastName}`.trim() || 'Unknown',
|
||||
phone: lead.contactPhone?.primaryPhoneNumber ?? '',
|
||||
isNew: false,
|
||||
|
||||
Reference in New Issue
Block a user