feat: implement material deletion functionality in 'Almoxarifado', including error handling for related stock movements and requests, and enhance user experience with confirmation modals
This commit is contained in:
@@ -1258,6 +1258,65 @@ export const ignorarAlerta = mutation({
|
||||
}
|
||||
});
|
||||
|
||||
export const deletarMaterial = mutation({
|
||||
args: {
|
||||
id: v.id('materiais')
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||
recurso: 'almoxarifado',
|
||||
acao: 'deletar_material'
|
||||
});
|
||||
|
||||
const material = await ctx.db.get(args.id);
|
||||
if (!material) throw new Error('Material não encontrado');
|
||||
|
||||
// Verificar se há movimentações relacionadas
|
||||
const movimentacoes = await ctx.db
|
||||
.query('movimentacoesEstoque')
|
||||
.withIndex('by_materialId', (q) => q.eq('materialId', args.id))
|
||||
.first();
|
||||
|
||||
if (movimentacoes) {
|
||||
throw new Error('Não é possível excluir material com movimentações de estoque registradas. O material possui histórico de movimentações e deve ser desativado ao invés de excluído.');
|
||||
}
|
||||
|
||||
// Verificar se há requisições relacionadas
|
||||
const requisicoes = await ctx.db
|
||||
.query('requisicaoItens')
|
||||
.withIndex('by_materialId', (q) => q.eq('materialId', args.id))
|
||||
.first();
|
||||
|
||||
if (requisicoes) {
|
||||
throw new Error('Não é possível excluir material com requisições registradas. O material possui requisições associadas e deve ser desativado ao invés de excluído.');
|
||||
}
|
||||
|
||||
// Verificar se há alertas relacionados
|
||||
const alertas = await ctx.db
|
||||
.query('alertasEstoque')
|
||||
.withIndex('by_materialId', (q) => q.eq('materialId', args.id))
|
||||
.first();
|
||||
|
||||
if (alertas) {
|
||||
// Deletar alertas relacionados
|
||||
const todosAlertas = await ctx.db
|
||||
.query('alertasEstoque')
|
||||
.withIndex('by_materialId', (q) => q.eq('materialId', args.id))
|
||||
.collect();
|
||||
|
||||
for (const alerta of todosAlertas) {
|
||||
await ctx.db.delete(alerta._id);
|
||||
}
|
||||
}
|
||||
|
||||
// Registrar histórico antes de deletar
|
||||
await registrarHistorico(ctx, 'material', args.id.toString(), 'exclusao', material, undefined);
|
||||
|
||||
// Deletar o material
|
||||
await ctx.db.delete(args.id);
|
||||
}
|
||||
});
|
||||
|
||||
// ========== INTERNAL ACTIONS ==========
|
||||
|
||||
export const registrarHistoricoAlteracao = internalMutation({
|
||||
|
||||
Reference in New Issue
Block a user