150 lines
5.4 KiB
Python
150 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Egyszerűbb változat: Garázs adatok ellenőrzése és javítása.
|
||
"""
|
||
|
||
import asyncio
|
||
import sys
|
||
from sqlalchemy import select
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
from sqlalchemy.orm import selectinload
|
||
|
||
sys.path.insert(0, '/app')
|
||
|
||
from app.db.session import AsyncSessionLocal
|
||
from app.models.identity import User
|
||
from app.models.marketplace.organization import Organization, OrganizationMember
|
||
from app.models.vehicle.asset import Asset
|
||
|
||
async def main():
|
||
print("=" * 60)
|
||
print("GARÁZS ADATOK ELLENŐRZÉSE (Egyszerű változat)")
|
||
print("=" * 60)
|
||
|
||
async with AsyncSessionLocal() as db:
|
||
# 1. Keressük meg a teszt felhasználót
|
||
print("\n1. TESZT FELHASZNÁLÓ KERESÉSE...")
|
||
stmt = select(User).where(User.email == "tester_pro@profibot.hu")
|
||
result = await db.execute(stmt)
|
||
test_user = result.scalar_one_or_none()
|
||
|
||
if not test_user:
|
||
print("❌ HIBA: A teszt felhasználó nem található!")
|
||
return
|
||
|
||
print(f" ✅ Teszt felhasználó: ID={test_user.id}, Email={test_user.email}")
|
||
|
||
# 2. Ellenőrizzük a szervezeti tagságot
|
||
print("\n2. SZERVEZETI TAGSÁG ELLENŐRZÉSE...")
|
||
org_stmt = (
|
||
select(Organization)
|
||
.join(OrganizationMember)
|
||
.where(OrganizationMember.user_id == test_user.id)
|
||
.where(Organization.is_deleted == False)
|
||
)
|
||
org_result = await db.execute(org_stmt)
|
||
user_orgs = org_result.scalars().all()
|
||
|
||
if user_orgs:
|
||
print(f" ✅ A felhasználó tagja {len(user_orgs)} szervezetnek:")
|
||
for org in user_orgs:
|
||
print(f" - {org.name} (ID: {org.id})")
|
||
target_org = user_orgs[0]
|
||
else:
|
||
print(" ℹ️ Nincs szervezet. Új létrehozása...")
|
||
# Egyszerű szervezet létrehozás
|
||
new_org = Organization(
|
||
full_name="Teszt Flotta Kft.",
|
||
name="Teszt Flotta",
|
||
display_name="Teszt Flotta Kft.",
|
||
tax_number="12345678-2-42",
|
||
country_code="HU",
|
||
folder_slug="teszt-flotta-123",
|
||
org_type="business",
|
||
status="active",
|
||
is_active=True,
|
||
owner_id=test_user.id
|
||
)
|
||
db.add(new_org)
|
||
await db.flush()
|
||
|
||
org_member = OrganizationMember(
|
||
organization_id=new_org.id,
|
||
user_id=test_user.id,
|
||
role="ADMIN"
|
||
)
|
||
db.add(org_member)
|
||
|
||
await db.commit()
|
||
await db.refresh(new_org)
|
||
target_org = new_org
|
||
print(f" ✅ Új szervezet: {target_org.name} (ID: {target_org.id})")
|
||
|
||
# 3. Járművek lekérdezése
|
||
print("\n3. JÁRMŰVEK LEKÉRDEZÉSE...")
|
||
asset_stmt = (
|
||
select(Asset)
|
||
.where(Asset.owner_person_id == test_user.id)
|
||
)
|
||
asset_result = await db.execute(asset_stmt)
|
||
assets = asset_result.scalars().all()
|
||
|
||
print(f" ✅ Összesen {len(assets)} jármű található.")
|
||
|
||
if not assets:
|
||
print(" ℹ️ Nincsenek járművek. Nincs mit felosztani.")
|
||
return
|
||
|
||
# 4. Jelenlegi felosztás
|
||
private = [a for a in assets if a.owner_org_id is None]
|
||
corporate = [a for a in assets if a.owner_org_id == target_org.id]
|
||
other = [a for a in assets if a.owner_org_id not in [None, target_org.id]]
|
||
|
||
print(f"\n JELENLEGI ÁLLAPOT:")
|
||
print(f" • Privát: {len(private)} db")
|
||
print(f" • Céges ({target_org.name}): {len(corporate)} db")
|
||
if other:
|
||
print(f" • Egyéb: {len(other)} db")
|
||
|
||
# 5. Ha nincs elég adat, felosztjuk
|
||
if len(private) == 0 or len(corporate) == 0:
|
||
print("\n ℹ️ Járművek felosztása...")
|
||
half = len(assets) // 2
|
||
|
||
for i, asset in enumerate(assets):
|
||
if i < half:
|
||
asset.owner_org_id = None
|
||
else:
|
||
asset.owner_org_id = target_org.id
|
||
|
||
await db.commit()
|
||
print(f" ✅ Felosztva: {half} privát, {len(assets)-half} céges")
|
||
else:
|
||
print("\n ✅ A járművek már megfelelően fel vannak osztva.")
|
||
|
||
# 6. Végeredmény
|
||
print("\n" + "=" * 60)
|
||
print("VÉGEREDMÉNY:")
|
||
print("=" * 60)
|
||
print(f"\n📋 TESZT FELHASZNÁLÓ:")
|
||
print(f" • Email: {test_user.email}")
|
||
print(f" • User ID: {test_user.id}")
|
||
print(f" • Szervezet: {target_org.name} (ID: {target_org.id})")
|
||
|
||
# Új lekérdezés
|
||
asset_result = await db.execute(asset_stmt)
|
||
assets = asset_result.scalars().all()
|
||
private = [a for a in assets if a.owner_org_id is None]
|
||
corporate = [a for a in assets if a.owner_org_id == target_org.id]
|
||
|
||
print(f"\n🚗 JÁRMŰVEGYÜTT:")
|
||
print(f" • Összesen: {len(assets)} db")
|
||
print(f" • Privát garázs: {len(private)} db")
|
||
print(f" • Céges flotta: {len(corporate)} db")
|
||
|
||
print("\n" + "=" * 60)
|
||
print("KÉSZ! A tesztadatok előkészítve.")
|
||
print("=" * 60)
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main()) |