fix: prevent duplicate consultations with db unique constraint, frontend init once guard, chat history preserved
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user