From f07ac623eb461f0b18a68953a42438cf442ba772 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Tue, 12 May 2026 21:58:55 -0500 Subject: [PATCH] Update migrate.go --- migrations/migrate.go | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/migrations/migrate.go b/migrations/migrate.go index 357d8d6..649cab1 100755 --- a/migrations/migrate.go +++ b/migrations/migrate.go @@ -101,6 +101,9 @@ func Migrate() { // Insertar submódulo "Statuspage" en el módulo Integraciones SeedStatuspage() + // Crear plantillas y reglas mínimas si no existen (bienvenida, pago_recibido) + SeedPlantillasBase() + log.Println("Migration Completed...") } @@ -158,6 +161,107 @@ func SeedStatuspage() { log.Println("[SEED] Seed de Statuspage completado.") } +// SeedPlantillasBase crea una plantilla mínima y la regla de pago_recibido si no existen. +// Es idempotente — no sobreescribe registros existentes. +func SeedPlantillasBase() { + db := app.Http.Database.DB + + plantillasBase := []struct { + nombre string + asunto string + tipo string + cuerpo string + }{ + { + nombre: "Pago recibido (base)", + asunto: "Pago recibido — gracias por tu pago", + tipo: "pago", + cuerpo: ` +

Pago recibido ✔

+

Hola {{.ClienteNombre}},

+

Hemos recibido y confirmado tu pago. Tu servicio ha sido renovado correctamente.

+ +{{range .Servicios}} + + + + + +{{end}} +
{{.Nombre}}{{.Moneda}} {{.Precio}}Vence: {{.FechaVenc}}
+

Si tienes alguna pregunta, responde a este correo.

+`, + }, + { + nombre: "Bienvenida (base)", + asunto: "Bienvenido — tu servicio está activo", + tipo: "bienvenida", + cuerpo: ` +

¡Bienvenido!

+

Hola {{.ClienteNombre}},

+

Tu servicio ha sido activado. A continuación el detalle:

+ +{{range .Servicios}} + + + + + +{{end}} +
{{.Nombre}}{{.Moneda}} {{.Precio}}Vence: {{.FechaVenc}}
+

Gracias por elegirnos.

+`, + }, + } + + for _, p := range plantillasBase { + var existing models.PlantillaCorreo + if err := db.Where("nombre = ?", p.nombre).First(&existing).Error; err != nil { + nueva := models.PlantillaCorreo{ + Nombre: p.nombre, + Asunto: p.asunto, + Tipo: p.tipo, + CuerpoHTML: p.cuerpo, + } + if err := db.Create(&nueva).Error; err != nil { + log.Printf("[SEED] Error creando plantilla '%s': %v", p.nombre, err) + continue + } + log.Printf("[SEED] Plantilla '%s' creada (ID %d)", p.nombre, nueva.ID) + } else { + log.Printf("[SEED] Plantilla '%s' ya existe (ID %d)", existing.Nombre, existing.ID) + } + } + + // Crear regla pago_recibido si no existe + var reglaExistente models.NotificacionRegla + if err := db.Where("tipo_evento = ?", "pago_recibido").First(®laExistente).Error; err != nil { + // Buscar la plantilla de pago + var plantilla models.PlantillaCorreo + if err2 := db.Where("tipo = ?", "pago").First(&plantilla).Error; err2 != nil { + log.Printf("[SEED] No se encontró plantilla de tipo 'pago' para crear regla pago_recibido") + return + } + regla := models.NotificacionRegla{ + Nombre: "Confirmación de pago", + TipoEvento: "pago_recibido", + PlantillaID: plantilla.ID, + Activo: true, + AplicaA: "todos", + PasarelaEnlace: "ninguna", + } + if err := db.Create(®la).Error; err != nil { + log.Printf("[SEED] Error creando regla pago_recibido: %v", err) + } else { + log.Printf("[SEED] Regla 'pago_recibido' creada (ID %d) con plantilla ID %d", regla.ID, plantilla.ID) + } + } else { + log.Printf("[SEED] Regla 'pago_recibido' ya existe (ID %d)", reglaExistente.ID) + } + + log.Println("[SEED] SeedPlantillasBase completado.") +} + // MigrateRenovaciones crea/actualiza las tablas del módulo de Renovaciones. // Es idempotente: GORM AutoMigrate solo añade columnas/tablas nuevas, nunca las borra. func MigrateRenovaciones() {