feat: enhance ata management by adding dataProrrogacao field and updating related logic for effective date handling, improving data integrity and user experience in pedidos

This commit is contained in:
2025-12-17 10:39:33 -03:00
parent fbf00c824e
commit 9072619e26
8 changed files with 390 additions and 115 deletions

View File

@@ -4,6 +4,7 @@ import type { Doc, Id } from './_generated/dataModel';
import type { MutationCtx, QueryCtx } from './_generated/server';
import { internalMutation, mutation, query } from './_generated/server';
import { getCurrentUserFunction } from './auth';
import { getTodayYMD, isWithinRangeYMD, maxYMD } from './utils/datas';
// ========== HELPERS ==========
@@ -309,6 +310,24 @@ async function assertAtaObjetoCanConsume(
delta: number
) {
if (!Number.isFinite(delta) || delta <= 0) return;
// Bloqueia consumo se a ata estiver fora da vigência (considerando prorrogação).
const ata = await ctx.db.get(ataId);
if (!ata) {
throw new Error('Ata não encontrada.');
}
const dataProrrogacao = (ata as { dataProrrogacao?: string }).dataProrrogacao ?? null;
const dataFimEfetiva = maxYMD(ata.dataFim ?? null, dataProrrogacao);
const today = getTodayYMD();
const vigenteHoje = isWithinRangeYMD(today, ata.dataInicio ?? null, dataFimEfetiva);
if (!vigenteHoje) {
const inicioLabel = ata.dataInicio ?? 'sem início';
const fimLabel = dataFimEfetiva ?? 'sem fim';
throw new Error(
`Não é possível consumir esta ata pois ela está fora da vigência. Vigência: ${inicioLabel} até ${fimLabel}.`
);
}
const info = await getAtaObjetoUsageInfo(ctx, ataId, objetoId, true);
if (info.quantidadeUsada + delta > info.limitePermitido) {
throw new Error(