Private
Public Access
primeros cambios hechos para tener el multitenant
This commit is contained in:
1 parent
34ad4424f9
commit
4e082e8767
132 files changed
+6202
-762
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"repuestera_back/dto"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -14,9 +15,12 @@ import (
|
||||
|
||||
func Register(c *gin.Context) {
|
||||
var req struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password" binding:"required,min=8"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
EmpresaNombre string `json:"empresa_nombre"`
|
||||
EmpresaSlug string `json:"empresa_slug"`
|
||||
EmpresaCuit string `json:"empresa_cuit"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -24,44 +28,61 @@ func Register(c *gin.Context) {
|
||||
}
|
||||
|
||||
email := strings.ToLower(strings.TrimSpace(req.Email))
|
||||
repo := r.ObtenerRepositorios()
|
||||
|
||||
if _, err := r.ObtenerRepositorios().Usuario.ObtenerPorEmail(email); err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "El email ya esta registrado"})
|
||||
if _, err := repo.Usuario.ObtenerPorEmail(email); err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "El email ya está registrado"})
|
||||
return
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al procesar la contrasena"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al procesar la contraseña"})
|
||||
return
|
||||
}
|
||||
|
||||
user := models_seguridad.Usuario{
|
||||
Email: email,
|
||||
Password: string(hashedPassword),
|
||||
Name: req.Name,
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Activo: true,
|
||||
}
|
||||
|
||||
//checkeo para ver si hay usuarios añadidos
|
||||
var cantUsuarios int64
|
||||
r.ObtenerRepositorios().Usuario.CantidadUsuarios(&cantUsuarios)
|
||||
if (cantUsuarios < 1){
|
||||
admin, err := r.ObtenerRepositorios().Grupo.ObtenerPorID(1)
|
||||
if err != nil{
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Hay un problema con la base de datos: Faltan Grupos?"})
|
||||
return
|
||||
}
|
||||
user.Grupos = append(user.Grupos, *admin)
|
||||
}
|
||||
|
||||
if err := r.ObtenerRepositorios().Usuario.Crear(&user); err != nil {
|
||||
if err := repo.Usuario.Crear(&user); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, dto.RegisterResponse{
|
||||
ID: user.ID,
|
||||
Email: user.Email,
|
||||
Name: user.Name,
|
||||
})
|
||||
nombreEmpresa := strings.TrimSpace(req.EmpresaNombre)
|
||||
if nombreEmpresa == "" {
|
||||
nombreEmpresa = "Empresa de " + user.Name
|
||||
}
|
||||
empresa := models_seguridad.Empresa{
|
||||
Nombre: nombreEmpresa,
|
||||
Slug: req.EmpresaSlug,
|
||||
}
|
||||
if strings.TrimSpace(req.EmpresaCuit) != "" {
|
||||
cuit := strings.TrimSpace(req.EmpresaCuit)
|
||||
empresa.Cuit = &cuit
|
||||
}
|
||||
|
||||
if err := repo.Empresa.Crear(&empresa, user.ID); err != nil {
|
||||
// Si falla la creación de la empresa no dejamos una cuenta huérfana.
|
||||
_ = repo.Usuario.Eliminar(user.ID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// En una instalación nueva, el primer usuario registrado es el
|
||||
// superadministrador de la plataforma. Los siguientes ya no.
|
||||
if total, err := repo.Empresa.ContarPlataformaAdmins(); err == nil && total == 0 {
|
||||
_ = repo.Empresa.AgregarPlataformaAdmin(user.ID, models_seguridad.RolSuperadmin)
|
||||
}
|
||||
|
||||
respuesta, err := services.ConstruirSesion(user.ID, empresa.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusCreated, dto.RegisterResponse{ID: user.ID, Email: user.Email, Name: user.Name})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, respuesta)
|
||||
}
|
||||
Reference in new issue
Block a user