Update migrate.go
This commit is contained in:
@@ -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: `<!DOCTYPE html><html><body style="font-family:sans-serif;color:#333;max-width:600px;margin:auto;padding:24px">
|
||||
<h2 style="color:#5a7d2b">Pago recibido ✔</h2>
|
||||
<p>Hola <strong>{{.ClienteNombre}}</strong>,</p>
|
||||
<p>Hemos recibido y confirmado tu pago. Tu servicio ha sido renovado correctamente.</p>
|
||||
<table style="width:100%;border-collapse:collapse;margin:16px 0">
|
||||
{{range .Servicios}}
|
||||
<tr style="border-bottom:1px solid #eee">
|
||||
<td style="padding:8px">{{.Nombre}}</td>
|
||||
<td style="padding:8px;text-align:right">{{.Moneda}} {{.Precio}}</td>
|
||||
<td style="padding:8px;text-align:right;color:#888">Vence: {{.FechaVenc}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
<p style="font-size:13px;color:#888">Si tienes alguna pregunta, responde a este correo.</p>
|
||||
</body></html>`,
|
||||
},
|
||||
{
|
||||
nombre: "Bienvenida (base)",
|
||||
asunto: "Bienvenido — tu servicio está activo",
|
||||
tipo: "bienvenida",
|
||||
cuerpo: `<!DOCTYPE html><html><body style="font-family:sans-serif;color:#333;max-width:600px;margin:auto;padding:24px">
|
||||
<h2 style="color:#5a7d2b">¡Bienvenido!</h2>
|
||||
<p>Hola <strong>{{.ClienteNombre}}</strong>,</p>
|
||||
<p>Tu servicio ha sido activado. A continuación el detalle:</p>
|
||||
<table style="width:100%;border-collapse:collapse;margin:16px 0">
|
||||
{{range .Servicios}}
|
||||
<tr style="border-bottom:1px solid #eee">
|
||||
<td style="padding:8px">{{.Nombre}}</td>
|
||||
<td style="padding:8px;text-align:right">{{.Moneda}} {{.Precio}}</td>
|
||||
<td style="padding:8px;text-align:right;color:#888">Vence: {{.FechaVenc}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
<p style="font-size:13px;color:#888">Gracias por elegirnos.</p>
|
||||
</body></html>`,
|
||||
},
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user