This commit is contained in:
Lizandro Guarnizo
2026-05-16 13:44:32 -05:00
parent 85595e639e
commit 76a0c28b16
16 changed files with 225 additions and 725 deletions
+12 -60
View File
@@ -8,7 +8,6 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/fiber-boilerplate/app"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
"github.com/sujit-baniya/fiber-boilerplate/pkg/services"
)
func PortalUsuariosIndex(c *fiber.Ctx) error {
@@ -30,14 +29,12 @@ func LoadPortalUsuarios(c *fiber.Ctx) error {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
clientes, _, _ := models.GetAllClientes(200, 0, "")
portalRoles, _ := models.GetPortalRoles()
return c.JSON(fiber.Map{
"items": items,
"total": total,
"totalPages": int(math.Ceil(float64(total) / float64(limit))),
"page": page,
"clientes": clientes,
"portalRoles": portalRoles,
"items": items,
"total": total,
"totalPages": int(math.Ceil(float64(total) / float64(limit))),
"page": page,
"clientes": clientes,
})
}
@@ -47,7 +44,7 @@ func CreatePortalUsuario(c *fiber.Ctx) error {
Email string `json:"email"`
Password string `json:"password"`
ClienteID *uint `json:"cliente_id"`
RoleID *uint `json:"role_id"`
Rol string `json:"rol"`
Notas string `json:"notas"`
}
var req Req
@@ -61,23 +58,15 @@ func CreatePortalUsuario(c *fiber.Ctx) error {
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Error al hashear contraseña"})
}
// Derivar Rol del tipo de rol seleccionado
rol := "cliente"
if req.RoleID != nil {
var role models.Roles
if err := app.Http.Database.DB.First(&role, *req.RoleID).Error; err == nil {
if role.EsPortalPartner {
rol = "partner"
}
}
if req.Rol == "" {
req.Rol = "cliente"
}
u := &models.PortalUser{
Nombre: req.Nombre,
Email: strings.ToLower(strings.TrimSpace(req.Email)),
Password: hashed,
ClienteID: req.ClienteID,
RoleID: req.RoleID,
Rol: rol,
Rol: req.Rol,
Activo: true,
Notas: req.Notas,
}
@@ -98,7 +87,7 @@ func UpdatePortalUsuario(c *fiber.Ctx) error {
Email string `json:"email"`
Password string `json:"password"`
ClienteID *uint `json:"cliente_id"`
RoleID *uint `json:"role_id"`
Rol string `json:"rol"`
Activo bool `json:"activo"`
Notas string `json:"notas"`
}
@@ -106,22 +95,11 @@ func UpdatePortalUsuario(c *fiber.Ctx) error {
if err := c.BodyParser(&req); err != nil {
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
}
// Derivar Rol del tipo de rol seleccionado
rol := "cliente"
if req.RoleID != nil {
var role models.Roles
if err := app.Http.Database.DB.First(&role, *req.RoleID).Error; err == nil {
if role.EsPortalPartner {
rol = "partner"
}
}
}
u := &models.PortalUser{
Nombre: req.Nombre,
Email: strings.ToLower(strings.TrimSpace(req.Email)),
ClienteID: req.ClienteID,
RoleID: req.RoleID,
Rol: rol,
Rol: req.Rol,
Activo: req.Activo,
Notas: req.Notas,
}
@@ -131,7 +109,7 @@ func UpdatePortalUsuario(c *fiber.Ctx) error {
}
// Actualizar password solo si se envió
if strings.TrimSpace(req.Password) != "" {
hashed, err := app.Http.Hash.Create(req.Password)
hashed, err := app.Http.Hash.Create(req.Password)
if err == nil {
_ = models.UpdatePortalUserPassword(uint(id), hashed)
}
@@ -173,29 +151,3 @@ func RemovePortalAcceso(c *fiber.Ctx) error {
}
return c.JSON(fiber.Map{"ok": true})
}
// SendPortalCredentials envía las credenciales de acceso al portal al correo del usuario.
func SendPortalCredentials(c *fiber.Ctx) error {
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
if err != nil {
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
}
u, err := models.GetPortalUserByID(uint(id))
if err != nil {
return c.Status(404).JSON(fiber.Map{"error": "Usuario no encontrado"})
}
type Req struct {
Password string `json:"password"`
}
var req Req
_ = c.BodyParser(&req)
password := req.Password
if password == "" {
password = "(la contraseña que configuraste)"
}
services.SendPortalCredentialsEmail(u.Email, u.Nombre, password)
return c.JSON(fiber.Map{"ok": true, "message": "Correo enviado a " + u.Email})
}