- Updated PedimentosService to create and update related tables for Pedimentos. - Added company_id filtering in queries for Pedimentos. - Improved error handling and logging during creation and update processes. - Refactored router tags for consistency and clarity. - Adjusted API endpoints for customs brokers to include company_id in requests. - Enhanced company selection logic in various components to prioritize active company. - Implemented event listeners for company changes to reload data dynamically. - Updated frontend components to handle loading states and errors more effectively. - Ensured all relevant routes and API calls are aligned with the new company context.
86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
from typing import Dict, Any
|
|
from api.v1.common.tenant_crud_routes import TenantCRUDRoutes
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user, validate_access_to_resource
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from . import dto, services
|
|
|
|
# Create main router
|
|
router = APIRouter()
|
|
|
|
# Create CRUD routes for CustomsBroker using TenantCRUDRoutes
|
|
customs_broker_crud = TenantCRUDRoutes(
|
|
service=services.CustomsBrokerService,
|
|
create_schema=dto.CustomsBrokerCreateDTO,
|
|
update_schema=dto.CustomsBrokerUpdateDTO,
|
|
response_schema=dto.CustomsBrokerResponseDTO,
|
|
prefix="/customs-brokers", # No prefix since it's already in the parent router
|
|
tags=[],
|
|
resource_name="Customs Broker",
|
|
id_name="broker_key",
|
|
id_type=str,
|
|
enable_list=True, # Enable list endpoint with pagination
|
|
)
|
|
|
|
# Include the CRUD routes
|
|
router.include_router(customs_broker_crud.router)
|
|
|
|
|
|
# Additional routes for child resources (CustomsBrokerVU and CustomsBrokerPersonnel)
|
|
# These remain as manual routes since they have different patterns
|
|
|
|
@router.put(
|
|
"/customs-broker-vu/{broker_key}",
|
|
response_model=dto.CustomsBrokerVUCreateDTO,
|
|
)
|
|
def update_customs_broker_vu(
|
|
broker_key: str,
|
|
vu_data: dto.CustomsBrokerVUCreateDTO,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
db: Session = Depends(get_core_db),
|
|
current_user: Dict[str, Any] = Depends(get_current_user),
|
|
):
|
|
tenant_id = validate_access_to_resource(db, company_id, current_user)
|
|
|
|
# Verify the broker exists and belongs to the tenant/company
|
|
broker = services.CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id)
|
|
if not broker:
|
|
raise HTTPException(status_code=404, detail="Customs Broker not found")
|
|
|
|
updated_vu = services.CustomsBrokerVUService.update_vu(db, broker_key, vu_data)
|
|
if not updated_vu:
|
|
raise HTTPException(status_code=404, detail="Customs Broker VU not found")
|
|
return updated_vu
|
|
|
|
|
|
@router.put(
|
|
"/customs-broker-personnel/{broker_key}/{line}",
|
|
response_model=dto.CustomsBrokerPersonnelDTO,
|
|
)
|
|
def update_customs_broker_personnel(
|
|
broker_key: str,
|
|
line: int,
|
|
personnel_data: dto.CustomsBrokerPersonnelDTO,
|
|
company_id: int = Query(..., description="Company ID"),
|
|
db: Session = Depends(get_core_db),
|
|
current_user: Dict[str, Any] = Depends(get_current_user),
|
|
):
|
|
tenant_id = validate_access_to_resource(db, company_id, current_user)
|
|
|
|
# Verify the broker exists and belongs to the tenant/company
|
|
broker = services.CustomsBrokerService.get_by_id(db, broker_key, tenant_id, company_id)
|
|
if not broker:
|
|
raise HTTPException(status_code=404, detail="Customs Broker not found")
|
|
|
|
updated_personnel = services.CustomsBrokerPersonnelService.update_personnel(
|
|
db, broker_key, line, personnel_data
|
|
)
|
|
if not updated_personnel:
|
|
raise HTTPException(
|
|
status_code=404, detail="Customs Broker Personnel not found"
|
|
)
|
|
return updated_personnel
|
|
|