Files
Repuestera/back/controllers/stock/ObtenerReservas.go
T
2026-11-23 20:09:25 -03:00

54 lines
1.3 KiB
Go

package stock
import (
applog "log"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
dto "repuestera_back/dto/Stock"
"repuestera_back/models"
r "repuestera_back/repositorios"
)
func ObtenerReservas(c *gin.Context) {
var q dto.FiltrosReservaQuery
if err := c.ShouldBindQuery(&q); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if !estadoReservaValido(q.Estado) {
c.JSON(http.StatusBadRequest, gin.H{"error": "estado de reserva inválido"})
return
}
repo := r.ObtenerRepositorios(c)
// D9: las reservas vencidas se procesan de forma lazy al consultar.
if err := marcarReservasVencidas(r.BaseDeDatos(), repo, c.GetUint("userID"), time.Now()); err != nil {
applog.Printf("stock: no se pudieron procesar las reservas vencidas: %v", err)
}
reservas, err := repo.Reserva.ListarActivas(q)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, reservas)
}
func estadoReservaValido(estado string) bool {
switch strings.ToLower(strings.TrimSpace(estado)) {
case "", "todas",
string(models.ReservaActiva),
string(models.ReservaVencida),
string(models.ReservaLiberada),
string(models.ReservaConsumida):
return true
default:
return false
}
}