66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
|
)
|
|
|
|
// ─── Configuración de canales de notificación ─────────────────────────────────
|
|
|
|
func NotifConfigIndex(c *fiber.Ctx) error {
|
|
return c.Render("notif_config", fiber.Map{
|
|
"user": c.Locals("user"),
|
|
"modules": c.Locals("userModules"),
|
|
}, "layouts/main")
|
|
}
|
|
|
|
func GetNotifConfigs(c *fiber.Ctx) error {
|
|
items, err := models.GetAllNotifConfigs()
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(items)
|
|
}
|
|
|
|
func SaveNotifConfig(c *fiber.Ctx) error {
|
|
var cfg models.NotifEventoConfig
|
|
if err := c.BodyParser(&cfg); err != nil {
|
|
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
if cfg.Evento == "" || cfg.Destinatario == "" {
|
|
return c.Status(400).JSON(fiber.Map{"error": "evento y destinatario son requeridos"})
|
|
}
|
|
if err := models.UpsertNotifConfig(&cfg); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"ok": true})
|
|
}
|
|
|
|
// ─── Notificaciones in-app (sistema) ─────────────────────────────────────────
|
|
|
|
// GetMisNotifs devuelve notificaciones del admin autenticado (tipo_usuario=admin, usuario_id=0).
|
|
func GetMisNotifs(c *fiber.Ctx) error {
|
|
soloNoLeidas := c.Query("no_leidas") == "1"
|
|
items, err := models.GetSistemaNotifs("admin", 0, soloNoLeidas)
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
count := models.CountUnreadNotifs("admin", 0)
|
|
return c.JSON(fiber.Map{"items": items, "unread": count})
|
|
}
|
|
|
|
func MarcarNotifLeida(c *fiber.Ctx) error {
|
|
id, _ := c.ParamsInt("id")
|
|
if err := models.MarcarNotifLeida(uint(id)); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"ok": true})
|
|
}
|
|
|
|
func MarcarTodasLeidas(c *fiber.Ctx) error {
|
|
if err := models.MarcarTodasLeidas("admin", 0); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"ok": true})
|
|
}
|