mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-05-18 20:08:19 +00:00
fix: fetch Lead first to resolve patientId before appointments query
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
The build() method previously fetched Lead and Appointments in parallel. When the input patientId was empty (outbound dial, first-time linkage), the appointments query was skipped even though the Lead record in the DB had a valid patientId. Now fetches Lead first, reads its patientId, then fetches appointments/calls/activities in parallel with the correct ID. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -89,19 +89,34 @@ export class CallerContextService {
|
|||||||
|
|
||||||
private async build(leadId: string, patientId: string, auth: string): Promise<CallerContext | null> {
|
private async build(leadId: string, patientId: string, auth: string): Promise<CallerContext | null> {
|
||||||
try {
|
try {
|
||||||
const [leadData, appointmentsData, callsData, activitiesData] = await Promise.all([
|
// Step 1: Fetch lead first to get the authoritative patientId
|
||||||
this.platform.queryWithAuth<any>(
|
const leadData = await this.platform.queryWithAuth<any>(
|
||||||
`{ lead(filter: { id: { eq: "${leadId}" } }) {
|
`{ lead(filter: { id: { eq: "${leadId}" } }) {
|
||||||
id contactName { firstName lastName }
|
id contactName { firstName lastName }
|
||||||
contactPhone { primaryPhoneNumber }
|
contactPhone { primaryPhoneNumber }
|
||||||
source status interestedService
|
source status interestedService
|
||||||
aiSummary contactAttempts lastContacted
|
aiSummary contactAttempts lastContacted
|
||||||
utmCampaign patientId
|
utmCampaign patientId
|
||||||
} }`,
|
} }`,
|
||||||
undefined, auth,
|
undefined, auth,
|
||||||
),
|
);
|
||||||
patientId ? this.platform.queryWithAuth<any>(
|
|
||||||
`{ appointments(first: 10, filter: { patientId: { eq: "${patientId}" } }, orderBy: [{ scheduledAt: DescNullsLast }]) { edges { node {
|
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 {
|
||||||
scheduledAt status doctorName department reasonForVisit
|
scheduledAt status doctorName department reasonForVisit
|
||||||
} } } }`,
|
} } } }`,
|
||||||
undefined, auth,
|
undefined, auth,
|
||||||
@@ -120,12 +135,6 @@ 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 appointments = (appointmentsData?.appointments?.edges ?? []).map((e: any) => e.node);
|
||||||
const calls = (callsData?.calls?.edges ?? []).map((e: any) => ({
|
const calls = (callsData?.calls?.edges ?? []).map((e: any) => ({
|
||||||
startedAt: e.node.startedAt,
|
startedAt: e.node.startedAt,
|
||||||
@@ -148,7 +157,7 @@ export class CallerContextService {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
leadId,
|
leadId,
|
||||||
patientId: patientId || lead.patientId || '',
|
patientId: resolvedPatientId,
|
||||||
name: `${firstName} ${lastName}`.trim() || 'Unknown',
|
name: `${firstName} ${lastName}`.trim() || 'Unknown',
|
||||||
phone: lead.contactPhone?.primaryPhoneNumber ?? '',
|
phone: lead.contactPhone?.primaryPhoneNumber ?? '',
|
||||||
isNew: false,
|
isNew: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user