feat: Enhance pedidos management with detailed item linking, object search, and improved UI for item configuration and details
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
import type { Id } from './_generated/dataModel';
|
||||
import { getCurrentUserFunction } from './auth';
|
||||
|
||||
export const list = query({
|
||||
@@ -16,6 +17,47 @@ export const get = query({
|
||||
}
|
||||
});
|
||||
|
||||
export const getObjetos = query({
|
||||
args: { id: v.id('atas') },
|
||||
handler: async (ctx, args) => {
|
||||
const links = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_ataId', (q) => q.eq('ataId', args.id))
|
||||
.collect();
|
||||
|
||||
const objetos = await Promise.all(links.map((link) => ctx.db.get(link.objetoId)));
|
||||
return objetos.filter((obj) => obj !== null);
|
||||
}
|
||||
});
|
||||
|
||||
export const listByObjetoIds = query({
|
||||
args: {
|
||||
objetoIds: v.array(v.id('objetos'))
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
if (args.objetoIds.length === 0) return [];
|
||||
|
||||
// Buscar todos os vínculos ata-objeto para os objetos informados
|
||||
const links = [];
|
||||
for (const objetoId of args.objetoIds) {
|
||||
const partial = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_objetoId', (q) => q.eq('objetoId', objetoId as Id<'objetos'>))
|
||||
.collect();
|
||||
links.push(...partial);
|
||||
}
|
||||
|
||||
const ataIds = Array.from(
|
||||
new Set(links.map((l) => l.ataId as Id<'atas'>))
|
||||
);
|
||||
|
||||
if (ataIds.length === 0) return [];
|
||||
|
||||
const atas = await Promise.all(ataIds.map((id) => ctx.db.get(id)));
|
||||
return atas.filter((a): a is NonNullable<typeof a> => a !== null);
|
||||
}
|
||||
});
|
||||
|
||||
export const create = mutation({
|
||||
args: {
|
||||
numero: v.string(),
|
||||
@@ -23,18 +65,35 @@ export const create = mutation({
|
||||
dataFim: v.optional(v.string()),
|
||||
empresaId: v.id('empresas'),
|
||||
pdf: v.optional(v.string()),
|
||||
numeroSei: v.string()
|
||||
numeroSei: v.string(),
|
||||
objetosIds: v.optional(v.array(v.id('objetos')))
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const user = await getCurrentUserFunction(ctx);
|
||||
if (!user) throw new Error('Unauthorized');
|
||||
|
||||
return await ctx.db.insert('atas', {
|
||||
...args,
|
||||
const ataId = await ctx.db.insert('atas', {
|
||||
numero: args.numero,
|
||||
dataInicio: args.dataInicio,
|
||||
dataFim: args.dataFim,
|
||||
empresaId: args.empresaId,
|
||||
pdf: args.pdf,
|
||||
numeroSei: args.numeroSei,
|
||||
criadoPor: user._id,
|
||||
criadoEm: Date.now(),
|
||||
atualizadoEm: Date.now()
|
||||
});
|
||||
|
||||
if (args.objetosIds) {
|
||||
for (const objetoId of args.objetosIds) {
|
||||
await ctx.db.insert('atasObjetos', {
|
||||
ataId,
|
||||
objetoId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return ataId;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -46,7 +105,8 @@ export const update = mutation({
|
||||
dataFim: v.optional(v.string()),
|
||||
empresaId: v.id('empresas'),
|
||||
pdf: v.optional(v.string()),
|
||||
numeroSei: v.string()
|
||||
numeroSei: v.string(),
|
||||
objetosIds: v.optional(v.array(v.id('objetos')))
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const user = await getCurrentUserFunction(ctx);
|
||||
@@ -61,6 +121,26 @@ export const update = mutation({
|
||||
numeroSei: args.numeroSei,
|
||||
atualizadoEm: Date.now()
|
||||
});
|
||||
|
||||
if (args.objetosIds !== undefined) {
|
||||
// Remove existing links
|
||||
const existingLinks = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_ataId', (q) => q.eq('ataId', args.id))
|
||||
.collect();
|
||||
|
||||
for (const link of existingLinks) {
|
||||
await ctx.db.delete(link._id);
|
||||
}
|
||||
|
||||
// Add new links
|
||||
for (const objetoId of args.objetosIds) {
|
||||
await ctx.db.insert('atasObjetos', {
|
||||
ataId: args.id,
|
||||
objetoId
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -72,6 +152,16 @@ export const remove = mutation({
|
||||
const user = await getCurrentUserFunction(ctx);
|
||||
if (!user) throw new Error('Unauthorized');
|
||||
|
||||
// Remove linked objects
|
||||
const links = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_ataId', (q) => q.eq('ataId', args.id))
|
||||
.collect();
|
||||
|
||||
for (const link of links) {
|
||||
await ctx.db.delete(link._id);
|
||||
}
|
||||
|
||||
await ctx.db.delete(args.id);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -498,6 +498,71 @@ export const removeItem = mutation({
|
||||
}
|
||||
});
|
||||
|
||||
export const updateItem = mutation({
|
||||
args: {
|
||||
itemId: v.id('objetoItems'),
|
||||
valorEstimado: v.string(),
|
||||
modalidade: v.union(
|
||||
v.literal('dispensa'),
|
||||
v.literal('inexgibilidade'),
|
||||
v.literal('adesao'),
|
||||
v.literal('consumo')
|
||||
),
|
||||
acaoId: v.optional(v.id('acoes')),
|
||||
ataId: v.optional(v.id('atas'))
|
||||
},
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
const user = await getUsuarioAutenticado(ctx);
|
||||
|
||||
if (!user.funcionarioId) {
|
||||
throw new Error('Usuário não vinculado a um funcionário.');
|
||||
}
|
||||
|
||||
const item = await ctx.db.get(args.itemId);
|
||||
if (!item) throw new Error('Item não encontrado.');
|
||||
|
||||
// Apenas quem adicionou o item pode editá-lo
|
||||
const isOwner = item.adicionadoPor === user.funcionarioId;
|
||||
if (!isOwner) {
|
||||
throw new Error('Apenas quem adicionou este item pode editá-lo.');
|
||||
}
|
||||
|
||||
const oldValues = {
|
||||
valorEstimado: item.valorEstimado,
|
||||
modalidade: item.modalidade,
|
||||
acaoId: 'acaoId' in item ? item.acaoId : undefined,
|
||||
ataId: 'ataId' in item ? item.ataId : undefined
|
||||
};
|
||||
|
||||
await ctx.db.patch(args.itemId, {
|
||||
valorEstimado: args.valorEstimado,
|
||||
modalidade: args.modalidade,
|
||||
acaoId: args.acaoId,
|
||||
ataId: args.ataId
|
||||
});
|
||||
|
||||
await ctx.db.patch(item.pedidoId, { atualizadoEm: Date.now() });
|
||||
|
||||
await ctx.db.insert('historicoPedidos', {
|
||||
pedidoId: item.pedidoId,
|
||||
usuarioId: user._id,
|
||||
acao: 'edicao_item',
|
||||
detalhes: JSON.stringify({
|
||||
objetoId: item.objetoId,
|
||||
de: oldValues,
|
||||
para: {
|
||||
valorEstimado: args.valorEstimado,
|
||||
modalidade: args.modalidade,
|
||||
acaoId: args.acaoId,
|
||||
ataId: args.ataId
|
||||
}
|
||||
}),
|
||||
data: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const updateStatus = mutation({
|
||||
args: {
|
||||
pedidoId: v.id('pedidos'),
|
||||
|
||||
Reference in New Issue
Block a user