69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/dictionaries.py
|
|
"""
|
|
Szótárak és katalógusok végpontjai.
|
|
- GET /dictionaries/cost-categories: Költségkategóriák lekérése visibility szerint szűrve
|
|
"""
|
|
import logging
|
|
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from app.db.session import get_db
|
|
from app.api import deps
|
|
from app.models.vehicle import CostCategory
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.get("/cost-categories")
|
|
async def list_cost_categories(
|
|
visibility: Optional[str] = Query(None, description="Szűrés láthatóságra: b2c, b2b, both, internal"),
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user = Depends(deps.get_current_user)
|
|
):
|
|
"""
|
|
Költségkategóriák lekérése hierarchikus struktúrában.
|
|
Opcionálisan szűrhető visibility alapján (pl. ha a frontend csak a 'b2c' és 'both' kategóriákat akarja).
|
|
"""
|
|
stmt = select(CostCategory).order_by(CostCategory.name)
|
|
|
|
if visibility:
|
|
# Ha 'b2c' van megadva, akkor a 'b2c' ÉS 'both' láthatóságú kategóriákat adjuk vissza
|
|
if visibility == "b2c":
|
|
stmt = stmt.where(CostCategory.visibility.in_(["b2c", "both"]))
|
|
elif visibility == "b2b":
|
|
stmt = stmt.where(CostCategory.visibility.in_(["b2b", "both"]))
|
|
else:
|
|
stmt = stmt.where(CostCategory.visibility == visibility)
|
|
|
|
result = await db.execute(stmt)
|
|
categories = result.scalars().all()
|
|
|
|
# Hierarchikus struktúra építése
|
|
category_map = {}
|
|
root_categories = []
|
|
|
|
for cat in categories:
|
|
cat_dict = {
|
|
"id": cat.id,
|
|
"code": cat.code,
|
|
"name": cat.name,
|
|
"description": cat.description,
|
|
"parent_id": cat.parent_id,
|
|
"visibility": cat.visibility,
|
|
"accounting_code": cat.accounting_code,
|
|
"is_system": cat.is_system,
|
|
"children": [],
|
|
}
|
|
category_map[cat.id] = cat_dict
|
|
|
|
for cat in categories:
|
|
cat_dict = category_map[cat.id]
|
|
if cat.parent_id and cat.parent_id in category_map:
|
|
category_map[cat.parent_id]["children"].append(cat_dict)
|
|
else:
|
|
root_categories.append(cat_dict)
|
|
|
|
return root_categories
|