From ff21aa426459eed3187d71b3b1cd6f6f3e1bed81 Mon Sep 17 00:00:00 2001 From: fede Date: Sun, 22 Nov 2026 20:09:00 -0300 Subject: [PATCH] refactor: separados handlers empresaaaaaa Signed-off-by: fede --- .../plataforma/ActualizarEmpresa.go | 71 ++++++ back/controllers/plataforma/CrearEmpresa.go | 112 ++++++++++ .../plataforma/DesactivarEmpresa.go | 23 ++ back/controllers/plataforma/Empresas.go | 204 ------------------ back/controllers/plataforma/ListarEmpresas.go | 33 +++ 5 files changed, 239 insertions(+), 204 deletions(-) create mode 100644 back/controllers/plataforma/ActualizarEmpresa.go create mode 100644 back/controllers/plataforma/CrearEmpresa.go create mode 100644 back/controllers/plataforma/DesactivarEmpresa.go delete mode 100644 back/controllers/plataforma/Empresas.go create mode 100644 back/controllers/plataforma/ListarEmpresas.go diff --git a/back/controllers/plataforma/ActualizarEmpresa.go b/back/controllers/plataforma/ActualizarEmpresa.go new file mode 100644 index 0000000..fd98c64 --- /dev/null +++ b/back/controllers/plataforma/ActualizarEmpresa.go @@ -0,0 +1,71 @@ +package plataforma + +import ( + "net/http" + dto_directors "repuestera_back/dto/Builders/Directors" + r "repuestera_back/repositorios" + "strconv" + "strings" + + "github.com/gin-gonic/gin" +) + +func ActualizarEmpresa(c *gin.Context) { + id, 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 { + Nombre string `json:"nombre"` + Slug string `json:"slug"` + Cuit *string `json:"cuit"` + Activa *bool `json:"activa"` + } + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + repo := r.ObtenerRepositorios() + empresa, err := repo.Empresa.ObtenerPorID(uint(id)) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"error": "Empresa no encontrada"}) + return + } + + if strings.TrimSpace(req.Nombre) != "" { + empresa.Nombre = strings.TrimSpace(req.Nombre) + } + if strings.TrimSpace(req.Slug) != "" { + empresa.Slug = strings.TrimSpace(req.Slug) + } + if req.Cuit != nil { + cuit := strings.TrimSpace(*req.Cuit) + if cuit == "" { + empresa.Cuit = nil + } else { + empresa.Cuit = &cuit + } + } + if req.Activa != nil { + empresa.Activa = *req.Activa + } + + if err := repo.Empresa.Guardar(empresa); err != nil { + 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().DesdeEmpresa(empresa, miembros, owners) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, respuesta) +} diff --git a/back/controllers/plataforma/CrearEmpresa.go b/back/controllers/plataforma/CrearEmpresa.go new file mode 100644 index 0000000..568fd2b --- /dev/null +++ b/back/controllers/plataforma/CrearEmpresa.go @@ -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) +} diff --git a/back/controllers/plataforma/DesactivarEmpresa.go b/back/controllers/plataforma/DesactivarEmpresa.go new file mode 100644 index 0000000..7f324a6 --- /dev/null +++ b/back/controllers/plataforma/DesactivarEmpresa.go @@ -0,0 +1,23 @@ +package plataforma + +import ( + "net/http" + r "repuestera_back/repositorios" + "strconv" + + "github.com/gin-gonic/gin" +) + +func DesactivarEmpresa(c *gin.Context) { + id, err := strconv.ParseUint(c.Param("id"), 10, 64) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"}) + return + } + + if err := r.ObtenerRepositorios().Empresa.Desactivar(uint(id)); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, gin.H{"message": "Empresa desactivada"}) +} diff --git a/back/controllers/plataforma/Empresas.go b/back/controllers/plataforma/Empresas.go deleted file mode 100644 index 1338a56..0000000 --- a/back/controllers/plataforma/Empresas.go +++ /dev/null @@ -1,204 +0,0 @@ -package plataforma - -import ( - "errors" - "net/http" - "strconv" - "strings" - - models_seguridad "repuestera_back/models/seguridad" - r "repuestera_back/repositorios" - "repuestera_back/services" - - "github.com/gin-gonic/gin" - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type EmpresaAdminResponse struct { - ID uint `json:"id"` - Nombre string `json:"nombre"` - Slug string `json:"slug"` - Cuit *string `json:"cuit,omitempty"` - Activa bool `json:"activa"` - Miembros int64 `json:"miembros"` - Owners int64 `json:"owners"` - CreatedAt any `json:"created_at,omitempty"` -} - -func ListarEmpresas(c *gin.Context) { - repo := r.ObtenerRepositorios() - empresas, err := repo.Empresa.ListarTodas() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - - respuesta := make([]EmpresaAdminResponse, 0, len(empresas)) - for _, empresa := range empresas { - miembros, _ := repo.Empresa.ContarMiembros(empresa.ID) - owners, _ := repo.Empresa.ContarOwners(empresa.ID) - respuesta = append(respuesta, EmpresaAdminResponse{ - ID: empresa.ID, - Nombre: empresa.Nombre, - Slug: empresa.Slug, - Cuit: empresa.Cuit, - Activa: empresa.Activa, - Miembros: miembros, - Owners: owners, - CreatedAt: empresa.CreatedAt, - }) - } - - c.JSON(http.StatusOK, gin.H{"empresas": respuesta, "total": len(respuesta)}) -} - -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 - } - - c.JSON(http.StatusCreated, gin.H{ - "empresa": empresa, - "owner": owner, - "password_temporal": passwordTemporal, - "owner_existente": !creado, - }) -} - -func ActualizarEmpresa(c *gin.Context) { - id, 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 { - Nombre string `json:"nombre"` - Slug string `json:"slug"` - Cuit *string `json:"cuit"` - Activa *bool `json:"activa"` - } - if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - - repo := r.ObtenerRepositorios() - empresa, err := repo.Empresa.ObtenerPorID(uint(id)) - if err != nil { - c.JSON(http.StatusNotFound, gin.H{"error": "Empresa no encontrada"}) - return - } - - if strings.TrimSpace(req.Nombre) != "" { - empresa.Nombre = strings.TrimSpace(req.Nombre) - } - if strings.TrimSpace(req.Slug) != "" { - empresa.Slug = strings.TrimSpace(req.Slug) - } - if req.Cuit != nil { - cuit := strings.TrimSpace(*req.Cuit) - if cuit == "" { - empresa.Cuit = nil - } else { - empresa.Cuit = &cuit - } - } - if req.Activa != nil { - empresa.Activa = *req.Activa - } - - if err := repo.Empresa.Guardar(empresa); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - - c.JSON(http.StatusOK, empresa) -} - -func DesactivarEmpresa(c *gin.Context) { - id, err := strconv.ParseUint(c.Param("id"), 10, 64) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"}) - return - } - - if err := r.ObtenerRepositorios().Empresa.Desactivar(uint(id)); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - c.JSON(http.StatusOK, gin.H{"message": "Empresa desactivada"}) -} diff --git a/back/controllers/plataforma/ListarEmpresas.go b/back/controllers/plataforma/ListarEmpresas.go new file mode 100644 index 0000000..18d5b8f --- /dev/null +++ b/back/controllers/plataforma/ListarEmpresas.go @@ -0,0 +1,33 @@ +package plataforma + +import ( + "net/http" + dto_directors "repuestera_back/dto/Builders/Directors" + r "repuestera_back/repositorios" + + "github.com/gin-gonic/gin" +) + +func ListarEmpresas(c *gin.Context) { + repo := r.ObtenerRepositorios() + empresas, err := repo.Empresa.ListarTodas() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + contadores := make(map[uint]dto_directors.ContadoresEmpresa, len(empresas)) + for _, empresa := range empresas { + miembros, _ := repo.Empresa.ContarMiembros(empresa.ID) + owners, _ := repo.Empresa.ContarOwners(empresa.ID) + contadores[empresa.ID] = dto_directors.ContadoresEmpresa{Miembros: miembros, Owners: owners} + } + + respuesta, err := dto_directors.NewEmpresaAdminDirector().Listar(empresas, contadores) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, respuesta) +}