Private
Public Access
205 lines
5.4 KiB
Go
205 lines
5.4 KiB
Go
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"})
|
|
}
|