231 lines
7.8 KiB
Go
231 lines
7.8 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"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
|
|
}
|
|
cfg := models.GetNotifConfig("ticket_nuevo", "admin")
|
|
if cfg == nil {
|
|
return
|
|
}
|
|
baseURL := getAppURL()
|
|
ticketURL := baseURL + "/app/tickets"
|
|
titulo := fmt.Sprintf("Nuevo ticket: %s", ticket.Titulo)
|
|
cuerpo := fmt.Sprintf("Cliente: %s\nProyecto: %s\n%s", ticket.AutorNombre, proyectoNombre, ticket.Descripcion)
|
|
|
|
if cfg.CanalSistema {
|
|
_ = models.CreateSistemaNotif(&models.SistemaNotificacion{
|
|
TipoUsuario: "admin",
|
|
UsuarioID: 0,
|
|
Titulo: titulo,
|
|
Cuerpo: cuerpo,
|
|
Url: ticketURL,
|
|
Icono: "🎫",
|
|
})
|
|
}
|
|
if cfg.CanalEmail {
|
|
if adminEmail := getAdminEmail(); adminEmail != "" {
|
|
go SendTicketNuevoAdmin(adminEmail, proyectoNombre, ticket.AutorNombre, ticket.Titulo, ticket.Descripcion, ticketURL)
|
|
}
|
|
}
|
|
if cfg.CanalTelegram {
|
|
msg := fmt.Sprintf("🎫 <b>Nuevo ticket</b>\nProyecto: <b>%s</b>\nCliente: %s\nTítulo: <b>%s</b>\n\n%s\n\n🔗 <a href=\"%s\">Ver tickets</a>",
|
|
proyectoNombre, ticket.AutorNombre, ticket.Titulo, ticket.Descripcion, ticketURL)
|
|
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
|
|
}
|
|
cfg := models.GetNotifConfig("ticket_respuesta_cliente", "admin")
|
|
if cfg == nil {
|
|
return
|
|
}
|
|
baseURL := getAppURL()
|
|
ticketURL := baseURL + "/app/tickets"
|
|
titulo := fmt.Sprintf("Respuesta de cliente en: %s", ticket.Titulo)
|
|
cuerpo := fmt.Sprintf("Cliente: %s\nProyecto: %s\n%s", ticket.AutorNombre, proyectoNombre, contenido)
|
|
|
|
if cfg.CanalSistema {
|
|
_ = models.CreateSistemaNotif(&models.SistemaNotificacion{
|
|
TipoUsuario: "admin",
|
|
UsuarioID: 0,
|
|
Titulo: titulo,
|
|
Cuerpo: cuerpo,
|
|
Url: ticketURL,
|
|
Icono: "💬",
|
|
})
|
|
}
|
|
if cfg.CanalEmail {
|
|
if adminEmail := getAdminEmail(); adminEmail != "" {
|
|
go SendTicketRespuestaAdmin(adminEmail, proyectoNombre, ticket.AutorNombre, ticket.Titulo, contenido, ticketURL)
|
|
}
|
|
}
|
|
if cfg.CanalTelegram {
|
|
msg := fmt.Sprintf("💬 <b>Respuesta de cliente</b>\nProyecto: <b>%s</b>\nCliente: %s\n\n%s\n\n🔗 <a href=\"%s\">Ver tickets</a>",
|
|
proyectoNombre, ticket.AutorNombre, contenido, ticketURL)
|
|
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
|
|
}
|
|
|
|
cfg := models.GetNotifConfig("ticket_respuesta_admin", "portal_user")
|
|
if cfg == nil {
|
|
return
|
|
}
|
|
|
|
baseURL := getAppURL()
|
|
portalURL := baseURL + "/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\n\n🔗 <a href=\"%s\">Ver tu portal</a>",
|
|
proyectoNombre, ticket.Titulo, contenido, portalURL)
|
|
if err := ts.SendMessageWithToken(portalUser.TelegramChatID, msg, getAdminBotToken()); err != nil {
|
|
log.Printf("[Notif] Error telegram portal_user %d: %v", portalUser.ID, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── 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
|
|
}
|
|
cfg := models.GetNotifConfig("factura_subida", "portal_user")
|
|
if cfg == nil {
|
|
return
|
|
}
|
|
portalUsers, err := models.GetPortalUsersByClienteID(factura.ClienteID)
|
|
if err != nil || len(portalUsers) == 0 {
|
|
return
|
|
}
|
|
|
|
clienteNombre := factura.Cliente.Empresa
|
|
if clienteNombre == "" {
|
|
clienteNombre = factura.Cliente.Nombre
|
|
}
|
|
|
|
for _, u := range portalUsers {
|
|
u := u
|
|
baseURL := getAppURL()
|
|
portalURL := baseURL + "/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 ─────────────────────────────────────────────────────────────────
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// getAppURL devuelve la URL base configurada en APP_URL (sin slash final).
|
|
func getAppURL() string {
|
|
u := app.Http.Server.Url
|
|
// quitar slash final si lo hay
|
|
if len(u) > 0 && u[len(u)-1] == '/' {
|
|
u = u[:len(u)-1]
|
|
}
|
|
return u
|
|
}
|