diff --git a/back/dto/Builders/LogDirector.go b/back/dto/Builders/LogDirector.go index 030feae..da110c2 100644 --- a/back/dto/Builders/LogDirector.go +++ b/back/dto/Builders/LogDirector.go @@ -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. diff --git a/back/models/seguridad/permisos_ids.go b/back/models/seguridad/permisos_ids.go index e3f5b14..d0d29db 100644 --- a/back/models/seguridad/permisos_ids.go +++ b/back/models/seguridad/permisos_ids.go @@ -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 ) diff --git a/back/repositorios/IRepositorio.go b/back/repositorios/IRepositorio.go index 33ec57a..be98c43 100644 --- a/back/repositorios/IRepositorio.go +++ b/back/repositorios/IRepositorio.go @@ -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 } diff --git a/back/routes/LogRoute.go b/back/routes/LogRoute.go index dc03a7e..a6768a1 100644 --- a/back/routes/LogRoute.go +++ b/back/routes/LogRoute.go @@ -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) diff --git a/back/routes/MarcaRoute.go b/back/routes/MarcaRoute.go index 653a0c4..d45cbc1 100644 --- a/back/routes/MarcaRoute.go +++ b/back/routes/MarcaRoute.go @@ -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) diff --git a/front/src/routes/logs/TablaLogs.svelte b/front/src/routes/logs/TablaLogs.svelte index b22805b..3b7d15b 100644 --- a/front/src/routes/logs/TablaLogs.svelte +++ b/front/src/routes/logs/TablaLogs.svelte @@ -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 @@
No hay logs para mostrar
{/if} -