#!/usr/bin/env python3 """ Create a business organization for User 55 using the official API endpoint. """ import sys import asyncio import httpx import json sys.path.append('/app/backend') from app.db.session import AsyncSessionLocal from sqlalchemy import select from app.models.identity import User from app.core.security import create_tokens async def get_auth_token(): """Get authentication token for User 55""" async with AsyncSessionLocal() as db: result = await db.execute(select(User).where(User.id == 55)) user = result.scalar_one_or_none() if user: access_token, _ = create_tokens(data={'sub': str(user.id)}) return access_token else: raise Exception("User 55 not found") async def create_business_organization(): """Create business organization via API""" # Get auth token token = await get_auth_token() print(f"Auth token obtained for User 55") # Prepare API request headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } payload = { "full_name": "Test-Robot Kft.", "name": "Test-Robot Kft.", "display_name": "Test-Robot", "tax_number": "12345678-1-12", "reg_number": "01-09-123456", "country_code": "HU", "language": "hu", "default_currency": "HUF", # Atomic address fields "address_zip": "1132", "address_city": "Budapest", "address_street_name": "Váci", "address_street_type": "út", "address_house_number": "47", "address_stairwell": "A", "address_floor": "1", "address_door": "1", "address_hrsz": None, "contacts": [ { "full_name": "Test User", "email": "test_fallback@profibot.hu", "phone": "+36301234567", "contact_type": "primary" } ] } # Make API call async with httpx.AsyncClient(base_url="http://sf_api:8000", timeout=30.0) as client: try: print("Sending POST request to /api/v1/organizations/onboard...") response = await client.post( "/api/v1/organizations/onboard", json=payload, headers=headers ) print(f"Response status: {response.status_code}") print(f"Response body: {response.text}") if response.status_code in (200, 201): result = response.json() print(f"\n✅ Business organization created successfully!") print(f"Organization ID: {result.get('organization_id')}") print(f"Status: {result.get('status')}") return result.get('organization_id') else: print(f"\n❌ Failed to create organization") return None except Exception as e: print(f"Error making API request: {e}") return None async def verify_organizations(): """Verify User 55's organizations""" async with AsyncSessionLocal() as db: from sqlalchemy import text print("\n=== VERIFYING ORGANIZATIONS FOR USER 55 ===") # Get all organizations for User 55 result = await db.execute(text(''' SELECT o.id, o.full_name, o.org_type, o.tax_number, o.status, om.role FROM fleet.organizations o JOIN fleet.organization_members om ON o.id = om.organization_id WHERE om.user_id = 55 ORDER BY o.org_type ''')) orgs = result.fetchall() print(f"User 55 has access to {len(orgs)} organizations:") for org in orgs: org_dict = dict(org._mapping) print(f" - {org_dict['full_name']} (ID: {org_dict['id']}, Type: {org_dict['org_type']}, Tax: {org_dict['tax_number']}, Role: {org_dict['role']})") # Check branches from app.models.marketplace import Branch from sqlalchemy import select for org in orgs: org_id = dict(org._mapping)['id'] branch_result = await db.execute( select(Branch).where(Branch.organization_id == org_id) ) branches = branch_result.scalars().all() print(f" Branches for org {org_id}: {len(branches)}") for branch in branches: print(f" - {branch.name} (Main: {branch.is_main})") if __name__ == "__main__": print("=== CREATING BUSINESS ORGANIZATION VIA API ===") # Run the async functions org_id = asyncio.run(create_business_organization()) if org_id: print(f"\n✅ Business organization created with ID: {org_id}") asyncio.run(verify_organizations()) else: print("\n❌ Failed to create business organization")