Shakeel401 commited on
Commit
a2ce744
·
verified ·
1 Parent(s): 616b275

Update routes/notification_routes.py

Browse files
Files changed (1) hide show
  1. routes/notification_routes.py +27 -2
routes/notification_routes.py CHANGED
@@ -328,10 +328,35 @@ def get_active_admin_tokens() -> list[dict[str, Any]]:
328
  supabase.table("admin_push_tokens")
329
  .select("id,admin_id,expo_push_token,device_name,platform,is_active,created_at,updated_at")
330
  .eq("is_active", True)
 
331
  )
332
  result = execute_supabase_optional(query, "active_admin_push_tokens")
333
 
334
- return result.data if result and result.data else []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  def notify_admins(
336
  event_type: str,
337
  title: str,
@@ -664,4 +689,4 @@ def notify_bulk_campaign(
664
  "type": "campaign_status",
665
  "campaign_id": campaign_id,
666
  },
667
- )
 
328
  supabase.table("admin_push_tokens")
329
  .select("id,admin_id,expo_push_token,device_name,platform,is_active,created_at,updated_at")
330
  .eq("is_active", True)
331
+ .order("updated_at", desc=True)
332
  )
333
  result = execute_supabase_optional(query, "active_admin_push_tokens")
334
 
335
+ rows = result.data if result and result.data else []
336
+ latest_rows: list[dict[str, Any]] = []
337
+ seen_tokens: set[str] = set()
338
+ seen_devices: set[tuple[str, str, str]] = set()
339
+
340
+ for row in rows:
341
+ token = str(row.get("expo_push_token") or "").strip()
342
+ admin_id = str(row.get("admin_id") or "").strip()
343
+ device_name = str(row.get("device_name") or "").strip()
344
+ platform = str(row.get("platform") or "").strip()
345
+ if not token or token in seen_tokens:
346
+ continue
347
+
348
+ # Reinstalling the app generates a new Expo token. Only its newest token
349
+ # should receive a notification for the same named device.
350
+ device_key = (admin_id, device_name, platform)
351
+ if device_name and device_key in seen_devices:
352
+ continue
353
+
354
+ seen_tokens.add(token)
355
+ if device_name:
356
+ seen_devices.add(device_key)
357
+ latest_rows.append(row)
358
+
359
+ return latest_rows
360
  def notify_admins(
361
  event_type: str,
362
  title: str,
 
689
  "type": "campaign_status",
690
  "campaign_id": campaign_id,
691
  },
692
+ )