Spaces:
Sleeping

axeronis commited on
Commit
f9d6bc4
·
1 Parent(s): 39ac4b7

feat: add reschedule notification when appointments are moved to new time

Browse files
backend/app/api/webhook.py CHANGED
@@ -975,10 +975,25 @@ async def process_ai_response_background(
975
  is_rescheduled = len(old_appointments) > 0 # Track if this is a reschedule
976
 
977
  for old_appt in old_appointments:
 
978
  old_appt.status = AppointmentStatus.CANCELLED
979
- old_appt.notes = f"[Перенесено] {old_appt.notes or ''}"
980
  session.add(old_appt)
981
  logger.info(f"🔄 Auto-cancelled old appointment {old_appt.id} for lead {lead_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982
 
983
  if old_appointments:
984
  await session.commit()
 
975
  is_rescheduled = len(old_appointments) > 0 # Track if this is a reschedule
976
 
977
  for old_appt in old_appointments:
978
+ old_time = old_appt.start_time
979
  old_appt.status = AppointmentStatus.CANCELLED
980
+ old_appt.notes = f"[Перенесено на {start_time.strftime('%d.%m в %H:%M')}] {old_appt.notes or ''}"
981
  session.add(old_appt)
982
  logger.info(f"🔄 Auto-cancelled old appointment {old_appt.id} for lead {lead_id}")
983
+
984
+ # 🔔 NOTIFY ADMIN about reschedule
985
+ try:
986
+ from app.services.notification import notification_service
987
+ old_appt.lead = lead
988
+ await notification_service.notify_reschedule(
989
+ tenant=tenant,
990
+ old_appointment=old_appt,
991
+ old_time=old_time,
992
+ new_time=start_time,
993
+ lead_name=lead.name or "Клиент"
994
+ )
995
+ except Exception as ns_err:
996
+ logger.error(f"⚠️ Reschedule notification failed: {ns_err}")
997
 
998
  if old_appointments:
999
  await session.commit()
backend/app/services/notification.py CHANGED
@@ -1,4 +1,5 @@
1
  from typing import Optional, Dict, Any
 
2
  from app.models.tenant import Tenant
3
  from app.models.order import Order
4
  from app.models.booking import Appointment
@@ -125,6 +126,36 @@ class NotificationService:
125
  )
126
  await self._send_telegram(tenant.telegram_bot_token, tg_chat_id, message)
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  async def _send_telegram(self, bot_token: str, chat_id: str, text: str):
129
  """Helper to send TG message."""
130
  # ALWAYS use Platform Notification Bot - NOT tenant's automation bot
 
1
  from typing import Optional, Dict, Any
2
+ from datetime import datetime
3
  from app.models.tenant import Tenant
4
  from app.models.order import Order
5
  from app.models.booking import Appointment
 
126
  )
127
  await self._send_telegram(tenant.telegram_bot_token, tg_chat_id, message)
128
 
129
+ async def notify_reschedule(
130
+ self,
131
+ tenant: Tenant,
132
+ old_appointment,
133
+ old_time: datetime,
134
+ new_time: datetime,
135
+ lead_name: str
136
+ ):
137
+ """Send alert when appointment is rescheduled."""
138
+ if not tenant.notification_settings:
139
+ return
140
+
141
+ settings = tenant.notification_settings
142
+
143
+ tg_chat_id = settings.get("telegram_chat_id")
144
+ if tg_chat_id and settings.get("notify_on_booking", True):
145
+ old_time_str = old_time.strftime('%d.%m в %H:%M')
146
+ new_time_str = new_time.strftime('%d.%m в %H:%M')
147
+
148
+ message = (
149
+ f"🔄 **ПЕРЕНОС ЗАПИСИ** 🔄\n\n"
150
+ f"👤 Клиент: {lead_name}\n"
151
+ f"───────────────\n"
152
+ f"❌ Было: {old_time_str}\n"
153
+ f"✅ Стало: {new_time_str}\n"
154
+ f"───────────────\n"
155
+ f"📝 Заметка: {old_appointment.notes or '—'}"
156
+ )
157
+ await self._send_telegram(tenant.telegram_bot_token, tg_chat_id, message)
158
+
159
  async def _send_telegram(self, bot_token: str, chat_id: str, text: str):
160
  """Helper to send TG message."""
161
  # ALWAYS use Platform Notification Bot - NOT tenant's automation bot