Spaces:
Running

axeronis commited on
Commit
786bf15
·
1 Parent(s): 4d35386

Fix: Added fallback to Tenant JSON for escalation notifications and improved logging

Browse files
backend/app/services/escalation_service.py CHANGED
@@ -513,14 +513,29 @@ class EscalationService:
513
  ns = result.scalar_one_or_none()
514
 
515
  if not ns:
516
- logger.warning(f"⚠️ No NotificationSettings found for tenant {tenant_id}")
517
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
 
519
- if not ns.telegram_enabled or not ns.notify_escalations:
520
  logger.info(f"📵 Escalation Telegram disabled for tenant {tenant_id}")
521
  break
522
 
523
- if not ns.telegram_chat_id:
524
  logger.warning(f"⚠️ Telegram chat_id not configured for tenant {tenant_id}")
525
  break
526
 
@@ -551,7 +566,8 @@ class EscalationService:
551
  chat_id=ns.telegram_chat_id,
552
  text=message.strip()
553
  )
554
- logger.info(f"📱 Escalation Telegram sent to {ns.telegram_chat_id} for lead {lead_id}")
 
555
  break
556
  except Exception as e:
557
  logger.warning(f"⚠️ Telegram escalation notify failed: {e}")
 
513
  ns = result.scalar_one_or_none()
514
 
515
  if not ns:
516
+ # Fallback: check Tenant table (some legacy tenants store settings in JSON)
517
+ from app.models.tenant import Tenant
518
+ tenant_stmt = select(Tenant).where(Tenant.id == UUID(tenant_id))
519
+ t_result = await session.execute(tenant_stmt)
520
+ tenant = t_result.scalar_one_or_none()
521
+
522
+ if tenant and tenant.notification_settings:
523
+ # Construct mock settings from JSON
524
+ logger.info(f"📂 Using fallback settings from Tenant JSON for {tenant_id}")
525
+ class MockNS: pass
526
+ ns = MockNS()
527
+ ns.telegram_enabled = tenant.notification_settings.get("telegram_enabled", True)
528
+ ns.telegram_chat_id = tenant.notification_settings.get("telegram_chat_id")
529
+ ns.notify_escalations = tenant.notification_settings.get("notify_escalations", True)
530
+ else:
531
+ logger.warning(f"⚠️ No NotificationSettings found (Table or JSON) for tenant {tenant_id}")
532
+ break
533
 
534
+ if not getattr(ns, 'telegram_enabled', True) or not getattr(ns, 'notify_escalations', True):
535
  logger.info(f"📵 Escalation Telegram disabled for tenant {tenant_id}")
536
  break
537
 
538
+ if not getattr(ns, 'telegram_chat_id', None):
539
  logger.warning(f"⚠️ Telegram chat_id not configured for tenant {tenant_id}")
540
  break
541
 
 
566
  chat_id=ns.telegram_chat_id,
567
  text=message.strip()
568
  )
569
+ masked_chat = f"{str(ns.telegram_chat_id)[:4]}...{str(ns.telegram_chat_id)[-4:]}" if ns.telegram_chat_id else "None"
570
+ logger.info(f"📱 Escalation Telegram sent to {masked_chat} for lead {lead_id}")
571
  break
572
  except Exception as e:
573
  logger.warning(f"⚠️ Telegram escalation notify failed: {e}")