Private
Public Access
revisados los directores de los builders
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
1a1aeaeba9
commit
2488c9f659
22 files changed
+887
No files matched your search
@@ -0,0 +1,224 @@
|
||||
package directors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
builders "repuestera_back/dto/Builders"
|
||||
"strings"
|
||||
|
||||
"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 := builders.NewLogBuilder()
|
||||
builder.Uid(uid)
|
||||
builder.Accion(accion)
|
||||
builder.AñadirDetalles(detalles)
|
||||
return builder.Build()
|
||||
}
|
||||
|
||||
// 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: "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.
|
||||
// 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
|
||||
}
|
||||
Reference in new issue
Block a user