Private
Public Access
feat: logs para cuando le volves a dar el alta a un repuesto
This commit is contained in:
1 parent
c906dd4511
commit
6d123ea01d
12 files changed
+213
-5
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
.gitea
|
||||||
|
repuestera_back
|
||||||
|
.env
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package logs
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package repuestos
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
r "repuestera_back/repositorios"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AltaRepuestos(c *gin.Context) {
|
||||||
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if id < 1 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uid := c.MustGet("userID").(uint)
|
||||||
|
if _, err := r.ObtenerRepositorios().Repuesto.AltaRepuesto(uint(id), uint(uid)); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Status(http.StatusOK)
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ type GrupoSimpleBuilder struct {
|
|||||||
grupo *models_seguridad.Grupo
|
grupo *models_seguridad.Grupo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGrupoSimpleBuilder(grupo *models_seguridad.Grupo) IBuilder[dto.GrupoSimpleDTO] {
|
func NewGrupoSimpleBuilder(grupo *models_seguridad.Grupo) *GrupoSimpleBuilder {
|
||||||
return &GrupoSimpleBuilder{
|
return &GrupoSimpleBuilder{
|
||||||
grupo: grupo,
|
grupo: grupo,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package dto_builders
|
package dto_builders
|
||||||
|
|
||||||
type IBuilder[T any] interface {
|
type IBuilder[T any] interface {
|
||||||
Build() T
|
Build() (*T, error)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package dto_builders
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"repuestera_back/models/logs"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogBuilder struct {
|
||||||
|
log logs.Log
|
||||||
|
error error
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ IBuilder[logs.Log] = (*LogBuilder)(nil)
|
||||||
|
|
||||||
|
func (b *LogBuilder) Uid(uid uint) {
|
||||||
|
if uid < 1 {
|
||||||
|
b.error = errors.Join(b.error, errors.New("La id no es válida"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b.log.UsuarioID = uid
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *LogBuilder) Accion(acc string) {
|
||||||
|
|
||||||
|
b.checkEspacios(&acc, "La accion no puede ser nula o vacia")
|
||||||
|
if b.error != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.log.Accion = acc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *LogBuilder) AñadirDetalle(detalle logs.LogDetalle) {
|
||||||
|
b.checkEspacios(&detalle.ValorNuevo, "ValorNuevo no puede ser nulo o vacío")
|
||||||
|
b.checkEspacios(&detalle.ValorAnterior, "ValorAnterior no puede ser nulo o vacío")
|
||||||
|
b.checkEspacios(&detalle.Columna, "Columna no puede ser nula o vacía")
|
||||||
|
b.checkEspacios(&detalle.NombreTabla, "el nombre de tabla no puede ser nulo o vacío")
|
||||||
|
|
||||||
|
if b.error != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
detalle.ID = uint(len(b.log.Detalles))
|
||||||
|
b.log.Detalles = append(b.log.Detalles, detalle)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *LogBuilder) checkEspacios(a *string, err string) {
|
||||||
|
*a = strings.TrimSpace(*a)
|
||||||
|
if len([]byte(*a)) == 0 {
|
||||||
|
b.error = errors.Join(b.error, errors.New(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *LogBuilder) Build() (*logs.Log, error) {
|
||||||
|
if b.error != nil {
|
||||||
|
return nil, b.error
|
||||||
|
}
|
||||||
|
return &b.log, nil
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Repuesto struct {
|
type Repuesto struct {
|
||||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||||
Nombre string `gorm:"type:varchar(50);not null" json:"nombre" binding:"required"`
|
Nombre string `gorm:"type:varchar(50);not null" json:"nombre" binding:"required"`
|
||||||
@@ -19,7 +18,7 @@ type Repuesto struct {
|
|||||||
Stocks []Stock `gorm:"foreignKey:RepuestoID" json:"stocks,omitempty"`
|
Stocks []Stock `gorm:"foreignKey:RepuestoID" json:"stocks,omitempty"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Repuesto) TableName() string {
|
func (Repuesto) TableName() string {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func (r *RepositorioBase[T]) Guardar(entidad *T) error {
|
|||||||
|
|
||||||
func (r *RepositorioBase[T]) ObtenerTodos() ([]T, error) {
|
func (r *RepositorioBase[T]) ObtenerTodos() ([]T, error) {
|
||||||
var entidades []T
|
var entidades []T
|
||||||
if err := r.db.Find(&entidades).Error; err != nil {
|
if err := r.db.Unscoped().Find(&entidades).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return entidades, nil
|
return entidades, nil
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package repositorios
|
package repositorios
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
dto_builders "repuestera_back/dto/Builders"
|
||||||
"repuestera_back/models"
|
"repuestera_back/models"
|
||||||
|
"repuestera_back/models/logs"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RepositorioRepuesto interface {
|
type RepositorioRepuesto interface {
|
||||||
IRepositorio[*models.Repuesto]
|
IRepositorio[*models.Repuesto]
|
||||||
|
AltaRepuesto(id uint, uid uint) (*models.Repuesto, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type repositorioRepuesto struct {
|
type repositorioRepuesto struct {
|
||||||
@@ -22,3 +25,42 @@ func NuevoRepositorioRepuesto(db *gorm.DB) RepositorioRepuesto {
|
|||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r repositorioRepuesto) AltaRepuesto(id uint, uid uint) (*models.Repuesto, error) {
|
||||||
|
rep := models.Repuesto{
|
||||||
|
ID: uint64(id),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := r.dbConAuditoria(uid).Unscoped().Model(&rep).Updates(map[string]any{"deleted_at": nil}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
builder := dto_builders.LogBuilder{}
|
||||||
|
builder.Uid(uid)
|
||||||
|
builder.Accion("Alta")
|
||||||
|
builder.AñadirDetalle(logs.LogDetalle{
|
||||||
|
NombreTabla: "repuestos",
|
||||||
|
Columna: "deleted_at",
|
||||||
|
ValorNuevo: "Borrado deleted_at",
|
||||||
|
ValorAnterior: ".",
|
||||||
|
})
|
||||||
|
|
||||||
|
log, err := builder.Build()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ObtenerRepositorios().Logs.Crear(log, uid); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &rep, nil
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,5 +17,7 @@ func RepuestoRoute(r *gin.Engine) {
|
|||||||
repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
||||||
repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
||||||
|
|
||||||
|
repuestoGrupo.PATCH("/:id/alta", repuestos.AltaRepuestos)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { API_URL } from '$lib/env';
|
||||||
|
import { getToken } from '../token';
|
||||||
|
|
||||||
|
export async function altaRepuesto(id: number) {
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/repuestos/${id}/alta`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (req.ok) {
|
||||||
|
return 'Ok';
|
||||||
|
}
|
||||||
|
const data = await req.json();
|
||||||
|
return data.error;
|
||||||
|
} catch {
|
||||||
|
return 'no se puso alcanzar el servidor';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
import { altaRepuesto } from '$lib/hooks/repuestos/altaRepuesto';
|
||||||
|
import { Modal } from 'carbon-components-svelte';
|
||||||
|
|
||||||
|
let {
|
||||||
|
show = $bindable(),
|
||||||
|
onclose,
|
||||||
|
repuesto
|
||||||
|
}: { show?: boolean; onclose: () => void; repuesto: { id: number } } = $props();
|
||||||
|
|
||||||
|
let error = $state('');
|
||||||
|
|
||||||
|
async function handleSubmit(e: Event) {
|
||||||
|
e.preventDefault();
|
||||||
|
error = '';
|
||||||
|
if (!repuesto.id) {
|
||||||
|
error = 'No se pudo obtener la id del Repuesto? recarga la pagina.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await altaRepuesto(repuesto.id);
|
||||||
|
if (result !== 'Ok') {
|
||||||
|
error = result;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
error = '';
|
||||||
|
invalidate('repuestos:load');
|
||||||
|
onclose();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<form id="altaRepuestoForm" onsubmit={handleSubmit}>
|
||||||
|
<Modal
|
||||||
|
formId="altaRepuestoForm"
|
||||||
|
bind:open={show}
|
||||||
|
modalHeading="Alta Repuesto"
|
||||||
|
primaryButtonText="Alta"
|
||||||
|
secondaryButtonText="Cerrar"
|
||||||
|
on:click:button--secondary={handleClose}
|
||||||
|
>
|
||||||
|
{#if error}
|
||||||
|
<div>{error}</div>
|
||||||
|
{/if}
|
||||||
|
Esta seguro que quiere volver a dar de alta a este Repuesto?
|
||||||
|
</Modal>
|
||||||
|
</form>
|
||||||
Reference in new issue
Block a user