Private
Public Access
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package empresa
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
dto_directors "repuestera_back/dto/Builders/Directors"
|
|
r "repuestera_back/repositorios"
|
|
"repuestera_back/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ActualizarEmpresa(c *gin.Context) {
|
|
var req struct {
|
|
Nombre string `json:"nombre"`
|
|
Slug string `json:"slug"`
|
|
Cuit string `json:"cuit"`
|
|
EmailAlertaStock *[]string `json:"email_alerta_stock"`
|
|
}
|
|
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(utils.EmpresaID(c))
|
|
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 strings.TrimSpace(req.Cuit) != "" {
|
|
cuit := strings.TrimSpace(req.Cuit)
|
|
empresa.Cuit = &cuit
|
|
}
|
|
if req.EmailAlertaStock != nil {
|
|
limpios := make([]string, 0, len(*req.EmailAlertaStock))
|
|
vistos := make(map[string]bool)
|
|
for _, email := range *req.EmailAlertaStock {
|
|
email = strings.TrimSpace(email)
|
|
if email == "" || vistos[email] {
|
|
continue
|
|
}
|
|
vistos[email] = true
|
|
limpios = append(limpios, email)
|
|
}
|
|
empresa.EmailAlertaStock = limpios
|
|
}
|
|
|
|
if err := repo.Empresa.Actualizar(empresa, *empresa); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
respuesta, err := dto_directors.NewEmpresaDirector().DesdeEmpresa(empresa)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, respuesta)
|
|
}
|