Private
Public Access
133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package empresa
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
models_seguridad "repuestera_back/models/seguridad"
|
|
r "repuestera_back/repositorios"
|
|
"repuestera_back/services"
|
|
"repuestera_back/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ListarInvitaciones(c *gin.Context) {
|
|
invitaciones, err := r.ObtenerRepositorios().Empresa.ListarInvitaciones(utils.EmpresaID(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"invitaciones": invitaciones})
|
|
}
|
|
|
|
func CrearInvitacion(c *gin.Context) {
|
|
var req struct {
|
|
Email string `json:"email" binding:"required,email"`
|
|
Rol string `json:"rol"`
|
|
GrupoID uint `json:"grupo_id"`
|
|
ExpiraHoras int `json:"expira_horas"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
empresaID := utils.EmpresaID(c)
|
|
callerRol := models_seguridad.RolEmpresa(utils.EmpresaRol(c))
|
|
rol, err := rolSolicitado(c, callerRol, req.Rol)
|
|
if err != nil {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if req.GrupoID > 0 {
|
|
grupo, err := r.ObtenerRepositorios(c).Grupo.ObtenerPorID(req.GrupoID)
|
|
if err != nil || grupo.EmpresaID != empresaID {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Grupo inválido para la empresa"})
|
|
return
|
|
}
|
|
}
|
|
|
|
repo := r.ObtenerRepositorios()
|
|
email := strings.ToLower(strings.TrimSpace(req.Email))
|
|
|
|
usuario, err := repo.Usuario.ObtenerPorEmail(email)
|
|
if err == nil {
|
|
if _, err := repo.Empresa.Membresia(empresaID, usuario.ID); err == nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": "El usuario ya pertenece a esta empresa"})
|
|
return
|
|
}
|
|
}
|
|
|
|
expira := time.Now().Add(7 * 24 * time.Hour)
|
|
if req.ExpiraHoras > 0 {
|
|
if req.ExpiraHoras > 24*30 {
|
|
req.ExpiraHoras = 24 * 30
|
|
}
|
|
expira = time.Now().Add(time.Duration(req.ExpiraHoras) * time.Hour)
|
|
}
|
|
|
|
userID, _ := c.Get("userID")
|
|
creadorID, _ := userID.(uint)
|
|
|
|
invitacion := models_seguridad.InvitacionEmpresa{
|
|
EmpresaID: empresaID,
|
|
Email: email,
|
|
Rol: rol,
|
|
GrupoID: req.GrupoID,
|
|
ExpiraEn: expira,
|
|
CreadaPor: creadorID,
|
|
}
|
|
|
|
token, err := repo.Empresa.CrearInvitacion(&invitacion)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
empresa, err := repo.Empresa.ObtenerPorID(empresaID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
link := construirLinkInvitacion(token)
|
|
enviado := services.EnviarEmailInvitacion(email, empresa.Nombre, string(rol), link, creadorID)
|
|
|
|
c.JSON(http.StatusCreated, gin.H{
|
|
"invitacion": invitacion,
|
|
"token": token,
|
|
"link": link,
|
|
"email_enviado": enviado,
|
|
"expira_en": invitacion.ExpiraEn,
|
|
})
|
|
}
|
|
|
|
func EliminarInvitacion(c *gin.Context) {
|
|
parsed, 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.EliminarInvitacion(utils.EmpresaID(c), uint(parsed)); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Invitación cancelada"})
|
|
}
|
|
|
|
func construirLinkInvitacion(token string) string {
|
|
base := strings.TrimRight(os.Getenv("APP_URL"), "/")
|
|
if base == "" {
|
|
base = "http://localhost:3000"
|
|
}
|
|
return base + "/invitaciones/" + token
|
|
}
|