Spaces:
Running
Running
axeronis commited on
Commit ·
39ac4b7
1
Parent(s): 03ee203
fix: prevent duplicate booking when AI confirms existing appointment
Browse files- backend/app/api/webhook.py +59 -43
backend/app/api/webhook.py
CHANGED
|
@@ -1018,53 +1018,69 @@ async def process_ai_response_background(
|
|
| 1018 |
|
| 1019 |
ai_response["text"] = ai_text
|
| 1020 |
else:
|
| 1021 |
-
#
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
if booking_info["phone"]:
|
| 1027 |
-
notes_parts.append(f"📞 {booking_info['phone']}")
|
| 1028 |
-
notes_parts.append(f"Тема: {booking_info['topic']}")
|
| 1029 |
-
notes = " | ".join(notes_parts)
|
| 1030 |
-
|
| 1031 |
-
new_appt = Appointment(
|
| 1032 |
-
tenant_id=tenant_id,
|
| 1033 |
-
lead_id=lead_id,
|
| 1034 |
-
service_id=default_srv.id,
|
| 1035 |
-
start_time=start_time,
|
| 1036 |
-
end_time=end_time,
|
| 1037 |
-
status=AppointmentStatus.SCHEDULED,
|
| 1038 |
-
notes=notes
|
| 1039 |
)
|
| 1040 |
-
session.
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
logger.info(f"📅 AI Booked Appointment: {date_str} for Lead {lead_id}")
|
| 1044 |
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
|
| 1051 |
-
# Pass AI summary for smart notification
|
| 1052 |
-
await notification_service.notify_new_appointment(
|
| 1053 |
-
tenant,
|
| 1054 |
-
new_appt,
|
| 1055 |
-
ai_summary=lead.ai_summary_json
|
| 1056 |
-
)
|
| 1057 |
-
except Exception as ns_err:
|
| 1058 |
-
logger.error(f"⚠️ Notification failed: {ns_err}")
|
| 1059 |
else:
|
| 1060 |
-
|
| 1061 |
-
|
| 1062 |
-
# Remove tag from text
|
| 1063 |
-
ai_text = ai_text.replace(booking_match.group(0), "").strip()
|
| 1064 |
-
ai_text += "\n\n✅ (Система: Запись создана успешно!)"
|
| 1065 |
|
| 1066 |
-
|
| 1067 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1068 |
|
| 1069 |
except Exception as e:
|
| 1070 |
logger.error(f"❌ Failed to process AI booking tag: {e}")
|
|
|
|
| 1018 |
|
| 1019 |
ai_response["text"] = ai_text
|
| 1020 |
else:
|
| 1021 |
+
# CHECK FOR DUPLICATE: Same lead + same time = already booked
|
| 1022 |
+
duplicate_stmt = select(Appointment).where(
|
| 1023 |
+
Appointment.lead_id == lead_id,
|
| 1024 |
+
Appointment.status == AppointmentStatus.SCHEDULED,
|
| 1025 |
+
Appointment.start_time == start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1026 |
)
|
| 1027 |
+
duplicate_result = await session.execute(duplicate_stmt)
|
| 1028 |
+
existing_for_lead = duplicate_result.scalar_one_or_none()
|
|
|
|
|
|
|
| 1029 |
|
| 1030 |
+
if existing_for_lead:
|
| 1031 |
+
# Already booked for this lead at this time - just confirm, don't create duplicate
|
| 1032 |
+
logger.info(f"📅 Booking already exists for lead {lead_id} at {start_time}, skipping duplicate")
|
| 1033 |
+
# Remove the BOOK_CONFIRMED tag from response since booking already exists
|
| 1034 |
+
ai_text = ai_text.replace(booking_match.group(0), "").strip()
|
| 1035 |
+
ai_response["text"] = ai_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1036 |
else:
|
| 1037 |
+
# Create new appointment with comprehensive notes
|
| 1038 |
+
booking_info = _extract_booking_info_from_conversation(conversation_history, lead, user_text)
|
|
|
|
|
|
|
|
|
|
| 1039 |
|
| 1040 |
+
# Build informative notes
|
| 1041 |
+
notes_parts = [booking_info["name"]]
|
| 1042 |
+
if booking_info["phone"]:
|
| 1043 |
+
notes_parts.append(f"📞 {booking_info['phone']}")
|
| 1044 |
+
notes_parts.append(f"Тема: {booking_info['topic']}")
|
| 1045 |
+
notes = " | ".join(notes_parts)
|
| 1046 |
+
|
| 1047 |
+
new_appt = Appointment(
|
| 1048 |
+
tenant_id=tenant_id,
|
| 1049 |
+
lead_id=lead_id,
|
| 1050 |
+
service_id=default_srv.id,
|
| 1051 |
+
start_time=start_time,
|
| 1052 |
+
end_time=end_time,
|
| 1053 |
+
status=AppointmentStatus.SCHEDULED,
|
| 1054 |
+
notes=notes
|
| 1055 |
+
)
|
| 1056 |
+
session.add(new_appt)
|
| 1057 |
+
await session.commit()
|
| 1058 |
+
|
| 1059 |
+
logger.info(f"📅 AI Booked Appointment: {date_str} for Lead {lead_id}")
|
| 1060 |
+
|
| 1061 |
+
# 🔔 NOTIFY ADMIN with AI context (only for first booking, not reschedules)
|
| 1062 |
+
if not is_rescheduled:
|
| 1063 |
+
try:
|
| 1064 |
+
from app.services.notification import notification_service
|
| 1065 |
+
new_appt.lead = lead
|
| 1066 |
+
new_appt.service = default_srv
|
| 1067 |
+
# Pass AI summary for smart notification
|
| 1068 |
+
await notification_service.notify_new_appointment(
|
| 1069 |
+
tenant,
|
| 1070 |
+
new_appt,
|
| 1071 |
+
ai_summary=lead.ai_summary_json
|
| 1072 |
+
)
|
| 1073 |
+
except Exception as ns_err:
|
| 1074 |
+
logger.error(f"⚠️ Notification failed: {ns_err}")
|
| 1075 |
+
else:
|
| 1076 |
+
logger.info(f"📅 Skipping notification for rescheduled appointment {new_appt.id}")
|
| 1077 |
+
|
| 1078 |
+
# Remove tag from text
|
| 1079 |
+
ai_text = ai_text.replace(booking_match.group(0), "").strip()
|
| 1080 |
+
ai_text += "\n\n✅ (Система: Запись создана успешно!)"
|
| 1081 |
+
|
| 1082 |
+
# Update response text in object
|
| 1083 |
+
ai_response["text"] = ai_text
|
| 1084 |
|
| 1085 |
except Exception as e:
|
| 1086 |
logger.error(f"❌ Failed to process AI booking tag: {e}")
|