feat: Add prefill functionality for pedidos and enhance item matching logic with modalidade support
This commit is contained in:
@@ -145,9 +145,20 @@ export const getHistory = query({
|
||||
|
||||
export const checkExisting = query({
|
||||
args: {
|
||||
acaoId: v.optional(v.id('acoes')), // Used to filter items
|
||||
numeroSei: v.optional(v.string()),
|
||||
objetoIds: v.optional(v.array(v.id('objetos')))
|
||||
itensFiltro: v.optional(
|
||||
v.array(
|
||||
v.object({
|
||||
objetoId: v.id('objetos'),
|
||||
modalidade: v.union(
|
||||
v.literal('dispensa'),
|
||||
v.literal('inexgibilidade'),
|
||||
v.literal('adesao'),
|
||||
v.literal('consumo')
|
||||
)
|
||||
})
|
||||
)
|
||||
)
|
||||
},
|
||||
returns: v.array(
|
||||
v.object({
|
||||
@@ -170,6 +181,12 @@ export const checkExisting = query({
|
||||
v.array(
|
||||
v.object({
|
||||
objetoId: v.id('objetos'),
|
||||
modalidade: v.union(
|
||||
v.literal('dispensa'),
|
||||
v.literal('inexgibilidade'),
|
||||
v.literal('adesao'),
|
||||
v.literal('consumo')
|
||||
),
|
||||
quantidade: v.number()
|
||||
})
|
||||
)
|
||||
@@ -200,38 +217,36 @@ export const checkExisting = query({
|
||||
return true;
|
||||
});
|
||||
|
||||
// 3) Filtro por acaoId (via items)
|
||||
if (args.acaoId) {
|
||||
// This is expensive, but for now we iterate. Better would be to query items by acaoId first.
|
||||
// Optimization: Query items by acaoId and get unique pedidoIds.
|
||||
const itemsComAcao = await ctx.db
|
||||
.query('objetoItems')
|
||||
.withIndex('by_acaoId', (q) => q.eq('acaoId', args.acaoId))
|
||||
.collect();
|
||||
|
||||
const pedidoIdsComAcao = new Set(itemsComAcao.map((i) => i.pedidoId));
|
||||
pedidosAbertos = pedidosAbertos.filter((p) => pedidoIdsComAcao.has(p._id));
|
||||
}
|
||||
|
||||
// 4) Filtro por objetos (se informado) e coleta de matchingItems
|
||||
// 3) Filtro por itens (objetoId + modalidade), se informado, e coleta de matchingItems
|
||||
const resultados = [];
|
||||
|
||||
const itensFiltro = args.itensFiltro ?? [];
|
||||
|
||||
for (const pedido of pedidosAbertos) {
|
||||
let include = true;
|
||||
let matchingItems: { objetoId: Id<'objetos'>; quantidade: number }[] = [];
|
||||
let matchingItems: {
|
||||
objetoId: Id<'objetos'>;
|
||||
modalidade: Doc<'objetoItems'>['modalidade'];
|
||||
quantidade: number;
|
||||
}[] = [];
|
||||
|
||||
// Se houver filtro de objetos, verificamos se o pedido tem ALGUM dos objetos
|
||||
if (args.objetoIds && args.objetoIds.length > 0) {
|
||||
// Se houver filtro de itens, verificamos se o pedido tem ALGUM dos itens (objetoId + modalidade)
|
||||
if (itensFiltro.length > 0) {
|
||||
const items = await ctx.db
|
||||
.query('objetoItems')
|
||||
.withIndex('by_pedidoId', (q) => q.eq('pedidoId', pedido._id))
|
||||
.collect();
|
||||
|
||||
const matching = items.filter((i) => args.objetoIds?.includes(i.objetoId));
|
||||
const matching = items.filter((i) =>
|
||||
itensFiltro.some(
|
||||
(f) => f.objetoId === i.objetoId && f.modalidade === i.modalidade
|
||||
)
|
||||
);
|
||||
|
||||
if (matching.length > 0) {
|
||||
matchingItems = matching.map((i) => ({
|
||||
objetoId: i.objetoId,
|
||||
modalidade: i.modalidade,
|
||||
quantidade: i.quantidade
|
||||
}));
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user