# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/evidence.py import logging from fastapi import APIRouter, UploadFile, File, HTTPException, status, Depends from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, func from app.api.deps import get_db, get_current_user from app.models.identity import User from app.models import Asset # JAVÍTVA: Asset modell from app.models.marketplace.organization import Organization from app.models.core_logic import SubscriptionTier logger = logging.getLogger(__name__) router = APIRouter() @router.post("/scan-registration") async def scan_registration_document(file: UploadFile = File(...), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user)): # ── P0 FEATURE: Quota engine sync — read org's subscription_tier rules ── # Instead of hardcoded plan-based lookup from system_parameters, # we now read the organization's assigned subscription_tier rules. max_allowed = 1 # default fallback if current_user.scope_id: # Get the organization and its subscription tier stmt_org = select(Organization).where(Organization.id == current_user.scope_id) result = await db.execute(stmt_org) org = result.scalar_one_or_none() if org and org.subscription_tier_id: stmt_tier = select(SubscriptionTier).where(SubscriptionTier.id == org.subscription_tier_id) tier_result = await db.execute(stmt_tier) tier = tier_result.scalar_one_or_none() if tier and tier.rules: max_vehicles = tier.rules.get("allowances", {}).get("max_vehicles", 1) max_allowed = max(int(max_vehicles), 1) elif org: # Fallback to legacy base_asset_limit if no tier assigned max_allowed = max(org.base_asset_limit or 1, 1) stmt_count = select(func.count(Asset.id)).where( Asset.owner_org_id == current_user.scope_id, Asset.status == "active" ) count = (await db.execute(stmt_count)).scalar() or 0 if count >= max_allowed: logger.error(f"QUOTA BLOCK TRIGGERED - User Person ID: {current_user.person_id}, Active Count: {count}, Limit: {max_allowed}") raise HTTPException(status_code=403, detail=f"Limit túllépés: {max_allowed} jármű engedélyezett.") # OCR hívás helye... return {"success": True, "message": "Feldolgozás megkezdődött."}