Private
Public Access
126 lines
3.4 KiB
Go
126 lines
3.4 KiB
Go
package miembros
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
dto_directors "repuestera_back/dto/Builders/Directors"
|
|
models_seguridad "repuestera_back/models/seguridad"
|
|
r "repuestera_back/repositorios"
|
|
"repuestera_back/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ActualizarMiembro(c *gin.Context) {
|
|
usuarioID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
Rol string `json:"rol"`
|
|
GrupoID uint `json:"grupo_id"`
|
|
Activo *bool `json:"activo"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
empresaID := utils.EmpresaID(c)
|
|
repo := r.ObtenerRepositorios()
|
|
callerRol := models_seguridad.RolEmpresa(utils.EmpresaRol(c))
|
|
|
|
miembro, err := repo.Empresa.Membresia(empresaID, uint(usuarioID))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "El usuario no pertenece a esta empresa"})
|
|
return
|
|
}
|
|
|
|
if err := PuedeModificarMiembro(c, callerRol, miembro.Rol); err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
usuario, err := repo.Usuario.ObtenerPorID(uint(usuarioID))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Usuario no encontrado"})
|
|
return
|
|
}
|
|
|
|
if req.Name != "" {
|
|
usuario.Name = strings.TrimSpace(req.Name)
|
|
}
|
|
if req.Email != "" {
|
|
email := strings.ToLower(strings.TrimSpace(req.Email))
|
|
if email != usuario.Email {
|
|
if existente, err := repo.Usuario.ObtenerPorEmail(email); err == nil && existente.ID != usuario.ID {
|
|
c.JSON(http.StatusConflict, gin.H{"error": "El email ya está registrado"})
|
|
return
|
|
}
|
|
usuario.Email = email
|
|
}
|
|
}
|
|
if err := repo.Usuario.Guardar(usuario); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if req.Rol != "" {
|
|
nuevoRol, err := RolSolicitado(c, callerRol, req.Rol)
|
|
if err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if miembro.Rol == models_seguridad.RolOwner && nuevoRol != models_seguridad.RolOwner {
|
|
if err := ValidarUltimoOwner(empresaID); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
}
|
|
if err := repo.Empresa.ActualizarRolMiembro(empresaID, usuario.ID, nuevoRol); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
miembro.Rol = nuevoRol
|
|
}
|
|
|
|
if req.Activo != nil {
|
|
if !*req.Activo && miembro.Rol == models_seguridad.RolOwner {
|
|
if err := ValidarUltimoOwner(empresaID); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
}
|
|
if *req.Activo {
|
|
if err := repo.Empresa.AgregarMiembro(empresaID, usuario.ID, miembro.Rol, 0); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
} else {
|
|
if err := repo.Empresa.DesactivarMiembro(empresaID, usuario.ID); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
miembro.Usuario = *usuario
|
|
if req.Activo != nil {
|
|
miembro.Activo = *req.Activo
|
|
}
|
|
|
|
respuesta, err := dto_directors.NewMiembroDirector().DesdeMembresia(*miembro)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, respuesta)
|
|
}
|