Spaces:
Running
Running
Update main.py
Browse files2881 unmodified lines288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102 unmodified lines29132914291529162917291829192881 unmodified linesuser_data: Tuple[str, str, str] = Depends(require_role(["owner", "admin"]))):await rate_limit(get_client_ip(request), user_data[1])_, org_id, _ = user_dataclient = get_supabase()# FIX: Get ALL members without pagination limits - no .range() callresult = await run_sync_db_call(client.table("org_members").select("user_id, role, joined_at, profiles(first_name, last_name, email, phone, status, current_points, tier, created_at, address, date_of_birth)").eq("org_id", org_id).execute)members = []for row in (result.data or []):profile = row.get("profiles", {})if isinstance(profile, list) and len(profile) > 0:profile = profile[0]members.append({"id": row["user_id"],"first_name": profile.get("first_name") if profile else None,"last_name": profile.get("last_name") if profile else None,"email": profile.get("email") if profile else None,"phone": profile.get("phone") if profile else None,"role": row["role"],"status": profile.get("status") if profile else "active","joined_at": row["joined_at"],"loyalty_points": profile.get("current_points", 0),2 unmodified lines"address": profile.get("address"),"date_of_birth": profile.get("date_of_birth")})return {"users": members}@app.post("/organisations/me/users")2881 unmodified lines288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312 unmodified lines29342935293629372938293929402881 unmodified linesuser_data: Tuple[str, str, str] = Depends(require_role(["owner", "admin"]))):await rate_limit(get_client_ip(request), user_data[1])user_id, _, _ = user_data # extract the logged-in user IDclient = get_supabase()# 1. Get all organisation IDs where this user is a member (any role)orgs_result = await run_sync_db_call(client.table("org_members").select("org_id").eq("user_id", user_id).execute)org_ids = [row["org_id"] for row in (orgs_result.data or [])]if not org_ids:return {"users": []}# 2. Fetch all members from those organisationsresult = await run_sync_db_call(client.table("org_members").select("user_id, role, joined_at, profiles(first_name, last_name, email, phone, status, current_points, tier, created_at, address, date_of_birth)").in_("org_id", org_ids).execute)# 3. Deduplicate by user_id and exclude owners/adminsseen = set()members = []for row in (result.data or []):uid = row["user_id"]role = row.get("role", "").lower()# Exclude owners and admins – we want only "members" (customers)if role in ("owner", "admin"):continueif uid in seen:continueseen.add(uid)profile = row.get("profiles", {})if isinstance(profile, list) and len(profile) > 0:profile = profile[0]members.append({"id": uid,"first_name": profile.get("first_name") if profile else None,"last_name": profile.get("last_name") if profile else None,"email": profile.get("email") if profile else None,"phone": profile.get("phone") if profile else None,"role": role,"status": profile.get("status") if profile else "active","joined_at": row["joined_at"],"loyalty_points": profile.get("current_points", 0),2 unmodified lines"address": profile.get("address"),"date_of_birth": profile.get("date_of_birth")})return {"users": members}@app.post("/organisations/me/users")
main.py+29-8