correjida parte de logs por ahora

This commit is contained in:
fede committed 2026-11-10 17:24:11 -03:00
1 parent 770aa7d6ed
commit fe1411437b
6 files changed
+86 -24

No files matched your search

+70 -8
View File
@@ -3,6 +3,7 @@ package dto_builders
import (
"fmt"
"reflect"
"strings"
"repuestera_back/models/logs"
)
@@ -22,15 +23,76 @@ func (d *LogDirector) Custom(uid uint, accion string, detalles []logs.LogDetalle
return builder.Build()
}
// Crear construye el log de un registro creado.
func (d *LogDirector) Crear(uid uint, tabla string, id any) (*logs.Log, error) {
return d.Custom(uid, logs.AccionCrear, []logs.LogDetalle{
{
// Crear construye el log de un registro creado, guardando los campos del
// registro en la lista de detalles (ValorNuevo).
func (d *LogDirector) Crear(uid uint, tabla string, entidad any) (*logs.Log, error) {
detalles := generarDetallesCreacion(tabla, entidad)
if len(detalles) == 0 {
id := ObtenerID(entidad)
detalles = []logs.LogDetalle{
{
NombreTabla: tabla,
Columna: "*",
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", id),
},
}
}
return d.Custom(uid, logs.AccionCrear, detalles)
}
// generarDetallesCreacion recorre los campos exportados de la entidad y los
// persiste como detalles con ValorNuevo (no hay estado anterior).
func generarDetallesCreacion(tabla string, entidad any) []logs.LogDetalle {
var detalles []logs.LogDetalle
val := reflect.ValueOf(entidad)
for val.IsValid() && val.Kind() == reflect.Ptr {
if val.IsNil() {
return detalles
}
val = val.Elem()
}
if !val.IsValid() || val.Kind() != reflect.Struct {
return detalles
}
tipo := val.Type()
// Incluye el ID aunque esté dentro del gorm.Model embebido.
if id := ObtenerID(entidad); id != nil {
detalles = append(detalles, logs.LogDetalle{
NombreTabla: tabla,
Columna: "*",
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", id),
},
})
Columna: "ID",
ValorNuevo: fmt.Sprintf("%v", id),
})
}
for i := 0; i < tipo.NumField(); i++ {
campo := tipo.Field(i)
if campo.PkgPath != "" {
continue
}
if campo.Name == "Model" || campo.Name == "CreatedAt" || campo.Name == "UpdatedAt" || campo.Name == "DeletedAt" || campo.Name == "ID" {
continue
}
valor := val.Field(i)
if !valor.CanInterface() {
continue
}
v := fmt.Sprintf("%v", valor.Interface())
if strings.TrimSpace(v) == "" || v == "0" {
continue
}
detalles = append(detalles, logs.LogDetalle{
NombreTabla: tabla,
Columna: campo.Name,
ValorNuevo: v,
})
}
return detalles
}
// Modificar construye el log comparando el estado anterior y el nuevo.
+2 -2
View File
@@ -7,8 +7,8 @@ const (
PermisoGestionGrupos uint = 2
PermisoGestionUsuarios uint = 3
PermisoGestionRepuestos uint = 4
PermisoGestionLogs uint = 5
PermisoGestionMarcas uint = 6
PermisoGestionLogs uint = 6
PermisoGestionMarcas uint = 5
PermisoGestionEmpresa uint = 7
PermisoPlataforma uint = 8
)
+1 -1
View File
@@ -62,7 +62,7 @@ func (r *RepositorioBase[T]) Crear(entidad T, usuarioID uint) error {
return err
}
log, err := dto_builders.NewLogDirector().Crear(usuarioID, r.tabla(), dto_builders.ObtenerID(entidad))
log, err := dto_builders.NewLogDirector().Crear(usuarioID, r.tabla(), entidad)
r.registrarLog(log, err, usuarioID, "creación")
return nil
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func LogsRoute(r *gin.Engine) {
repuestoGrupo := r.Group("/logs")
repuestoGrupo.Use(middleware.AuthMiddleware(5))
repuestoGrupo.Use(middleware.AuthMiddleware(6))
{
repuestoGrupo.GET("/:p/", logs.ObtenerLogs)
repuestoGrupo.POST("/:p/busqueda", logs.BusquedaLogs)
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func MarcaRoute(r *gin.Engine) {
marcaGrupo := r.Group("/marcas")
marcaGrupo.Use(middleware.AuthMiddleware(6))
marcaGrupo.Use(middleware.AuthMiddleware(5))
{
marcaGrupo.GET("/", marca.ObtenerMarcas)
// marcaGrupo.GET("/:id", repuestos.ObtenerRepuesto)
+11 -11
View File
@@ -3,10 +3,10 @@
import type { LogDetalle, LogEntry } from '../../types';
const headers = [
{ key: 'id', value: '#' },
{ key: 'created_at', value: 'Fecha' },
{ key: 'usuario', value: 'Usuario' },
{ key: 'accion', value: 'Acción' }
{ key: 'id', value: '#', empty: false },
{ key: 'created_at', value: 'Fecha', empty: false },
{ key: 'usuario', value: 'Usuario', empty: false },
{ key: 'accion', value: 'Acción', empty: false }
];
let { logs }: { logs?: LogEntry[] } = $props();
@@ -26,7 +26,7 @@
<p>No hay logs para mostrar</p>
{/if}
<DataTable {headers} rows={logs ?? []} expandable>
<DataTable headers={headers as any} rows={logs ?? []} expandable>
<svelte:fragment slot="cell" let:row let:cell>
{#if cell.key === 'created_at'}
{formatFecha(row.created_at)}
@@ -43,15 +43,15 @@
{#if row.detalles && row.detalles.length > 0}
{@const detallesConId = row.detalles.map((d: LogDetalle, i: number) => ({
...d,
id: d.id || i + 1
id: i + 1
}))}
<DataTable
headers={[
{ key: 'nombreTabla', value: 'Tabla' },
{ key: 'columna', value: 'Columna' },
{ key: 'valorAnterior', value: 'Valor anterior' },
{ key: 'valorNuevo', value: 'Valor nuevo' }
]}
{ key: 'nombreTabla', value: 'Tabla', empty: false },
{ key: 'columna', value: 'Columna', empty: false },
{ key: 'valorAnterior', value: 'Valor anterior', empty: false },
{ key: 'valorNuevo', value: 'Valor nuevo', empty: false }
] as any}
rows={detallesConId}
size="compact"
>