up
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// ─── NotifEventoConfig ────────────────────────────────────────────────────────
|
||||
// Define qué canales activar para cada combinación evento × destinatario.
|
||||
//
|
||||
// Eventos disponibles (extensibles):
|
||||
// ticket_nuevo → admin recibe cuando el cliente abre un ticket
|
||||
// ticket_respuesta_cliente → admin recibe cuando el cliente responde
|
||||
// ticket_respuesta_admin → portal_user recibe cuando admin responde
|
||||
// factura_emitida → portal_user recibe cuando se emite factura
|
||||
// avance_publicado → portal_user recibe cuando se publica avance
|
||||
//
|
||||
// Destinatarios: "admin" | "portal_user"
|
||||
type NotifEventoConfig struct {
|
||||
gorm.Model
|
||||
Evento string `json:"evento" gorm:"column:evento;uniqueIndex:uidx_notif_ev_dest"`
|
||||
Destinatario string `json:"destinatario" gorm:"column:destinatario;uniqueIndex:uidx_notif_ev_dest"`
|
||||
CanalEmail bool `json:"canal_email" gorm:"column:canal_email;default:true"`
|
||||
CanalTelegram bool `json:"canal_telegram" gorm:"column:canal_telegram;default:false"`
|
||||
CanalSistema bool `json:"canal_sistema" gorm:"column:canal_sistema;default:true"`
|
||||
Descripcion string `json:"descripcion" gorm:"column:descripcion"`
|
||||
}
|
||||
|
||||
func (NotifEventoConfig) TableName() string { return "notif_evento_configs" }
|
||||
|
||||
// ─── SistemaNotificacion ──────────────────────────────────────────────────────
|
||||
// Notificaciones in-app visibles en el badge del header.
|
||||
// tipo_usuario = "admin" → visible para todos los admins (usuario_id = 0)
|
||||
// tipo_usuario = "portal_user" → visible solo para ese portal user
|
||||
type SistemaNotificacion struct {
|
||||
gorm.Model
|
||||
TipoUsuario string `json:"tipo_usuario" gorm:"column:tipo_usuario;index"`
|
||||
UsuarioID uint `json:"usuario_id" gorm:"column:usuario_id;index"`
|
||||
Titulo string `json:"titulo" gorm:"column:titulo"`
|
||||
Cuerpo string `json:"cuerpo" gorm:"column:cuerpo;type:text"`
|
||||
Leida bool `json:"leida" gorm:"column:leida;default:false"`
|
||||
Url string `json:"url" gorm:"column:url"`
|
||||
Icono string `json:"icono" gorm:"column:icono"` // emoji: 🎫 🧾 📦
|
||||
}
|
||||
|
||||
func (SistemaNotificacion) TableName() string { return "sistema_notificaciones" }
|
||||
|
||||
// ─── CRUD NotifEventoConfig ───────────────────────────────────────────────────
|
||||
|
||||
func GetAllNotifConfigs() ([]NotifEventoConfig, error) {
|
||||
var items []NotifEventoConfig
|
||||
err := app.Http.Database.DB.Order("evento, destinatario").Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
func GetNotifConfig(evento, destinatario string) *NotifEventoConfig {
|
||||
var item NotifEventoConfig
|
||||
if err := app.Http.Database.DB.
|
||||
Where("evento = ? AND destinatario = ?", evento, destinatario).
|
||||
First(&item).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
return &item
|
||||
}
|
||||
|
||||
// UpsertNotifConfig inserta o actualiza los canales para un evento × destinatario.
|
||||
func UpsertNotifConfig(cfg *NotifEventoConfig) error {
|
||||
return app.Http.Database.DB.
|
||||
Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "evento"}, {Name: "destinatario"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"canal_email", "canal_telegram", "canal_sistema"}),
|
||||
}).
|
||||
Create(cfg).Error
|
||||
}
|
||||
|
||||
// ─── CRUD SistemaNotificacion ─────────────────────────────────────────────────
|
||||
|
||||
func CreateSistemaNotif(n *SistemaNotificacion) error {
|
||||
return app.Http.Database.DB.Create(n).Error
|
||||
}
|
||||
|
||||
func GetSistemaNotifs(tipoUsuario string, usuarioID uint, soloNoLeidas bool) ([]SistemaNotificacion, error) {
|
||||
var items []SistemaNotificacion
|
||||
db := app.Http.Database.DB.Where("tipo_usuario = ? AND (usuario_id = ? OR usuario_id = 0)", tipoUsuario, usuarioID)
|
||||
if soloNoLeidas {
|
||||
db = db.Where("leida = false")
|
||||
}
|
||||
err := db.Order("created_at DESC").Limit(50).Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
func CountUnreadNotifs(tipoUsuario string, usuarioID uint) int64 {
|
||||
var count int64
|
||||
app.Http.Database.DB.Model(&SistemaNotificacion{}).
|
||||
Where("tipo_usuario = ? AND (usuario_id = ? OR usuario_id = 0) AND leida = false", tipoUsuario, usuarioID).
|
||||
Count(&count)
|
||||
return count
|
||||
}
|
||||
|
||||
func MarcarNotifLeida(id uint) error {
|
||||
return app.Http.Database.DB.Model(&SistemaNotificacion{}).
|
||||
Where("id = ?", id).Update("leida", true).Error
|
||||
}
|
||||
|
||||
func MarcarTodasLeidas(tipoUsuario string, usuarioID uint) error {
|
||||
return app.Http.Database.DB.Model(&SistemaNotificacion{}).
|
||||
Where("tipo_usuario = ? AND (usuario_id = ? OR usuario_id = 0)", tipoUsuario, usuarioID).
|
||||
Update("leida", true).Error
|
||||
}
|
||||
@@ -22,9 +22,12 @@ type PortalUser struct {
|
||||
Rol string `json:"rol" gorm:"column:rol;default:'cliente'"`
|
||||
RoleID *uint `json:"role_id" gorm:"column:role_id;index"`
|
||||
Role *Roles `json:"role" gorm:"foreignKey:RoleID"`
|
||||
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
||||
Notas string `json:"notas" gorm:"column:notas;type:text"`
|
||||
PortalAccesos []PortalAcceso `json:"portal_accesos" gorm:"foreignKey:PortalUserID"`
|
||||
Activo bool `json:"activo" gorm:"column:activo;default:true"`
|
||||
Notas string `json:"notas" gorm:"column:notas;type:text"`
|
||||
TelegramChatID string `json:"telegram_chat_id" gorm:"column:telegram_chat_id"`
|
||||
Telefono string `json:"telefono" gorm:"column:telefono"`
|
||||
Pais string `json:"pais" gorm:"column:pais"`
|
||||
PortalAccesos []PortalAcceso `json:"portal_accesos" gorm:"foreignKey:PortalUserID"`
|
||||
}
|
||||
|
||||
func (PortalUser) TableName() string { return "portal_users" }
|
||||
@@ -78,8 +81,11 @@ func UpdatePortalUser(u *PortalUser) error {
|
||||
"cliente_id": u.ClienteID,
|
||||
"rol": u.Rol,
|
||||
"role_id": u.RoleID,
|
||||
"activo": u.Activo,
|
||||
"notas": u.Notas,
|
||||
"activo": u.Activo,
|
||||
"notas": u.Notas,
|
||||
"telegram_chat_id": u.TelegramChatID,
|
||||
"telefono": u.Telefono,
|
||||
"pais": u.Pais,
|
||||
}).Error
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user