Spaces:
Running
Running
Update main.py
Browse files2894 unmodified lines2895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329241 unmodified line29262927292829292930293129322894 unmodified lines_, org_id, _ = user_dataclient = get_supabase()result = 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 profile:profile = profile[0]members.append({"id": row["user_id"],"first_name": profile.get("first_name"),"last_name": profile.get("last_name"),"email": profile.get("email"),"phone": profile.get("phone"),"role": row["role"],"status": profile.get("status", "active"),"joined_at": row["joined_at"],"loyalty_points": profile.get("current_points", 0),"loyalty_tier": profile.get("tier", "bronze"),"created_at": profile.get("created_at"),1 unmodified line"date_of_birth": profile.get("date_of_birth")})return {"users": members}@app.post("/organisations/me/users")async def create_staff_member(request: Request,2894 unmodified lines2895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829391 unmodified line294129422943294429452946294729482894 unmodified lines_, org_id, _ = user_dataclient = get_supabase()# 1. Get all members of this orgmembers_result = await run_sync_db_call(client.table("org_members").select("user_id, role, joined_at").eq("org_id", org_id).execute)members = members_result.data or []logger.info(f"π Found {len(members)} members in org {org_id}")if not members:return {"users": []}# 2. Collect user IDsuser_ids = [m["user_id"] for m in members]# 3. Fetch profiles for these users in one queryprofiles_result = await run_sync_db_call(client.table("profiles").select("id, first_name, last_name, email, phone, status, current_points, tier, created_at, address, date_of_birth").in_("id", user_ids).execute)profile_map = {p["id"]: p for p in (profiles_result.data or [])}logger.info(f"π Fetched {len(profile_map)} profiles")# 4. Build response (include all members, even if profile missing)users = []for m in members:profile = profile_map.get(m["user_id"], {})users.append({"id": m["user_id"],"first_name": profile.get("first_name"),"last_name": profile.get("last_name"),"email": profile.get("email"),"phone": profile.get("phone"),"role": m["role"],"status": profile.get("status", "active"),"joined_at": m["joined_at"],"loyalty_points": profile.get("current_points", 0),"loyalty_tier": profile.get("tier", "bronze"),"created_at": profile.get("created_at"),1 unmodified line"date_of_birth": profile.get("date_of_birth")})logger.info(f"β Returning {len(users)} users")return {"users": users}@app.post("/organisations/me/users")async def create_staff_member(request: Request,
main.py+31-15