Private
Public Access
118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
package miembros
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
dto_directors "repuestera_back/dto/Builders/Directors"
|
|
models_seguridad "repuestera_back/models/seguridad"
|
|
r "repuestera_back/repositorios"
|
|
"repuestera_back/services"
|
|
"repuestera_back/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CrearMiembro(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Email string `json:"email" binding:"required,email"`
|
|
Password string `json:"password"`
|
|
Rol string `json:"rol"`
|
|
GrupoID uint `json:"grupo_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
empresaID := utils.EmpresaID(c)
|
|
callerRol := models_seguridad.RolEmpresa(utils.EmpresaRol(c))
|
|
rol, err := RolSolicitado(c, callerRol, req.Rol)
|
|
if err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if req.GrupoID > 0 {
|
|
grupo, err := r.ObtenerRepositorios(c).Grupo.ObtenerPorID(req.GrupoID)
|
|
if err != nil || grupo.EmpresaID != empresaID {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Grupo inválido para la empresa"})
|
|
return
|
|
}
|
|
}
|
|
|
|
repo := r.ObtenerRepositorios()
|
|
email := strings.ToLower(strings.TrimSpace(req.Email))
|
|
|
|
usuario, err := repo.Usuario.ObtenerPorEmail(email)
|
|
passwordTemporal := ""
|
|
creado := false
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
password := req.Password
|
|
if password == "" {
|
|
password, err = services.GenerarPasswordTemporal()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "No se pudo generar la contraseña"})
|
|
return
|
|
}
|
|
passwordTemporal = password
|
|
}
|
|
if len(password) < 8 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "La contraseña debe tener al menos 8 caracteres"})
|
|
return
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al procesar la contraseña"})
|
|
return
|
|
}
|
|
|
|
usuario = &models_seguridad.Usuario{
|
|
Email: email,
|
|
Name: strings.TrimSpace(req.Name),
|
|
Password: string(hash),
|
|
Activo: true,
|
|
DebeCambiarPassword: true,
|
|
}
|
|
if err := repo.Usuario.Crear(usuario); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
creado = true
|
|
} else if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
} else {
|
|
if _, err := repo.Empresa.Membresia(empresaID, usuario.ID); err == nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": "El usuario ya pertenece a esta empresa"})
|
|
return
|
|
}
|
|
}
|
|
|
|
if err := repo.Empresa.AgregarMiembro(empresaID, usuario.ID, rol, req.GrupoID); err != nil {
|
|
if creado {
|
|
_ = repo.Usuario.Eliminar(usuario.ID)
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
respuesta, err := dto_directors.NewMiembroDirector().Creado(models_seguridad.EmpresaUsuario{
|
|
Usuario: *usuario,
|
|
Rol: rol,
|
|
Activo: true,
|
|
}, passwordTemporal, !creado)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, respuesta)
|
|
}
|