Private
Public Access
162 lines
4.0 KiB
Go
162 lines
4.0 KiB
Go
package dto_builders
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"repuestera_back/models/logs"
|
|
)
|
|
|
|
type LogDirector struct{}
|
|
|
|
func NewLogDirector() *LogDirector {
|
|
return &LogDirector{}
|
|
}
|
|
|
|
// Custom construye un log para una acción y detalles arbitrarios.
|
|
func (d *LogDirector) Custom(uid uint, accion string, detalles []logs.LogDetalle) (*logs.Log, error) {
|
|
builder := NewLogBuilder()
|
|
builder.Uid(uid)
|
|
builder.Accion(accion)
|
|
builder.AñadirDetalles(detalles)
|
|
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{
|
|
{
|
|
NombreTabla: tabla,
|
|
Columna: "*",
|
|
ValorNuevo: fmt.Sprintf("Registro creado (ID: %v)", id),
|
|
},
|
|
})
|
|
}
|
|
|
|
// Modificar construye el log comparando el estado anterior y el nuevo.
|
|
// Si no hay diferencias, retorna (nil, nil).
|
|
func (d *LogDirector) Modificar(uid uint, tabla string, anterior, nuevo any) (*logs.Log, error) {
|
|
detalles := generarDetalles(tabla, anterior, nuevo)
|
|
if len(detalles) == 0 {
|
|
return nil, nil
|
|
}
|
|
return d.Custom(uid, logs.AccionModificar, detalles)
|
|
}
|
|
|
|
// Eliminar construye el log de un borrado físico.
|
|
func (d *LogDirector) Eliminar(uid uint, tabla string, id any) (*logs.Log, error) {
|
|
return d.Custom(uid, logs.AccionEliminar, []logs.LogDetalle{
|
|
{
|
|
NombreTabla: tabla,
|
|
Columna: "*",
|
|
ValorAnterior: fmt.Sprintf("Registro eliminado (ID: %v)", id),
|
|
},
|
|
})
|
|
}
|
|
|
|
// Baja construye el log de un borrado lógico.
|
|
func (d *LogDirector) Baja(uid uint, tabla string, id any) (*logs.Log, error) {
|
|
return d.Custom(uid, logs.AccionBaja, []logs.LogDetalle{
|
|
{
|
|
NombreTabla: tabla,
|
|
Columna: "*",
|
|
ValorAnterior: fmt.Sprintf("Registro activo (ID: %v)", id),
|
|
ValorNuevo: "Registro dado de baja",
|
|
},
|
|
})
|
|
}
|
|
|
|
// Alta construye el log de la restauración de un registro dado de baja.
|
|
func (d *LogDirector) Alta(uid uint, tabla string, id any) (*logs.Log, error) {
|
|
return d.Custom(uid, logs.AccionAlta, []logs.LogDetalle{
|
|
{
|
|
NombreTabla: tabla,
|
|
Columna: "*",
|
|
ValorAnterior: fmt.Sprintf("Registro dado de baja (ID: %v)", id),
|
|
ValorNuevo: "Registro dado de alta",
|
|
},
|
|
})
|
|
}
|
|
|
|
func generarDetalles(tabla string, anterior, nuevo any) []logs.LogDetalle {
|
|
var detalles []logs.LogDetalle
|
|
|
|
valAnterior := reflect.ValueOf(anterior)
|
|
valNuevo := reflect.ValueOf(nuevo)
|
|
|
|
for valAnterior.IsValid() && valAnterior.Kind() == reflect.Ptr {
|
|
if valAnterior.IsNil() {
|
|
return detalles
|
|
}
|
|
valAnterior = valAnterior.Elem()
|
|
}
|
|
for valNuevo.IsValid() && valNuevo.Kind() == reflect.Ptr {
|
|
if valNuevo.IsNil() {
|
|
return detalles
|
|
}
|
|
valNuevo = valNuevo.Elem()
|
|
}
|
|
|
|
if !valAnterior.IsValid() || !valNuevo.IsValid() {
|
|
return detalles
|
|
}
|
|
if valAnterior.Kind() != reflect.Struct || valNuevo.Kind() != reflect.Struct {
|
|
return detalles
|
|
}
|
|
if valAnterior.Type() != valNuevo.Type() {
|
|
return detalles
|
|
}
|
|
|
|
tipo := valAnterior.Type()
|
|
|
|
for i := 0; i < tipo.NumField(); i++ {
|
|
campo := tipo.Field(i)
|
|
|
|
if campo.PkgPath != "" { // campo no exportado
|
|
continue
|
|
}
|
|
if campo.Name == "Model" || campo.Name == "CreatedAt" || campo.Name == "UpdatedAt" || campo.Name == "DeletedAt" {
|
|
continue
|
|
}
|
|
|
|
campoNuevo := valNuevo.FieldByName(campo.Name)
|
|
if !campoNuevo.IsValid() {
|
|
continue
|
|
}
|
|
|
|
vAnt := fmt.Sprintf("%v", valAnterior.Field(i).Interface())
|
|
vNue := fmt.Sprintf("%v", campoNuevo.Interface())
|
|
|
|
if vAnt != vNue {
|
|
detalles = append(detalles, logs.LogDetalle{
|
|
ID: uint(i),
|
|
NombreTabla: tabla,
|
|
Columna: campo.Name,
|
|
ValorAnterior: vAnt,
|
|
ValorNuevo: vNue,
|
|
})
|
|
}
|
|
}
|
|
|
|
return detalles
|
|
}
|
|
|
|
// ObtenerID extrae el campo ID de una entidad, siguiendo punteros.
|
|
func ObtenerID(dest any) any {
|
|
val := reflect.ValueOf(dest)
|
|
for val.IsValid() && val.Kind() == reflect.Ptr {
|
|
if val.IsNil() {
|
|
return nil
|
|
}
|
|
val = val.Elem()
|
|
}
|
|
if !val.IsValid() || val.Kind() != reflect.Struct {
|
|
return nil
|
|
}
|
|
campoID := val.FieldByName("ID")
|
|
if campoID.IsValid() && campoID.CanInterface() {
|
|
return campoID.Interface()
|
|
}
|
|
return nil
|
|
}
|