This commit is contained in:
Lizandro Guarnizo
2026-05-16 15:17:49 -05:00
parent 9820405475
commit f5eef70e09
4 changed files with 184 additions and 8 deletions
+68 -5
View File
@@ -40,11 +40,37 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
log.Printf("[Notif] PortalUser %d no encontrado: %v", ticket.PortalUserID, err)
return
}
if portalUser.Email == "" {
cfg := models.GetNotifConfig("ticket_respuesta_admin", "portal_user")
if cfg == nil {
return
}
// TODO: enviar email al portal user cuando mail_service implemente SendTicketRespuestaPortalUser
_ = fmt.Sprintf("/portal/dashboard")
portalURL := "/portal/dashboard"
titulo := fmt.Sprintf("Respuesta en tu ticket: %s", ticket.Titulo)
cuerpo := fmt.Sprintf("El equipo respondió: %s", contenido)
if cfg.CanalSistema {
_ = models.CreateSistemaNotif(&models.SistemaNotificacion{
TipoUsuario: "portal_user",
UsuarioID: portalUser.ID,
Titulo: titulo,
Cuerpo: cuerpo,
Url: portalURL,
Icono: "💬",
})
}
if cfg.CanalEmail && portalUser.Email != "" {
go SendTicketRespuestaPortalUser(portalUser.Email, portalUser.Nombre, proyectoNombre, ticket.Titulo, contenido, portalURL)
}
if cfg.CanalTelegram && portalUser.TelegramChatID != "" {
ts := NewTelegramService()
msg := fmt.Sprintf("💬 <b>El equipo respondió tu ticket</b>\nProyecto: <b>%s</b>\nTicket: <b>%s</b>\n\n%s",
proyectoNombre, ticket.Titulo, contenido)
if err := ts.SendMessageWithToken(portalUser.TelegramChatID, msg, getAdminBotToken()); err != nil {
log.Printf("[Notif] Error telegram portal_user %d: %v", portalUser.ID, err)
}
}
}
// ─── DispatchFacturaSubida ────────────────────────────────────────────────────
@@ -53,12 +79,49 @@ func DispatchFacturaSubida(factura *models.Factura) {
if factura == nil || factura.ClienteID == 0 {
return
}
cfg := models.GetNotifConfig("factura_subida", "portal_user")
if cfg == nil {
return
}
portalUsers, err := models.GetPortalUsersByClienteID(factura.ClienteID)
if err != nil || len(portalUsers) == 0 {
return
}
log.Printf("[Notif] DispatchFacturaSubida factura=%d portal_users=%d (pendiente de implementar)",
factura.ID, len(portalUsers))
clienteNombre := factura.Cliente.Empresa
if clienteNombre == "" {
clienteNombre = factura.Cliente.Nombre
}
for _, u := range portalUsers {
u := u
portalURL := "/portal/dashboard"
titulo := fmt.Sprintf("Nueva factura disponible: %s", factura.Numero)
cuerpo := fmt.Sprintf("Factura %s por %s %.2f", factura.Numero, factura.Moneda, factura.Monto)
if cfg.CanalSistema {
_ = models.CreateSistemaNotif(&models.SistemaNotificacion{
TipoUsuario: "portal_user",
UsuarioID: u.ID,
Titulo: titulo,
Cuerpo: cuerpo,
Url: portalURL,
Icono: "🧾",
})
}
if cfg.CanalEmail && u.Email != "" {
go SendFacturaSubidaPortalUser(u.Email, u.Nombre, clienteNombre, factura.Numero, factura.Monto, factura.Moneda, portalURL)
}
if cfg.CanalTelegram && u.TelegramChatID != "" {
ts := NewTelegramService()
msg := fmt.Sprintf("🧾 <b>Nueva factura disponible</b>\nNúmero: <b>%s</b>\nMonto: %s %.2f",
factura.Numero, factura.Moneda, factura.Monto)
if err := ts.SendMessageWithToken(u.TelegramChatID, msg, getAdminBotToken()); err != nil {
log.Printf("[Notif] Error telegram portal_user %d: %v", u.ID, err)
}
}
}
log.Printf("[Notif] DispatchFacturaSubida factura=%d clientes notificados=%d", factura.ID, len(portalUsers))
}
// ─── helpers ─────────────────────────────────────────────────────────────────
+72 -3
View File
@@ -20,15 +20,84 @@
<body class="min-h-screen flex flex-col">
<!-- Navbar -->
<header class="bg-white border-b border-slate-200 sticky top-0 z-30">
<header class="bg-white border-b border-slate-200 sticky top-0 z-30"
x-data="{
bellOpen: false,
notifs: [],
unread: 0,
async loadNotifs() {
try {
const r = await axios.get('/portal/mis-notifs?no_leidas=0');
this.notifs = r.data.items || [];
this.unread = r.data.unread || 0;
} catch {}
},
async markRead(id) {
await axios.put('/portal/mis-notifs/' + id + '/leida');
const n = this.notifs.find(x => x.ID === id);
if (n) { n.leida = true; this.unread = Math.max(0, this.unread - 1); }
},
async markAll() {
await axios.post('/portal/mis-notifs/marcar-todas');
this.notifs.forEach(n => n.leida = true);
this.unread = 0;
}
}"
x-init="loadNotifs(); setInterval(() => loadNotifs(), 30000)">
<div class="max-w-6xl mx-auto px-4 sm:px-6 h-16 flex items-center justify-between">
<!-- Logo -->
<a href="/portal/dashboard" class="flex items-center gap-2 font-bold text-slate-800 text-lg">
<span class="w-7 h-7 rounded-full flex items-center justify-center text-white text-xs font-bold" style="background:#8eb02f">U</span>
<span>Portal de Clientes</span>
</a>
<!-- User -->
<div class="flex items-center gap-4">
<!-- Right: bell + user -->
<div class="flex items-center gap-3">
<!-- 🔔 Bell -->
<div class="relative">
<button @click="bellOpen = !bellOpen; if(bellOpen) loadNotifs()"
class="relative p-2 rounded-xl hover:bg-slate-100 transition-colors focus:outline-none">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-slate-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0"/>
</svg>
<span x-show="unread > 0"
class="absolute -top-0.5 -right-0.5 h-4 w-4 flex items-center justify-center rounded-full text-white text-[10px] font-bold"
style="background:#8eb02f"
x-text="unread > 9 ? '9+' : unread">
</span>
</button>
<!-- Dropdown -->
<div x-show="bellOpen" @click.outside="bellOpen = false"
x-transition:enter="transition ease-out duration-150"
x-transition:enter-start="opacity-0 scale-95 -translate-y-1"
x-transition:enter-end="opacity-100 scale-100 translate-y-0"
x-transition:leave="transition ease-in duration-100"
x-transition:leave-start="opacity-100 scale-100 translate-y-0"
x-transition:leave-end="opacity-0 scale-95 -translate-y-1"
class="absolute right-0 top-12 w-72 bg-white rounded-xl shadow-lg border border-slate-200 overflow-hidden z-50">
<div class="px-4 py-3 border-b border-slate-100 flex items-center justify-between">
<span class="text-sm font-semibold text-slate-800">Notificaciones</span>
<button x-show="unread > 0" @click="markAll()" class="text-xs text-slate-400 hover:text-slate-600">Marcar todas leídas</button>
</div>
<div class="max-h-64 overflow-y-auto divide-y divide-slate-100">
<template x-if="notifs.length === 0">
<div class="px-4 py-6 text-center text-slate-400 text-sm">Sin notificaciones</div>
</template>
<template x-for="n in notifs" :key="n.ID">
<a :href="n.url || '#'" @click="if(!n.leida) markRead(n.ID)"
class="flex items-start gap-3 px-4 py-3 hover:bg-slate-50 transition-colors"
:class="!n.leida ? 'bg-emerald-50/50' : ''">
<span class="text-lg leading-none mt-0.5" x-text="n.icono || '🔔'"></span>
<div class="flex-1 min-w-0">
<p class="text-sm font-medium text-slate-800 truncate" x-text="n.titulo"></p>
<p class="text-xs text-slate-500 truncate mt-0.5" x-text="n.cuerpo"></p>
</div>
<div x-show="!n.leida" class="w-2 h-2 rounded-full flex-shrink-0 mt-1.5" style="background:#8eb02f"></div>
</a>
</template>
</div>
</div>
</div>
<!-- User name -->
<span class="text-sm text-slate-600 hidden sm:block">
{{ if .portalUser }}{{ .portalUser.Nombre }}{{ end }}
</span>
+39
View File
@@ -324,3 +324,42 @@ func PortalDownloadFactura(c *fiber.Ctx) error {
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, f.OriginalName))
return c.SendFile(clean)
}
// ─── Notificaciones del portal user ──────────────────────────────────────────
func PortalGetMisNotifs(c *fiber.Ctx) error {
u := middlewares.PortalUserFromLocals(c)
if u == nil {
return c.Status(401).JSON(fiber.Map{"error": "no autenticado"})
}
soloNoLeidas := c.Query("no_leidas") == "1"
items, err := models.GetSistemaNotifs("portal_user", u.ID, soloNoLeidas)
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
count := models.CountUnreadNotifs("portal_user", u.ID)
return c.JSON(fiber.Map{"items": items, "unread": count})
}
func PortalMarcarNotifLeida(c *fiber.Ctx) error {
u := middlewares.PortalUserFromLocals(c)
if u == nil {
return c.Status(401).JSON(fiber.Map{"error": "no autenticado"})
}
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 PortalMarcarTodasLeidas(c *fiber.Ctx) error {
u := middlewares.PortalUserFromLocals(c)
if u == nil {
return c.Status(401).JSON(fiber.Map{"error": "no autenticado"})
}
if err := models.MarcarTodasLeidas("portal_user", u.ID); err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(fiber.Map{"ok": true})
}
+5
View File
@@ -29,4 +29,9 @@ func PortalRoutes(app fiber.Router) {
// Descargas
portal.Get("/entregables/:id/download", controllers.PortalDownloadEntregable)
portal.Get("/facturas/:id/download", controllers.PortalDownloadFactura)
// Notificaciones in-app del portal user
portal.Get("/mis-notifs", controllers.PortalGetMisNotifs)
portal.Put("/mis-notifs/:id/leida", controllers.PortalMarcarNotifLeida)
portal.Post("/mis-notifs/marcar-todas", controllers.PortalMarcarTodasLeidas)
}