feat(captura): fechas de Obra bloqueadas tras establecerse, solo admin las cambia
This commit is contained in:
@@ -1,5 +1,42 @@
|
||||
import { prisma } from './prisma.js'
|
||||
import { CAMPOS_POR_DEPARTAMENTO, DEPARTAMENTOS } from '../config/campos-departamento.js'
|
||||
import { AppError } from '../utils/errors.js'
|
||||
|
||||
/** Fechas de Obra emparejadas a checkboxes: tras establecerse solo las cambia un admin */
|
||||
const CAMPOS_FECHA_OBRA = new Set(
|
||||
(CAMPOS_POR_DEPARTAMENTO.OBRA ?? [])
|
||||
.filter((c) => c.conFecha)
|
||||
.map((c) => c.conFecha as string),
|
||||
)
|
||||
|
||||
/** Bloquear modificación de fechas de Obra ya establecidas (solo admin) */
|
||||
async function validarFechasObra(
|
||||
desarrollo: string,
|
||||
departamento: string,
|
||||
registros: { unidad: string; campo: string; valor: string | null }[],
|
||||
rol: string,
|
||||
): Promise<void> {
|
||||
if (rol === 'admin' || departamento !== 'OBRA') return
|
||||
const candidatos = registros.filter((r) => CAMPOS_FECHA_OBRA.has(r.campo))
|
||||
if (candidatos.length === 0) return
|
||||
|
||||
const existentes = await prisma.capturaDepartamento.findMany({
|
||||
where: {
|
||||
desarrollo,
|
||||
departamento,
|
||||
unidadCodigo: { in: [...new Set(candidatos.map((r) => r.unidad))] },
|
||||
campo: { in: candidatos.map((r) => r.campo) },
|
||||
},
|
||||
})
|
||||
const previos = new Map(existentes.map((e) => [`${e.unidadCodigo}|${e.campo}`, e.valor]))
|
||||
|
||||
for (const r of candidatos) {
|
||||
const previo = previos.get(`${r.unidad}|${r.campo}`)
|
||||
if (previo != null && previo !== r.valor) {
|
||||
throw new AppError(403, 'Las fechas ya establecidas solo puede modificarlas un administrador')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Obtener unidades únicas de un desarrollo ──────────────────────────
|
||||
|
||||
@@ -67,7 +104,10 @@ export async function guardarCampo(
|
||||
campo: string,
|
||||
valor: string | null,
|
||||
usuarioId: string,
|
||||
rol: string,
|
||||
): Promise<void> {
|
||||
await validarFechasObra(desarrollo, departamento, [{ unidad: unidadCodigo, campo, valor }], rol)
|
||||
|
||||
const tipo = CAMPOS_POR_DEPARTAMENTO[departamento]?.find((c) => c.campo === campo)?.tipo ?? 'text'
|
||||
|
||||
await prisma.capturaDepartamento.upsert({
|
||||
@@ -91,7 +131,10 @@ export async function guardarBulk(
|
||||
departamento: string,
|
||||
registros: { unidad: string; campo: string; valor: string | null }[],
|
||||
usuarioId: string,
|
||||
rol: string,
|
||||
): Promise<void> {
|
||||
await validarFechasObra(desarrollo, departamento, registros, rol)
|
||||
|
||||
const tipoPorCampo = new Map(
|
||||
(CAMPOS_POR_DEPARTAMENTO[departamento] ?? []).map((c) => [c.campo, c.tipo]),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user