feat: restringe soloAdmin (Bucefalo CRM) a agentes, default WhatsApp+fallbackEmail y archivado reversible
This commit is contained in:
@@ -23,6 +23,7 @@ type ServicioConCategoria = {
|
||||
variante: string | null;
|
||||
nivel: string | null;
|
||||
activo: boolean;
|
||||
soloAdmin: boolean;
|
||||
orden: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
@@ -48,6 +49,7 @@ export function serializarServicio(s: ServicioConCategoria) {
|
||||
variante: s.variante,
|
||||
nivel: s.nivel,
|
||||
activo: s.activo,
|
||||
soloAdmin: Boolean(s.soloAdmin),
|
||||
orden: s.orden,
|
||||
enCotizaciones: s._count?.servicios ?? 0,
|
||||
createdAt: s.createdAt,
|
||||
|
||||
+4
-1
@@ -11,7 +11,9 @@ export const clienteSelect = {
|
||||
email: true,
|
||||
telefono: true,
|
||||
rfc: true,
|
||||
_count: { select: { cotizaciones: true } },
|
||||
archivado: true,
|
||||
archivadoAt: true,
|
||||
_count: { select: { cotizaciones: { where: { archivado: false } } } },
|
||||
usuario: { select: { id: true, email: true } },
|
||||
} as const;
|
||||
|
||||
@@ -21,6 +23,7 @@ export const clienteSelect = {
|
||||
export function buscarDuplicado(nombre: string, empresa: string | null, excluirId?: string) {
|
||||
return prisma.cliente.findFirst({
|
||||
where: {
|
||||
archivado: false,
|
||||
nombre: { equals: nombre, mode: "insensitive" },
|
||||
empresa: empresa ? { equals: empresa, mode: "insensitive" } : null,
|
||||
...(excluirId ? { NOT: { id: excluirId } } : {}),
|
||||
|
||||
@@ -156,6 +156,7 @@ const camposServicio = {
|
||||
variante: textoCatalogo,
|
||||
nivel: nivelServicio,
|
||||
activo: z.boolean().optional(),
|
||||
soloAdmin: z.boolean().optional(),
|
||||
};
|
||||
|
||||
// Campos que no existen en el catalogo se rechazan con su nombre (en vez de
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// Reglas puras para restringir servicios/planes soloAdmin (p. ej. Bucefalo CRM)
|
||||
// frente a claves de agentes/bots. Jareth (usuario humano en web) nunca es bloqueado.
|
||||
|
||||
export interface ServicioDraftInput {
|
||||
catalogoId?: string | null;
|
||||
nombre?: string | null;
|
||||
esPersonalizado?: boolean;
|
||||
}
|
||||
|
||||
export interface EvaluarRestriccionSoloAdminInput {
|
||||
esAgente: boolean;
|
||||
planBucefalo?: unknown | null;
|
||||
servicios: ServicioDraftInput[];
|
||||
/** IDs de ServicioCatalogo existentes en BD marcados con soloAdmin=true */
|
||||
soloAdminCatalogIds: Iterable<string>;
|
||||
}
|
||||
|
||||
export interface EvaluarRestriccionSoloAdminResult {
|
||||
ok: boolean;
|
||||
motivos: string[];
|
||||
itemsBloqueados: string[];
|
||||
}
|
||||
|
||||
const BUCEFALO_RE = /buc[eé]falo/i;
|
||||
|
||||
export function evaluarRestriccionSoloAdmin(
|
||||
input: EvaluarRestriccionSoloAdminInput
|
||||
): EvaluarRestriccionSoloAdminResult {
|
||||
if (!input.esAgente) {
|
||||
return { ok: true, motivos: [], itemsBloqueados: [] };
|
||||
}
|
||||
|
||||
const motivos: string[] = [];
|
||||
const itemsBloqueados: string[] = [];
|
||||
const restrictedIds = new Set(input.soloAdminCatalogIds);
|
||||
|
||||
if (input.planBucefalo !== null && input.planBucefalo !== undefined) {
|
||||
motivos.push("planBucefalo esta reservado al administrador humano");
|
||||
itemsBloqueados.push("planBucefalo");
|
||||
}
|
||||
|
||||
for (const svc of input.servicios) {
|
||||
const cid = (svc.catalogoId || "").trim();
|
||||
const nombre = (svc.nombre || "").trim();
|
||||
|
||||
if (cid && /^bucefalo-/i.test(cid)) {
|
||||
motivos.push(`catalogoId restringido (${cid})`);
|
||||
itemsBloqueados.push(cid);
|
||||
continue;
|
||||
}
|
||||
if (cid && restrictedIds.has(cid)) {
|
||||
motivos.push(`servicio marcado soloAdmin (${cid})`);
|
||||
itemsBloqueados.push(cid);
|
||||
continue;
|
||||
}
|
||||
if (nombre && BUCEFALO_RE.test(nombre)) {
|
||||
motivos.push(`servicio restringido por marca soloAdmin (${nombre})`);
|
||||
itemsBloqueados.push(nombre);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ok: motivos.length === 0,
|
||||
motivos,
|
||||
itemsBloqueados,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user