Private
Public Access
correjida parte de logs por ahora
This commit is contained in:
1 parent
770aa7d6ed
commit
fe1411437b
6 files changed
+81
-19
No files matched your search
@@ -3,6 +3,7 @@ package dto_builders
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"repuestera_back/models/logs"
|
"repuestera_back/models/logs"
|
||||||
)
|
)
|
||||||
@@ -22,15 +23,76 @@ func (d *LogDirector) Custom(uid uint, accion string, detalles []logs.LogDetalle
|
|||||||
return builder.Build()
|
return builder.Build()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crear construye el log de un registro creado.
|
// Crear construye el log de un registro creado, guardando los campos del
|
||||||
func (d *LogDirector) Crear(uid uint, tabla string, id any) (*logs.Log, error) {
|
// registro en la lista de detalles (ValorNuevo).
|
||||||
return d.Custom(uid, logs.AccionCrear, []logs.LogDetalle{
|
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,
|
NombreTabla: tabla,
|
||||||
Columna: "*",
|
Columna: "*",
|
||||||
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", id),
|
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: "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.
|
// Modificar construye el log comparando el estado anterior y el nuevo.
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ const (
|
|||||||
PermisoGestionGrupos uint = 2
|
PermisoGestionGrupos uint = 2
|
||||||
PermisoGestionUsuarios uint = 3
|
PermisoGestionUsuarios uint = 3
|
||||||
PermisoGestionRepuestos uint = 4
|
PermisoGestionRepuestos uint = 4
|
||||||
PermisoGestionLogs uint = 5
|
PermisoGestionLogs uint = 6
|
||||||
PermisoGestionMarcas uint = 6
|
PermisoGestionMarcas uint = 5
|
||||||
PermisoGestionEmpresa uint = 7
|
PermisoGestionEmpresa uint = 7
|
||||||
PermisoPlataforma uint = 8
|
PermisoPlataforma uint = 8
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (r *RepositorioBase[T]) Crear(entidad T, usuarioID uint) error {
|
|||||||
return err
|
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")
|
r.registrarLog(log, err, usuarioID, "creación")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
func LogsRoute(r *gin.Engine) {
|
func LogsRoute(r *gin.Engine) {
|
||||||
repuestoGrupo := r.Group("/logs")
|
repuestoGrupo := r.Group("/logs")
|
||||||
repuestoGrupo.Use(middleware.AuthMiddleware(5))
|
repuestoGrupo.Use(middleware.AuthMiddleware(6))
|
||||||
{
|
{
|
||||||
repuestoGrupo.GET("/:p/", logs.ObtenerLogs)
|
repuestoGrupo.GET("/:p/", logs.ObtenerLogs)
|
||||||
repuestoGrupo.POST("/:p/busqueda", logs.BusquedaLogs)
|
repuestoGrupo.POST("/:p/busqueda", logs.BusquedaLogs)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
func MarcaRoute(r *gin.Engine) {
|
func MarcaRoute(r *gin.Engine) {
|
||||||
marcaGrupo := r.Group("/marcas")
|
marcaGrupo := r.Group("/marcas")
|
||||||
marcaGrupo.Use(middleware.AuthMiddleware(6))
|
marcaGrupo.Use(middleware.AuthMiddleware(5))
|
||||||
{
|
{
|
||||||
marcaGrupo.GET("/", marca.ObtenerMarcas)
|
marcaGrupo.GET("/", marca.ObtenerMarcas)
|
||||||
// marcaGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
// marcaGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
import type { LogDetalle, LogEntry } from '../../types';
|
import type { LogDetalle, LogEntry } from '../../types';
|
||||||
|
|
||||||
const headers = [
|
const headers = [
|
||||||
{ key: 'id', value: '#' },
|
{ key: 'id', value: '#', empty: false },
|
||||||
{ key: 'created_at', value: 'Fecha' },
|
{ key: 'created_at', value: 'Fecha', empty: false },
|
||||||
{ key: 'usuario', value: 'Usuario' },
|
{ key: 'usuario', value: 'Usuario', empty: false },
|
||||||
{ key: 'accion', value: 'Acción' }
|
{ key: 'accion', value: 'Acción', empty: false }
|
||||||
];
|
];
|
||||||
|
|
||||||
let { logs }: { logs?: LogEntry[] } = $props();
|
let { logs }: { logs?: LogEntry[] } = $props();
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
<p>No hay logs para mostrar</p>
|
<p>No hay logs para mostrar</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<DataTable {headers} rows={logs ?? []} expandable>
|
<DataTable headers={headers as any} rows={logs ?? []} expandable>
|
||||||
<svelte:fragment slot="cell" let:row let:cell>
|
<svelte:fragment slot="cell" let:row let:cell>
|
||||||
{#if cell.key === 'created_at'}
|
{#if cell.key === 'created_at'}
|
||||||
{formatFecha(row.created_at)}
|
{formatFecha(row.created_at)}
|
||||||
@@ -43,15 +43,15 @@
|
|||||||
{#if row.detalles && row.detalles.length > 0}
|
{#if row.detalles && row.detalles.length > 0}
|
||||||
{@const detallesConId = row.detalles.map((d: LogDetalle, i: number) => ({
|
{@const detallesConId = row.detalles.map((d: LogDetalle, i: number) => ({
|
||||||
...d,
|
...d,
|
||||||
id: d.id || i + 1
|
id: i + 1
|
||||||
}))}
|
}))}
|
||||||
<DataTable
|
<DataTable
|
||||||
headers={[
|
headers={[
|
||||||
{ key: 'nombreTabla', value: 'Tabla' },
|
{ key: 'nombreTabla', value: 'Tabla', empty: false },
|
||||||
{ key: 'columna', value: 'Columna' },
|
{ key: 'columna', value: 'Columna', empty: false },
|
||||||
{ key: 'valorAnterior', value: 'Valor anterior' },
|
{ key: 'valorAnterior', value: 'Valor anterior', empty: false },
|
||||||
{ key: 'valorNuevo', value: 'Valor nuevo' }
|
{ key: 'valorNuevo', value: 'Valor nuevo', empty: false }
|
||||||
]}
|
] as any}
|
||||||
rows={detallesConId}
|
rows={detallesConId}
|
||||||
size="compact"
|
size="compact"
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in new issue
Block a user