Saltar a contenido

Menú — core.identidad.cfg_menu

Menú jerárquico por empresa y módulo: hasta tres niveles de navegación.

from datetime import datetime, date
from typing import Optional
import uuid

from sqlalchemy import (
    BigInteger,
    Boolean,
    CheckConstraint,
    Date,
    DateTime,
    ForeignKey,
    Identity,
    Index,
    Integer,
    SmallInteger,
    String,
    Text,
    UniqueConstraint,
    text,
)
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


class Base(DeclarativeBase):
    pass

class CfgMenu(Base):
    __tablename__ = 'cfg_menu'
    __table_args__ = (
        UniqueConstraint('modulo_id', 'codigo', name='uq_cfg_menu_modulo_codigo'),
        CheckConstraint('nivel IN (1, 2, 3)', name='ck_cfg_menu_nivel'),
        Index('ix_cfg_menu_empresa_id', 'empresa_id'),
        Index('ix_cfg_menu_modulo_id', 'modulo_id'),
        Index('ix_cfg_menu_padre_id', 'padre_id'),
        Index('ix_cfg_menu_nivel', 'nivel'),
        {"schema": 'core.identidad'},
    )

    id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        primary_key=True,
        server_default=text("gen_random_uuid()"),
    )
    empresa_id: Mapped[int] = mapped_column(
        BigInteger,
        nullable=False,
        comment='Empresa configurada',
    )
    modulo_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        nullable=False,
        ForeignKey("cfg_modulos.id"),
        comment='Módulo propietario del menú',
    )
    padre_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("cfg_menu.id"),
        comment='Menú padre (autoreferencia)',
    )
    nivel: Mapped[int] = mapped_column(
        SmallInteger,
        nullable=False,
        comment='Nivel jerárquico (1..3)',
    )
    codigo: Mapped[str] = mapped_column(
        String(50),
        nullable=False,
        comment='Código del menú',
    )
    nombre: Mapped[str] = mapped_column(
        String(150),
        nullable=False,
        comment='Nombre del menú',
    )
    ruta_frontend: Mapped[str | None] = mapped_column(
        String(200),
        comment='Ruta de la aplicación frontend',
    )
    icono: Mapped[str | None] = mapped_column(
        String(50),
        comment='Clase o ruta del ícono',
    )
    orden: Mapped[int] = mapped_column(
        Integer,
        nullable=False,
        server_default=text("0"),
        comment='Orden de presentación',
    )
    is_visible: Mapped[bool | None] = mapped_column(
        Boolean,
        server_default=text("true"),
        comment='Visible en la navegación',
    )

    # Auditoría General
    is_activo: Mapped[bool] = mapped_column(
        Boolean,
        nullable=False,
        server_default=text("true"),
        comment='Estado lógico del registro',
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        server_default=text("now()"),
        comment='Fecha de creación',
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        server_default=text("now()"),
        comment='Fecha de última actualización',
    )
    deleted_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True),
        comment='Fecha de eliminación lógica',
    )
    created_by: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        comment='Usuario creador',
    )
    updated_by: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True),
        comment='Usuario modificador',
    )