up
This commit is contained in:
+49
-36
@@ -14,20 +14,15 @@ import (
|
||||
// - Rol "partner": accede a los proyectos de todos sus ClienteIDs via PortalAccesos.
|
||||
type PortalUser struct {
|
||||
gorm.Model
|
||||
Nombre string `json:"nombre" gorm:"column:nombre;not null"`
|
||||
Email string `json:"email" gorm:"column:email;uniqueIndex;not null"`
|
||||
Password string `json:"-" gorm:"column:password;type:text"`
|
||||
ClienteID *uint `json:"cliente_id" gorm:"column:cliente_id;index"`
|
||||
Cliente *Cliente `json:"cliente" gorm:"foreignKey:ClienteID"`
|
||||
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"`
|
||||
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"`
|
||||
Nombre string `json:"nombre" gorm:"column:nombre;not null"`
|
||||
Email string `json:"email" gorm:"column:email;uniqueIndex;not null"`
|
||||
Password string `json:"-" gorm:"column:password;type:text"`
|
||||
ClienteID *uint `json:"cliente_id" gorm:"column:cliente_id;index"` // nil si es partner
|
||||
Cliente *Cliente `json:"cliente" gorm:"foreignKey:ClienteID"`
|
||||
Rol string `json:"rol" gorm:"column:rol;default:'cliente'"` // cliente|partner
|
||||
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"`
|
||||
}
|
||||
|
||||
func (PortalUser) TableName() string { return "portal_users" }
|
||||
@@ -48,7 +43,7 @@ func (PortalAcceso) TableName() string { return "portal_accesos" }
|
||||
func GetAllPortalUsers(limit, offset int) ([]PortalUser, int64, error) {
|
||||
var items []PortalUser
|
||||
var total int64
|
||||
db := app.Http.Database.DB.Model(&PortalUser{}).Preload("Cliente").Preload("PortalAccesos.Cliente").Preload("Role")
|
||||
db := app.Http.Database.DB.Model(&PortalUser{}).Preload("Cliente").Preload("PortalAccesos.Cliente")
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -60,7 +55,7 @@ func GetAllPortalUsers(limit, offset int) ([]PortalUser, int64, error) {
|
||||
|
||||
func GetPortalUserByID(id uint) (*PortalUser, error) {
|
||||
var item PortalUser
|
||||
err := app.Http.Database.DB.Preload("Cliente").Preload("PortalAccesos.Cliente").Preload("Role").First(&item, id).Error
|
||||
err := app.Http.Database.DB.Preload("Cliente").Preload("PortalAccesos.Cliente").First(&item, id).Error
|
||||
return &item, err
|
||||
}
|
||||
|
||||
@@ -80,12 +75,8 @@ func UpdatePortalUser(u *PortalUser) error {
|
||||
"email": u.Email,
|
||||
"cliente_id": u.ClienteID,
|
||||
"rol": u.Rol,
|
||||
"role_id": u.RoleID,
|
||||
"activo": u.Activo,
|
||||
"notas": u.Notas,
|
||||
"telegram_chat_id": u.TelegramChatID,
|
||||
"telefono": u.Telefono,
|
||||
"pais": u.Pais,
|
||||
"activo": u.Activo,
|
||||
"notas": u.Notas,
|
||||
}).Error
|
||||
}
|
||||
|
||||
@@ -101,6 +92,41 @@ func DeletePortalUser(id uint) error {
|
||||
return app.Http.Database.DB.Delete(&PortalUser{}, id).Error
|
||||
}
|
||||
|
||||
// GetPortalUsersByClienteID devuelve todos los portal_users activos asociados a un cliente,
|
||||
// ya sea directamente (rol cliente) o mediante PortalAcceso (rol partner).
|
||||
func GetPortalUsersByClienteID(clienteID uint) ([]PortalUser, error) {
|
||||
var result []PortalUser
|
||||
db := app.Http.Database.DB
|
||||
|
||||
// Usuarios directos del cliente
|
||||
var directos []PortalUser
|
||||
if err := db.Where("cliente_id = ? AND activo = true", clienteID).Find(&directos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, directos...)
|
||||
|
||||
// Usuarios partner con acceso al cliente via PortalAcceso
|
||||
var accesos []PortalAcceso
|
||||
if err := db.Where("cliente_id = ?", clienteID).Find(&accesos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen := make(map[uint]bool)
|
||||
for _, d := range directos {
|
||||
seen[d.ID] = true
|
||||
}
|
||||
for _, a := range accesos {
|
||||
if seen[a.PortalUserID] {
|
||||
continue
|
||||
}
|
||||
var u PortalUser
|
||||
if err := db.Where("id = ? AND activo = true", a.PortalUserID).First(&u).Error; err == nil {
|
||||
result = append(result, u)
|
||||
seen[u.ID] = true
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func AddPortalAcceso(portalUserID, clienteID uint) error {
|
||||
// Evitar duplicados
|
||||
var existing PortalAcceso
|
||||
@@ -121,11 +147,7 @@ func RemovePortalAcceso(portalUserID, clienteID uint) error {
|
||||
|
||||
// GetClienteIDsForPortalUser devuelve todos los clienteIDs accesibles para un portal user.
|
||||
func GetClienteIDsForPortalUser(u *PortalUser) []uint {
|
||||
isPartner := u.Rol == "partner"
|
||||
if u.Role != nil {
|
||||
isPartner = u.Role.EsPortalPartner
|
||||
}
|
||||
if isPartner {
|
||||
if u.Rol == "partner" {
|
||||
ids := make([]uint, 0, len(u.PortalAccesos))
|
||||
for _, a := range u.PortalAccesos {
|
||||
ids = append(ids, a.ClienteID)
|
||||
@@ -153,12 +175,3 @@ func CheckPortalLogin(email, password string) (*PortalUser, error) {
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// GetPortalUsersByClienteID devuelve todos los portal users activos de un cliente.
|
||||
func GetPortalUsersByClienteID(clienteID uint) ([]PortalUser, error) {
|
||||
var users []PortalUser
|
||||
err := app.Http.Database.DB.
|
||||
Where("cliente_id = ? AND activo = true", clienteID).
|
||||
Find(&users).Error
|
||||
return users, err
|
||||
}
|
||||
|
||||
@@ -124,11 +124,8 @@ func GetAllProyectos(limit, offset int, search string) ([]Proyecto, int64, error
|
||||
}
|
||||
|
||||
func GetProyectosByClienteIDs(clienteIDs []uint) ([]Proyecto, error) {
|
||||
if len(clienteIDs) == 0 {
|
||||
return []Proyecto{}, nil
|
||||
}
|
||||
var items []Proyecto
|
||||
err := app.Http.Database.DB.Preload("Cliente").Where("cliente_id IN ?", clienteIDs).Order("created_at DESC").Find(&items).Error
|
||||
err := app.Http.Database.DB.Where("cliente_id IN ?", clienteIDs).Order("created_at DESC").Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,7 @@ import (
|
||||
|
||||
type ProyectoTicket struct {
|
||||
gorm.Model
|
||||
ProyectoID uint `json:"proyecto_id" gorm:"column:proyecto_id;index"`
|
||||
Proyecto Proyecto `json:"proyecto" gorm:"foreignKey:ProyectoID"`
|
||||
ProyectoID uint `json:"proyecto_id" gorm:"column:proyecto_id;index"`
|
||||
PortalUserID uint `json:"portal_user_id" gorm:"column:portal_user_id;index"`
|
||||
AutorNombre string `json:"autor_nombre" gorm:"column:autor_nombre"`
|
||||
Titulo string `json:"titulo" gorm:"column:titulo"`
|
||||
@@ -68,13 +67,3 @@ func GetTicketsByPortalUser(portalUserID uint) ([]ProyectoTicket, error) {
|
||||
Preload("Mensajes").Order("created_at DESC").Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
func GetAllTickets(estado string) ([]ProyectoTicket, error) {
|
||||
var items []ProyectoTicket
|
||||
db := app.Http.Database.DB.Preload("Proyecto").Preload("Mensajes").Order("created_at DESC")
|
||||
if estado != "" && estado != "todos" {
|
||||
db = db.Where("estado = ?", estado)
|
||||
}
|
||||
err := db.Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user