This commit is contained in:
Lizandro Guarnizo
2026-05-18 23:45:11 -05:00
parent 25f21be966
commit bbe83c79ba
8 changed files with 188 additions and 47 deletions
+39 -33
View File
@@ -3,8 +3,8 @@ package services
import (
"fmt"
"log"
"net/url"
"github.com/sujit-baniya/fiber-boilerplate/app"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
)
@@ -18,8 +18,8 @@ func DispatchTicketNuevo(ticket *models.ProyectoTicket, portalUser *models.Porta
if cfg == nil {
return
}
baseURL := getAppURL()
ticketURL := baseURL + "/app/tickets"
ticketPath := adminTicketPath(ticket.ID)
ticketURL := absAppURL(ticketPath)
titulo := fmt.Sprintf("Nuevo ticket: %s", ticket.Titulo)
cuerpo := fmt.Sprintf("Cliente: %s\nProyecto: %s\n%s", ticket.AutorNombre, proyectoNombre, ticket.Descripcion)
@@ -29,7 +29,7 @@ func DispatchTicketNuevo(ticket *models.ProyectoTicket, portalUser *models.Porta
UsuarioID: 0,
Titulo: titulo,
Cuerpo: cuerpo,
Url: ticketURL,
Url: ticketPath,
Icono: "🎫",
})
}
@@ -39,8 +39,12 @@ func DispatchTicketNuevo(ticket *models.ProyectoTicket, portalUser *models.Porta
}
}
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)
msg := fmt.Sprintf("🎫 <b>Nuevo ticket</b>\nProyecto: <b>%s</b>\nCliente: %s\nTítulo: <b>%s</b>\n\n%s\n\n🔗 %s",
escapeTelegramHTML(proyectoNombre),
escapeTelegramHTML(ticket.AutorNombre),
escapeTelegramHTML(ticket.Titulo),
escapeTelegramHTML(ticket.Descripcion),
telegramHTMLLink(ticketURL, "Ver ticket"))
sendTelegramAdmin(msg)
}
}
@@ -55,8 +59,8 @@ func DispatchTicketRespuestaCliente(ticket *models.ProyectoTicket, contenido str
if cfg == nil {
return
}
baseURL := getAppURL()
ticketURL := baseURL + "/app/tickets"
ticketPath := adminTicketPath(ticket.ID)
ticketURL := absAppURL(ticketPath)
titulo := fmt.Sprintf("Respuesta de cliente en: %s", ticket.Titulo)
cuerpo := fmt.Sprintf("Cliente: %s\nProyecto: %s\n%s", ticket.AutorNombre, proyectoNombre, contenido)
@@ -66,7 +70,7 @@ func DispatchTicketRespuestaCliente(ticket *models.ProyectoTicket, contenido str
UsuarioID: 0,
Titulo: titulo,
Cuerpo: cuerpo,
Url: ticketURL,
Url: ticketPath,
Icono: "💬",
})
}
@@ -76,8 +80,11 @@ func DispatchTicketRespuestaCliente(ticket *models.ProyectoTicket, contenido str
}
}
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)
msg := fmt.Sprintf("💬 <b>Respuesta de cliente</b>\nProyecto: <b>%s</b>\nCliente: %s\n\n%s\n\n🔗 %s",
escapeTelegramHTML(proyectoNombre),
escapeTelegramHTML(ticket.AutorNombre),
escapeTelegramHTML(contenido),
telegramHTMLLink(ticketURL, "Ver ticket"))
sendTelegramAdmin(msg)
}
}
@@ -99,8 +106,12 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
return
}
baseURL := getAppURL()
portalURL := baseURL + "/portal/dashboard"
proyectoSlug := ""
if proy, err := models.GetProyectoByID(ticket.ProyectoID); err == nil {
proyectoSlug = proy.Slug
}
portalPath := portalTicketPath(proyectoSlug, ticket.ID)
portalURL := absAppURL(portalPath)
titulo := fmt.Sprintf("Respuesta en tu ticket: %s", ticket.Titulo)
cuerpo := fmt.Sprintf("El equipo respondió: %s", contenido)
@@ -110,7 +121,7 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
UsuarioID: portalUser.ID,
Titulo: titulo,
Cuerpo: cuerpo,
Url: portalURL,
Url: portalPath,
Icono: "💬",
})
}
@@ -119,8 +130,11 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
}
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)
msg := fmt.Sprintf("💬 <b>El equipo respondió tu ticket</b>\nProyecto: <b>%s</b>\nTicket: <b>%s</b>\n\n%s\n\n🔗 %s",
escapeTelegramHTML(proyectoNombre),
escapeTelegramHTML(ticket.Titulo),
escapeTelegramHTML(contenido),
telegramHTMLLink(portalURL, "Ver en el portal"))
if err := ts.SendMessageWithToken(portalUser.TelegramChatID, msg, getAdminBotToken()); err != nil {
log.Printf("[Notif] Error telegram portal_user %d: %v", portalUser.ID, err)
}
@@ -147,12 +161,11 @@ func DispatchFacturaSubida(factura *models.Factura) {
clienteNombre = factura.Cliente.Nombre
}
baseURL := getAppURL()
// Construir URL apuntando a la pestaña Facturas del proyecto (o dashboard si no tiene proyecto)
portalURL := baseURL + "/portal/dashboard"
portalPath := "/portal/dashboard"
if factura.ProyectoID != nil && factura.Proyecto != nil && factura.Proyecto.Slug != "" {
portalURL = baseURL + "/portal/proyecto/" + factura.Proyecto.Slug + "?tab=Facturas"
portalPath = fmt.Sprintf("/portal/proyecto/%s?tab=Facturas", url.PathEscape(factura.Proyecto.Slug))
}
portalURL := absAppURL(portalPath)
for _, u := range portalUsers {
u := u
@@ -165,7 +178,7 @@ func DispatchFacturaSubida(factura *models.Factura) {
UsuarioID: u.ID,
Titulo: titulo,
Cuerpo: cuerpo,
Url: portalURL,
Url: portalPath,
Icono: "🧾",
})
}
@@ -174,8 +187,11 @@ func DispatchFacturaSubida(factura *models.Factura) {
}
if cfg.CanalTelegram && u.TelegramChatID != "" {
ts := NewTelegramService()
msg := fmt.Sprintf("🧾 <b>Nueva factura disponible</b>\nNúmero: <b>%s</b>\nMonto: %s %.2f\n\n🔗 <a href=\"%s\">Ver en el portal</a>",
factura.Numero, factura.Moneda, factura.Monto, portalURL)
msg := fmt.Sprintf("🧾 <b>Nueva factura disponible</b>\nNúmero: <b>%s</b>\nMonto: %s %.2f\n\n🔗 %s",
escapeTelegramHTML(factura.Numero),
escapeTelegramHTML(factura.Moneda),
factura.Monto,
telegramHTMLLink(portalURL, "Ver en el portal"))
if err := ts.SendMessageWithToken(u.TelegramChatID, msg, getAdminBotToken()); err != nil {
log.Printf("[Notif] Error telegram portal_user %d: %v", u.ID, err)
}
@@ -233,13 +249,3 @@ func sendTelegramAdmin(mensaje string) {
_ = models.CreateTelegramLog(logEntry)
}
}
// 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
}