feat: update requisition approval process in 'Almoxarifado' to prevent approval with stock issues, enhance error handling, and conditionally render approval button based on stock availability
This commit is contained in:
@@ -1196,35 +1196,8 @@ export const aprovarRequisicao = mutation({
|
||||
throw new Error('Apenas requisições pendentes podem ser aprovadas');
|
||||
}
|
||||
|
||||
// Verificar estoque antes de aprovar
|
||||
const itens = await ctx.db
|
||||
.query('requisicaoItens')
|
||||
.withIndex('by_requisicaoId', (q) => q.eq('requisicaoId', args.id))
|
||||
.collect();
|
||||
|
||||
const problemasEstoque: Array<{
|
||||
materialNome: string;
|
||||
quantidadeSolicitada: number;
|
||||
estoqueAtual: number;
|
||||
}> = [];
|
||||
|
||||
for (const item of itens) {
|
||||
const material = await ctx.db.get(item.materialId);
|
||||
if (!material) continue;
|
||||
|
||||
if (material.estoqueAtual < item.quantidadeSolicitada) {
|
||||
problemasEstoque.push({
|
||||
materialNome: material.nome,
|
||||
quantidadeSolicitada: item.quantidadeSolicitada,
|
||||
estoqueAtual: material.estoqueAtual
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (problemasEstoque.length > 0) {
|
||||
const mensagem = `Estoque insuficiente para os seguintes materiais:\n${problemasEstoque.map(p => `- ${p.materialNome}: solicitado ${p.quantidadeSolicitada}, disponível ${p.estoqueAtual}`).join('\n')}`;
|
||||
throw new Error(mensagem);
|
||||
}
|
||||
// Nota: A verificação de estoque é feita no frontend antes de permitir a aprovação
|
||||
// O botão de aprovar só aparece quando há estoque suficiente
|
||||
|
||||
const usuario = await getCurrentUserFunction(ctx);
|
||||
if (!usuario) throw new Error('Usuário não autenticado');
|
||||
|
||||
Reference in New Issue
Block a user