Private
Public Access
commit parcial de controllers refactorizados y implementando builder
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
4704777050
commit
3825492899
11 files changed
+616
-1
No files matched your search
@@ -0,0 +1,117 @@
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user