2026.03.29 20:00 Gitea_manager javítás előtt
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user