103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
|
)
|
|
|
|
// ─── DispatchTicketNuevo ──────────────────────────────────────────────────────
|
|
// Notifica al admin cuando un portal user crea un ticket.
|
|
func DispatchTicketNuevo(ticket *models.ProyectoTicket, portalUser *models.PortalUser, proyectoNombre string) {
|
|
if ticket == nil || portalUser == nil {
|
|
return
|
|
}
|
|
msg := fmt.Sprintf("🎫 <b>Nuevo ticket</b>\nProyecto: <b>%s</b>\nCliente: %s\nTítulo: <b>%s</b>\n\n%s",
|
|
proyectoNombre, ticket.AutorNombre, ticket.Titulo, ticket.Descripcion)
|
|
sendTelegramAdmin(msg)
|
|
}
|
|
|
|
// ─── DispatchTicketRespuestaCliente ──────────────────────────────────────────
|
|
// Notifica al admin cuando el portal user responde un ticket.
|
|
func DispatchTicketRespuestaCliente(ticket *models.ProyectoTicket, contenido string, proyectoNombre string) {
|
|
if ticket == nil {
|
|
return
|
|
}
|
|
msg := fmt.Sprintf("💬 <b>Respuesta de cliente</b>\nProyecto: <b>%s</b>\nCliente: %s\n\n%s",
|
|
proyectoNombre, ticket.AutorNombre, contenido)
|
|
sendTelegramAdmin(msg)
|
|
}
|
|
|
|
// ─── DispatchTicketRespuestaAdmin ─────────────────────────────────────────────
|
|
// Notifica al portal user cuando el admin responde un ticket.
|
|
func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido string, proyectoNombre string) {
|
|
if ticket == nil {
|
|
return
|
|
}
|
|
portalUser, err := models.GetPortalUserByID(ticket.PortalUserID)
|
|
if err != nil || portalUser == nil {
|
|
log.Printf("[Notif] PortalUser %d no encontrado: %v", ticket.PortalUserID, err)
|
|
return
|
|
}
|
|
if portalUser.Email == "" {
|
|
return
|
|
}
|
|
// TODO: enviar email al portal user cuando mail_service implemente SendTicketRespuestaPortalUser
|
|
_ = fmt.Sprintf("/portal/dashboard")
|
|
}
|
|
|
|
// ─── DispatchFacturaSubida ────────────────────────────────────────────────────
|
|
// Notifica a los portal_users del cliente cuando se sube una factura.
|
|
func DispatchFacturaSubida(factura *models.Factura) {
|
|
if factura == nil || factura.ClienteID == 0 {
|
|
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))
|
|
}
|
|
|
|
// ─── helpers ─────────────────────────────────────────────────────────────────
|
|
|
|
func getAdminEmail() string {
|
|
cfg, err := models.GetSmtpConfig()
|
|
if err != nil || cfg == nil {
|
|
return ""
|
|
}
|
|
return cfg.FromAddress
|
|
}
|
|
|
|
func getAdminBotToken() string {
|
|
configs, err := models.GetAllTelegramConfigs()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
for _, c := range configs {
|
|
if c.Activo {
|
|
return c.BotToken
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func sendTelegramAdmin(mensaje string) {
|
|
configs, err := models.GetAllTelegramConfigs()
|
|
if err != nil {
|
|
log.Printf("[Notif] Error obteniendo configs telegram: %v", err)
|
|
return
|
|
}
|
|
for _, cfg := range configs {
|
|
if !cfg.Activo {
|
|
continue
|
|
}
|
|
ts := &TelegramService{BotToken: cfg.BotToken}
|
|
if err := ts.SendMessage(cfg.ChatID, mensaje); err != nil {
|
|
log.Printf("[Notif] Error enviando telegram admin (cfg %d): %v", cfg.ID, err)
|
|
}
|
|
}
|
|
}
|