feat: Refactor order flow initiation logic and enhance UI button styles for better user experience
This commit is contained in:
@@ -20,6 +20,59 @@ async function getUsuarioAutenticado(ctx: QueryCtx | MutationCtx) {
|
||||
return usuario;
|
||||
}
|
||||
|
||||
export async function iniciarFluxoPedidoInternal(
|
||||
ctx: MutationCtx,
|
||||
pedidoId: Id<'pedidos'>,
|
||||
usuarioId: Id<'usuarios'>
|
||||
) {
|
||||
const pedido = await ctx.db.get(pedidoId);
|
||||
if (!pedido) {
|
||||
throw new Error('Pedido não encontrado');
|
||||
}
|
||||
|
||||
// Verificar se já tem histórico
|
||||
const historicoExistente = await ctx.db
|
||||
.query('pedidoEtapasHistorico')
|
||||
.withIndex('by_pedidoId', (q) => q.eq('pedidoId', pedidoId))
|
||||
.first();
|
||||
|
||||
if (historicoExistente) {
|
||||
return historicoExistente._id;
|
||||
}
|
||||
|
||||
// Buscar primeira etapa do fluxo
|
||||
const primeiraEtapa = await ctx.db.query('pedidoFluxoEtapa').withIndex('by_ordem').first();
|
||||
|
||||
if (!primeiraEtapa) {
|
||||
console.warn('Nenhuma etapa configurada no fluxo. Timeline ficará vazia.');
|
||||
return null;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
// Criar primeiro registro
|
||||
const historicoId = await ctx.db.insert('pedidoEtapasHistorico', {
|
||||
pedidoId,
|
||||
etapaId: primeiraEtapa._id,
|
||||
inicioData: now,
|
||||
atual: true
|
||||
});
|
||||
|
||||
// Registrar no histórico
|
||||
await ctx.db.insert('historicoPedidos', {
|
||||
pedidoId,
|
||||
usuarioId,
|
||||
acao: 'inicio_fluxo',
|
||||
detalhes: JSON.stringify({
|
||||
etapa: primeiraEtapa.codigo,
|
||||
etapaNome: primeiraEtapa.nome
|
||||
}),
|
||||
data: now
|
||||
});
|
||||
|
||||
return historicoId;
|
||||
}
|
||||
|
||||
async function getEtapaAtualDoPedido(ctx: QueryCtx | MutationCtx, pedidoId: Id<'pedidos'>) {
|
||||
const etapaAtual = await ctx.db
|
||||
.query('pedidoEtapasHistorico')
|
||||
@@ -720,55 +773,10 @@ export const iniciarFluxoPedido = mutation({
|
||||
args: {
|
||||
pedidoId: v.id('pedidos')
|
||||
},
|
||||
returns: v.id('pedidoEtapasHistorico'),
|
||||
returns: v.union(v.id('pedidoEtapasHistorico'), v.null()),
|
||||
handler: async (ctx, args) => {
|
||||
const usuario = await getUsuarioAutenticado(ctx);
|
||||
|
||||
const pedido = await ctx.db.get(args.pedidoId);
|
||||
if (!pedido) {
|
||||
throw new Error('Pedido não encontrado');
|
||||
}
|
||||
|
||||
// Verificar se já tem histórico
|
||||
const historicoExistente = await ctx.db
|
||||
.query('pedidoEtapasHistorico')
|
||||
.withIndex('by_pedidoId', (q) => q.eq('pedidoId', args.pedidoId))
|
||||
.first();
|
||||
|
||||
if (historicoExistente) {
|
||||
throw new Error('Este pedido já possui histórico de etapas');
|
||||
}
|
||||
|
||||
// Buscar primeira etapa do fluxo
|
||||
const primeiraEtapa = await ctx.db.query('pedidoFluxoEtapa').withIndex('by_ordem').first();
|
||||
|
||||
if (!primeiraEtapa) {
|
||||
throw new Error('Nenhuma etapa configurada no fluxo');
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
// Criar primeiro registro
|
||||
const historicoId = await ctx.db.insert('pedidoEtapasHistorico', {
|
||||
pedidoId: args.pedidoId,
|
||||
etapaId: primeiraEtapa._id,
|
||||
inicioData: now,
|
||||
atual: true
|
||||
});
|
||||
|
||||
// Registrar no histórico
|
||||
await ctx.db.insert('historicoPedidos', {
|
||||
pedidoId: args.pedidoId,
|
||||
usuarioId: usuario._id,
|
||||
acao: 'inicio_fluxo',
|
||||
detalhes: JSON.stringify({
|
||||
etapa: primeiraEtapa.codigo,
|
||||
etapaNome: primeiraEtapa.nome
|
||||
}),
|
||||
data: now
|
||||
});
|
||||
|
||||
return historicoId;
|
||||
return await iniciarFluxoPedidoInternal(ctx, args.pedidoId, usuario._id);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user