refactor: separados handlers empresaaaaaa

Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
fede committed 2026-11-22 20:09:09 -03:00
1 parent 4e7992ba2b
commit ff21aa4264
5 files changed
+239 -204

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
package plataforma
import (
"errors"
"net/http"
dto_directors "repuestera_back/dto/Builders/Directors"
models_seguridad "repuestera_back/models/seguridad"
r "repuestera_back/repositorios"
"repuestera_back/services"
"strings"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
func CrearEmpresa(c *gin.Context) {
var req struct {
Nombre string `json:"nombre" binding:"required"`
Slug string `json:"slug"`
Cuit string `json:"cuit"`
OwnerEmail string `json:"owner_email" binding:"required,email"`
OwnerName string `json:"owner_name"`
OwnerPassword string `json:"owner_password"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
repo := r.ObtenerRepositorios()
email := strings.ToLower(strings.TrimSpace(req.OwnerEmail))
owner, err := repo.Usuario.ObtenerPorEmail(email)
passwordTemporal := ""
creado := false
if errors.Is(err, gorm.ErrRecordNotFound) {
password := req.OwnerPassword
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 del owner 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
}
nombre := strings.TrimSpace(req.OwnerName)
if nombre == "" {
nombre = strings.Split(email, "@")[0]
}
owner = &models_seguridad.Usuario{
Email: email,
Name: nombre,
Password: string(hash),
Activo: true,
DebeCambiarPassword: true,
}
if err := repo.Usuario.Crear(owner); 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
}
empresa := models_seguridad.Empresa{
Nombre: strings.TrimSpace(req.Nombre),
Slug: strings.TrimSpace(req.Slug),
}
if strings.TrimSpace(req.Cuit) != "" {
cuit := strings.TrimSpace(req.Cuit)
empresa.Cuit = &cuit
}
if err := repo.Empresa.Crear(&empresa, owner.ID); err != nil {
if creado {
_ = repo.Usuario.Eliminar(owner.ID)
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
miembros, _ := repo.Empresa.ContarMiembros(empresa.ID)
owners, _ := repo.Empresa.ContarOwners(empresa.ID)
respuesta, err := dto_directors.NewEmpresaAdminDirector().Creada(
&empresa,
owner,
models_seguridad.RolOwner,
miembros,
owners,
passwordTemporal,
!creado,
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, respuesta)
}