2026.03.29 20:00 Gitea_manager javítás előtt

This commit is contained in:
Roo
2026-03-29 17:59:06 +00:00
parent 03258db091
commit ba8b6579ef
148 changed files with 7951 additions and 591 deletions

View File

@@ -102,6 +102,58 @@ async def list_asset_costs(
return res.scalars().all()
@router.get("/{asset_id}", response_model=AssetResponse)
async def get_asset(
asset_id: uuid.UUID,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Get detailed information about a specific vehicle/asset.
Returns the asset's full technical profile including catalog data
and vehicle model definition specifications.
"""
# Check if user has access to this asset
from sqlalchemy import or_
from app.models.marketplace.organization import OrganizationMember
# Get user's organization memberships
org_stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
)
org_result = await db.execute(org_stmt)
user_org_ids = [row[0] for row in org_result.all()]
# Query asset with catalog and master definition
stmt = (
select(Asset)
.where(
Asset.id == asset_id,
or_(
Asset.owner_person_id == current_user.id,
Asset.owner_org_id.in_(user_org_ids) if user_org_ids else False,
Asset.operator_person_id == current_user.id,
Asset.operator_org_id.in_(user_org_ids) if user_org_ids else False
)
)
.options(
selectinload(Asset.catalog).selectinload(AssetCatalog.master_definition)
)
)
result = await db.execute(stmt)
asset = result.scalar_one_or_none()
if not asset:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Asset not found or you don't have permission to access it"
)
return asset
@router.post("/vehicles", response_model=AssetResponse, status_code=status.HTTP_201_CREATED)
async def create_or_claim_vehicle(
payload: AssetCreate,
@@ -118,10 +170,30 @@ async def create_or_claim_vehicle(
- XP jutalom adása a felhasználónak
"""
try:
# Determine organization ID: use provided or default to user's first organization
org_id = payload.organization_id
if org_id is None:
# Get user's organization memberships
from app.models.marketplace.organization import OrganizationMember
org_stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
).limit(1)
org_result = await db.execute(org_stmt)
user_org = org_result.scalar_one_or_none()
if user_org is None:
# User has no organization - create a personal organization or use default
# For now, raise an error (in future, we could create a personal org)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="No organization found for user. Please specify an organization_id or join/create an organization first."
)
org_id = user_org
asset = await AssetService.create_or_claim_vehicle(
db=db,
user_id=current_user.id,
org_id=payload.organization_id,
org_id=org_id,
vin=payload.vin,
license_plate=payload.license_plate,
catalog_id=payload.catalog_id
@@ -134,4 +206,205 @@ async def create_or_claim_vehicle(
except Exception as e:
logger = logging.getLogger(__name__)
logger.error(f"Vehicle creation error: {e}")
raise HTTPException(status_code=500, detail="Belső szerverhiba a jármű létrehozásakor")
raise HTTPException(status_code=500, detail="Belső szerverhiba a jármű létrehozásakor")
@router.get("/{asset_id}/maintenance", response_model=List[AssetCostResponse])
async def list_maintenance_records(
asset_id: uuid.UUID,
skip: int = 0,
limit: int = 100,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
List maintenance records for a specific vehicle.
Returns paginated list of maintenance costs with cost_category = 'maintenance'.
"""
# Check if user has access to this asset
from sqlalchemy import or_
from app.models.marketplace.organization import OrganizationMember
# Get user's organization memberships
org_stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
)
org_result = await db.execute(org_stmt)
user_org_ids = [row[0] for row in org_result.all()]
# Check asset access
asset_stmt = select(Asset).where(
Asset.id == asset_id,
or_(
Asset.owner_person_id == current_user.id,
Asset.owner_org_id.in_(user_org_ids) if user_org_ids else False,
Asset.operator_person_id == current_user.id,
Asset.operator_org_id.in_(user_org_ids) if user_org_ids else False
)
)
asset_result = await db.execute(asset_stmt)
asset = asset_result.scalar_one_or_none()
if not asset:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Asset not found or you don't have permission to access it"
)
# Query maintenance costs
stmt = (
select(AssetCost)
.where(
AssetCost.asset_id == asset_id,
AssetCost.cost_category == "maintenance"
)
.order_by(desc(AssetCost.date))
.offset(skip)
.limit(limit)
)
res = await db.execute(stmt)
return res.scalars().all()
@router.post("/{asset_id}/maintenance", response_model=AssetCostResponse, status_code=status.HTTP_201_CREATED)
async def create_maintenance_record(
asset_id: uuid.UUID,
payload: Dict[str, Any],
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""
Add a maintenance record for a vehicle.
Expected payload fields:
- date: ISO datetime string (required)
- odometer: integer (optional, current mileage)
- description: string (required)
- cost: float (required, net amount)
- currency: string (optional, default: "EUR")
- invoice_number: string (optional)
"""
# Check if user has access to this asset
from sqlalchemy import or_
from app.models.marketplace.organization import OrganizationMember
# Get user's organization memberships
org_stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
)
org_result = await db.execute(org_stmt)
user_org_ids = [row[0] for row in org_result.all()]
# Check asset access and get asset
asset_stmt = select(Asset).where(
Asset.id == asset_id,
or_(
Asset.owner_person_id == current_user.id,
Asset.owner_org_id.in_(user_org_ids) if user_org_ids else False,
Asset.operator_person_id == current_user.id,
Asset.operator_org_id.in_(user_org_ids) if user_org_ids else False
)
)
asset_result = await db.execute(asset_stmt)
asset = asset_result.scalar_one_or_none()
if not asset:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Asset not found or you don't have permission to access it"
)
# Validate required fields
required_fields = ["date", "description", "cost"]
for field in required_fields:
if field not in payload:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"Missing required field: {field}"
)
try:
# Parse date
from datetime import datetime
date = datetime.fromisoformat(payload["date"].replace("Z", "+00:00"))
# Determine organization ID: use asset's current org, owner org, or user's active organization
organization_id = asset.current_organization_id or asset.owner_org_id
if not organization_id:
# Get user's active organization from their scope_id
if current_user.scope_id:
try:
organization_id = int(current_user.scope_id)
except (ValueError, TypeError):
# If scope_id is not a valid integer, try to get from organization memberships
from sqlalchemy import select
from app.models.marketplace.organization import OrganizationMember
stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
).limit(1)
result = await db.execute(stmt)
org_row = result.first()
organization_id = org_row[0] if org_row else None
else:
# Try to get from organization memberships
from sqlalchemy import select
from app.models.marketplace.organization import OrganizationMember
stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
).limit(1)
result = await db.execute(stmt)
org_row = result.first()
organization_id = org_row[0] if org_row else None
if not organization_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Cannot determine organization for this cost record. Please ensure you have an active organization."
)
# Create AssetCost record
maintenance_cost = AssetCost(
asset_id=asset_id,
organization_id=organization_id,
cost_category="maintenance",
amount_net=float(payload["cost"]),
currency=payload.get("currency", "EUR"),
date=date,
invoice_number=payload.get("invoice_number"),
data={
"odometer": payload.get("odometer"),
"description": payload["description"],
"type": "maintenance"
}
)
db.add(maintenance_cost)
await db.commit()
await db.refresh(maintenance_cost)
# Also create an AssetEvent for the maintenance
from app.models.vehicle import AssetEvent
maintenance_event = AssetEvent(
asset_id=asset_id,
event_type="maintenance"
)
db.add(maintenance_event)
await db.commit()
return maintenance_cost
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=f"Invalid data format: {str(e)}"
)
except Exception as e:
await db.rollback()
logger = logging.getLogger(__name__)
logger.error(f"Maintenance record creation error: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Internal server error while creating maintenance record"
)

View File

@@ -66,4 +66,12 @@ async def complete_kyc(kyc_in: UserKYCComplete, db: AsyncSession = Depends(get_d
user = await AuthService.complete_kyc(db, current_user.id, kyc_in)
if not user:
raise HTTPException(status_code=404, detail="User nem található.")
return {"status": "success", "message": "Fiók aktiválva."}
return {"status": "success", "message": "Fiók aktiválva."}
@router.get("/me")
async def get_current_user_profile(current_user: User = Depends(get_current_user)):
"""
Return current user profile (alias for /users/me).
"""
from app.schemas.user import UserResponse
return UserResponse.model_validate(current_user)

View File

@@ -24,10 +24,13 @@ async def list_models(
current_user = Depends(deps.get_current_user)
):
"""2. Szint: Típusok listázása egy adott márkához."""
# Handle empty or invalid parameters gracefully
if not make or make.strip() == "":
return []
models = await AssetService.get_models(db, make)
if not models:
raise HTTPException(status_code=404, detail="Márka nem található vagy nincsenek típusok.")
return models
# Return empty list instead of 404 - frontend can handle empty dropdown
return models or []
# Secured endpoint: Closed premium ecosystem
@router.get("/generations", response_model=List[str])
@@ -38,10 +41,13 @@ async def list_generations(
current_user = Depends(deps.get_current_user)
):
"""3. Szint: Generációk/Évjáratok listázása."""
# Handle empty or invalid parameters gracefully
if not make or not model or make.strip() == "" or model.strip() == "":
return []
generations = await AssetService.get_generations(db, make, model)
if not generations:
raise HTTPException(status_code=404, detail="Nincs generációs adat ehhez a típushoz.")
return generations
# Return empty list instead of 404 - frontend can handle empty dropdown
return generations or []
# Secured endpoint: Closed premium ecosystem
@router.get("/engines")

View File

@@ -1,9 +1,9 @@
# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/expenses.py
from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from sqlalchemy import select, func
from app.api.deps import get_db, get_current_user
from app.models import Asset, AssetCost
from app.models import Asset, AssetCost, SystemParameter
from app.schemas.asset_cost import AssetCostCreate
from datetime import datetime
@@ -26,6 +26,33 @@ async def create_expense(
if not asset:
raise HTTPException(status_code=404, detail="Asset not found.")
# Dynamic Gatekeeper: Check draft expense limit
if asset.status == "draft":
# 1. Get VEHICLE_DRAFT_MAX_EXPENSES parameter
param_stmt = select(SystemParameter).where(
SystemParameter.key == "VEHICLE_DRAFT_MAX_EXPENSES",
SystemParameter.scope_level == "global"
)
param_result = await db.execute(param_stmt)
param = param_result.scalar_one_or_none()
if param:
limit = param.value.get("limit", 10) # Default to 10 if not found
else:
limit = 10 # Default fallback
# 2. Count existing expenses for this asset
count_stmt = select(func.count(AssetCost.id)).where(AssetCost.asset_id == asset.id)
count_result = await db.execute(count_stmt)
expense_count = count_result.scalar()
# 3. Check if limit reached
if expense_count >= limit:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"DRAFT_LIMIT_REACHED: Draft vehicles are limited to {limit} expenses. This asset already has {expense_count} expenses."
)
# Determine organization_id from asset (required by AssetCost model)
organization_id = asset.current_organization_id or asset.owner_org_id
if not organization_id:

View File

@@ -17,7 +17,79 @@ async def read_users_me(
current_user: User = Depends(get_current_user),
):
"""Visszaadja a bejelentkezett felhasználó profilját"""
return current_user
from sqlalchemy import select, or_
from app.models.marketplace.organization import Organization, OrganizationMember
from app.models.marketplace.organization import OrgUserRole
# Determine active organization ID
active_org_id = None
# If user already has a scope_id, use it
if current_user.scope_id is not None:
try:
active_org_id = int(current_user.scope_id)
except (ValueError, TypeError):
active_org_id = None
# If still no active org ID, try to find user's primary organization
if active_org_id is None:
# 1. Check if user is a member of any organization with ADMIN/OWNER role
stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id,
or_(
OrganizationMember.role == OrgUserRole.ADMIN,
OrganizationMember.role == OrgUserRole.OWNER
)
).limit(1)
result = await db.execute(stmt)
org_member_row = result.first()
if org_member_row:
active_org_id = org_member_row[0]
else:
# 2. Check if user owns any organization (owner_id matches user.id)
stmt = select(Organization.id).where(
Organization.owner_id == current_user.id
).limit(1)
result = await db.execute(stmt)
org_owner_row = result.first()
if org_owner_row:
active_org_id = org_owner_row[0]
else:
# 3. Fallback: get first organization they're a member of
stmt = select(OrganizationMember.organization_id).where(
OrganizationMember.user_id == current_user.id
).limit(1)
result = await db.execute(stmt)
org_row = result.first()
active_org_id = org_row[0] if org_row else None
# Create a response dictionary with the active_organization_id
# Get first_name and last_name from person relation if available
person = current_user.person
first_name = person.first_name if person else ""
last_name = person.last_name if person else ""
response_data = {
"id": current_user.id,
"email": current_user.email,
"first_name": first_name,
"last_name": last_name,
"is_active": current_user.is_active,
"region_code": current_user.region_code,
"person_id": current_user.person_id,
"role": current_user.role.value if hasattr(current_user.role, 'value') else str(current_user.role),
"subscription_plan": current_user.subscription_plan,
"scope_level": current_user.scope_level or "individual",
"scope_id": str(active_org_id) if active_org_id else None,
"ui_mode": current_user.ui_mode or "personal",
"active_organization_id": active_org_id
}
return UserResponse.model_validate(response_data)
@router.get("/me/trust")
async def get_user_trust(