up
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
)
|
||||
|
||||
// getAppURL devuelve la URL base absoluta (sin slash final).
|
||||
// Si APP_URL es http://localhost sin puerto, añade APP_PORT para enlaces en desarrollo.
|
||||
func getAppURL() string {
|
||||
u := strings.TrimRight(app.Http.Server.Url, "/")
|
||||
if u == "" {
|
||||
return fmt.Sprintf("http://localhost:%s", app.Http.Server.Port)
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(u)
|
||||
if err != nil || parsed.Host == "" {
|
||||
return u
|
||||
}
|
||||
|
||||
host := parsed.Hostname()
|
||||
if parsed.Port() == "" && app.Http.Server.Port != "" {
|
||||
if host == "localhost" || host == "127.0.0.1" {
|
||||
parsed.Host = net.JoinHostPort(host, app.Http.Server.Port)
|
||||
return strings.TrimRight(parsed.String(), "/")
|
||||
}
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
// absAppURL convierte una ruta relativa en URL absoluta para correo/Telegram.
|
||||
func absAppURL(path string) string {
|
||||
if path == "" {
|
||||
return getAppURL()
|
||||
}
|
||||
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
|
||||
return path
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
return getAppURL() + path
|
||||
}
|
||||
|
||||
func adminTicketPath(ticketID uint) string {
|
||||
return fmt.Sprintf("/app/tickets?ticket=%d", ticketID)
|
||||
}
|
||||
|
||||
func portalTicketPath(proyectoSlug string, ticketID uint) string {
|
||||
if proyectoSlug == "" {
|
||||
return "/portal/dashboard"
|
||||
}
|
||||
return fmt.Sprintf("/portal/proyecto/%s?tab=Tickets&ticket=%d", url.PathEscape(proyectoSlug), ticketID)
|
||||
}
|
||||
|
||||
func escapeTelegramHTML(s string) string {
|
||||
return html.EscapeString(s)
|
||||
}
|
||||
|
||||
func telegramHTMLLink(absURL, label string) string {
|
||||
return fmt.Sprintf(`<a href="%s">%s</a>`, escapeTelegramHTML(absURL), escapeTelegramHTML(label))
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/config"
|
||||
)
|
||||
|
||||
func TestGetAppURLLocalhostAddsPort(t *testing.T) {
|
||||
app.Http = &config.AppConfig{
|
||||
Server: config.ServerConfig{
|
||||
Url: "http://localhost",
|
||||
Port: "8084",
|
||||
},
|
||||
}
|
||||
if got := getAppURL(); got != "http://localhost:8084" {
|
||||
t.Fatalf("getAppURL() = %q, want http://localhost:8084", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsAppURLRelativePath(t *testing.T) {
|
||||
app.Http = &config.AppConfig{
|
||||
Server: config.ServerConfig{
|
||||
Url: "http://localhost",
|
||||
Port: "8084",
|
||||
},
|
||||
}
|
||||
got := absAppURL("/app/tickets?ticket=3")
|
||||
want := "http://localhost:8084/app/tickets?ticket=3"
|
||||
if got != want {
|
||||
t.Fatalf("absAppURL() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTelegramHTMLLinkEscapesAmpersand(t *testing.T) {
|
||||
link := telegramHTMLLink("http://localhost:8084/portal/p?tab=Tickets&ticket=1", "Ver portal")
|
||||
if link != `<a href="http://localhost:8084/portal/p?tab=Tickets&ticket=1">Ver portal</a>` {
|
||||
t.Fatalf("telegramHTMLLink() = %q", link)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user