147 lines
4.1 KiB
Go
147 lines
4.1 KiB
Go
package domain
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type ActorInfo struct {
|
|
Type string // "user" | "agent" | "enlace_firmado"
|
|
Role string // "admin" | "asesor" | "cliente"
|
|
Scopes []string
|
|
}
|
|
|
|
type ValidarTransicionInput struct {
|
|
Actual string
|
|
Nuevo string
|
|
Actor ActorInfo
|
|
ViaEndpoint string // "aprobar" | "enlace_firmado" | "enviar" | "estado" | "put"
|
|
AprobadaAt *time.Time
|
|
}
|
|
|
|
type ValidarTransicionResult struct {
|
|
OK bool
|
|
Status int
|
|
Error string
|
|
}
|
|
|
|
func ValidarTransicionEstado(in ValidarTransicionInput) ValidarTransicionResult {
|
|
if !EsEstadoCotizacionValido(in.Nuevo) {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 400,
|
|
Error: fmt.Sprintf("Estado invalido. Permitidos: %s", strings.Join(EstadosCotizacion, ", ")),
|
|
}
|
|
}
|
|
|
|
if in.Actual == in.Nuevo {
|
|
return ValidarTransicionResult{OK: true}
|
|
}
|
|
|
|
esAdminHumano := in.Actor.Type == "user" && in.Actor.Role == "admin"
|
|
esStaffHumano := in.Actor.Type == "user" && in.Actor.Role != "cliente"
|
|
esEnlaceFirmado := in.Actor.Type == "enlace_firmado" && in.ViaEndpoint == "enlace_firmado"
|
|
esAgente := in.Actor.Type == "agent"
|
|
|
|
// REGLA DE HIERRO 1: Nadie salvo admin humano o enlace firmado de un solo uso puede aprobar.
|
|
if in.Nuevo == "aprobada" {
|
|
if !esAdminHumano && !esEnlaceFirmado {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 403,
|
|
Error: "Solo un administrador humano o el enlace firmado puede aprobar una cotizacion",
|
|
}
|
|
}
|
|
if in.Actual != "borrador" {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 409,
|
|
Error: fmt.Sprintf("Solo se puede aprobar una cotizacion en borrador (estado actual: %s)", in.Actual),
|
|
}
|
|
}
|
|
return ValidarTransicionResult{OK: true}
|
|
}
|
|
|
|
// REGLA DE HIERRO 2: Nadie puede pasar a "enviada" sin estar previamente aprobada (con aprobadaAt).
|
|
if in.Nuevo == "enviada" {
|
|
if esAgente && in.ViaEndpoint != "enviar" {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 403,
|
|
Error: "Los agentes solo pueden enviar cotizaciones mediante POST /api/cotizaciones/:id/enviar",
|
|
}
|
|
}
|
|
if in.Actual != "aprobada" || in.AprobadaAt == nil {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 409,
|
|
Error: "La cotizacion debe estar aprobada por el administrador antes de enviarse",
|
|
}
|
|
}
|
|
if esAgente {
|
|
if !slices.Contains(in.Actor.Scopes, "cotizaciones:enviar") {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 403,
|
|
Error: "El agente no tiene el alcance cotizaciones:enviar",
|
|
}
|
|
}
|
|
return ValidarTransicionResult{OK: true}
|
|
}
|
|
if !esStaffHumano {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 403,
|
|
Error: "No autorizado para enviar cotizaciones",
|
|
}
|
|
}
|
|
return ValidarTransicionResult{OK: true}
|
|
}
|
|
|
|
// Reabrir a borrador: solo admin humano desde aprobada, rechazada o vencida.
|
|
if in.Nuevo == "borrador" {
|
|
if !esAdminHumano {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 403,
|
|
Error: "Solo un administrador humano puede regresar una cotizacion a borrador",
|
|
}
|
|
}
|
|
if in.Actual != "aprobada" && in.Actual != "rechazada" && in.Actual != "vencida" {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 409,
|
|
Error: fmt.Sprintf("No se puede regresar a borrador desde el estado %s", in.Actual),
|
|
}
|
|
}
|
|
return ValidarTransicionResult{OK: true}
|
|
}
|
|
|
|
// Estados de cierre post-envio: aceptada | rechazada | vencida
|
|
if in.Nuevo == "aceptada" || in.Nuevo == "rechazada" || in.Nuevo == "vencida" {
|
|
if !esStaffHumano {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 403,
|
|
Error: "Solo el equipo humano puede marcar una cotizacion como aceptada, rechazada o vencida",
|
|
}
|
|
}
|
|
if in.Actual != "enviada" {
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 409,
|
|
Error: fmt.Sprintf("Solo se puede pasar a %s desde el estado enviada (estado actual: %s)", in.Nuevo, in.Actual),
|
|
}
|
|
}
|
|
return ValidarTransicionResult{OK: true}
|
|
}
|
|
|
|
return ValidarTransicionResult{
|
|
OK: false,
|
|
Status: 409,
|
|
Error: fmt.Sprintf("Transicion no permitida de %s a %s", in.Actual, in.Nuevo),
|
|
}
|
|
}
|