80 lines
2.7 KiB
Go
80 lines
2.7 KiB
Go
package models
|
|
|
|
import (
|
|
"github.com/sujit-baniya/fiber-boilerplate/app"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type NotificacionRegla struct {
|
|
gorm.Model
|
|
Nombre string `json:"nombre" gorm:"column:nombre"`
|
|
TipoEvento string `json:"tipo_evento" gorm:"column:tipo_evento;default:'vencimiento_proximo'"` // vencimiento_proximo | ya_vencido | bienvenida | pago_recibido | manual
|
|
DiasAntes int `json:"dias_antes" gorm:"column:dias_antes"`
|
|
PlantillaID uint `json:"plantilla_id" gorm:"column:plantilla_id"`
|
|
Plantilla PlantillaCorreo `json:"plantilla" gorm:"foreignKey:PlantillaID"`
|
|
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
|
AplicaA string `json:"aplica_a" gorm:"column:aplica_a;default:'todos'"` // todos | renovable | unico
|
|
PasarelaEnlace string `json:"pasarela_enlace" gorm:"column:pasarela_enlace;default:'bold'"` // bold | dlocal | ninguna
|
|
}
|
|
|
|
func (NotificacionRegla) TableName() string { return "notificacion_reglas" }
|
|
|
|
func GetAllReglas() ([]NotificacionRegla, error) {
|
|
var items []NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").Order("dias_antes DESC").Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetReglasActivas() ([]NotificacionRegla, error) {
|
|
var items []NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").
|
|
Where("activo = ?", true).Order("dias_antes DESC").Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetReglasByTipoEvento(tipoEvento string) ([]NotificacionRegla, error) {
|
|
var items []NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").
|
|
Where("activo = ? AND tipo_evento = ?", true, tipoEvento).
|
|
Order("dias_antes DESC").Find(&items).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetReglaByID(id uint) (*NotificacionRegla, error) {
|
|
var item NotificacionRegla
|
|
if err := app.Http.Database.DB.Preload("Plantilla").First(&item, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func CreateRegla(r NotificacionRegla) error {
|
|
return app.Http.Database.DB.Create(&r).Error
|
|
}
|
|
|
|
func UpdateRegla(r NotificacionRegla) error {
|
|
return app.Http.Database.DB.Model(&r).Updates(map[string]interface{}{
|
|
"nombre": r.Nombre,
|
|
"tipo_evento": r.TipoEvento,
|
|
"dias_antes": r.DiasAntes,
|
|
"plantilla_id": r.PlantillaID,
|
|
"activo": r.Activo,
|
|
"aplica_a": r.AplicaA,
|
|
"pasarela_enlace": r.PasarelaEnlace,
|
|
}).Error
|
|
}
|
|
|
|
func DeleteRegla(id uint) error {
|
|
var r NotificacionRegla
|
|
if err := app.Http.Database.DB.First(&r, id).Error; err != nil {
|
|
return err
|
|
}
|
|
return app.Http.Database.DB.Delete(&r).Error
|
|
}
|