feat: enhance ata and objeto management by adding configuration options for quantity limits and usage tracking, improving data integrity and user feedback in pedidos

This commit is contained in:
2025-12-16 14:20:31 -03:00
parent fd2669aa4f
commit f90b27648f
8 changed files with 618 additions and 55 deletions

View File

@@ -44,6 +44,11 @@
dataFim: ''
});
let selectedObjetos = $state<Id<'objetos'>[]>([]);
type ObjetoAtaConfig = {
quantidadeTotal: number | undefined;
limitePercentual: number | undefined;
};
let objetosConfig = $state<Record<string, ObjetoAtaConfig>>({});
let searchObjeto = $state('');
let saving = $state(false);
@@ -73,6 +78,15 @@
const linkedObjetos = await client.query(api.atas.getObjetos, { id: ata._id });
selectedObjetos = linkedObjetos.map((o) => o._id);
const linkedConfigs = await client.query(api.atas.getObjetosConfig, { id: ata._id });
objetosConfig = {};
for (const cfg of linkedConfigs) {
objetosConfig[String(cfg.objetoId)] = {
quantidadeTotal: cfg.quantidadeTotal ?? undefined,
limitePercentual: cfg.limitePercentual ?? 50
};
}
// Fetch attachments
attachments = await client.query(api.atas.getDocumentos, { ataId: ata._id });
} else {
@@ -85,6 +99,7 @@
dataFim: ''
};
selectedObjetos = [];
objetosConfig = {};
attachments = [];
}
attachmentFiles = [];
@@ -97,11 +112,22 @@
editingId = null;
}
function getObjetoConfig(id: Id<'objetos'>): ObjetoAtaConfig {
const key = String(id);
if (!objetosConfig[key]) {
objetosConfig[key] = { quantidadeTotal: 1, limitePercentual: 50 };
}
return objetosConfig[key];
}
function toggleObjeto(id: Id<'objetos'>) {
const key = String(id);
if (selectedObjetos.includes(id)) {
selectedObjetos = selectedObjetos.filter((oid) => oid !== id);
delete objetosConfig[key];
} else {
selectedObjetos = [...selectedObjetos, id];
objetosConfig[key] = { quantidadeTotal: 1, limitePercentual: 50 };
}
}
@@ -124,13 +150,37 @@
}
saving = true;
try {
const objetos = selectedObjetos.map((objetoId) => {
const cfg = objetosConfig[String(objetoId)];
if (
!cfg ||
cfg.quantidadeTotal === undefined ||
!Number.isFinite(cfg.quantidadeTotal) ||
cfg.quantidadeTotal <= 0
) {
throw new Error(
'Informe a quantidade (maior que zero) para todos os objetos vinculados.'
);
}
const limitePercentual =
cfg.limitePercentual === undefined || !Number.isFinite(cfg.limitePercentual)
? 50
: cfg.limitePercentual;
return {
objetoId,
quantidadeTotal: cfg.quantidadeTotal,
limitePercentual
};
});
const payload = {
numero: formData.numero,
numeroSei: formData.numeroSei,
empresaId: formData.empresaId as Id<'empresas'>,
dataInicio: formData.dataInicio || undefined,
dataFim: formData.dataFim || undefined,
objetosIds: selectedObjetos
objetos
};
let ataId: Id<'atas'>;
@@ -537,6 +587,54 @@
{/if}
</div>
{#if selectedObjetos.length > 0}
<div class="border-base-300 border-t pt-4">
<div class="font-semibold">Configuração por objeto</div>
<p class="text-base-content/60 mt-1 text-xs">
Defina a quantidade total do objeto na ata e o limite de uso em % (padrão 50%).
</p>
<div class="mt-3 max-h-52 space-y-3 overflow-y-auto">
{#each selectedObjetos as objetoId (objetoId)}
{@const obj = objetos.find((o) => o._id === objetoId)}
{@const cfg = getObjetoConfig(objetoId)}
<div class="border-base-300 rounded-lg border p-3">
<div class="text-sm font-semibold">{obj?.nome || 'Objeto'}</div>
<div class="mt-3 grid grid-cols-1 gap-3 md:grid-cols-2">
<div class="form-control w-full">
<label class="label" for={`qtd_${objetoId}`}>
<span class="label-text text-xs font-semibold">Quantidade na ata</span
>
</label>
<input
id={`qtd_${objetoId}`}
class="input input-bordered input-sm focus:input-primary w-full"
type="number"
min="1"
required
bind:value={cfg.quantidadeTotal}
/>
</div>
<div class="form-control w-full">
<label class="label" for={`limite_${objetoId}`}>
<span class="label-text text-xs font-semibold">Limite (%)</span>
</label>
<input
id={`limite_${objetoId}`}
class="input input-bordered input-sm focus:input-primary w-full"
type="number"
min="0"
max="100"
placeholder="50"
bind:value={cfg.limitePercentual}
/>
</div>
</div>
</div>
{/each}
</div>
</div>
{/if}
<div class="border-base-300 border-t pt-4">
<div class="font-semibold">Anexos</div>
<div class="mt-2 space-y-2">

View File

@@ -1,6 +1,7 @@
<script lang="ts">
import { api } from '@sgse-app/backend/convex/_generated/api';
import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel';
import type { FunctionReturnType } from 'convex/server';
import { useConvexClient, useQuery } from 'convex-svelte';
import { SvelteSet } from 'svelte/reactivity';
import { toast } from 'svelte-sonner';
@@ -54,7 +55,12 @@
type Modalidade = 'dispensa' | 'inexgibilidade' | 'adesao' | 'consumo';
function coerceModalidade(value: string): Modalidade {
if (value === 'dispensa' || value === 'inexgibilidade' || value === 'adesao' || value === 'consumo') {
if (
value === 'dispensa' ||
value === 'inexgibilidade' ||
value === 'adesao' ||
value === 'consumo'
) {
return value;
}
return 'consumo';
@@ -77,7 +83,8 @@
};
// Atas por objeto (carregadas sob demanda)
let atasPorObjeto = $state<Record<string, Array<Doc<'atas'>>>>({});
type AtasComLimite = FunctionReturnType<typeof api.objetos.getAtasComLimite>;
let atasPorObjeto = $state<Record<string, AtasComLimite>>({});
let editingItems = $state<Record<string, EditingItem>>({});
@@ -235,7 +242,9 @@
let solicitacaoDocsRequestId = $state<Id<'solicitacoesItens'> | null>(null);
let solicitacaoDocsSolicitadoPor = $state<Id<'funcionarios'> | null>(null);
let solicitacaoDocsTipo = $state<string | null>(null);
let solicitacaoDocs = $state<any[]>([]);
let solicitacaoDocs = $state<FunctionReturnType<typeof api.pedidos.listSolicitacaoDocumentos>>(
[]
);
let carregandoSolicitacaoDocs = $state(false);
let solicitacaoDocumentoDescricao = $state('');
@@ -651,19 +660,16 @@
async function loadAtasForObjeto(objetoId: string) {
if (atasPorObjeto[objetoId]) return;
try {
const linkedAtas = await client.query(api.objetos.getAtas, {
const linkedAtas = await client.query(api.objetos.getAtasComLimite, {
objetoId: objetoId as Id<'objetos'>
});
atasPorObjeto = {
...atasPorObjeto,
[objetoId]: linkedAtas
};
atasPorObjeto[objetoId] = linkedAtas;
} catch (e) {
console.error('Erro ao carregar atas para objeto', objetoId, e);
}
}
function getAtasForObjeto(objetoId: string): Array<Doc<'atas'>> {
function getAtasForObjeto(objetoId: string): AtasComLimite {
return atasPorObjeto[objetoId] || [];
}
@@ -1464,7 +1470,9 @@
<XCircle size={16} />
</button>
{:else}
<span class="ml-2 self-center text-xs text-gray-400">Aguardando Análise</span>
<span class="ml-2 self-center text-xs text-gray-400"
>Aguardando Análise</span
>
{/if}
</div>
</td>
@@ -1563,7 +1571,15 @@
>
<option value="">Nenhuma</option>
{#each getAtasForObjeto(newItem.objetoId) as ata (ata._id)}
<option value={ata._id}>Ata {ata.numero} (SEI: {ata.numeroSei})</option>
{@const isSelectedAta = String(ata._id) === newItem.ataId}
{@const reason = !ata.quantidadeTotal
? 'não configurada'
: ata.quantidadeUsada >= ata.limitePermitido
? 'limite atingido'
: null}
<option value={ata._id} disabled={ata.isLocked && !isSelectedAta}>
Ata {ata.numero} (SEI: {ata.numeroSei}){reason ? ` (${reason})` : ''}
</option>
{/each}
</select>
</div>
@@ -1810,7 +1826,16 @@
>
<option value="">Nenhuma</option>
{#each getAtasForObjeto(item.objetoId) as ata (ata._id)}
<option value={ata._id}>Ata {ata.numero} (SEI: {ata.numeroSei})</option>
{@const currentAtaId = ensureEditingItem(item).ataId}
{@const isSelectedAta = String(ata._id) === currentAtaId}
{@const reason = !ata.quantidadeTotal
? 'não configurada'
: ata.quantidadeUsada >= ata.limitePermitido
? 'limite atingido'
: null}
<option value={ata._id} disabled={ata.isLocked && !isSelectedAta}>
Ata {ata.numero} (SEI: {ata.numeroSei}){reason ? ` (${reason})` : ''}
</option>
{/each}
</select>
{:else if item.ataId}

View File

@@ -1,6 +1,7 @@
<script lang="ts">
import { api } from '@sgse-app/backend/convex/_generated/api';
import type { Doc, Id } from '@sgse-app/backend/convex/_generated/dataModel';
import type { FunctionReturnType } from 'convex/server';
import { useConvexClient, useQuery } from 'convex-svelte';
import { Plus, Trash2, X, Info } from 'lucide-svelte';
import { goto } from '$app/navigation';
@@ -32,7 +33,7 @@
acaoId?: Id<'acoes'>;
ataId?: Id<'atas'>;
ataNumero?: string; // For display
ata?: Doc<'atas'>; // Full ata object for details
ata?: FunctionReturnType<typeof api.objetos.getAtasComLimite>[number]; // dados mínimos p/ exibir detalhes
};
let selectedItems = $state<SelectedItem[]>([]);
@@ -55,7 +56,8 @@
ataId: ''
});
let availableAtas = $state<Doc<'atas'>[]>([]);
type AtasComLimite = FunctionReturnType<typeof api.objetos.getAtasComLimite>;
let availableAtas = $state<AtasComLimite>([]);
// Item Details Modal
let showDetailsModal = $state(false);
@@ -73,7 +75,7 @@
async function openItemModal(objeto: Doc<'objetos'>) {
// Fetch linked Atas for this object
const linkedAtas = await client.query(api.objetos.getAtas, { objetoId: objeto._id });
const linkedAtas = await client.query(api.objetos.getAtasComLimite, { objetoId: objeto._id });
availableAtas = linkedAtas;
itemConfig = {
@@ -226,11 +228,11 @@
return null;
}
function buildPedidoHref(pedido: (typeof existingPedidos)[0]) {
function buildPedidoHref(pedido: (typeof existingPedidos)[0]): `/pedidos/${string}` {
const matchedItem = getFirstMatchingSelectedItem(pedido);
if (!matchedItem) {
return resolve(`/pedidos/${pedido._id}`);
return `/pedidos/${pedido._id}` as `/pedidos/${string}`;
}
const params = new URLSearchParams();
@@ -246,7 +248,7 @@
params.set('ata', matchedItem.ataId);
}
return resolve(`/pedidos/${pedido._id}?${params.toString()}`);
return `/pedidos/${pedido._id}?${params.toString()}` as `/pedidos/${string}`;
}
async function checkExisting() {
@@ -505,25 +507,21 @@
<p class="mb-3 font-semibold text-yellow-900">Pedidos similares encontrados:</p>
<ul class="space-y-2">
{#each existingPedidos as pedido (pedido._id)}
{@const first = getFirstMatchingSelectedItem(pedido)}
<li class="flex flex-col rounded-lg bg-white px-4 py-3 shadow-sm">
<div class="flex items-center justify-between gap-3">
<div class="space-y-1">
<p class="text-sm font-medium text-gray-900">
Pedido {pedido.numeroSei || 'sem número SEI'}{formatStatus(pedido.status)}
</p>
{#if getFirstMatchingSelectedItem(pedido)}
{#key pedido._id}
{#if getFirstMatchingSelectedItem(pedido)}
<span
class={`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold ${getModalidadeBadgeClasses(
getFirstMatchingSelectedItem(pedido).modalidade
)}`}
>
Modalidade:{' '}
{formatModalidade(getFirstMatchingSelectedItem(pedido).modalidade)}
</span>
{/if}
{/key}
{#if first}
<span
class={`inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold ${getModalidadeBadgeClasses(
first.modalidade
)}`}
>
Modalidade: {formatModalidade(first.modalidade)}
</span>
{/if}
{#if getMatchingInfo(pedido)}
<p class="mt-1 text-xs text-blue-700">
@@ -532,7 +530,7 @@
{/if}
</div>
<a
href={buildPedidoHref(pedido)}
href={resolve(buildPedidoHref(pedido))}
class="text-sm font-medium text-blue-600 hover:text-blue-800"
>
Abrir
@@ -637,7 +635,14 @@
>
<option value="">Nenhuma</option>
{#each availableAtas as ata (ata._id)}
<option value={ata._id}>Ata {ata.numero} (SEI: {ata.numeroSei})</option>
{@const reason = !ata.quantidadeTotal
? 'não configurada'
: ata.quantidadeUsada >= ata.limitePermitido
? 'limite atingido'
: null}
<option value={ata._id} disabled={ata.isLocked}>
Ata {ata.numero} (SEI: {ata.numeroSei}){reason ? ` (${reason})` : ''}
</option>
{/each}
</select>
</div>