feature/refactorizacion-tabla-sector

This commit is contained in:
hreyes
2026-03-17 11:21:28 -06:00
parent 20a351c475
commit fbbe4370de
32 changed files with 4167 additions and 3947 deletions

View File

@@ -17,7 +17,6 @@ from .material_types.routes import router as material_types_router
from .payment_methods.routes import router as payment_methods_router
from .pedimento_codes.routes import router as pedimento_codes_router
from .pedimento_regimens.routes import router as pedimento_regimens_router
from .sectors.routes import router as sectors_router
from .states.routes import router as states_router
from .trailer_types.routes import router as trailer_types_router
from .transport_modes.routes import router as transport_modes_router
@@ -81,11 +80,6 @@ router.include_router(
prefix="/reference_data",
tags=["public / reference_data / valuation_methods"],
)
router.include_router(
sectors_router,
prefix="/reference_data",
tags=["public / public / reference_data / sectors"],
)
router.include_router(
transport_modes_router,
prefix="/reference_data",

View File

@@ -1,9 +0,0 @@
from pydantic import BaseModel, ConfigDict, Field
class SectorDTO(BaseModel):
key: str = Field(..., min_length=1, max_length=8)
description: str
authorized: bool
model_config = ConfigDict(from_attributes=True)

View File

@@ -1,23 +0,0 @@
from core.database import Base
from sqlalchemy import Boolean, PrimaryKeyConstraint, SmallInteger, String
from sqlalchemy.orm import Mapped, mapped_column
class Sector(Base):
__tablename__ = "sectors" # GSectores
__table_args__ = (
PrimaryKeyConstraint("key", name="sectors_pkey"),
{"schema": "public", "extend_existing": True}, # opcional
)
key: Mapped[str] = mapped_column(
String(8), nullable=False) # clave del sector
description: Mapped[str] = mapped_column(
String(150), nullable=False
) # descripción oficial (en español)
authorized: Mapped[bool] = mapped_column(
Boolean
) # True = autorizado, False = no autorizado
def __repr__(self):
return f"<Sector(key={self.key}, description={self.description}, authorized={self.authorized})>"

View File

@@ -1,56 +0,0 @@
from typing import Any, Dict, Optional
from core.database import get_core_db
from core.security import get_current_user
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy import or_
from sqlalchemy.orm import Session
from .dto import SectorDTO
from .models import Sector
router = APIRouter(prefix="/sectors")
@router.get("/", response_model=Dict[str, Any])
def list_sectors(
page: int = Query(1, ge=1, description="Número de página"),
page_size: int = Query(50, ge=1, le=100, description="Tamaño de página"),
search: Optional[str] = Query(None, description="Término de búsqueda"),
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user),
):
skip = (page - 1) * page_size
query = db.query(Sector)
if search:
search_filter = or_(
Sector.key.ilike(f"%{search}%"),
Sector.description.ilike(f"%{search}%")
)
query = query.filter(search_filter)
total = query.count()
# Add deterministic sort order
query = query.order_by(Sector.key)
items = query.offset(skip).limit(page_size).all()
return {
"items": [SectorDTO.model_validate(obj) for obj in items],
"total": total,
"page": page,
"page_size": page_size,
}
@router.get("/{key}", response_model=SectorDTO)
def get_sector(
key: str,
db: Session = Depends(get_core_db),
current_user: dict = Depends(get_current_user),
):
obj = db.query(Sector).filter(Sector.key == key).first()
if not obj:
raise HTTPException(status_code=404, detail="Not found")
return obj

View File

@@ -1,55 +0,0 @@
seed = [
("I", "INDUSTRIA ELECTRICA", "0"),
("II", "INDUSTRIA ELECTRONICA", "0"),
(
"IIa",
"PARA LOS BIENES A QUE SE REFIERE LA FRACCION II, INCISO a) o b), DE ARTICULO 4to DE ESTE DECRETO.",
"0",
),
(
"IIb",
"PARA LOS BIENES A QUE SE REFIERE LA FRACCION II, INCISO b), DE ARTICULO 4to DE ESTE DECRETO.",
"0",
),
("III", "INDUSTRIA DEL MUEBLE", "0"),
("IV", "INDUSTRIA DEL JUGUETE, JUEGOS DE RECREO Y ARTICULOS DEPORTIVOS", "0"),
("IX", "INDUSTRIA DE MAQUINARIA AGRICOLA", "0"),
("V", "INDUSTRIA DEL CALZADO", "0"),
("VI", "INDUSTRIA MINERA Y METALURGICA", "0"),
("VII", "INDUSTRIA DE BIENES DE CAPITAL", "0"),
("VIII", "INDUSTRIA FOTOGRAFICA", "0"),
("X", "INDUSTRIAS DIVERSAS", "0"),
("XI", "INDUSTRIA QUIMICA", "0"),
("XII", "INDUSTRIAS DE MANUFACTURAS DEL CAUCHO Y PLASTICOS", "0"),
("XIII", "INDUSTRIA SIDERURGICA", "0"),
("XIV", "INDUSTRIA DE PRODUCTOS FARMOQUIMICOS, MEDICAMENTOS Y EQUIPO MEDICO", "0"),
("XIX", "INDUSTRIA AUTOMOTRIZ Y DE AUTOPARTES", "0"),
("XIXa", "INDUSTRIA AUTOMOTRIZ Y DE AUTOPARTES", "0"),
("XIXb", "INDUSTRIA AUTOMOTRIZ Y DE AUTOPARTES", "0"),
(
"XV",
"INDUSTRIA DEL TRANSPORTE, EXCEPTO EL SECTOR DE LA INDUSTRIA AUTOMOTRIZ Y DE AUTOPARTES",
"0",
),
(
"XVa",
"INDUSTRIA DEL TRANSPORTE, EXCEPTO EL SECTOR DE LA INDUSTRIA AUTOMOTRIZ Y DE AUTOPARTES.",
"0",
),
(
"XVb",
"INDUSTRIA DEL TRANSPORTE, EXCEPTO EL SECTOR DE LA INDUSTRIA AUTOMOTRIZ Y DE AUTOPARTES.",
"0",
),
("XVI", "INDUSTRIA DEL PAPEL Y CARTON", "0"),
("XVII", "INDUSTRIA DE LA MADERA", "0"),
("XVIII", "INDUSTRIA DEL CUERO Y PIELES", "0"),
("XX", "INDUSTRIA TEXTIL Y DE LA CONFECCION", "0"),
("XXa", "INDUSTRIA TEXTIL Y DE LA CONFECCION", "0"),
("XXb", "INDUSTRIA TEXTIL Y DE LA CONFECCION", "0"),
("XXc", "INDUSTRIA TEXTIL Y DE LA CONFECCION", "0"),
("XXd", "INDUSTRIA TEXTIL Y DE LA CONFECCION", "0"),
("XXe", "INDUSTRIA TEXTIL Y DE LA CONFECCION", "0"),
("XXI", "INDUSTRIA DE CHOCOLATES, DULCES Y SIMILARES", "0"),
("XXII", "INDUSTRIA DEL CAFE", "0"),
]

View File

@@ -1,40 +0,0 @@
import pytest
from api.v1.modules.public.reference_data.sectors.routes import router
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
app.include_router(router)
client = TestClient(app)
@pytest.mark.usefixtures("client", "access_token")
def test_list_sectors(client, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = client.get("/sectors/", headers=headers)
assert response.status_code == 200
assert "items" in response.json()
assert "page" in response.json()
assert "page_size" in response.json()
@pytest.mark.usefixtures("client", "access_token")
def test_get_sector_not_found(client, access_token):
headers = {"Authorization": f"Bearer {access_token}"}
response = client.get("/sectors/invalid_key", headers=headers)
assert response.status_code == 404
def test_create_sector_forbidden():
response = client.post("/sectors/", json={"key": "TST", "description": "Test"})
assert response.status_code in (403, 405, 404)
def test_update_sector_forbidden():
response = client.put("/sectors/TST", json={"key": "TST", "description": "Test"})
assert response.status_code in (403, 405, 404)
def test_delete_sector_forbidden():
response = client.delete("/sectors/TST")
assert response.status_code in (403, 405, 404)