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:
@@ -1,7 +1,154 @@
|
||||
import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
import type { Id } from './_generated/dataModel';
|
||||
import type { QueryCtx } from './_generated/server';
|
||||
import { getCurrentUserFunction } from './auth';
|
||||
import { addMonthsClampedYMD, getTodayYMD, isWithinRangeYMD, maxYMD } from './utils/datas';
|
||||
|
||||
const ataComLimiteValidator = v.object({
|
||||
_id: v.id('atas'),
|
||||
numero: v.string(),
|
||||
numeroSei: v.string(),
|
||||
dataInicio: v.union(v.string(), v.null()),
|
||||
dataFim: v.union(v.string(), v.null()),
|
||||
dataProrrogacao: v.union(v.string(), v.null()),
|
||||
dataFimEfetiva: v.union(v.string(), v.null()),
|
||||
quantidadeTotal: v.union(v.number(), v.null()),
|
||||
limitePercentual: v.number(),
|
||||
quantidadeUsada: v.number(),
|
||||
limitePermitido: v.number(),
|
||||
restante: v.number(),
|
||||
isLocked: v.boolean(),
|
||||
isVencidaRecentemente: v.boolean(),
|
||||
lockReason: v.union(
|
||||
v.literal('vigencia_expirada'),
|
||||
v.literal('limite_atingido'),
|
||||
v.literal('nao_configurada'),
|
||||
v.null()
|
||||
)
|
||||
});
|
||||
|
||||
async function computeAtasComLimiteForObjeto(
|
||||
ctx: QueryCtx,
|
||||
objetoId: Id<'objetos'>,
|
||||
includeAtaIds?: Id<'atas'>[]
|
||||
) {
|
||||
const today = getTodayYMD();
|
||||
const threeMonthsAgo = addMonthsClampedYMD(today, -3);
|
||||
const includeSet = new Set<Id<'atas'>>(includeAtaIds ?? []);
|
||||
|
||||
const links = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_objetoId', (q) => q.eq('objetoId', objetoId))
|
||||
.collect();
|
||||
|
||||
const results: Array<{
|
||||
_id: Id<'atas'>;
|
||||
numero: string;
|
||||
numeroSei: string;
|
||||
dataInicio: string | null;
|
||||
dataFim: string | null;
|
||||
dataProrrogacao: string | null;
|
||||
dataFimEfetiva: string | null;
|
||||
quantidadeTotal: number | null;
|
||||
limitePercentual: number;
|
||||
quantidadeUsada: number;
|
||||
limitePermitido: number;
|
||||
restante: number;
|
||||
isLocked: boolean;
|
||||
isVencidaRecentemente: boolean;
|
||||
lockReason: 'vigencia_expirada' | 'limite_atingido' | 'nao_configurada' | null;
|
||||
}> = [];
|
||||
|
||||
for (const link of links) {
|
||||
const ata = await ctx.db.get(link.ataId);
|
||||
if (!ata) continue;
|
||||
|
||||
const dataProrrogacao = (ata as { dataProrrogacao?: string }).dataProrrogacao ?? null;
|
||||
const dataFimEfetiva = maxYMD(ata.dataFim ?? null, dataProrrogacao);
|
||||
|
||||
const vigenteHoje = isWithinRangeYMD(today, ata.dataInicio ?? null, dataFimEfetiva);
|
||||
const isVencidaRecentemente =
|
||||
!!dataFimEfetiva && dataFimEfetiva < today && dataFimEfetiva >= threeMonthsAgo;
|
||||
|
||||
// Para seleção em pedidos: manter somente vigentes e vencidas recentemente,
|
||||
// mas permitir incluir atas específicas (ex.: já selecionadas em pedido antigo).
|
||||
const shouldForceInclude = includeSet.has(ata._id);
|
||||
if (!shouldForceInclude && !vigenteHoje && !isVencidaRecentemente) {
|
||||
continue;
|
||||
}
|
||||
const isForaDaVigencia = !vigenteHoje;
|
||||
|
||||
let quantidadeUsada = link.quantidadeUsada ?? 0;
|
||||
if (link.quantidadeUsada === undefined) {
|
||||
const items = await ctx.db
|
||||
.query('objetoItems')
|
||||
.withIndex('by_ataId_and_objetoId', (q) =>
|
||||
q.eq('ataId', link.ataId).eq('objetoId', link.objetoId)
|
||||
)
|
||||
.collect();
|
||||
|
||||
const sumByPedidoId = new Map<Id<'pedidos'>, number>();
|
||||
for (const item of items) {
|
||||
const prev = sumByPedidoId.get(item.pedidoId) ?? 0;
|
||||
sumByPedidoId.set(item.pedidoId, prev + item.quantidade);
|
||||
}
|
||||
|
||||
let total = 0;
|
||||
for (const [pedidoId, sum] of sumByPedidoId.entries()) {
|
||||
const pedido = await ctx.db.get(pedidoId);
|
||||
if (pedido && pedido.status !== 'cancelado') {
|
||||
total += sum;
|
||||
}
|
||||
}
|
||||
quantidadeUsada = total;
|
||||
}
|
||||
|
||||
const quantidadeTotal = link.quantidadeTotal ?? null;
|
||||
const limitePercentualRaw = link.limitePercentual ?? 50;
|
||||
const limitePercentual = Number.isFinite(limitePercentualRaw)
|
||||
? Math.min(100, Math.max(0, limitePercentualRaw))
|
||||
: 50;
|
||||
|
||||
const limitePermitido =
|
||||
quantidadeTotal && quantidadeTotal > 0
|
||||
? Math.floor(quantidadeTotal * (limitePercentual / 100))
|
||||
: 0;
|
||||
|
||||
const restante = Math.max(0, limitePermitido - quantidadeUsada);
|
||||
|
||||
const lockReason: 'nao_configurada' | 'limite_atingido' | 'vigencia_expirada' | null =
|
||||
!quantidadeTotal || quantidadeTotal <= 0
|
||||
? 'nao_configurada'
|
||||
: quantidadeUsada >= limitePermitido
|
||||
? 'limite_atingido'
|
||||
: isForaDaVigencia
|
||||
? 'vigencia_expirada'
|
||||
: null;
|
||||
|
||||
const isLocked = lockReason !== null;
|
||||
|
||||
results.push({
|
||||
_id: ata._id,
|
||||
numero: ata.numero,
|
||||
numeroSei: ata.numeroSei,
|
||||
dataInicio: ata.dataInicio ?? null,
|
||||
dataFim: ata.dataFim ?? null,
|
||||
dataProrrogacao,
|
||||
dataFimEfetiva,
|
||||
quantidadeTotal,
|
||||
limitePercentual,
|
||||
quantidadeUsada,
|
||||
limitePermitido,
|
||||
restante,
|
||||
isLocked,
|
||||
isVencidaRecentemente,
|
||||
lockReason
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export const list = query({
|
||||
args: {
|
||||
@@ -147,89 +294,39 @@ export const getAtas = query({
|
||||
});
|
||||
|
||||
export const getAtasComLimite = query({
|
||||
args: { objetoId: v.id('objetos') },
|
||||
args: {
|
||||
objetoId: v.id('objetos'),
|
||||
// Permite incluir atas específicas (ex.: já selecionadas em um pedido antigo),
|
||||
// mesmo que não estejam vigentes ou não tenham vencido nos últimos 3 meses.
|
||||
includeAtaIds: v.optional(v.array(v.id('atas')))
|
||||
},
|
||||
returns: v.array(ataComLimiteValidator),
|
||||
handler: async (ctx, args) => {
|
||||
return await computeAtasComLimiteForObjeto(ctx, args.objetoId, args.includeAtaIds);
|
||||
}
|
||||
});
|
||||
|
||||
export const getAtasComLimiteBatch = query({
|
||||
args: {
|
||||
objetoIds: v.array(v.id('objetos')),
|
||||
includeAtaIds: v.optional(v.array(v.id('atas')))
|
||||
},
|
||||
returns: v.array(
|
||||
v.object({
|
||||
_id: v.id('atas'),
|
||||
numero: v.string(),
|
||||
numeroSei: v.string(),
|
||||
dataInicio: v.union(v.string(), v.null()),
|
||||
dataFim: v.union(v.string(), v.null()),
|
||||
quantidadeTotal: v.union(v.number(), v.null()),
|
||||
limitePercentual: v.number(),
|
||||
quantidadeUsada: v.number(),
|
||||
limitePermitido: v.number(),
|
||||
restante: v.number(),
|
||||
isLocked: v.boolean()
|
||||
objetoId: v.id('objetos'),
|
||||
atas: v.array(ataComLimiteValidator)
|
||||
})
|
||||
),
|
||||
handler: async (ctx, args) => {
|
||||
const links = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_objetoId', (q) => q.eq('objetoId', args.objetoId))
|
||||
.collect();
|
||||
if (args.objetoIds.length === 0) return [];
|
||||
const include = args.includeAtaIds ?? [];
|
||||
|
||||
const results = [];
|
||||
for (const link of links) {
|
||||
const ata = await ctx.db.get(link.ataId);
|
||||
if (!ata) continue;
|
||||
|
||||
let quantidadeUsada = link.quantidadeUsada ?? 0;
|
||||
if (link.quantidadeUsada === undefined) {
|
||||
const items = await ctx.db
|
||||
.query('objetoItems')
|
||||
.withIndex('by_ataId_and_objetoId', (q) =>
|
||||
q.eq('ataId', link.ataId).eq('objetoId', link.objetoId)
|
||||
)
|
||||
.collect();
|
||||
|
||||
const sumByPedidoId = new Map<Id<'pedidos'>, number>();
|
||||
for (const item of items) {
|
||||
const prev = sumByPedidoId.get(item.pedidoId) ?? 0;
|
||||
sumByPedidoId.set(item.pedidoId, prev + item.quantidade);
|
||||
}
|
||||
|
||||
let total = 0;
|
||||
for (const [pedidoId, sum] of sumByPedidoId.entries()) {
|
||||
const pedido = await ctx.db.get(pedidoId);
|
||||
if (pedido && pedido.status !== 'cancelado') {
|
||||
total += sum;
|
||||
}
|
||||
}
|
||||
quantidadeUsada = total;
|
||||
}
|
||||
|
||||
const quantidadeTotal = link.quantidadeTotal ?? null;
|
||||
const limitePercentualRaw = link.limitePercentual ?? 50;
|
||||
const limitePercentual = Number.isFinite(limitePercentualRaw)
|
||||
? Math.min(100, Math.max(0, limitePercentualRaw))
|
||||
: 50;
|
||||
|
||||
const limitePermitido =
|
||||
quantidadeTotal && quantidadeTotal > 0
|
||||
? Math.floor(quantidadeTotal * (limitePercentual / 100))
|
||||
: 0;
|
||||
|
||||
const restante = Math.max(0, limitePermitido - quantidadeUsada);
|
||||
const isLocked =
|
||||
!quantidadeTotal || quantidadeTotal <= 0 || quantidadeUsada >= limitePermitido;
|
||||
|
||||
results.push({
|
||||
_id: ata._id,
|
||||
numero: ata.numero,
|
||||
numeroSei: ata.numeroSei,
|
||||
dataInicio: ata.dataInicio ?? null,
|
||||
dataFim: ata.dataFim ?? null,
|
||||
quantidadeTotal,
|
||||
limitePercentual,
|
||||
quantidadeUsada,
|
||||
limitePermitido,
|
||||
restante,
|
||||
isLocked
|
||||
});
|
||||
const out = [];
|
||||
for (const objetoId of args.objetoIds) {
|
||||
const atas = await computeAtasComLimiteForObjeto(ctx, objetoId, include);
|
||||
out.push({ objetoId, atas });
|
||||
}
|
||||
|
||||
return results;
|
||||
return out;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user