Okidi Norbert commited on
Commit
896b5bc
·
1 Parent(s): 9ab3de6

fix: allow organization profile creation if missing (upsert)

Browse files
Files changed (1) hide show
  1. app/api/admin.py +20 -2
app/api/admin.py CHANGED
@@ -886,10 +886,28 @@ async def update_profile(
886
  print(f"DEBUG update_profile: updating org {orgs[0]['id']} with payload: {org_payload}")
887
  try:
888
  await supabase.update("organizations", orgs[0]["id"], org_payload)
889
- print("DEBUG update_profile: Supabase update successful")
890
  except Exception as e:
891
- print(f"DEBUG update_profile: Supabase update failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
892
 
 
 
 
 
 
 
 
 
893
  return {"message": "Profile updated"}
894
 
895
  @router.get("/security/settings")
 
886
  print(f"DEBUG update_profile: updating org {orgs[0]['id']} with payload: {org_payload}")
887
  try:
888
  await supabase.update("organizations", orgs[0]["id"], org_payload)
 
889
  except Exception as e:
890
+ print(f"ERROR updating organization: {e}")
891
+ raise HTTPException(status_code=500, detail=f"Failed to update organization: {str(e)}")
892
+ else:
893
+ # Create new organization
894
+ org_payload = {
895
+ "owner_id": current_user["id"],
896
+ "name": profile_data["organization"].get("name", "New Team")
897
+ }
898
+ for k, v in (profile_data["organization"] or {}).items():
899
+ if k in ["id", "owner_id", "created_at", "updated_at"]:
900
+ continue
901
+ org_payload[k] = v
902
 
903
+ print(f"DEBUG update_profile: creating new org for user {current_user['id']} with payload: {org_payload}")
904
+ try:
905
+ new_org = await supabase.insert("organizations", org_payload)
906
+ # Also link user to this org
907
+ await supabase.update("users", current_user["id"], {"organization_id": new_org["id"]})
908
+ except Exception as e:
909
+ print(f"ERROR creating organization: {e}")
910
+ raise HTTPException(status_code=500, detail=f"Failed to create organization: {str(e)}")
911
  return {"message": "Profile updated"}
912
 
913
  @router.get("/security/settings")