Deduplicate ops memberships by user and latest expiry

This commit is contained in:
2569718930@qq.com
2026-03-21 13:06:10 +08:00
parent 75c2baf1d1
commit 49d883e3a2
+10 -3
View File
@@ -259,12 +259,11 @@ async def ops_memberships(request: Request, limit: int = 200):
user_map = db.get_users_by_supabase_user_ids( user_map = db.get_users_by_supabase_user_ids(
[str(item.get("user_id") or "") for item in subscriptions] [str(item.get("user_id") or "") for item in subscriptions]
) )
rows = [] deduped: dict[str, dict] = {}
for item in subscriptions: for item in subscriptions:
user_id = str(item.get("user_id") or "").strip().lower() user_id = str(item.get("user_id") or "").strip().lower()
local_user = user_map.get(user_id, {}) local_user = user_map.get(user_id, {})
rows.append( row = {
{
"user_id": user_id, "user_id": user_id,
"email": str(local_user.get("supabase_email") or ""), "email": str(local_user.get("supabase_email") or ""),
"telegram_id": local_user.get("telegram_id"), "telegram_id": local_user.get("telegram_id"),
@@ -274,6 +273,14 @@ async def ops_memberships(request: Request, limit: int = 200):
"starts_at": item.get("starts_at"), "starts_at": item.get("starts_at"),
"expires_at": item.get("expires_at"), "expires_at": item.get("expires_at"),
} }
existing = deduped.get(user_id)
existing_expires = str(existing.get("expires_at") or "") if existing else ""
current_expires = str(row.get("expires_at") or "")
if existing is None or current_expires > existing_expires:
deduped[user_id] = row
rows = sorted(
deduped.values(),
key=lambda item: str(item.get("expires_at") or ""),
) )
return {"memberships": rows} return {"memberships": rows}