up
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
@@ -85,3 +86,19 @@ func GetAllLogs(limit, offset int) ([]NotificacionLog, int64, error) {
|
||||
func GetLogByID(id uint) (*NotificacionLog, error) {
|
||||
return GetNotificacionLogByID(id)
|
||||
}
|
||||
|
||||
// GetLogsByContratoID devuelve los logs de notificaciones que incluyen un contrato específico
|
||||
func GetLogsByContratoID(contratoID uint) ([]NotificacionLog, error) {
|
||||
var items []NotificacionLog
|
||||
// contratos_ids es un JSON array, buscamos que contenga el ID
|
||||
pattern := fmt.Sprintf(`%%[%d]%%`, contratoID)
|
||||
if err := app.Http.Database.DB.
|
||||
Preload("Regla").
|
||||
Where("contratos_ids LIKE ?", pattern).
|
||||
Order("created_at DESC").
|
||||
Limit(50).
|
||||
Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -120,3 +120,16 @@ func GetSaasDispatchLogs(limit, offset int) ([]SaasDispatchLog, int64, error) {
|
||||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
// GetDispatchLogsByContratoID devuelve los pagos/dispatches de un contrato específico
|
||||
func GetDispatchLogsByContratoID(contratoID uint) ([]SaasDispatchLog, error) {
|
||||
var items []SaasDispatchLog
|
||||
if err := app.Http.Database.DB.
|
||||
Where("contrato_id = ?", contratoID).
|
||||
Order("created_at DESC").
|
||||
Limit(50).
|
||||
Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -273,6 +273,36 @@ func ReenviarLog(logEntry *models.NotificacionLog) error {
|
||||
return sendErr
|
||||
}
|
||||
|
||||
// EnviarCorreoConfirmacionPago envía el correo de "pago recibido" para un contrato.
|
||||
// Usa la regla/plantilla con tipo_evento = 'pago_recibido'. Si no existe, no hace nada.
|
||||
// Es idempotente: no reenvía si ya se envió hoy (YaEnviadoHoy por regla+cliente).
|
||||
func EnviarCorreoConfirmacionPago(contratoID uint) {
|
||||
contrato, err := models.GetContratoByID(contratoID)
|
||||
if err != nil {
|
||||
log.Printf("[PAGO-CORREO] contrato %d no encontrado: %v", contratoID, err)
|
||||
return
|
||||
}
|
||||
|
||||
reglas, err := models.GetReglasByTipoEvento("pago_recibido")
|
||||
if err != nil || len(reglas) == 0 {
|
||||
log.Printf("[PAGO-CORREO] sin regla pago_recibido configurada — se omite correo de confirmación")
|
||||
return
|
||||
}
|
||||
regla := ®las[0]
|
||||
|
||||
// Idempotencia: si ya se envió hoy para esta regla+cliente, no reenviar
|
||||
if models.YaEnviadoHoy(contrato.ClienteID, regla.ID) {
|
||||
log.Printf("[PAGO-CORREO] correo de confirmación ya enviado hoy para cliente %d", contrato.ClienteID)
|
||||
return
|
||||
}
|
||||
|
||||
if err := EnviarNotificacionGrupo(regla, &contrato.Cliente, []models.Contrato{*contrato}); err != nil {
|
||||
log.Printf("[PAGO-CORREO] error enviando correo de confirmación para contrato %d: %v", contratoID, err)
|
||||
} else {
|
||||
log.Printf("[PAGO-CORREO] correo de confirmación enviado para contrato %d → %s", contratoID, contrato.Cliente.Email)
|
||||
}
|
||||
}
|
||||
|
||||
// ObtenerOCrearEnlacePago devuelve el enlace de pago vigente del contrato.
|
||||
// gateway puede ser "bold", "dlocal" o "ninguna"/"".
|
||||
// Si ya existe un enlace guardado, lo reutiliza (mismo ciclo).
|
||||
|
||||
Reference in New Issue
Block a user