#!/usr/bin/env python3 """ Script to create a vehicle for Organization 34 and Branch 18fcd55f... """ import asyncio import aiohttp import sys import os sys.path.insert(0, '/app') from app.db.session import AsyncSessionLocal from app.models.identity import User from app.core.security import create_tokens from sqlalchemy import select async def get_token_for_user(user_id: int): """Generate JWT token for given user.""" async with AsyncSessionLocal() as db: result = await db.execute(select(User).where(User.id == user_id)) user = result.scalar_one_or_none() if not user: raise ValueError(f"User {user_id} not found") token_payload = { "sub": str(user.id), "role": user.role.value if hasattr(user.role, 'value') else user.role, "rank": 10, "scope_level": user.scope_level.value if hasattr(user.scope_level, 'value') else (user.scope_level or "individual"), "scope_id": str(user.scope_id) if user.scope_id else str(user.id) } access_token, refresh_token = create_tokens(data=token_payload) return access_token async def create_vehicle(token: str): """POST /api/v1/assets/vehicles with test data.""" url = "http://sf_api:8000/api/v1/assets/vehicles" headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } # Branch ID: 18fcd55f... (need full UUID). Let's find it from database. async with AsyncSessionLocal() as db: from app.models.marketplace.organization import Branch result = await db.execute(select(Branch).where(Branch.organization_id == 34)) branches = result.scalars().all() for b in branches: print(f"Branch: {b.id}, name: {b.name}") # Use the first branch with ID starting with 18fcd55f if str(b.id).startswith("18fcd55f"): branch_id = b.id break else: # fallback to first branch if branches: branch_id = branches[0].id else: raise ValueError("No branch found for organization 34") payload = { "license_plate": "ABC-123", "vin": "1HGCM82633A123456", "brand": "Toyota", "model": "Corolla", "vehicle_class": "Passenger Car", "fuel_type": "Petrol", "organization_id": 34, # branch_id is not in schema? Actually branch_id is not in AssetCreate. # The branch assignment likely happens via organization_id and garage logic. # We'll rely on the service to assign to the correct branch. } async with aiohttp.ClientSession() as session: async with session.post(url, headers=headers, json=payload) as resp: print(f"Status: {resp.status}") response_text = await resp.text() print(f"Response: {response_text}") if resp.status == 201: data = await resp.json() print(f"Vehicle created successfully: ID={data.get('id')}") return data else: raise Exception(f"Failed to create vehicle: {resp.status} {response_text}") async def main(): try: token = await get_token_for_user(28) print(f"Token obtained: {token[:30]}...") vehicle = await create_vehicle(token) print("Vehicle creation successful.") print(f"Vehicle ID: {vehicle['id']}") print(f"Status: {vehicle.get('status')}") print(f"Data Enrichment Status: {vehicle.get('data_status')}") except Exception as e: print(f"Error: {e}") sys.exit(1) if __name__ == "__main__": asyncio.run(main())