fix: prevent duplicate consultations with db unique constraint, frontend init once guard, chat history preserved

This commit is contained in:
MingNian
2026-05-22 11:20:48 +08:00
parent 8caa374699
commit 90615a6cb3
2 changed files with 33 additions and 4 deletions

View File

@@ -41,7 +41,19 @@ public class ConsultationService(AppDbContext db)
Subject = subject,
};
db.Consultations.Add(consultation);
await db.SaveChangesAsync();
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
// Race condition: another request created one between our check and save
// The unique index on (PatientId, DoctorId) where Status='active' caught it
db.ChangeTracker.Clear();
var retry = await db.Consultations
.FirstOrDefaultAsync(c => c.PatientId == patientId && c.DoctorId == doctorId && c.Status == "active");
return retry!;
}
return consultation;
}