feature/app-selector

This commit is contained in:
2026-05-26 16:59:32 -06:00
parent 904fa965c8
commit dbf68986da
34 changed files with 792 additions and 169 deletions

View File

@@ -4,8 +4,8 @@ from typing import Dict, Any, Literal, Optional
from core.config import settings
from core.database import get_core_db
from core.exceptions import BaseAPIException
from core.security import collect_user_role_names, get_current_user, validate_access_to_resource
from fastapi import APIRouter, Depends, HTTPException, Query, Path
from core.security import collect_user_role_names, get_current_user, validate_access_to_resource, get_active_system
from fastapi import APIRouter, Depends, HTTPException, Query, Path, Request
from sqlalchemy import func, or_, and_
from sqlalchemy.orm import Session
@@ -181,6 +181,7 @@ def get_remesa_suggestion(
@router.post("/invoices/", response_model=schemas.InvoiceHeaderResponse)
def create_invoice(
request: Request,
data: schemas.InvoiceHeaderCreate,
company_id: int = Query(..., description="Company ID"),
db: Session = Depends(get_core_db),
@@ -188,11 +189,16 @@ def create_invoice(
):
try:
tenant_id = validate_access_to_resource(db, company_id, current_user)
# Sistema autoritativo desde el contexto activo (header/cookie)
active_system = get_active_system(request)
if active_system:
data = data.model_copy(update={"system": active_system})
# Validamos usando los datos que vienen en el body (payload)
perm_base = get_invoice_permission_base(data.operation_type, data.invoice_type)
validate_access_to_resource(db, company_id, current_user, required_permissions=[f"{perm_base}.create"])
return services.InvoiceService.create(db, data, tenant_id, company_id)
except HTTPException:
raise
@@ -506,6 +512,7 @@ def interface_cp_genesis(
@router.get("/invoices/", response_model=schemas.InvoiceHeaderListResponse)
def list_invoices(
request: Request,
company_id: int = Query(..., description="Company ID"),
page: int = Query(1, ge=1, description="Page number"),
page_size: int = Query(50, ge=1, le=200, description="Items per page"),
@@ -575,9 +582,10 @@ def list_invoices(
"pedimento": pedimento,
"project_number": project_number,
"year": year,
"allowed_types": allowed_filters
"allowed_types": allowed_filters,
"system": get_active_system(request),
}
filters = {k: v for k, v in filters.items() if v is not None}
items, total = services.InvoiceService.get_all(

View File

@@ -369,6 +369,10 @@ class InvoiceService:
if not filters.get("invoice_type") and ot_exp_val == "exp":
query = query.filter(models.InvoiceHeader.invoice_type != "REPAR")
# Filtro por sistema activo (SCAF / SCAII)
if filters.get("system"):
query = query.filter(models.InvoiceHeader.system == filters["system"])
# Filtro por permisos granulares (allowed_types)
if "allowed_types" in filters:
allowed = filters["allowed_types"]