Private
Public Access
primeros cambios hechos para tener el multitenant
This commit is contained in:
1 parent
34ad4424f9
commit
4e082e8767
132 files changed
+6202
-762
No files matched your search
@@ -1,7 +1,9 @@
|
||||
package models
|
||||
|
||||
type Almacen struct {
|
||||
ID string `gorm:"primaryKey;type:varchar(5)" json:"id"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_almacen_empresa_codigo,priority:1" json:"empresaId"`
|
||||
Codigo string `gorm:"type:varchar(5);not null;uniqueIndex:uq_almacen_empresa_codigo,priority:2" json:"codigo"`
|
||||
Ubicacion string `gorm:"type:varchar(40)" json:"ubicacion"`
|
||||
Stocks []Stock `gorm:"foreignKey:AlmacenID" json:"stocks,omitempty"`
|
||||
}
|
||||
|
||||
@@ -8,9 +8,10 @@ import (
|
||||
|
||||
type Cliente struct {
|
||||
gorm.Model
|
||||
Nombre string `gorm:"type:varchar(40)" json:"nombre"`
|
||||
Cuit string `gorm:"type:varchar(11);uniqueIndex;not null" json:"cuit"`
|
||||
Email sql.NullString `gorm:"type:varchar(80)" json:"email"`
|
||||
Facturas []Factura `gorm:"foreignKey:ClienteID" json:"facturas,omitempty"`
|
||||
Recibos []Recibo `gorm:"foreignKey:ClienteID" json:"recibos,omitempty"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_cliente_empresa_cuit,priority:1" json:"empresaId"`
|
||||
Nombre string `gorm:"type:varchar(40)" json:"nombre"`
|
||||
Cuit string `gorm:"type:varchar(11);not null;uniqueIndex:uq_cliente_empresa_cuit,priority:2" json:"cuit"`
|
||||
Email sql.NullString `gorm:"type:varchar(80)" json:"email"`
|
||||
Facturas []Factura `gorm:"foreignKey:ClienteID" json:"facturas,omitempty"`
|
||||
Recibos []Recibo `gorm:"foreignKey:ClienteID" json:"recibos,omitempty"`
|
||||
}
|
||||
@@ -1,15 +1,16 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
"repuestera_back/models/seguridad"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// este lo uso de base para los distintos tipos de comprobantes
|
||||
// Comprobante es la base de los distintos tipos de comprobantes.
|
||||
// EmpresaID se propaga automáticamente a todas las tablas que lo embeben.
|
||||
type Comprobante struct {
|
||||
gorm.Model
|
||||
// Numero string `gorm:"size:50;not null" json:"numero"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index" json:"empresaId"`
|
||||
Estado string `gorm:"type:varchar(20);not null" json:"estado"`
|
||||
Total float64 `gorm:"type:decimal(15,2);not null" json:"total"`
|
||||
ResponsableID uint `gorm:"column:usuarioID"`
|
||||
|
||||
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type AsientoContable struct {
|
||||
gorm.Model
|
||||
EmpresaID uint `gorm:"not null;default:1;index" json:"empresaId"`
|
||||
ComprobanteTipo string `gorm:"type:varchar(20);not null"`
|
||||
ComprobanteID uint
|
||||
Movimientos []MovimientoContable `gorm:"foreignKey:AsientoID"`
|
||||
|
||||
@@ -4,7 +4,8 @@ import "gorm.io/gorm"
|
||||
|
||||
type CuentaContable struct {
|
||||
gorm.Model
|
||||
Nombre string `gorm:"type:varchar(30);not null"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_cuenta_empresa_nombre,priority:1" json:"empresaId"`
|
||||
Nombre string `gorm:"type:varchar(30);not null;uniqueIndex:uq_cuenta_empresa_nombre,priority:2"`
|
||||
Tipo string `gorm:"type:varchar(12);not null"`
|
||||
Movimientos []MovimientoContable `gorm:"foreignKey:CuentaID"`
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type MovimientoContable struct {
|
||||
gorm.Model
|
||||
EmpresaID uint `gorm:"not null;default:1;index" json:"empresaId"`
|
||||
AsientoID uint `gorm:"column:asientoID"`
|
||||
Asiento AsientoContable `gorm:"foreignKey:AsientoID"`
|
||||
CuentaID uint `gorm:"column:cuentaID"`
|
||||
|
||||
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type Descuento struct {
|
||||
gorm.Model
|
||||
EmpresaID uint `gorm:"not null;default:1;index" json:"empresaId"`
|
||||
|
||||
Nombre string `gorm:"type:varchar(100);not null"`
|
||||
Descripcion string `gorm:"type:text"`
|
||||
|
||||
@@ -4,7 +4,8 @@ import "gorm.io/gorm"
|
||||
|
||||
type FormaDePago struct {
|
||||
gorm.Model
|
||||
Descripcion string `gorm:"type:varchar(20);not null" json:"desripcion" `
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_formapago_empresa_desc,priority:1" json:"empresaId"`
|
||||
Descripcion string `gorm:"type:varchar(20);not null;uniqueIndex:uq_formapago_empresa_desc,priority:2" json:"descripcion"`
|
||||
}
|
||||
|
||||
func (FormaDePago) TableName() string {
|
||||
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
|
||||
type Log struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index:idx_logs_empresa_created,priority:1" json:"empresaId"`
|
||||
UsuarioID uint `gorm:"index;not null" json:"usuarioId"`
|
||||
Accion string `gorm:"type:varchar(50);not null" json:"accion"`
|
||||
Detalles datatypes.JSONSlice[LogDetalle] `gorm:"type:json" json:"detalles,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
CreatedAt time.Time `gorm:"index:idx_logs_empresa_created,priority:2" json:"created_at"`
|
||||
Usuario models_seguridad.Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ import (
|
||||
|
||||
type Marca struct {
|
||||
gorm.Model
|
||||
Nombre string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"nombre"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_marca_empresa_nombre,priority:1" json:"empresaId"`
|
||||
Nombre string `gorm:"not null;type:varchar(70);uniqueIndex:uq_marca_empresa_nombre,priority:2" json:"nombre"`
|
||||
Repuestos []Repuesto `gorm:"foreignKey:MarcaID" json:"repuestos"`
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type Pago struct {
|
||||
gorm.Model
|
||||
EmpresaID uint `gorm:"not null;default:1;index" json:"empresaId"`
|
||||
FormaDePagoID uint `gorm:"column:formaDePagoID"`
|
||||
FormaDePago FormaDePago `gorm:"foreignKey:FormaDePagoID"`
|
||||
ReciboID uint `json:"reciboId"`
|
||||
|
||||
@@ -4,6 +4,7 @@ import "gorm.io/gorm"
|
||||
|
||||
type Proveedor struct {
|
||||
gorm.Model
|
||||
Nombre string `gorm:"type:varchar(40)" json:"nombre"`
|
||||
Facturas []Factura `gorm:"foreignKey:ProveedorID" json:"facturas,omitempty"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index" json:"empresaId"`
|
||||
Nombre string `gorm:"type:varchar(40)" json:"nombre"`
|
||||
Facturas []Factura `gorm:"foreignKey:ProveedorID" json:"facturas,omitempty"`
|
||||
}
|
||||
@@ -13,7 +13,7 @@ type Remito struct {
|
||||
EmisorID uint `gorm:"not null" json:"emisorId"`
|
||||
ReceptorID uint `gorm:"not null" json:"receptorId"`
|
||||
|
||||
AlmacenOrigenID *string `gorm:"column:almacen_origen_id;type:varchar(5)" json:"almacenOrigenId,omitempty"`
|
||||
AlmacenOrigenID *uint `gorm:"column:almacen_origen_id" json:"almacenOrigenId,omitempty"`
|
||||
AlmacenOrigen *Almacen `gorm:"foreignKey:AlmacenOrigenID" json:"almacenOrigen,omitempty"`
|
||||
|
||||
Detalles datatypes.JSONSlice[RemitoDetalle] `gorm:"type:json" json:"detalles,omitempty"`
|
||||
|
||||
@@ -7,10 +7,11 @@ import (
|
||||
|
||||
type Repuesto struct {
|
||||
gorm.Model
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_repuesto_empresa_desc,priority:1" json:"empresaId"`
|
||||
Nombre string `gorm:"type:varchar(50);not null" json:"nombre" binding:"required"`
|
||||
Precio float64 `gorm:"not null;type:decimal(10,2)" json:"precio" binding:"required"`
|
||||
StockMinimo int64 `gorm:"not null;type:bigint" json:"stockMinimo" binding:"required"`
|
||||
Descripcion string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"descripcion" binding:"required"`
|
||||
Descripcion string `gorm:"not null;type:varchar(70);uniqueIndex:uq_repuesto_empresa_desc,priority:2" json:"descripcion" binding:"required"`
|
||||
MarcaID uint `gorm:"column:marcaID"`
|
||||
Marca Marca `gorm:"foreignKey:MarcaID" json:"marca"`
|
||||
Aliases datatypes.JSONSlice[string] `gorm:"type:json" json:"aliases,omitempty"`
|
||||
|
||||
@@ -8,10 +8,11 @@ import (
|
||||
|
||||
type Reserva struct {
|
||||
gorm.Model
|
||||
EmpresaID uint `gorm:"not null;default:1;index;index:idx_reserva_empresa_repuesto_almacen,priority:1" json:"empresaId"`
|
||||
|
||||
RepuestoID uint `gorm:"not null;index:idx_reserva_repuesto_almacen" json:"repuesto_id"`
|
||||
RepuestoID uint `gorm:"not null;index:idx_reserva_empresa_repuesto_almacen,priority:2" json:"repuesto_id"`
|
||||
Repuesto Repuesto `gorm:"foreignKey:RepuestoID" json:"repuesto,omitempty"`
|
||||
AlmacenID string `gorm:"type:varchar(5);not null;index:idx_reserva_repuesto_almacen" json:"almacen_id"`
|
||||
AlmacenID uint `gorm:"not null;index:idx_reserva_empresa_repuesto_almacen,priority:3" json:"almacen_id"`
|
||||
Almacen Almacen `gorm:"foreignKey:AlmacenID" json:"almacen,omitempty"`
|
||||
Cantidad float64 `gorm:"not null;check:cantidad > 0" json:"cantidad"`
|
||||
Estado EstadoReserva `gorm:"type:varchar(20);not null;default:'activa';index" json:"estado"`
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package models_seguridad
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Empresa es el tenant del sistema. Todos los datos de negocio pertenecen a
|
||||
// una empresa y se filtran por EmpresaID.
|
||||
type Empresa struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Nombre string `gorm:"type:varchar(120);not null" json:"nombre"`
|
||||
Slug string `gorm:"type:varchar(60);uniqueIndex;not null" json:"slug"`
|
||||
Cuit *string `gorm:"type:varchar(11);uniqueIndex" json:"cuit,omitempty"`
|
||||
Activa bool `gorm:"not null;default:true" json:"activa"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
func (Empresa) TableName() string {
|
||||
return "empresas"
|
||||
}
|
||||
|
||||
type RolEmpresa string
|
||||
|
||||
const (
|
||||
RolOwner RolEmpresa = "OWNER"
|
||||
RolAdmin RolEmpresa = "ADMIN"
|
||||
RolMiembro RolEmpresa = "MEMBER"
|
||||
)
|
||||
|
||||
func (r RolEmpresa) Valido() bool {
|
||||
switch r {
|
||||
case RolOwner, RolAdmin, RolMiembro:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (r RolEmpresa) EsAdministrador() bool {
|
||||
return r == RolOwner || r == RolAdmin
|
||||
}
|
||||
|
||||
// EmpresaUsuario es la membresía de un usuario global dentro de una empresa.
|
||||
type EmpresaUsuario struct {
|
||||
EmpresaID uint `gorm:"primaryKey" json:"empresa_id"`
|
||||
UsuarioID uint `gorm:"primaryKey" json:"usuario_id"`
|
||||
Rol RolEmpresa `gorm:"type:varchar(20);not null;default:'MEMBER'" json:"rol"`
|
||||
Activo bool `gorm:"not null;default:true" json:"activo"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
Empresa Empresa `gorm:"foreignKey:EmpresaID" json:"empresa,omitempty"`
|
||||
Usuario Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
|
||||
}
|
||||
|
||||
func (EmpresaUsuario) TableName() string {
|
||||
return "empresa_usuarios"
|
||||
}
|
||||
|
||||
type RolPlataforma string
|
||||
|
||||
const (
|
||||
RolSuperadmin RolPlataforma = "SUPERADMIN"
|
||||
RolSoporte RolPlataforma = "SOPORTE"
|
||||
)
|
||||
|
||||
// PlataformaAdmin es un rol transversal, separado de los grupos de cada
|
||||
// empresa. No debe usarse para acceder a datos de negocio sin membresía.
|
||||
type PlataformaAdmin struct {
|
||||
UsuarioID uint `gorm:"primaryKey" json:"usuario_id"`
|
||||
Rol RolPlataforma `gorm:"type:varchar(20);not null;default:'SUPERADMIN'" json:"rol"`
|
||||
Activo bool `gorm:"not null;default:true" json:"activo"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
Usuario Usuario `gorm:"foreignKey:UsuarioID" json:"usuario,omitempty"`
|
||||
}
|
||||
|
||||
func (PlataformaAdmin) TableName() string {
|
||||
return "plataforma_admins"
|
||||
}
|
||||
|
||||
// InvitacionEmpresa permite sumar un usuario existente o crear uno nuevo en
|
||||
// una empresa sin compartir contraseñas.
|
||||
type InvitacionEmpresa struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
EmpresaID uint `gorm:"not null;index" json:"empresa_id"`
|
||||
Email string `gorm:"type:varchar(100);not null;index" json:"email"`
|
||||
Rol RolEmpresa `gorm:"type:varchar(20);not null;default:'MEMBER'" json:"rol"`
|
||||
GrupoID uint `gorm:"not null;default:0" json:"grupo_id"`
|
||||
TokenHash string `gorm:"type:varchar(64);uniqueIndex;not null" json:"-"`
|
||||
ExpiraEn time.Time `gorm:"not null;index" json:"expira_en"`
|
||||
UsadaEn *time.Time `json:"usada_en,omitempty"`
|
||||
CreadaPor uint `gorm:"not null" json:"creada_por"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
||||
Empresa Empresa `gorm:"foreignKey:EmpresaID" json:"empresa,omitempty"`
|
||||
}
|
||||
|
||||
func (InvitacionEmpresa) TableName() string {
|
||||
return "invitaciones_empresa"
|
||||
}
|
||||
@@ -8,12 +8,13 @@ import (
|
||||
|
||||
type Grupo struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Nombre string `gorm:"uniqueIndex;not null;type:varchar(100)" json:"nombre"`
|
||||
EmpresaID uint `gorm:"not null;default:1;index;uniqueIndex:uq_grupo_empresa_nombre,priority:1" json:"empresaId"`
|
||||
Nombre string `gorm:"not null;type:varchar(100);uniqueIndex:uq_grupo_empresa_nombre,priority:2" json:"nombre"`
|
||||
Descripcion string `gorm:"type:text" json:"descripcion"`
|
||||
Imagen string `gorm:"type:varchar(500)" json:"imagen"`
|
||||
Permisos []Permiso `gorm:"many2many:grupo_permisos;" json:"permisos,omitempty"`
|
||||
Grupos []Grupo `gorm:"many2many:grupo_grupos;joinForeignKey:ParentGroupID;joinReferences:ChildGroupID" json:"grupos_hijos,omitempty"`
|
||||
Usuarios []Usuario `gorm:"many2many:usuario_grupos;" json:"usuarios,omitempty"`
|
||||
Usuarios []Usuario `gorm:"many2many:usuarios_grupos;" json:"usuarios,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package models_seguridad
|
||||
|
||||
// IDs estables del catálogo global de permisos. Se mantienen fijos para no
|
||||
// romper tokens, grupos ni datos existentes.
|
||||
const (
|
||||
PermisoGestionPermisos uint = 1
|
||||
PermisoGestionGrupos uint = 2
|
||||
PermisoGestionUsuarios uint = 3
|
||||
PermisoGestionRepuestos uint = 4
|
||||
PermisoGestionLogs uint = 5
|
||||
PermisoGestionMarcas uint = 6
|
||||
PermisoGestionEmpresa uint = 7
|
||||
PermisoPlataforma uint = 8
|
||||
)
|
||||
|
||||
// PermisosDeEmpresa son los permisos que puede recibir una empresa. Los
|
||||
// permisos de plataforma (1 y 8) nunca se asignan a grupos de tenant.
|
||||
func PermisosDeEmpresa() []uint {
|
||||
return []uint{
|
||||
PermisoGestionGrupos,
|
||||
PermisoGestionUsuarios,
|
||||
PermisoGestionRepuestos,
|
||||
PermisoGestionLogs,
|
||||
PermisoGestionMarcas,
|
||||
PermisoGestionEmpresa,
|
||||
}
|
||||
}
|
||||
|
||||
func EsPermisoDePlataforma(id uint) bool {
|
||||
return id == PermisoGestionPermisos || id == PermisoPlataforma
|
||||
}
|
||||
@@ -7,15 +7,17 @@ import (
|
||||
)
|
||||
|
||||
type Usuario struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Email string `gorm:"uniqueIndex;not null;type:varchar(100)" json:"email"`
|
||||
Password string `gorm:"not null" json:"-"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Grupos []Grupo `gorm:"many2many:usuarios_grupos;" json:"grupos,omitempty"`
|
||||
// Empresas []Empresa `gorm:"many2many:user_empresa;" json:"empresas,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Email string `gorm:"uniqueIndex;not null;type:varchar(100)" json:"email"`
|
||||
Password string `gorm:"not null" json:"-"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Activo bool `gorm:"not null;default:true" json:"activo"`
|
||||
DebeCambiarPassword bool `gorm:"not null;default:false" json:"-"`
|
||||
Grupos []Grupo `gorm:"many2many:usuarios_grupos;" json:"grupos,omitempty"`
|
||||
Empresas []Empresa `gorm:"many2many:empresa_usuarios;" json:"empresas,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
func (Usuario) TableName() string {
|
||||
|
||||
@@ -7,12 +7,14 @@ import (
|
||||
)
|
||||
|
||||
type Stock struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||
EmpresaID uint64 `gorm:"not null;default:1;index;uniqueIndex:uq_stock_empresa_repuesto_almacen,priority:1" json:"empresaId"`
|
||||
|
||||
Cantidad uint `gorm:"not null;default:0" json:"cantidad"`
|
||||
CantidadReservada uint `gorm:"not null:default:0" json:"cantidadReservada"`
|
||||
RepuestoID uint64 `gorm:"index;not null" json:"repuestoId"`
|
||||
CantidadReservada uint `gorm:"not null;default:0" json:"cantidadReservada"`
|
||||
RepuestoID uint64 `gorm:"not null;uniqueIndex:uq_stock_empresa_repuesto_almacen,priority:2" json:"repuestoId"`
|
||||
Repuesto Repuesto `gorm:"foreignKey:RepuestoID" json:"repuesto"`
|
||||
AlmacenID string `gorm:"type:varchar(5);index" json:"almacenId"`
|
||||
AlmacenID uint `gorm:"not null;uniqueIndex:uq_stock_empresa_repuesto_almacen,priority:3" json:"almacenId"`
|
||||
Almacen Almacen `gorm:"foreignKey:AlmacenID" json:"almacen,omitempty"`
|
||||
Observacion string `gorm:"type:varchar(60)" json:"observacion"`
|
||||
|
||||
|
||||
Reference in new issue
Block a user