Private
Public Access
commit parcial de implementacion de builder
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
f2cf7927eb
commit
4704777050
53 files changed
+1054
-863
No files matched your search
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"repuestera_back/dto"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
"repuestera_back/jwtutils"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
@@ -28,21 +28,15 @@ func ObtenerInvitacion(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
expirada := time.Now().After(invitacion.ExpiraEn)
|
||||
usada := invitacion.UsadaEn != nil
|
||||
_, errUsuario := repo.Usuario.ObtenerPorEmail(invitacion.Email)
|
||||
|
||||
c.JSON(http.StatusOK, dto.InvitacionInfo{
|
||||
Email: invitacion.Email,
|
||||
Empresa: invitacion.Empresa.Nombre,
|
||||
EmpresaID: invitacion.EmpresaID,
|
||||
Rol: string(invitacion.Rol),
|
||||
ExpiraEn: invitacion.ExpiraEn,
|
||||
Valida: !expirada && !usada && invitacion.Empresa.Activa,
|
||||
Usada: usada,
|
||||
Expirada: expirada,
|
||||
UsuarioExistente: errUsuario == nil,
|
||||
})
|
||||
respuesta, err := dto_directors.NewInvitacionDirector().Info(invitacion, errUsuario == nil)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
|
||||
// AceptarInvitacion suma al usuario a la empresa. Si la cuenta no existe, la
|
||||
@@ -143,11 +137,12 @@ func AceptarInvitacion(c *gin.Context) {
|
||||
|
||||
respuesta, err := services.ConstruirSesion(usuario.ID, invitacion.EmpresaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Invitación aceptada",
|
||||
"usuario_id": usuario.ID,
|
||||
"empresa_id": invitacion.EmpresaID,
|
||||
})
|
||||
aceptada, buildErr := dto_directors.NewInvitacionDirector().Aceptada(usuario.ID, invitacion.EmpresaID)
|
||||
if buildErr != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": buildErr.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, aceptada)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+17
-24
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"repuestera_back/dto"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/services"
|
||||
"repuestera_back/utils"
|
||||
@@ -25,23 +26,19 @@ func Me(c *gin.Context) {
|
||||
}
|
||||
|
||||
empresaID := utils.EmpresaID(c)
|
||||
respuesta, err := services.ConstruirSesion(id, empresaID)
|
||||
sesion, err := services.ConstruirSesion(id, empresaID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, dto.MeResponse{
|
||||
ID: respuesta.Usuario.ID,
|
||||
Email: respuesta.Usuario.Email,
|
||||
Name: respuesta.Usuario.Name,
|
||||
Permissions: respuesta.Permissions,
|
||||
Empresa: respuesta.Empresa,
|
||||
Empresas: respuesta.Empresas,
|
||||
EmpresaRol: respuesta.EmpresaRol,
|
||||
Activo: respuesta.Usuario.Activo,
|
||||
DebeCambiarPassword: respuesta.DebeCambiarPassword,
|
||||
})
|
||||
respuesta, err := dto_directors.NewMeDirector().DesdeSesion(sesion)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
|
||||
// ObtenerMisEmpresas devuelve las empresas donde el usuario es miembro.
|
||||
@@ -55,9 +52,7 @@ func ObtenerMisEmpresas(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"empresas": empresas,
|
||||
})
|
||||
c.JSON(http.StatusOK, dto.EmpresasResponse{Empresas: empresas})
|
||||
}
|
||||
|
||||
// CambiarEmpresa emite una sesión nueva para otra empresa del usuario.
|
||||
@@ -96,13 +91,11 @@ func ObtenerEmpresaActual(c *gin.Context) {
|
||||
|
||||
miembros, _ := r.ObtenerRepositorios().Empresa.ContarMiembros(empresa.ID)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"id": empresa.ID,
|
||||
"nombre": empresa.Nombre,
|
||||
"slug": empresa.Slug,
|
||||
"cuit": empresa.Cuit,
|
||||
"activa": empresa.Activa,
|
||||
"rol": string(rol),
|
||||
"miembros_activos": miembros,
|
||||
})
|
||||
respuesta, err := dto_directors.NewEmpresaActualDirector().DesdeEmpresa(empresa, rol, miembros)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"repuestera_back/dto"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/services"
|
||||
@@ -74,7 +74,12 @@ func Register(c *gin.Context) {
|
||||
|
||||
respuesta, err := services.ConstruirSesion(user.ID, empresa.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusCreated, dto.RegisterResponse{ID: user.ID, Email: user.Email, Name: user.Name})
|
||||
registro, buildErr := dto_directors.NewRegisterDirector().DesdeUsuario(&user)
|
||||
if buildErr != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": buildErr.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, registro)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
@@ -47,5 +48,11 @@ func AltaMiembro(c *gin.Context) {
|
||||
miembro.DeletedAt = gorm.DeletedAt{}
|
||||
miembro.Activo = true
|
||||
|
||||
c.JSON(http.StatusOK, respuestaMiembro(*miembro))
|
||||
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)
|
||||
}
|
||||
@@ -1,392 +0,0 @@
|
||||
package empresa
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"repuestera_back/dto"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/services"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type rolRequest struct {
|
||||
Rol string `json:"rol"`
|
||||
}
|
||||
|
||||
func respuestaMiembro(miembro models_seguridad.EmpresaUsuario) dto.MiembroResponse {
|
||||
respuesta := dto.MiembroResponse{
|
||||
ID: miembro.Usuario.ID,
|
||||
Email: miembro.Usuario.Email,
|
||||
Name: miembro.Usuario.Name,
|
||||
Activo: miembro.Usuario.Activo,
|
||||
DebeCambiarPassword: miembro.Usuario.DebeCambiarPassword,
|
||||
Rol: string(miembro.Rol),
|
||||
MembresiaActiva: miembro.Activo && !miembro.DeletedAt.Valid,
|
||||
CreatedAt: miembro.Usuario.CreatedAt,
|
||||
UpdatedAt: miembro.Usuario.UpdatedAt,
|
||||
}
|
||||
if miembro.DeletedAt.Valid {
|
||||
deletedAt := miembro.DeletedAt.Time
|
||||
respuesta.DeletedAt = &deletedAt
|
||||
}
|
||||
return respuesta
|
||||
}
|
||||
|
||||
func ObtenerMiembros(c *gin.Context) {
|
||||
empresaID := utils.EmpresaID(c)
|
||||
pagina, _ := strconv.Atoi(c.DefaultQuery("p", "0"))
|
||||
tamano, _ := strconv.Atoi(c.DefaultQuery("s", "10"))
|
||||
|
||||
miembros, total, err := r.ObtenerRepositorios().Empresa.ListarMiembros(empresaID, pagina, tamano)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
respuesta := make([]dto.MiembroResponse, 0, len(miembros))
|
||||
for _, miembro := range miembros {
|
||||
respuesta = append(respuesta, respuestaMiembro(miembro))
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"usuarios": respuesta,
|
||||
"miembros": respuesta,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
func ObtenerMiembro(c *gin.Context) {
|
||||
empresaID := utils.EmpresaID(c)
|
||||
usuarioID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
||||
return
|
||||
}
|
||||
|
||||
miembro, err := r.ObtenerRepositorios().Empresa.Membresia(empresaID, uint(usuarioID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "El usuario no pertenece a esta empresa"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuestaMiembro(*miembro))
|
||||
}
|
||||
|
||||
func CrearMiembro(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Password string `json:"password"`
|
||||
Rol string `json:"rol"`
|
||||
GrupoID uint `json:"grupo_id"`
|
||||
}
|
||||
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)
|
||||
passwordTemporal := ""
|
||||
creado := false
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
password := req.Password
|
||||
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 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
|
||||
}
|
||||
|
||||
usuario = &models_seguridad.Usuario{
|
||||
Email: email,
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Password: string(hash),
|
||||
Activo: true,
|
||||
DebeCambiarPassword: true,
|
||||
}
|
||||
if err := repo.Usuario.Crear(usuario); 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
|
||||
} else {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if err := repo.Empresa.AgregarMiembro(empresaID, usuario.ID, rol, req.GrupoID); err != nil {
|
||||
if creado {
|
||||
_ = repo.Usuario.Eliminar(usuario.ID)
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"usuario": respuestaMiembro(models_seguridad.EmpresaUsuario{
|
||||
Usuario: *usuario,
|
||||
Rol: rol,
|
||||
Activo: true,
|
||||
}),
|
||||
"password_temporal": passwordTemporal,
|
||||
"usuario_existente": !creado,
|
||||
})
|
||||
}
|
||||
|
||||
func ActualizarMiembro(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
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Rol string `json:"rol"`
|
||||
GrupoID uint `json:"grupo_id"`
|
||||
Activo *bool `json:"activo"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
empresaID := utils.EmpresaID(c)
|
||||
repo := r.ObtenerRepositorios()
|
||||
callerRol := models_seguridad.RolEmpresa(utils.EmpresaRol(c))
|
||||
|
||||
miembro, err := repo.Empresa.Membresia(empresaID, uint(usuarioID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "El usuario no pertenece a esta empresa"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := puedeModificarMiembro(c, callerRol, miembro.Rol); err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
usuario, err := repo.Usuario.ObtenerPorID(uint(usuarioID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Usuario no encontrado"})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Name != "" {
|
||||
usuario.Name = strings.TrimSpace(req.Name)
|
||||
}
|
||||
if req.Email != "" {
|
||||
email := strings.ToLower(strings.TrimSpace(req.Email))
|
||||
if email != usuario.Email {
|
||||
if existente, err := repo.Usuario.ObtenerPorEmail(email); err == nil && existente.ID != usuario.ID {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "El email ya está registrado"})
|
||||
return
|
||||
}
|
||||
usuario.Email = email
|
||||
}
|
||||
}
|
||||
if err := repo.Usuario.Guardar(usuario); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Rol != "" {
|
||||
nuevoRol, err := rolSolicitado(c, callerRol, req.Rol)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if miembro.Rol == models_seguridad.RolOwner && nuevoRol != models_seguridad.RolOwner {
|
||||
if err := validarUltimoOwner(empresaID); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := repo.Empresa.ActualizarRolMiembro(empresaID, usuario.ID, nuevoRol); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
miembro.Rol = nuevoRol
|
||||
}
|
||||
|
||||
if req.Activo != nil {
|
||||
if !*req.Activo && miembro.Rol == models_seguridad.RolOwner {
|
||||
if err := validarUltimoOwner(empresaID); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
if *req.Activo {
|
||||
if err := repo.Empresa.AgregarMiembro(empresaID, usuario.ID, miembro.Rol, 0); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := repo.Empresa.DesactivarMiembro(empresaID, usuario.ID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
miembro.Usuario = *usuario
|
||||
if req.Activo != nil {
|
||||
miembro.Activo = *req.Activo
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuestaMiembro(*miembro))
|
||||
}
|
||||
|
||||
func EliminarMiembro(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.Membresia(empresaID, uint(usuarioID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "El usuario no pertenece a esta empresa"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := puedeModificarMiembro(c, callerRol, miembro.Rol); err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if miembro.Rol == models_seguridad.RolOwner {
|
||||
if err := validarUltimoOwner(empresaID); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := repo.Empresa.EliminarMiembro(empresaID, uint(usuarioID)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "El usuario fue removido de la empresa"})
|
||||
}
|
||||
|
||||
func ActualizarEmpresa(c *gin.Context) {
|
||||
var req struct {
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit string `json:"cuit"`
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
input := models_seguridad.Empresa{Nombre: strings.TrimSpace(req.Nombre), Slug: strings.TrimSpace(req.Slug)}
|
||||
if req.Cuit != "" {
|
||||
cuit := strings.TrimSpace(req.Cuit)
|
||||
input.Cuit = &cuit
|
||||
}
|
||||
|
||||
if err := repo.Empresa.Actualizar(empresa, input); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, empresa)
|
||||
}
|
||||
|
||||
func rolSolicitado(c *gin.Context, callerRol models_seguridad.RolEmpresa, solicitado string) (models_seguridad.RolEmpresa, error) {
|
||||
rol := models_seguridad.RolEmpresa(strings.ToUpper(strings.TrimSpace(solicitado)))
|
||||
if rol == "" {
|
||||
rol = models_seguridad.RolMiembro
|
||||
}
|
||||
if !rol.Valido() {
|
||||
return "", errors.New("rol inválido")
|
||||
}
|
||||
|
||||
if rol == models_seguridad.RolOwner && callerRol != models_seguridad.RolOwner {
|
||||
return "", errors.New("solo el owner puede asignar el rol OWNER")
|
||||
}
|
||||
return rol, nil
|
||||
}
|
||||
|
||||
func puedeModificarMiembro(c *gin.Context, callerRol, targetRol models_seguridad.RolEmpresa) error {
|
||||
if callerRol == models_seguridad.RolOwner {
|
||||
return nil
|
||||
}
|
||||
if targetRol == models_seguridad.RolOwner {
|
||||
return errors.New("solo el owner puede administrar a otro owner")
|
||||
}
|
||||
if callerRol == models_seguridad.RolAdmin && targetRol == models_seguridad.RolAdmin {
|
||||
return errors.New("un administrador no puede modificar a otro administrador")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validarUltimoOwner(empresaID uint) error {
|
||||
owners, err := r.ObtenerRepositorios().Empresa.ContarOwners(empresaID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if owners <= 1 {
|
||||
return errors.New("la empresa debe conservar al menos un owner activo")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
@@ -28,5 +29,11 @@ func ObtenerFactura(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, factura)
|
||||
respuesta, err := dto_directors.NewFacturaDirector().DesdeFactura(factura)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
@@ -43,7 +44,14 @@ func ObtenerFacturas(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, facturas)
|
||||
|
||||
respuesta, err := dto_directors.NewFacturaDirector().Listar(facturas)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
|
||||
func fechaQuery(valor string) (*time.Time, error) {
|
||||
|
||||
@@ -2,9 +2,11 @@ package grupo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -25,6 +27,16 @@ func ActualizarGrupo(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
r.ObtenerRepositorios(c).Grupo.Actualizar(grupo, input)
|
||||
c.JSON(http.StatusOK, grupo)
|
||||
if err := r.ObtenerRepositorios(c).Grupo.Actualizar(grupo, input); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
respuesta, err := dto_directors.NewGrupoDirector().DesdeGrupo(grupo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package grupo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
@@ -18,5 +20,12 @@ func CrearGrupo(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, grupo)
|
||||
|
||||
respuesta, err := dto_directors.NewGrupoDirector().DesdeGrupo(&grupo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, respuesta)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package grupo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
@@ -21,9 +21,11 @@ func ObtenerGrupo(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
dto := dto_builders.
|
||||
NewGrupoSimpleBuilder(grupo).
|
||||
Build()
|
||||
respuesta, err := dto_directors.NewGrupoSimpleDirector().DesdeGrupo(grupo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, dto)
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package grupo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -13,5 +15,12 @@ func ObtenerGrupos(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, grupos)
|
||||
|
||||
respuesta, err := dto_directors.NewGrupoDirector().Listar(grupos)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,9 +2,11 @@ package grupo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -13,7 +15,7 @@ func ObtenerGruposDeUsuario(c *gin.Context) {
|
||||
|
||||
usuarioID, err := strconv.ParseUint(usuarioIDStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "id invalido"})
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "id invalido"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -22,5 +24,12 @@ func ObtenerGruposDeUsuario(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "no se ha podido obtener los grupos del usuario"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, grupos)
|
||||
|
||||
respuesta, err := dto_directors.NewGrupoDirector().Listar(grupos)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,8 +2,9 @@ package grupo
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"repuestera_back/dto"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -40,20 +41,28 @@ func ObtenerGruposYPermisosPorEmail(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
grupos := make([]dto.GrupoSimpleDTO, 0, len(usuario.Grupos))
|
||||
|
||||
grupos := make([]*models_seguridad.Grupo, 0, len(usuario.Grupos))
|
||||
for i := range usuario.Grupos {
|
||||
grupoCompleto, err := r.ObtenerRepositorios(c).Grupo.ObtenerPorIDConRelaciones(usuario.Grupos[i].ID, true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
g := dto_builders.NewGrupoSimpleBuilder(grupoCompleto).Build()
|
||||
g.ID = grupoCompleto.ID
|
||||
grupos = append(grupos, g)
|
||||
grupos = append(grupos, grupoCompleto)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, dto.GruposYPermisosResponse{
|
||||
Email: usuario.Email,
|
||||
Groups: grupos,
|
||||
})
|
||||
respuesta, err := dto_directors.NewGrupoSimpleDirector().Listar(grupos)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
envelope, err := dto_directors.NewGrupoDirector().GruposYPermisos(usuario.Email, respuesta)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, envelope)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package logs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
dto "repuestera_back/dto/Logs"
|
||||
"repuestera_back/models/logs"
|
||||
r "repuestera_back/repositorios"
|
||||
@@ -30,7 +30,11 @@ func BusquedaLogs(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logsdto := dto_builders.NewLogTablaBuilder().ConLogs(logs).Build()
|
||||
logsdto, err := dto_directors.NewLogTablaDirector().Listar(logs)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
cantpags, err := r.ObtenerRepositorios(c).Logs.CantPaginasConFiltros(filtros)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package logs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
@@ -23,5 +24,11 @@ func ObtenerLog(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, logs)
|
||||
respuesta, err := dto_directors.NewLogResponseDirector().Listar(logs)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package logs
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
@@ -32,7 +32,11 @@ func ObtenerLogs(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
logsdto := dto_builders.NewLogTablaBuilder().ConLogs(logs).Build()
|
||||
logsdto, err := dto_directors.NewLogTablaDirector().Listar(logs)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"logs": logsdto,
|
||||
|
||||
@@ -2,9 +2,11 @@ package permiso
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -25,6 +27,16 @@ func ActualizarPermiso(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
r.ObtenerRepositorios(c).Permiso.Actualizar(permiso, input)
|
||||
c.JSON(http.StatusOK, permiso)
|
||||
if err := r.ObtenerRepositorios(c).Permiso.Actualizar(permiso, input); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
respuesta, err := dto_directors.NewPermisoDirector().DesdePermiso(permiso)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package permiso
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
@@ -18,5 +20,12 @@ func CrearPermiso(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, permiso)
|
||||
|
||||
respuesta, err := dto_directors.NewPermisoDirector().DesdePermiso(&permiso)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, respuesta)
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package permiso
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -13,5 +15,12 @@ func ObtenerPermisos(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, permisos)
|
||||
|
||||
respuesta, err := dto_directors.NewPermisoDirector().Listar(permisos)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package permiso
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"repuestera_back/dto"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ObtenerPermisosUsuario(c *gin.Context) {
|
||||
@@ -26,25 +28,11 @@ func ObtenerPermisosUsuario(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
permisoMap := make(map[string]bool)
|
||||
var permisos []string
|
||||
|
||||
for _, grupo := range usuario.Grupos {
|
||||
for _, permiso := range grupo.Permisos {
|
||||
if !permisoMap[permiso.Nombre] {
|
||||
permisoMap[permiso.Nombre] = true
|
||||
permisos = append(permisos, permiso.Nombre)
|
||||
}
|
||||
}
|
||||
for _, grupoHijo := range grupo.Grupos {
|
||||
for _, permiso := range grupoHijo.Permisos {
|
||||
if !permisoMap[permiso.Nombre] {
|
||||
permisoMap[permiso.Nombre] = true
|
||||
permisos = append(permisos, permiso.Nombre)
|
||||
}
|
||||
}
|
||||
}
|
||||
respuesta, err := dto_directors.NewPermisosUsuarioDirector().DesdeGrupos(usuario.Grupos)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, dto.PermisosUsuarioResponse{Permisos: permisos})
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package repuestos
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
dto "repuestera_back/dto/Repuestos"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
@@ -35,10 +35,11 @@ func ActualizarRepuesto(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
repuesto := dto_builders.NewActualizarRepuestoBuilder().
|
||||
ConDto(data).
|
||||
ConMarca(marca.ID).
|
||||
Build()
|
||||
repuesto, err := dto_directors.NewRepuestoDirector().Actualizar(data, marca.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
entity, err := r.ObtenerRepositorios(c).Repuesto.ObtenerPorID(uint(id))
|
||||
if err != nil {
|
||||
@@ -47,10 +48,10 @@ func ActualizarRepuesto(c *gin.Context) {
|
||||
}
|
||||
|
||||
uid, _ := c.Get("userID")
|
||||
if err := r.ObtenerRepositorios(c).Repuesto.Actualizar(entity, &repuesto, uid.(uint)); err != nil {
|
||||
if err := r.ObtenerRepositorios(c).Repuesto.Actualizar(entity, repuesto, uid.(uint)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, repuesto)
|
||||
c.JSON(http.StatusOK, repuesto)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package repuestos
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
dto "repuestera_back/dto/Repuestos"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -28,13 +28,14 @@ func CrearRepuesto(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
repuesto := dto_builders.NewRepuestoBuilder().
|
||||
ConDto(data).
|
||||
ConMarca(marca.ID).
|
||||
Build()
|
||||
repuesto, err := dto_directors.NewRepuestoDirector().Crear(data, marca.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
uid, _ := c.Get("userID")
|
||||
if err := r.ObtenerRepositorios(c).Repuesto.Crear(&repuesto, uid.(uint)); err != nil {
|
||||
if err := r.ObtenerRepositorios(c).Repuesto.Crear(repuesto, uid.(uint)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
dto "repuestera_back/dto/Stock"
|
||||
"repuestera_back/models"
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
@@ -32,16 +32,23 @@ func ObtenerStock(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if q.Estado != "" {
|
||||
filtrado := make([]models.Stock, 0, len(stocks))
|
||||
for i := range stocks {
|
||||
estado := string(estadoDe(&stocks[i], &stocks[i].Repuesto))
|
||||
if strings.EqualFold(estado, q.Estado) {
|
||||
filtrado = append(filtrado, stocks[i])
|
||||
}
|
||||
}
|
||||
stocks = filtrado
|
||||
director := dto_directors.NewStockDirector()
|
||||
|
||||
respuesta, err := director.Listar(stocks)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, stocks)
|
||||
if q.Estado != "" {
|
||||
filtrado := make([]dto.StockResponse, 0, len(respuesta))
|
||||
for _, item := range respuesta {
|
||||
if strings.EqualFold(item.Estado, q.Estado) {
|
||||
filtrado = append(filtrado, item)
|
||||
}
|
||||
}
|
||||
respuesta = filtrado
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
@@ -20,5 +21,12 @@ func ObtenerStockPorID(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "stock no encontrado"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, stock)
|
||||
|
||||
respuesta, err := dto_directors.NewStockDirector().DesdeStock(stock)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
"repuestera_back/models"
|
||||
"repuestera_back/models/contabilidad"
|
||||
models_logs "repuestera_back/models/logs"
|
||||
@@ -44,15 +44,6 @@ var (
|
||||
ErrSinAlmacen = errors.New("no hay almacenes cargados en la empresa")
|
||||
)
|
||||
|
||||
// estadoStock clasifica un ítem según su cantidad física y su mínimo.
|
||||
type estadoStock string
|
||||
|
||||
const (
|
||||
estadoAgotado estadoStock = "AGOTADO"
|
||||
estadoBajoMinimo estadoStock = "BAJO_MINIMO"
|
||||
estadoNormal estadoStock = "NORMAL"
|
||||
)
|
||||
|
||||
func codigoHTTP(err error) int {
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
@@ -107,20 +98,6 @@ func disponible(stock *models.Stock) float64 {
|
||||
return redondearCantidad(stock.Cantidad - stock.CantidadReservada)
|
||||
}
|
||||
|
||||
// estadoDe devuelve AGOTADO, BAJO_MINIMO o NORMAL.
|
||||
func estadoDe(stock *models.Stock, repuesto *models.Repuesto) estadoStock {
|
||||
if stock == nil {
|
||||
return estadoAgotado
|
||||
}
|
||||
if stock.Cantidad <= 0 {
|
||||
return estadoAgotado
|
||||
}
|
||||
if repuesto != nil && stock.Cantidad < float64(repuesto.StockMinimo) {
|
||||
return estadoBajoMinimo
|
||||
}
|
||||
return estadoNormal
|
||||
}
|
||||
|
||||
// validarCantidad acepta solo cantidades positivas y finitas.
|
||||
func validarCantidad(cantidad float64) error {
|
||||
if math.IsNaN(cantidad) || math.IsInf(cantidad, 0) || cantidad <= 0 {
|
||||
@@ -247,7 +224,7 @@ func registrarAsiento(tx *gorm.DB, repo *repositorios.Repositorios, comprobanteT
|
||||
// registrarLog guarda la auditoría fuera de la transacción: si falla, la
|
||||
// operación de negocio ya confirmada no se revierte.
|
||||
func registrarLog(repo *repositorios.Repositorios, empresaID, usuarioID uint, accion string, detalles []models_logs.LogDetalle) {
|
||||
log, err := dto_builders.NewLogDirector().Custom(usuarioID, accion, detalles)
|
||||
log, err := dto_directors.NewLogDirector().Custom(usuarioID, accion, detalles)
|
||||
if err != nil {
|
||||
applog.Printf("stock: no se pudo construir el log de %s: %v", accion, err)
|
||||
return
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -39,7 +41,16 @@ func ActualizarUsuario(c *gin.Context) {
|
||||
usuario.Email = usuarioReq.Email
|
||||
usuario.Name = usuarioReq.Name
|
||||
|
||||
r.ObtenerRepositorios(c).Usuario.Guardar(usuario)
|
||||
if err := r.ObtenerRepositorios(c).Usuario.Guardar(usuario); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, usuario)
|
||||
respuesta, err := dto_directors.NewUsuarioDirector().DesdeUsuario(usuario)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -20,5 +22,11 @@ func CrearUsuario(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, usuario)
|
||||
respuesta, err := dto_directors.NewUsuarioDirector().DesdeUsuario(&usuario)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, respuesta)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package usuarios
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
@@ -21,5 +22,11 @@ func ObtenerUsuario(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, usuario)
|
||||
respuesta, err := dto_directors.NewUsuarioDirector().DesdeUsuario(usuario)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package usuarios
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
@@ -26,8 +27,14 @@ func ObtenerUsuarios(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
respuesta, err := dto_directors.NewUsuarioDirector().Listar(usuarios)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"usuarios": usuarios,
|
||||
"usuarios": respuesta,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user