Files
plantillas-proyectos/backend/api/v1/modules/a76/licenses/routes.py
acazares b68c4316ff Refactor backend and frontend code for improved structure and functionality
- 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.
2025-11-11 17:20:47 -06:00

120 lines
3.4 KiB
Python

"""
Endpoints API para gestión de licencias
"""
from core.database import get_core_db
from core.security import get_current_user, has_role
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy.orm import Session
from .dto import (
LicenseCreateDTO,
LicenseResponseDTO,
LicenseUpdateDTO,
LicenseUsageResponseDTO,
LicenseValidationResponseDTO,
)
from .service import LicenseService
router = APIRouter(prefix="/licenses")
@router.post("/", response_model=LicenseResponseDTO, status_code=201)
async def create_license(
license_data: LicenseCreateDTO,
db: Session = Depends(get_core_db),
current_user: dict = Depends(has_role("admin")),
):
"""
Crea una nueva licencia para un tenant
Requiere rol: admin
"""
service = LicenseService(db)
return service.create_license(license_data)
@router.get("/tenant/{tenant_id}", response_model=LicenseResponseDTO)
async def get_license_by_tenant(
tenant_id: int,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user),
):
"""
Obtiene la licencia de un tenant específico
"""
service = LicenseService(db)
license = service.get_license_by_tenant(tenant_id)
if not license:
raise HTTPException(status_code=404, detail="License not found")
return license
@router.put("/tenant/{tenant_id}", response_model=LicenseResponseDTO)
async def update_license(
tenant_id: int,
license_data: LicenseUpdateDTO,
db: Session = Depends(get_core_db),
current_user: dict = Depends(has_role("admin")),
):
"""
Actualiza la licencia de un tenant
Requiere rol: admin
"""
service = LicenseService(db)
license = service.update_license(tenant_id, license_data)
if not license:
raise HTTPException(status_code=404, detail="License not found")
return license
@router.get("/validate/{tenant_id}", response_model=LicenseValidationResponseDTO)
async def validate_license(
tenant_id: int,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user),
):
"""
Valida si la licencia de un tenant está activa y vigente
"""
service = LicenseService(db)
validation = service.validate_license(tenant_id)
return LicenseValidationResponseDTO(**validation)
@router.get("/usage/{tenant_id}", response_model=LicenseUsageResponseDTO)
async def get_license_usage(
tenant_id: int,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user),
):
"""
Obtiene el uso actual de la licencia de un tenant
"""
service = LicenseService(db)
usage = service.get_usage(tenant_id)
if not usage:
raise HTTPException(status_code=404, detail="License not found")
return usage
@router.get("/my-license", response_model=LicenseResponseDTO)
async def get_my_license(
request: Request,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user),
):
"""
Obtiene la licencia del tenant del usuario actual
"""
tenant_id = getattr(request.state, "tenant_id", None)
if not tenant_id:
raise HTTPException(status_code=400, detail="Tenant ID not found in request")
service = LicenseService(db)
license = service.get_license_by_tenant(tenant_id)
if not license:
raise HTTPException(status_code=404, detail="License not found")
return license