2026.06.04 frontend építés közben
This commit is contained in:
@@ -132,14 +132,28 @@ class AssetService:
|
||||
# Get default vehicle class from config if not provided
|
||||
default_vehicle_class = await config.get_setting(db, "DEFAULT_VEHICLE_CLASS", default="car")
|
||||
|
||||
# --- BRANCH (GARÁZS) HOZZÁRENDELÉSI LOGIKA ---
|
||||
branch_id = getattr(asset_data, 'branch_id', None)
|
||||
if not branch_id and target_org_id:
|
||||
from app.models.marketplace.organization import Branch
|
||||
branch_stmt = select(Branch.id).where(Branch.organization_id == target_org_id, Branch.is_main == True)
|
||||
main_branch_id = (await db.execute(branch_stmt)).scalar()
|
||||
if not main_branch_id:
|
||||
branch_stmt_any = select(Branch.id).where(Branch.organization_id == target_org_id).limit(1)
|
||||
main_branch_id = (await db.execute(branch_stmt_any)).scalar()
|
||||
branch_id = main_branch_id
|
||||
print(f"DEBUG: resolved branch_id={branch_id} for target_org_id={target_org_id}")
|
||||
|
||||
|
||||
asset_fields = {
|
||||
'vin': vin_clean,
|
||||
'license_plate': license_plate_clean,
|
||||
'catalog_id': asset_data.catalog_id,
|
||||
'current_organization_id': target_org_id,
|
||||
'branch_id': branch_id,
|
||||
'owner_person_id': user.person_id,
|
||||
'owner_org_id': asset_data.owner_org_id or target_org_id,
|
||||
'operator_org_id': asset_data.operator_org_id,
|
||||
'owner_org_id': asset_data.organization_id or target_org_id,
|
||||
'operator_org_id': None, # AssetCreate doesn't have operator_org_id
|
||||
'status': status,
|
||||
'individual_equipment': asset_data.individual_equipment or {},
|
||||
'created_at': datetime.utcnow(),
|
||||
@@ -184,8 +198,23 @@ class AssetService:
|
||||
db.add(new_asset)
|
||||
await db.flush()
|
||||
|
||||
# --- MATCHER INTEGRÁCIÓ (HA NINCS CATALOG_ID DE VANNAK ADATOK) ---
|
||||
if not new_asset.catalog_id and new_asset.brand and new_asset.model:
|
||||
from app.services.asset_matcher_service import AssetMatcherService
|
||||
print(f"DEBUG: Matcher args: make={new_asset.brand}, model={new_asset.model}, year={new_asset.year_of_manufacture}")
|
||||
matched_result = await AssetMatcherService.find_best_match(db, new_asset)
|
||||
if matched_result:
|
||||
matched_def, conf = matched_result
|
||||
if matched_def:
|
||||
new_asset.catalog_id = matched_def.id
|
||||
await AssetMatcherService.enrich_asset_from_definition(db, new_asset, matched_def, conf)
|
||||
await db.flush()
|
||||
|
||||
|
||||
# Digitális Iker Alapmodulok
|
||||
db.add(AssetAssignment(asset_id=new_asset.id, organization_id=target_org_id, status="active"))
|
||||
# Only create AssetAssignment if we have an organization_id
|
||||
if target_org_id is not None:
|
||||
db.add(AssetAssignment(asset_id=new_asset.id, organization_id=target_org_id, status="active"))
|
||||
db.add(AssetTelemetry(asset_id=new_asset.id))
|
||||
db.add(AssetFinancials(
|
||||
asset_id=new_asset.id,
|
||||
@@ -202,6 +231,9 @@ class AssetService:
|
||||
await AssetService._award_first_car_badge(db, user_id, target_org_id)
|
||||
|
||||
await db.commit()
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
new_asset = (await db.execute(select(Asset).where(Asset.id == new_asset.id).options(selectinload(Asset.catalog)))).scalar_one()
|
||||
return new_asset
|
||||
|
||||
except Exception as e:
|
||||
@@ -269,16 +301,20 @@ class AssetService:
|
||||
else:
|
||||
logger.warning("execute_final_transfer called without user_id, ownership fields not updated")
|
||||
|
||||
db.add(AssetAssignment(asset_id=asset.id, organization_id=new_org_id, status="active"))
|
||||
# Only create AssetAssignment if we have an organization_id
|
||||
if new_org_id is not None:
|
||||
db.add(AssetAssignment(asset_id=asset.id, organization_id=new_org_id, status="active"))
|
||||
|
||||
await db.commit()
|
||||
return asset
|
||||
|
||||
# --- CATALOG METHODS ---
|
||||
@staticmethod
|
||||
async def get_makes(db: AsyncSession) -> List[str]:
|
||||
"""Get all distinct makes from vehicle model definitions."""
|
||||
async def get_makes(db: AsyncSession, vehicle_class: Optional[str] = None) -> List[str]:
|
||||
"""Get all distinct makes from vehicle model definitions, optionally filtered by vehicle_class."""
|
||||
stmt = select(distinct(VehicleModelDefinition.make)).order_by(VehicleModelDefinition.make)
|
||||
if vehicle_class:
|
||||
stmt = stmt.where(VehicleModelDefinition.vehicle_class == vehicle_class)
|
||||
result = await db.execute(stmt)
|
||||
makes = result.scalars().all()
|
||||
return [make for make in makes if make] # Filter out None/empty
|
||||
|
||||
Reference in New Issue
Block a user