feat: Add prefill functionality for pedidos and enhance item matching logic with modalidade support

This commit is contained in:
2025-12-03 11:31:33 -03:00
parent d86d7d8dbb
commit fb78866a0e
3 changed files with 132 additions and 26 deletions

View File

@@ -125,7 +125,11 @@
numeroSei?: string;
status: string;
criadoEm: number;
matchingItems?: { objetoId: Id<'objetos'>; quantidade: number }[];
matchingItems?: {
objetoId: Id<'objetos'>;
modalidade: SelectedItem['modalidade'];
quantidade: number;
}[];
}[]
>([]);
let checking = $state(false);
@@ -175,20 +179,64 @@
return `Contém: ${details}`;
}
function getFirstMatchingSelectedItem(pedido: (typeof existingPedidos)[0]) {
if (!pedido.matchingItems || pedido.matchingItems.length === 0) return null;
for (const match of pedido.matchingItems) {
const item = selectedItems.find(
(p) => p.objeto._id === match.objetoId && p.modalidade === match.modalidade
);
if (item) {
return item;
}
}
return null;
}
function buildPedidoHref(pedido: (typeof existingPedidos)[0]) {
const matchedItem = getFirstMatchingSelectedItem(pedido);
if (!matchedItem) {
return resolve(`/pedidos/${pedido._id}`);
}
const params = new URLSearchParams();
params.set('obj', matchedItem.objeto._id);
params.set('qtd', String(matchedItem.quantidade));
params.set('mod', matchedItem.modalidade);
if (matchedItem.acaoId) {
params.set('acao', matchedItem.acaoId);
}
if (matchedItem.ataId) {
params.set('ata', matchedItem.ataId);
}
return resolve(`/pedidos/${pedido._id}?${params.toString()}`);
}
async function checkExisting() {
warning = null;
existingPedidos = [];
const hasFilters = formData.numeroSei || selectedObjetoIds.length > 0;
const hasFilters = formData.numeroSei || selectedItems.length > 0;
if (!hasFilters) return;
checking = true;
try {
// Note: checkExisting query might need update to handle item-level acaoId if we want to filter by it.
// Currently we only filter by numeroSei and objetoIds.
const itensFiltro =
selectedItems.length > 0
? selectedItems.map((item) => ({
objetoId: item.objeto._id,
modalidade: item.modalidade
}))
: undefined;
const result = await client.query(api.pedidos.checkExisting, {
numeroSei: formData.numeroSei || undefined,
objetoIds: selectedObjetoIds.length ? (selectedObjetoIds as Id<'objetos'>[]) : undefined
itensFiltro
});
existingPedidos = result;
@@ -407,7 +455,7 @@
{/if}
</div>
<a
href={resolve(`/pedidos/${pedido._id}`)}
href={buildPedidoHref(pedido)}
class="text-sm font-medium text-blue-600 hover:text-blue-800"
>
Abrir