Files
CRM_AGENTES_CARGA/backend/api/v1/modules/crm/pipelines/routes.py
Aduanasoft 088a8fc4df feat(crm): dominio backend (cuentas, contactos, prospectos, embudos, oportunidades, actividades)
- Nuevo schema `crm` con 7 tablas multi-tenant (TenantScopedMixin + soft delete)
- Módulos FastAPI por dominio: models/dto/service/routes (patrón example)
- Métricas del dashboard (KPIs + embudo por etapa)
- Conversión de prospecto → cuenta/contacto/oportunidad (idempotente)
- Movimiento de oportunidad entre etapas (Kanban) con estado/probabilidad derivados
- 25 permisos registrados en PermissionRegistry
- Migración Alembic con upgrade/downgrade completos
- 24 tests de servicios (pytest) en verde

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-14 09:32:05 -06:00

111 lines
3.5 KiB
Python

from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from core.database import get_core_db
from core.security import get_current_user
from . import service
from .dto import (
PipelineCreate,
PipelineResponse,
PipelineUpdate,
StageCreate,
StageResponse,
StageUpdate,
)
router = APIRouter()
# ----- Pipelines -----
@router.get("/pipelines", response_model=list[PipelineResponse])
def list_pipelines(
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
return service.get_pipelines(db, tenant_id, company_id)
@router.post("/pipelines", response_model=PipelineResponse, status_code=status.HTTP_201_CREATED)
def create_pipeline(
payload: PipelineCreate,
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
return service.create_pipeline(db, payload, tenant_id, company_id)
@router.patch("/pipelines/{pipeline_id}", response_model=PipelineResponse)
def update_pipeline(
pipeline_id: int,
payload: PipelineUpdate,
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
return service.update_pipeline(db, pipeline_id, payload, tenant_id, company_id)
@router.delete("/pipelines/{pipeline_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_pipeline(
pipeline_id: int,
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
service.delete_pipeline(db, pipeline_id, tenant_id, company_id)
# ----- Stages -----
@router.get("/stages", response_model=list[StageResponse])
def list_stages(
company_id: int = Query(..., description="Company ID"),
pipeline_id: int | None = Query(None, description="Filtrar por embudo"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
return service.get_stages(db, tenant_id, company_id, pipeline_id)
@router.post("/stages", response_model=StageResponse, status_code=status.HTTP_201_CREATED)
def create_stage(
payload: StageCreate,
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
return service.create_stage(db, payload, tenant_id, company_id)
@router.patch("/stages/{stage_id}", response_model=StageResponse)
def update_stage(
stage_id: int,
payload: StageUpdate,
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
return service.update_stage(db, stage_id, payload, tenant_id, company_id)
@router.delete("/stages/{stage_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_stage(
stage_id: int,
company_id: int = Query(..., description="Company ID"),
current_user: dict = Depends(get_current_user),
db: Session = Depends(get_core_db),
):
tenant_id = current_user["tenant_id"]
service.delete_stage(db, stage_id, tenant_id, company_id)