Spaces:
Running
Running
axeronis commited on
Commit Β·
c13b20f
1
Parent(s): 9b16b22
debug: detailed logging in _get_target_chat_id to diagnose None chat_id
Browse files
backend/app/services/notification.py
CHANGED
|
@@ -28,19 +28,38 @@ class NotificationService:
|
|
| 28 |
Get chat ID from settings, OR fallback to Owner's personal chat ID.
|
| 29 |
Fail-safe for new users who haven't configured notification settings yet.
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Fallback: Find Owner
|
|
|
|
| 35 |
stmt = select(User).where(User.tenant_id == tenant_id, User.role == UserRole.OWNER)
|
| 36 |
result = await session.execute(stmt)
|
| 37 |
owner_users = result.scalars().all()
|
|
|
|
| 38 |
|
| 39 |
# Return first owner with chat_id
|
| 40 |
for user in owner_users:
|
| 41 |
if user.telegram_chat_id:
|
|
|
|
| 42 |
return user.telegram_chat_id
|
| 43 |
|
|
|
|
| 44 |
return None
|
| 45 |
|
| 46 |
async def notify_new_order(self, session: Any, tenant: Tenant, order: Order, items_text: str):
|
|
|
|
| 28 |
Get chat ID from settings, OR fallback to Owner's personal chat ID.
|
| 29 |
Fail-safe for new users who haven't configured notification settings yet.
|
| 30 |
"""
|
| 31 |
+
logger.info(f"π [GET_CHAT_ID] Getting chat_id for tenant {tenant_id}")
|
| 32 |
+
logger.info(f"π [GET_CHAT_ID] Settings object: {settings}")
|
| 33 |
+
|
| 34 |
+
if settings:
|
| 35 |
+
logger.info(f"π [GET_CHAT_ID] telegram_enabled={settings.telegram_enabled}, telegram_chat_id='{settings.telegram_chat_id}', telegram_channel_id='{settings.telegram_channel_id}'")
|
| 36 |
+
|
| 37 |
+
# First try personal chat_id
|
| 38 |
+
if settings.telegram_enabled and settings.telegram_chat_id:
|
| 39 |
+
logger.info(f"π [GET_CHAT_ID] Using settings.telegram_chat_id: {settings.telegram_chat_id}")
|
| 40 |
+
return settings.telegram_chat_id
|
| 41 |
+
|
| 42 |
+
# Then try team channel
|
| 43 |
+
if settings.telegram_enabled and settings.telegram_channel_id:
|
| 44 |
+
logger.info(f"π [GET_CHAT_ID] Using settings.telegram_channel_id: {settings.telegram_channel_id}")
|
| 45 |
+
return settings.telegram_channel_id
|
| 46 |
+
else:
|
| 47 |
+
logger.warning(f"π [GET_CHAT_ID] Settings is None!")
|
| 48 |
|
| 49 |
# Fallback: Find Owner
|
| 50 |
+
logger.info(f"π [GET_CHAT_ID] Trying fallback to Owner's chat_id...")
|
| 51 |
stmt = select(User).where(User.tenant_id == tenant_id, User.role == UserRole.OWNER)
|
| 52 |
result = await session.execute(stmt)
|
| 53 |
owner_users = result.scalars().all()
|
| 54 |
+
logger.info(f"π [GET_CHAT_ID] Found {len(owner_users)} owner(s)")
|
| 55 |
|
| 56 |
# Return first owner with chat_id
|
| 57 |
for user in owner_users:
|
| 58 |
if user.telegram_chat_id:
|
| 59 |
+
logger.info(f"π [GET_CHAT_ID] Using Owner {user.email}'s chat_id: {user.telegram_chat_id}")
|
| 60 |
return user.telegram_chat_id
|
| 61 |
|
| 62 |
+
logger.warning(f"π [GET_CHAT_ID] No chat_id found anywhere!")
|
| 63 |
return None
|
| 64 |
|
| 65 |
async def notify_new_order(self, session: Any, tenant: Tenant, order: Order, items_text: str):
|