Merge remote-tracking branch 'origin/main'
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> # Conflicts: # main.go
This commit is contained in:
@@ -95,9 +95,25 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
|
||||
if ticket == nil {
|
||||
return
|
||||
}
|
||||
portalUser, err := models.GetPortalUserByID(ticket.PortalUserID)
|
||||
proyectoSlug := ""
|
||||
if ticket.ProyectoID != nil {
|
||||
if proy, err := models.GetProyectoByID(*ticket.ProyectoID); err == nil {
|
||||
proyectoSlug = proy.Slug
|
||||
proyectoNombre = proy.Nombre
|
||||
}
|
||||
}
|
||||
|
||||
// Si no está asociado a un portal user (ej: ticket por email), responder por email directo
|
||||
if ticket.PortalUserID == nil {
|
||||
if ticket.EmailFrom != "" {
|
||||
go SendTicketRespuestaPortalUser(ticket.EmailFrom, ticket.AutorNombre, proyectoNombre, ticket.Titulo, contenido, "")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
portalUser, err := models.GetPortalUserByID(*ticket.PortalUserID)
|
||||
if err != nil || portalUser == nil {
|
||||
log.Printf("[Notif] PortalUser %d no encontrado: %v", ticket.PortalUserID, err)
|
||||
log.Printf("[Notif] PortalUser %d no encontrado: %v", *ticket.PortalUserID, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -106,10 +122,6 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -82,18 +82,22 @@ func openDynamicDB(c models.ConxDb) (*sql.DB, error) {
|
||||
|
||||
// ExecuteSQL ejecuta SQL arbitrario contra la conexión y devuelve QueryResult.
|
||||
// También guarda en query_history.
|
||||
func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
func ExecuteSQL(conx models.ConxDb, database, sqlText string, userID ...uint) QueryResult {
|
||||
uid := uint(0)
|
||||
if len(userID) > 0 {
|
||||
uid = userID[0]
|
||||
}
|
||||
if isMongoDriver(strings.ToLower(conx.TipoDb.Nombre)) {
|
||||
return mongoExecuteSQL(conx, database, sqlText)
|
||||
return mongoExecuteSQL(conx, database, sqlText, uid)
|
||||
}
|
||||
if isRedisDriver(strings.ToLower(conx.TipoDb.Nombre)) {
|
||||
return redisExecuteCommand(conx, database, sqlText)
|
||||
return redisExecuteCommand(conx, database, sqlText, uid)
|
||||
}
|
||||
start := time.Now()
|
||||
|
||||
db, err := openDynamicDB(conx)
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, sqlText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
defer db.Close()
|
||||
@@ -110,7 +114,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
}
|
||||
} else {
|
||||
if _, err2 := db.Exec("USE " + quoteIdentifier(database, conx.TipoDb.Nombre)); err2 != nil {
|
||||
saveHistory(conx.ID, sqlText, "error", err2.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err2.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err2.Error()}
|
||||
}
|
||||
}
|
||||
@@ -126,7 +130,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
rows, err := db.Query(trimmed)
|
||||
if err != nil {
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
saveHistory(conx.ID, sqlText, "error", err.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err.Error(), 0, elapsed)
|
||||
return QueryResult{Error: err.Error(), IsSelect: true}
|
||||
}
|
||||
defer rows.Close()
|
||||
@@ -170,7 +174,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
if len(preview) > 80 {
|
||||
preview = preview[:80] + "..."
|
||||
}
|
||||
saveHistory(conx.ID, sqlText, "error", execErr.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "error", execErr.Error(), 0, elapsed)
|
||||
return QueryResult{Error: fmt.Sprintf("[%s]: %s", preview, execErr.Error())}
|
||||
}
|
||||
if affected, err2 := res.RowsAffected(); err2 == nil {
|
||||
@@ -180,7 +184,7 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
result.AffectedRows = totalAffected
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, sqlText, "ok", "", totalAffected, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "ok", "", totalAffected, elapsed)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -188,18 +192,18 @@ func ExecuteSQL(conx models.ConxDb, database, sqlText string) QueryResult {
|
||||
res, err := db.Exec(trimmed)
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, sqlText, "error", err.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "error", err.Error(), 0, elapsed)
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
affected, _ := res.RowsAffected()
|
||||
result.AffectedRows = affected
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, sqlText, "ok", "", affected, elapsed)
|
||||
saveHistory(conx.ID, uid, sqlText, "ok", "", affected, elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
result.DurationMs = time.Since(start).Milliseconds()
|
||||
saveHistory(conx.ID, sqlText, "ok", "", int64(result.RowCount), result.DurationMs)
|
||||
saveHistory(conx.ID, uid, sqlText, "ok", "", int64(result.RowCount), result.DurationMs)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -559,9 +563,10 @@ func quoteIdentifier(name, driver string) string {
|
||||
return "`" + strings.ReplaceAll(name, "`", "``") + "`"
|
||||
}
|
||||
|
||||
func saveHistory(conxID uint, sqlText, status, errMsg string, rows, durationMs int64) {
|
||||
func saveHistory(conxID, userID uint, sqlText, status, errMsg string, rows, durationMs int64) {
|
||||
models.SaveQueryHistory(models.QueryHistory{
|
||||
ConxDbID: conxID,
|
||||
UserID: userID,
|
||||
SQL: sqlText,
|
||||
Status: status,
|
||||
ErrorMsg: errMsg,
|
||||
@@ -670,7 +675,11 @@ func redisListKeys(c models.ConxDb, database string) ([]string, error) {
|
||||
// redisExecuteCommand parsea y ejecuta un comando Redis.
|
||||
// Los comandos se escriben como en redis-cli: GET key / SET key value / etc.
|
||||
// Soporta múltiples líneas: cada línea no vacía es un comando independiente.
|
||||
func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResult {
|
||||
func redisExecuteCommand(conx models.ConxDb, database, cmdText string, userID ...uint) QueryResult {
|
||||
uid := uint(0)
|
||||
if len(userID) > 0 {
|
||||
uid = userID[0]
|
||||
}
|
||||
start := time.Now()
|
||||
|
||||
dbIndex := 0
|
||||
@@ -682,7 +691,7 @@ func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResu
|
||||
|
||||
client, err := redisConnect(conx, dbIndex)
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, cmdText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, cmdText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
defer client.Close()
|
||||
@@ -735,7 +744,7 @@ func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResu
|
||||
IsSelect: true,
|
||||
DurationMs: elapsed,
|
||||
}
|
||||
saveHistory(conx.ID, cmdText, "ok", "", int64(len(rows)), elapsed)
|
||||
saveHistory(conx.ID, uid, cmdText, "ok", "", int64(len(rows)), elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -753,13 +762,13 @@ func redisExecuteCommand(conx models.ConxDb, database, cmdText string) QueryResu
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
|
||||
if execErr != nil && execErr != goredis.Nil {
|
||||
saveHistory(conx.ID, cmdText, "error", execErr.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, cmdText, "error", execErr.Error(), 0, elapsed)
|
||||
return QueryResult{Error: execErr.Error(), DurationMs: elapsed}
|
||||
}
|
||||
|
||||
result := redisResultToQueryResult(val, lines[0])
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, cmdText, "ok", "", int64(result.RowCount), elapsed)
|
||||
saveHistory(conx.ID, uid, cmdText, "ok", "", int64(result.RowCount), elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1052,11 +1061,15 @@ func mongoSingleToDoubleQuotes(s string) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult {
|
||||
func mongoExecuteSQL(conx models.ConxDb, database, queryText string, userID ...uint) QueryResult {
|
||||
uid := uint(0)
|
||||
if len(userID) > 0 {
|
||||
uid = userID[0]
|
||||
}
|
||||
start := time.Now()
|
||||
client, err := mongoConnect(conx)
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, queryText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
saveHistory(conx.ID, uid, queryText, "error", err.Error(), 0, time.Since(start).Milliseconds())
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
defer client.Disconnect(context.Background()) //nolint
|
||||
@@ -1066,11 +1079,11 @@ func mongoExecuteSQL(conx models.ConxDb, database, queryText string) QueryResult
|
||||
result, err := mongoRunQuery(ctx, db, strings.TrimSpace(queryText))
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
if err != nil {
|
||||
saveHistory(conx.ID, queryText, "error", err.Error(), 0, elapsed)
|
||||
saveHistory(conx.ID, uid, queryText, "error", err.Error(), 0, elapsed)
|
||||
return QueryResult{Error: err.Error()}
|
||||
}
|
||||
result.DurationMs = elapsed
|
||||
saveHistory(conx.ID, queryText, "ok", "", int64(result.RowCount)+result.AffectedRows, elapsed)
|
||||
saveHistory(conx.ID, uid, queryText, "ok", "", int64(result.RowCount)+result.AffectedRows, elapsed)
|
||||
return *result
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
||||
)
|
||||
|
||||
// soporteSendMail envía un correo usando la configuración SMTP del webhook de soporte.
|
||||
// Si no hay SMTP configurado, usa app.Http.Mail como fallback.
|
||||
func soporteSendMail(to, subject, htmlBody string) error {
|
||||
cfg, err := models.GetSoporteWebhookActivo()
|
||||
if err != nil || cfg == nil || cfg.SmtpHost == "" {
|
||||
return app.Http.Mail.Send(to, subject, htmlBody)
|
||||
}
|
||||
from := cfg.SmtpFromAddr
|
||||
if from == "" {
|
||||
from = cfg.EmailDestino
|
||||
}
|
||||
if from == "" {
|
||||
return app.Http.Mail.Send(to, subject, htmlBody)
|
||||
}
|
||||
fromName := cfg.SmtpFromName
|
||||
if fromName == "" {
|
||||
fromName = "Soporte"
|
||||
}
|
||||
port := cfg.SmtpPort
|
||||
if port == 0 {
|
||||
port = 587
|
||||
}
|
||||
auth := smtp.PlainAuth("", cfg.SmtpUsername, cfg.SmtpPassword, cfg.SmtpHost)
|
||||
msg := []byte(fmt.Sprintf("From: %s <%s>\r\nTo: %s\r\nSubject: %s\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n%s", fromName, from, to, subject, htmlBody))
|
||||
addr := fmt.Sprintf("%s:%d", cfg.SmtpHost, port)
|
||||
enc := strings.ToLower(cfg.SmtpEncryption)
|
||||
if enc == "tls" {
|
||||
tlsCfg := &tls.Config{ServerName: cfg.SmtpHost}
|
||||
conn, err := tls.Dial("tcp", addr, tlsCfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("soporte SMTP TLS: %w", err)
|
||||
}
|
||||
client, err := smtp.NewClient(conn, cfg.SmtpHost)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return fmt.Errorf("soporte SMTP client: %w", err)
|
||||
}
|
||||
defer client.Close()
|
||||
if err = client.Auth(auth); err != nil {
|
||||
return fmt.Errorf("soporte SMTP auth: %w", err)
|
||||
}
|
||||
if err = client.Mail(from); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = client.Rcpt(to); err != nil {
|
||||
return err
|
||||
}
|
||||
w, err := client.Data()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Close()
|
||||
}
|
||||
if enc == "starttls" {
|
||||
tlsCfg := &tls.Config{ServerName: cfg.SmtpHost}
|
||||
conn, err := smtp.Dial(addr)
|
||||
if err != nil {
|
||||
goto fallback
|
||||
}
|
||||
if err = conn.StartTLS(tlsCfg); err != nil {
|
||||
conn.Close()
|
||||
goto fallback
|
||||
}
|
||||
if err = conn.Auth(auth); err != nil {
|
||||
conn.Close()
|
||||
return fmt.Errorf("soporte SMTP STARTTLS auth: %w", err)
|
||||
}
|
||||
return smtp.SendMail(addr, auth, from, []string{to}, msg)
|
||||
}
|
||||
return smtp.SendMail(addr, auth, from, []string{to}, msg)
|
||||
|
||||
fallback:
|
||||
return app.Http.Mail.Send(to, subject, htmlBody)
|
||||
}
|
||||
|
||||
// SendSoporteAutoRespuesta envía acuse de recibo automático al crear un ticket por email
|
||||
func SendSoporteAutoRespuesta(ticket *models.ProyectoTicket) {
|
||||
if ticket == nil || ticket.EmailFrom == "" {
|
||||
return
|
||||
}
|
||||
subject := fmt.Sprintf("Recibimos tu solicitud: %s", ticket.Titulo)
|
||||
mensaje := "Hemos recibido tu solicitud y te responderemos a la brevedad."
|
||||
if app.Http.Database.DB != nil {
|
||||
if cfg, err := models.GetSoporteWebhookActivo(); err == nil && cfg.MensajeAuto != "" {
|
||||
mensaje = cfg.MensajeAuto
|
||||
}
|
||||
}
|
||||
htmlBody := fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html><body style="font-family:Inter,sans-serif;background:#f1f5f9;padding:32px">
|
||||
<div style="max-width:520px;margin:0 auto;background:#fff;border-radius:16px;padding:32px;border:1px solid #e2e8f0">
|
||||
<div style="text-align:center;margin-bottom:24px">
|
||||
<div style="width:48px;height:48px;border-radius:50%%;background:#8eb02f;display:inline-flex;align-items:center;justify-content:center;color:#fff;font-weight:700;font-size:20px">U</div>
|
||||
<h2 style="margin:12px 0 4px;color:#1e293b">Ticket recibido</h2>
|
||||
<p style="color:#64748b;font-size:14px;margin:0">Soporte U-site</p>
|
||||
</div>
|
||||
<p style="color:#334155;font-size:15px">Hola <strong>%s</strong>,</p>
|
||||
<p style="color:#334155;font-size:14px">%s</p>
|
||||
<div style="background:#f8fafc;border-radius:10px;padding:16px;margin:20px 0;border:1px solid #e2e8f0">
|
||||
<p style="margin:0 0 6px;font-size:13px;color:#64748b"><strong>Ticket:</strong> #%d</p>
|
||||
<p style="margin:0 0 6px;font-size:13px;color:#64748b"><strong>Asunto:</strong> %s</p>
|
||||
<p style="margin:0;font-size:13px;color:#64748b"><strong>Mensaje:</strong> %s</p>
|
||||
</div>
|
||||
<p style="color:#94a3b8;font-size:12px">Este es un mensaje automático. No respondas a este correo.</p>
|
||||
</div></body></html>`, ticket.AutorNombre, mensaje, ticket.ID, ticket.Titulo, ticket.Descripcion)
|
||||
|
||||
go func() {
|
||||
if err := soporteSendMail(ticket.EmailFrom, subject, htmlBody); err != nil {
|
||||
log.Printf("[Soporte] Error enviando auto-respuesta a %s: %v", ticket.EmailFrom, err)
|
||||
} else {
|
||||
log.Printf("[Soporte] Auto-respuesta enviada a %s (ticket #%d)", ticket.EmailFrom, ticket.ID)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// SendSoporteNotifAdmin notifica a los admins sobre un nuevo ticket de email
|
||||
func SendSoporteNotifAdmin(ticket *models.ProyectoTicket) {
|
||||
if ticket == nil {
|
||||
return
|
||||
}
|
||||
adminEmail := ""
|
||||
if app.Http.Database.DB != nil {
|
||||
if cfg, err := models.GetSmtpConfig(); err == nil {
|
||||
adminEmail = cfg.FromAddress
|
||||
}
|
||||
}
|
||||
if adminEmail == "" {
|
||||
return
|
||||
}
|
||||
subject := fmt.Sprintf("🎫 Nuevo ticket por email: %s", ticket.Titulo)
|
||||
htmlBody := fmt.Sprintf(`<!DOCTYPE html>
|
||||
<html><body style="font-family:Inter,sans-serif;background:#f1f5f9;padding:32px">
|
||||
<div style="max-width:520px;margin:0 auto;background:#fff;border-radius:16px;padding:32px;border:1px solid #e2e8f0">
|
||||
<div style="display:flex;align-items:center;gap:12px;margin-bottom:20px">
|
||||
<div style="width:40px;height:40px;border-radius:50%%;background:#8eb02f;display:flex;align-items:center;justify-content:center;color:#fff;font-size:18px">🎫</div>
|
||||
<div><h2 style="margin:0;color:#1e293b;font-size:17px">Nuevo ticket por email</h2>
|
||||
<p style="margin:0;color:#64748b;font-size:13px">De: <strong>%s</strong> <%s></p></div>
|
||||
</div>
|
||||
<div style="background:#f8fafc;border-radius:10px;padding:16px;margin-bottom:20px;border:1px solid #e2e8f0">
|
||||
<p style="margin:0 0 6px;font-size:14px;color:#334155"><strong>Asunto:</strong> %s</p>
|
||||
<p style="margin:0;font-size:14px;color:#334155"><strong>Mensaje:</strong> %s</p>
|
||||
</div>
|
||||
<a href="%s/app/tickets?ticket=%d" style="display:block;text-align:center;background:#8eb02f;color:#fff;padding:12px 24px;border-radius:10px;font-weight:600;text-decoration:none;font-size:15px">Ver ticket</a>
|
||||
</div></body></html>`, ticket.AutorNombre, ticket.EmailFrom, ticket.Titulo, ticket.Descripcion, absAppURL(""), ticket.ID)
|
||||
|
||||
go func() {
|
||||
if err := soporteSendMail(adminEmail, subject, htmlBody); err != nil {
|
||||
log.Printf("[Soporte] Error notificando admin: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/config"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
||||
)
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
func setupTestApp() {
|
||||
if app.Http == nil {
|
||||
app.Http = &config.AppConfig{
|
||||
Server: config.ServerConfig{
|
||||
Url: "http://localhost",
|
||||
Port: "8080",
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendSoporteAutoRespuestaNilTicket(t *testing.T) {
|
||||
setupTestApp()
|
||||
// No debe panic si ticket es nil
|
||||
SendSoporteAutoRespuesta(nil)
|
||||
}
|
||||
|
||||
func TestSendSoporteAutoRespuestaSinEmail(t *testing.T) {
|
||||
setupTestApp()
|
||||
ticket := &models.ProyectoTicket{
|
||||
Titulo: "Test",
|
||||
Descripcion: "Desc",
|
||||
AutorNombre: "Cliente",
|
||||
Origen: "email",
|
||||
}
|
||||
// No debe hacer nada si no tiene email
|
||||
SendSoporteAutoRespuesta(ticket)
|
||||
}
|
||||
|
||||
func TestSendSoporteAutoRespuestaConEmail(t *testing.T) {
|
||||
setupTestApp()
|
||||
ticket := &models.ProyectoTicket{
|
||||
EmailFrom: "cliente@test.com",
|
||||
Titulo: "Ayuda con factura",
|
||||
Descripcion: "No puedo ver mi factura de enero",
|
||||
AutorNombre: "Juan Pérez",
|
||||
Origen: "email",
|
||||
}
|
||||
app.Http.Mail.FromAddress = "soporte@u-s.app"
|
||||
app.Http.Mail.Host = "127.0.0.1"
|
||||
app.Http.Mail.Port = 2525
|
||||
// Solo verificar que no panic (enviaría a un SMTP que no existe, pero el go routine captura error)
|
||||
SendSoporteAutoRespuesta(ticket)
|
||||
}
|
||||
|
||||
func TestSendSoporteNotifAdminNil(t *testing.T) {
|
||||
setupTestApp()
|
||||
SendSoporteNotifAdmin(nil)
|
||||
}
|
||||
|
||||
func TestSendSoporteNotifAdminSinSmtp(t *testing.T) {
|
||||
setupTestApp()
|
||||
ticket := &models.ProyectoTicket{
|
||||
EmailFrom: "cliente@test.com",
|
||||
Titulo: "Problema",
|
||||
Descripcion: "No funciona",
|
||||
AutorNombre: "Cliente",
|
||||
}
|
||||
// Sin SMTP config no debe panic
|
||||
SendSoporteNotifAdmin(ticket)
|
||||
}
|
||||
|
||||
func TestSendSoporteAutoRespuestaOrigenPortal(t *testing.T) {
|
||||
setupTestApp()
|
||||
ticket := &models.ProyectoTicket{
|
||||
EmailFrom: "",
|
||||
Titulo: "Portal ticket",
|
||||
Descripcion: "Desc",
|
||||
AutorNombre: "Portal User",
|
||||
Origen: "portal",
|
||||
}
|
||||
// No debe intentar enviar email porque EmailFrom está vacío
|
||||
SendSoporteAutoRespuesta(ticket)
|
||||
}
|
||||
|
||||
func TestSendSoporteNotifAdminConSmtp(t *testing.T) {
|
||||
setupTestApp()
|
||||
// Simular SMTP config en app.Http.Mail
|
||||
app.Http.Mail.FromAddress = "admin@u-s.app"
|
||||
app.Http.Mail.Host = "127.0.0.1"
|
||||
app.Http.Mail.Port = 2525
|
||||
|
||||
ticket := &models.ProyectoTicket{
|
||||
// gorm.Model embedido: ID se asigna via Create en DB real
|
||||
EmailFrom: "cliente@test.com",
|
||||
Titulo: "Error en el sistema",
|
||||
Descripcion: "Recibo un error 500",
|
||||
AutorNombre: "María López",
|
||||
Origen: "email",
|
||||
}
|
||||
// Verificar que no panic (el email se envía en goroutine)
|
||||
SendSoporteNotifAdmin(ticket)
|
||||
}
|
||||
Reference in New Issue
Block a user