From a837c95d8c48fe9f2e74edfb8f3a4cec228547c8 Mon Sep 17 00:00:00 2001 From: saridsa2 Date: Tue, 21 Apr 2026 16:41:11 +0530 Subject: [PATCH] fix: contact form creates Lead only, not Patient MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Widget contact + chat-start were creating Patient records for new visitors. Patient should only be created during appointment booking. Added createPatient flag to findOrCreateLeadByPhone — defaults to false, only bookAppointment passes true. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/widget/widget.service.ts | 39 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/widget/widget.service.ts b/src/widget/widget.service.ts index 41a0559..e2cbeb7 100644 --- a/src/widget/widget.service.ts +++ b/src/widget/widget.service.ts @@ -17,6 +17,7 @@ export type FindOrCreateLeadOpts = { source?: string; status?: string; interestedService?: string; + createPatient?: boolean; // default false — only booking creates patients }; @Injectable() @@ -60,26 +61,27 @@ export class WidgetService { const lastName = name.split(' ').slice(1).join(' ') || ''; if (resolved.isNew) { - // Net-new visitor — create Patient + Lead with the widget- - // collected name. Both records get the real name from the - // first moment they exist. + // Net-new visitor — create Lead. Patient is only created + // when explicitly requested (e.g., booking an appointment). let patientId: string | undefined; - try { - const p = await this.platform.queryWithAuth( - `mutation($data: PatientCreateInput!) { createPatient(data: $data) { id } }`, - { - data: { - name: `${firstName} ${lastName}`.trim() || 'Unknown', - fullName: { firstName, lastName }, - phones: { primaryPhoneNumber: `+91${phone}` }, - patientType: 'NEW', + if (opts.createPatient) { + try { + const p = await this.platform.queryWithAuth( + `mutation($data: PatientCreateInput!) { createPatient(data: $data) { id } }`, + { + data: { + name: `${firstName} ${lastName}`.trim() || 'Unknown', + fullName: { firstName, lastName }, + phones: { primaryPhoneNumber: `+91${phone}` }, + patientType: 'NEW', + }, }, - }, - this.auth, - ); - patientId = p?.createPatient?.id; - } catch (err) { - this.logger.warn(`Widget patient create failed (${phone}): ${err}`); + this.auth, + ); + patientId = p?.createPatient?.id; + } catch (err) { + this.logger.warn(`Widget patient create failed (${phone}): ${err}`); + } } const created = await this.platform.queryWithAuth( `mutation($data: LeadCreateInput!) { createLead(data: $data) { id } }`, @@ -263,6 +265,7 @@ export class WidgetService { source: 'WEBSITE', status: 'APPOINTMENT_SET', interestedService: req.chiefComplaint ?? 'Appointment Booking', + createPatient: true, }); // Idempotent upgrade: if the lead was reused from an earlier chat/ // contact, promote its status and reflect the new interest.