fix: stop auto-creating Unknown leads on caller resolve

Return empty IDs for unrecognized numbers instead of creating lead+patient.
Per PRD: 'System will not identify the patient — no summary shown.'
Records are created when agent books appointment or logs enquiry.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-13 11:23:22 +05:30
parent 5969441868
commit a4ff052fef

View File

@@ -23,7 +23,12 @@ export class CallerResolutionService {
private readonly cache: SessionService,
) {}
// Resolve a caller by phone number. Always returns a paired lead + patient.
// Resolve a caller by phone number.
// Looks up existing lead + patient by phone. If neither exists, returns
// isNew: true with no IDs — records are NOT created automatically.
// Record creation happens when the agent explicitly books an appointment
// or logs an enquiry (per PRD: "System will not identify the patient —
// no summary shown" for unregistered numbers).
async resolve(phone: string, auth: string): Promise<ResolvedCaller> {
const normalized = phone.replace(/\D/g, '').slice(-10);
if (normalized.length < 10) {
@@ -85,13 +90,12 @@ export class CallerResolutionService {
isNew: false,
};
} else {
// Neither exists — create both
const newPatient = await this.createPatient('', '', normalized, auth);
const newLead = await this.createLead('', '', normalized, newPatient.id, auth);
this.logger.log(`[RESOLVE] Created new lead ${newLead.id} + patient ${newPatient.id} for ${normalized}`);
// Neither exists — return empty result, don't create records.
// Agent will create records when they book an appointment or log an enquiry.
this.logger.log(`[RESOLVE] No existing records for ${normalized} — returning unresolved`);
result = {
leadId: newLead.id,
patientId: newPatient.id,
leadId: '',
patientId: '',
firstName: '',
lastName: '',
phone: normalized,