Private
Public Access
correjida parte de logs por ahora
This commit is contained in:
1 parent
770aa7d6ed
commit
fe1411437b
6 files changed
+86
-24
No files matched your search
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user