|
|
|
|
@@ -33,18 +33,21 @@ export const listarMateriais = query({
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let query = ctx.db.query('materiais');
|
|
|
|
|
|
|
|
|
|
let materiais;
|
|
|
|
|
if (args.ativo !== undefined) {
|
|
|
|
|
query = query.withIndex('by_ativo', (q) => q.eq('ativo', args.ativo));
|
|
|
|
|
materiais = await ctx.db
|
|
|
|
|
.query('materiais')
|
|
|
|
|
.withIndex('by_ativo', (q) => q.eq('ativo', args.ativo!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else if (args.categoria) {
|
|
|
|
|
query = query.withIndex('by_categoria', (q) => q.eq('categoria', args.categoria));
|
|
|
|
|
materiais = await ctx.db
|
|
|
|
|
.query('materiais')
|
|
|
|
|
.withIndex('by_categoria', (q) => q.eq('categoria', args.categoria!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else {
|
|
|
|
|
query = query;
|
|
|
|
|
materiais = await ctx.db.query('materiais').collect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let materiais = await query.collect();
|
|
|
|
|
|
|
|
|
|
// Filtros adicionais
|
|
|
|
|
if (args.busca) {
|
|
|
|
|
const buscaLower = args.busca.toLowerCase();
|
|
|
|
|
@@ -98,20 +101,29 @@ export const listarMovimentacoes = query({
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let query = ctx.db.query('movimentacoesEstoque');
|
|
|
|
|
|
|
|
|
|
let movimentacoes;
|
|
|
|
|
if (args.materialId) {
|
|
|
|
|
query = query.withIndex('by_materialId', (q) => q.eq('materialId', args.materialId));
|
|
|
|
|
movimentacoes = await ctx.db
|
|
|
|
|
.query('movimentacoesEstoque')
|
|
|
|
|
.withIndex('by_materialId', (q) => q.eq('materialId', args.materialId!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else if (args.tipo) {
|
|
|
|
|
query = query.withIndex('by_tipo', (q) => q.eq('tipo', args.tipo));
|
|
|
|
|
movimentacoes = await ctx.db
|
|
|
|
|
.query('movimentacoesEstoque')
|
|
|
|
|
.withIndex('by_tipo', (q) => q.eq('tipo', args.tipo!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else if (args.funcionarioId) {
|
|
|
|
|
query = query.withIndex('by_funcionarioId', (q) => q.eq('funcionarioId', args.funcionarioId));
|
|
|
|
|
movimentacoes = await ctx.db
|
|
|
|
|
.query('movimentacoesEstoque')
|
|
|
|
|
.withIndex('by_funcionarioId', (q) => q.eq('funcionarioId', args.funcionarioId!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else {
|
|
|
|
|
query = query.withIndex('by_data');
|
|
|
|
|
movimentacoes = await ctx.db
|
|
|
|
|
.query('movimentacoesEstoque')
|
|
|
|
|
.withIndex('by_data')
|
|
|
|
|
.collect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let movimentacoes = await query.collect();
|
|
|
|
|
|
|
|
|
|
// Filtros de data
|
|
|
|
|
if (args.dataInicio) {
|
|
|
|
|
movimentacoes = movimentacoes.filter((m) => m.data >= args.dataInicio!);
|
|
|
|
|
@@ -146,18 +158,26 @@ export const listarRequisicoes = query({
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let query = ctx.db.query('requisicoesMaterial');
|
|
|
|
|
|
|
|
|
|
let requisicoes;
|
|
|
|
|
if (args.status) {
|
|
|
|
|
query = query.withIndex('by_status', (q) => q.eq('status', args.status));
|
|
|
|
|
requisicoes = await ctx.db
|
|
|
|
|
.query('requisicoesMaterial')
|
|
|
|
|
.withIndex('by_status', (q) => q.eq('status', args.status!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else if (args.solicitanteId) {
|
|
|
|
|
query = query.withIndex('by_solicitanteId', (q) => q.eq('solicitanteId', args.solicitanteId));
|
|
|
|
|
requisicoes = await ctx.db
|
|
|
|
|
.query('requisicoesMaterial')
|
|
|
|
|
.withIndex('by_solicitanteId', (q) => q.eq('solicitanteId', args.solicitanteId!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else if (args.setorId) {
|
|
|
|
|
query = query.withIndex('by_setorId', (q) => q.eq('setorId', args.setorId));
|
|
|
|
|
requisicoes = await ctx.db
|
|
|
|
|
.query('requisicoesMaterial')
|
|
|
|
|
.withIndex('by_setorId', (q) => q.eq('setorId', args.setorId!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else {
|
|
|
|
|
requisicoes = await ctx.db.query('requisicoesMaterial').collect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requisicoes = await query.collect();
|
|
|
|
|
|
|
|
|
|
// Ordenar por data de criação (mais recente primeiro)
|
|
|
|
|
requisicoes.sort((a, b) => b.criadoEm - a.criadoEm);
|
|
|
|
|
|
|
|
|
|
@@ -207,16 +227,21 @@ export const listarAlertas = query({
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let query = ctx.db.query('alertasEstoque');
|
|
|
|
|
|
|
|
|
|
let alertas;
|
|
|
|
|
if (args.status) {
|
|
|
|
|
query = query.withIndex('by_status', (q) => q.eq('status', args.status));
|
|
|
|
|
alertas = await ctx.db
|
|
|
|
|
.query('alertasEstoque')
|
|
|
|
|
.withIndex('by_status', (q) => q.eq('status', args.status!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else if (args.tipo) {
|
|
|
|
|
query = query.withIndex('by_tipo', (q) => q.eq('tipo', args.tipo));
|
|
|
|
|
alertas = await ctx.db
|
|
|
|
|
.query('alertasEstoque')
|
|
|
|
|
.withIndex('by_tipo', (q) => q.eq('tipo', args.tipo!))
|
|
|
|
|
.collect();
|
|
|
|
|
} else {
|
|
|
|
|
alertas = await ctx.db.query('alertasEstoque').collect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const alertas = await query.collect();
|
|
|
|
|
|
|
|
|
|
// Ordenar por data de criação (mais recente primeiro)
|
|
|
|
|
alertas.sort((a, b) => b.criadoEm - a.criadoEm);
|
|
|
|
|
|
|
|
|
|
@@ -486,7 +511,7 @@ export const editarMaterial = mutation({
|
|
|
|
|
if (args.codigo && args.codigo !== material.codigo) {
|
|
|
|
|
const codigoExistente = await ctx.db
|
|
|
|
|
.query('materiais')
|
|
|
|
|
.withIndex('by_codigo', (q) => q.eq('codigo', args.codigo))
|
|
|
|
|
.withIndex('by_codigo', (q) => q.eq('codigo', args.codigo!))
|
|
|
|
|
.unique();
|
|
|
|
|
|
|
|
|
|
if (codigoExistente) {
|
|
|
|
|
@@ -1100,11 +1125,13 @@ export const listarMateriaisInterno = internalQuery({
|
|
|
|
|
ativo: v.optional(v.boolean())
|
|
|
|
|
},
|
|
|
|
|
handler: async (ctx, args) => {
|
|
|
|
|
let query = ctx.db.query('materiais');
|
|
|
|
|
if (args.ativo !== undefined) {
|
|
|
|
|
query = query.withIndex('by_ativo', (q) => q.eq('ativo', args.ativo));
|
|
|
|
|
return await ctx.db
|
|
|
|
|
.query('materiais')
|
|
|
|
|
.withIndex('by_ativo', (q) => q.eq('ativo', args.ativo!))
|
|
|
|
|
.collect();
|
|
|
|
|
}
|
|
|
|
|
return await query.collect();
|
|
|
|
|
return await ctx.db.query('materiais').collect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@@ -1114,11 +1141,11 @@ export const listarAlertasPorMaterial = internalQuery({
|
|
|
|
|
status: v.optional(alertaStatus)
|
|
|
|
|
},
|
|
|
|
|
handler: async (ctx, args) => {
|
|
|
|
|
let query = ctx.db
|
|
|
|
|
const query = ctx.db
|
|
|
|
|
.query('alertasEstoque')
|
|
|
|
|
.withIndex('by_materialId', (q) => q.eq('materialId', args.materialId));
|
|
|
|
|
if (args.status) {
|
|
|
|
|
query = query.filter((q) => q.eq(q.field('status'), args.status));
|
|
|
|
|
return await query.filter((q) => q.eq(q.field('status'), args.status!)).collect();
|
|
|
|
|
}
|
|
|
|
|
return await query.collect();
|
|
|
|
|
}
|
|
|
|
|
@@ -1129,11 +1156,13 @@ export const listarAlertasInterno = internalQuery({
|
|
|
|
|
status: v.optional(alertaStatus)
|
|
|
|
|
},
|
|
|
|
|
handler: async (ctx, args) => {
|
|
|
|
|
let query = ctx.db.query('alertasEstoque');
|
|
|
|
|
if (args.status) {
|
|
|
|
|
query = query.withIndex('by_status', (q) => q.eq('status', args.status));
|
|
|
|
|
return await ctx.db
|
|
|
|
|
.query('alertasEstoque')
|
|
|
|
|
.withIndex('by_status', (q) => q.eq('status', args.status!))
|
|
|
|
|
.collect();
|
|
|
|
|
}
|
|
|
|
|
return await query.collect();
|
|
|
|
|
return await ctx.db.query('alertasEstoque').collect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|