Private
Public Access
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package empresa
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"repuestera_back/controllers/empresa/miembros"
|
|
dto_directors "repuestera_back/dto/Builders/Directors"
|
|
models_seguridad "repuestera_back/models/seguridad"
|
|
r "repuestera_back/repositorios"
|
|
"repuestera_back/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func AltaMiembro(c *gin.Context) {
|
|
usuarioID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
|
return
|
|
}
|
|
|
|
empresaID := utils.EmpresaID(c)
|
|
repo := r.ObtenerRepositorios()
|
|
callerRol := models_seguridad.RolEmpresa(utils.EmpresaRol(c))
|
|
|
|
miembro, err := repo.Empresa.MiembrosIncluyendoEliminados(empresaID, uint(usuarioID))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "El usuario no pertenece a esta empresa"})
|
|
return
|
|
}
|
|
|
|
if err := miembros.PuedeModificarMiembro(c, callerRol, miembro.Rol); err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if !miembro.DeletedAt.Valid {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "El miembro ya está activo"})
|
|
return
|
|
}
|
|
|
|
if err := repo.Empresa.RestaurarMiembro(empresaID, uint(usuarioID)); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
miembro.DeletedAt = gorm.DeletedAt{}
|
|
miembro.Activo = true
|
|
|
|
respuesta, err := dto_directors.NewMiembroDirector().DesdeMembresia(*miembro)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, respuesta)
|
|
}
|