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

@@ -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)
}
+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)
}
@@ -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"})
}
-204
View File
@@ -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"})
}
@@ -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)
}