Spaces:
Sleeping

axeronis commited on
Commit
9b16b22
·
1 Parent(s): 3753e03

debug: add extensive logging to qualification notifications

Browse files
backend/app/services/ai_core/lead_analyzer.py CHANGED
@@ -240,10 +240,14 @@ async def check_and_notify_qualification_complete(
240
  SELECTED = fields from exit_criteria of all active stages.
241
  No duplicates - only sends once per lead.
242
  """
 
 
243
  if lead.qualification_notified:
 
244
  return # Already notified
245
 
246
  qualification_data = lead.qualification_data or {}
 
247
 
248
  # Get SELECTED fields from exit_criteria of all active stages
249
  from app.models.sales_stage import SalesStage
@@ -263,7 +267,10 @@ async def check_and_notify_qualification_complete(
263
  if field:
264
  selected_fields.add(field.lower())
265
 
 
 
266
  if not selected_fields:
 
267
  return # No selected fields configured
268
 
269
  # Check if all SELECTED fields are filled (not "не выявлено")
@@ -284,8 +291,11 @@ async def check_and_notify_qualification_complete(
284
  if value and value.lower() not in ["не выявлено", "не указано", "нет данных", ""]:
285
  filled_count += 1
286
 
 
 
287
  # Mark complete if ALL selected fields are filled
288
  if filled_count >= total_selected:
 
289
  lead.qualification_complete = True
290
 
291
  # Send smart notification via consolidated service
@@ -294,10 +304,10 @@ async def check_and_notify_qualification_complete(
294
  await notification_service.notify_qualification_complete(session, lead, tenant, qualification_data)
295
 
296
  lead.qualification_notified = True
 
297
 
298
  except Exception as e:
299
- # logger is not imported in this scope, use print or ignore
300
- print(f"Failed to send qualification notification: {e}")
301
 
302
 
303
  async def detect_payment_or_order(
 
240
  SELECTED = fields from exit_criteria of all active stages.
241
  No duplicates - only sends once per lead.
242
  """
243
+ logger.info(f"📋 [QUALIFY_CHECK] Checking qualification for lead={lead.id}")
244
+
245
  if lead.qualification_notified:
246
+ logger.info(f"📋 [QUALIFY_CHECK] Already notified, skipping")
247
  return # Already notified
248
 
249
  qualification_data = lead.qualification_data or {}
250
+ logger.info(f"📋 [QUALIFY_CHECK] qualification_data: {qualification_data}")
251
 
252
  # Get SELECTED fields from exit_criteria of all active stages
253
  from app.models.sales_stage import SalesStage
 
267
  if field:
268
  selected_fields.add(field.lower())
269
 
270
+ logger.info(f"📋 [QUALIFY_CHECK] Selected fields from stages: {selected_fields}")
271
+
272
  if not selected_fields:
273
+ logger.info(f"📋 [QUALIFY_CHECK] No selected fields configured, skipping")
274
  return # No selected fields configured
275
 
276
  # Check if all SELECTED fields are filled (not "не выявлено")
 
291
  if value and value.lower() not in ["не выявлено", "не указано", "нет данных", ""]:
292
  filled_count += 1
293
 
294
+ logger.info(f"📋 [QUALIFY_CHECK] Filled: {filled_count}/{total_selected}")
295
+
296
  # Mark complete if ALL selected fields are filled
297
  if filled_count >= total_selected:
298
+ logger.info(f"📋 [QUALIFY_CHECK] All fields filled! Sending notification...")
299
  lead.qualification_complete = True
300
 
301
  # Send smart notification via consolidated service
 
304
  await notification_service.notify_qualification_complete(session, lead, tenant, qualification_data)
305
 
306
  lead.qualification_notified = True
307
+ logger.info(f"📋 [QUALIFY_CHECK] Notification sent, lead marked as notified")
308
 
309
  except Exception as e:
310
+ logger.error(f"📋 [QUALIFY_CHECK] Failed to send qualification notification: {e}", exc_info=True)
 
311
 
312
 
313
  async def detect_payment_or_order(
backend/app/services/notification.py CHANGED
@@ -185,14 +185,22 @@ class NotificationService:
185
 
186
  async def notify_qualification_complete(self, session: Any, lead, tenant: Tenant, qualification_data: Dict = None):
187
  """Send alert when lead is qualified."""
 
 
188
  if not qualification_data:
189
  qualification_data = lead.qualification_data or {}
 
 
190
 
191
  settings = await self._get_settings(session, tenant.id)
 
192
 
193
  # Fail-safe check
194
  tg_chat_id = await self._get_target_chat_id(session, tenant.id, settings)
 
 
195
  if not tg_chat_id:
 
196
  return
197
 
198
  # Format fields
@@ -202,6 +210,7 @@ class NotificationService:
202
  fields_text += f"✅ {k}: {v}\n"
203
 
204
  if not fields_text:
 
205
  return # Don't send empty alerts
206
 
207
  lead_name = lead.name or "Неизвестно"
@@ -212,7 +221,9 @@ class NotificationService:
212
  f"{fields_text}\n"
213
  f"👉 <a href='https://app.microfunnel.online/leads/{lead.id}'>Открыть карточку</a>"
214
  )
 
215
  await self._send_telegram(tenant.telegram_bot_token, tg_chat_id, message)
 
216
 
217
  async def notify_reschedule(
218
  self,
 
185
 
186
  async def notify_qualification_complete(self, session: Any, lead, tenant: Tenant, qualification_data: Dict = None):
187
  """Send alert when lead is qualified."""
188
+ logger.info(f"📋 [QUALIFICATION] notify_qualification_complete called for lead={lead.id}, tenant={tenant.id}")
189
+
190
  if not qualification_data:
191
  qualification_data = lead.qualification_data or {}
192
+
193
+ logger.info(f"📋 [QUALIFICATION] qualification_data: {qualification_data}")
194
 
195
  settings = await self._get_settings(session, tenant.id)
196
+ logger.info(f"📋 [QUALIFICATION] Settings: {settings}, notify_qualification_complete={getattr(settings, 'notify_qualification_complete', None) if settings else None}")
197
 
198
  # Fail-safe check
199
  tg_chat_id = await self._get_target_chat_id(session, tenant.id, settings)
200
+ logger.info(f"📋 [QUALIFICATION] Target chat_id: {tg_chat_id}")
201
+
202
  if not tg_chat_id:
203
+ logger.warning(f"📋 [QUALIFICATION] No chat_id found - notification NOT sent!")
204
  return
205
 
206
  # Format fields
 
210
  fields_text += f"✅ {k}: {v}\n"
211
 
212
  if not fields_text:
213
+ logger.info(f"📋 [QUALIFICATION] No fields to report - skipping notification")
214
  return # Don't send empty alerts
215
 
216
  lead_name = lead.name or "Неизвестно"
 
221
  f"{fields_text}\n"
222
  f"👉 <a href='https://app.microfunnel.online/leads/{lead.id}'>Открыть карточку</a>"
223
  )
224
+ logger.info(f"📋 [QUALIFICATION] Sending notification to {tg_chat_id}...")
225
  await self._send_telegram(tenant.telegram_bot_token, tg_chat_id, message)
226
+ logger.info(f"📋 [QUALIFICATION] Notification sent successfully!")
227
 
228
  async def notify_reschedule(
229
  self,