- Rearranged imports in multiple files for consistency and clarity. - Updated logging middleware to exclude specific paths from logging. - Enhanced security module by cleaning up token handling and improving tenant validation. - Added tenant and company scoped mixins for better database model management. - Implemented generic CRUD routes for tenant-scoped resources. - Improved error handling and response management in API routes. - Cleaned up login and logout processes to ensure proper session management. - Introduced mechanisms to clear local storage and cookies on tenant change. - Enhanced company store to detect tenant changes and clear data accordingly. - Added new DTO mixins for currency and value affect flags.
128 lines
3.7 KiB
Python
128 lines
3.7 KiB
Python
from typing import List
|
|
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from .dto import GBultoCreateDTO, GBultoResponseDTO, GBultoUpdateDTO
|
|
from .models import Package
|
|
from .services import GBultoService
|
|
|
|
router = APIRouter(prefix="/bultos", tags=["GBultos"])
|
|
|
|
|
|
@router.get("/", response_model=List[GBultoResponseDTO])
|
|
async def list_bultos(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
List all GBultos with pagination.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
return db.query(Package).offset(skip).limit(limit).all()
|
|
|
|
|
|
@router.get("/{code}", response_model=GBultoResponseDTO)
|
|
async def read_bulto(
|
|
code: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Get a specific Package by its CODE.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
bulto = GBultoService.get_bulto_by_code(db, code)
|
|
if not bulto:
|
|
raise HTTPException(status_code=404, detail="Package not found")
|
|
return bulto
|
|
|
|
|
|
@router.post("/", response_model=GBultoResponseDTO, status_code=status.HTTP_201_CREATED)
|
|
async def create_gbulto(
|
|
bulto_data: GBultoCreateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Create a new Package.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
return GBultoService.create_gbulto(db, bulto_data)
|
|
|
|
|
|
@router.put("/{code}", response_model=GBultoResponseDTO)
|
|
async def update_bulto(
|
|
code: str,
|
|
bulto_data: GBultoUpdateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Update an existing Package.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
bulto = GBultoService.update_bulto(db, code, bulto_data)
|
|
if not bulto:
|
|
raise HTTPException(status_code=404, detail="Package not found")
|
|
return bulto
|
|
|
|
|
|
@router.delete("/{code}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_bulto(
|
|
code: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Delete a Package by its CODE.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
bulto = GBultoService.delete_bulto(db, code)
|
|
if not bulto:
|
|
raise HTTPException(status_code=404, detail="Package not found")
|