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.