feat: Implement Ata de Registro de Preços management and linking to objetos and pedidos
This commit is contained in:
@@ -27,17 +27,35 @@ export const create = mutation({
|
||||
codigoEfisco: v.string(),
|
||||
codigoCatmat: v.optional(v.string()),
|
||||
codigoCatserv: v.optional(v.string()),
|
||||
unidade: v.string()
|
||||
unidade: v.string(),
|
||||
atas: v.optional(v.array(v.id('atas')))
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const user = await getCurrentUserFunction(ctx);
|
||||
if (!user) throw new Error('Unauthorized');
|
||||
|
||||
return await ctx.db.insert('objetos', {
|
||||
...args,
|
||||
const objetoId = await ctx.db.insert('objetos', {
|
||||
nome: args.nome,
|
||||
valorEstimado: args.valorEstimado,
|
||||
tipo: args.tipo,
|
||||
codigoEfisco: args.codigoEfisco,
|
||||
codigoCatmat: args.codigoCatmat,
|
||||
codigoCatserv: args.codigoCatserv,
|
||||
unidade: args.unidade,
|
||||
criadoPor: user._id,
|
||||
criadoEm: Date.now()
|
||||
});
|
||||
|
||||
if (args.atas) {
|
||||
for (const ataId of args.atas) {
|
||||
await ctx.db.insert('atasObjetos', {
|
||||
ataId,
|
||||
objetoId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return objetoId;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -50,7 +68,8 @@ export const update = mutation({
|
||||
codigoEfisco: v.string(),
|
||||
codigoCatmat: v.optional(v.string()),
|
||||
codigoCatserv: v.optional(v.string()),
|
||||
unidade: v.string()
|
||||
unidade: v.string(),
|
||||
atas: v.optional(v.array(v.id('atas')))
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const user = await getCurrentUserFunction(ctx);
|
||||
@@ -65,6 +84,39 @@ export const update = mutation({
|
||||
codigoCatserv: args.codigoCatserv,
|
||||
unidade: args.unidade
|
||||
});
|
||||
|
||||
if (args.atas !== undefined) {
|
||||
// Remove existing links
|
||||
const existingLinks = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_objetoId', (q) => q.eq('objetoId', args.id))
|
||||
.collect();
|
||||
|
||||
for (const link of existingLinks) {
|
||||
await ctx.db.delete(link._id);
|
||||
}
|
||||
|
||||
// Add new links
|
||||
for (const ataId of args.atas) {
|
||||
await ctx.db.insert('atasObjetos', {
|
||||
ataId,
|
||||
objetoId: args.id
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const getAtas = query({
|
||||
args: { objetoId: v.id('objetos') },
|
||||
handler: async (ctx, args) => {
|
||||
const links = await ctx.db
|
||||
.query('atasObjetos')
|
||||
.withIndex('by_objetoId', (q) => q.eq('objetoId', args.objetoId))
|
||||
.collect();
|
||||
|
||||
const atas = await Promise.all(links.map((link) => ctx.db.get(link.ataId)));
|
||||
return atas.filter((ata) => ata !== null);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user