Spaces:
Sleeping

axeronis commited on
Commit
07ef569
·
1 Parent(s): 864bdf8

feat: add booking slot conflict check - prevent double bookings

Browse files
Files changed (1) hide show
  1. backend/app/api/webhook.py +17 -0
backend/app/api/webhook.py CHANGED
@@ -637,6 +637,23 @@ async def process_ai_response_background(
637
  if old_appointments:
638
  await session.commit()
639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
640
  # Create new appointment with comprehensive notes
641
  booking_info = _extract_booking_info_from_conversation(conversation_history, lead, user_text)
642
 
 
637
  if old_appointments:
638
  await session.commit()
639
 
640
+ # CHECK FOR TIME SLOT CONFLICT
641
+ conflict_stmt = select(Appointment).where(
642
+ Appointment.tenant_id == tenant_id,
643
+ Appointment.status == AppointmentStatus.SCHEDULED,
644
+ Appointment.start_time == start_time
645
+ )
646
+ conflict_result = await session.execute(conflict_stmt)
647
+ existing_booking = conflict_result.scalar_one_or_none()
648
+
649
+ if existing_booking:
650
+ # Slot is taken - don't create booking, inform user
651
+ logger.warning(f"⚠️ Time slot {start_time} already taken for tenant {tenant_id}")
652
+ ai_text = ai_text.replace(booking_match.group(0), "").strip()
653
+ ai_text += "\n\n⚠️ К сожалению, это время уже занято. Пожалуйста, выберите другое время."
654
+ ai_response["text"] = ai_text
655
+ continue # Skip creating this appointment
656
+
657
  # Create new appointment with comprehensive notes
658
  booking_info = _extract_booking_info_from_conversation(conversation_history, lead, user_text)
659