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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -76,6 +76,7 @@ export const getItems = query({
|
||||
_creationTime: v.number(),
|
||||
pedidoId: v.id('pedidos'),
|
||||
objetoId: v.id('objetos'),
|
||||
ataId: v.optional(v.id('atas')),
|
||||
acaoId: v.optional(v.id('acoes')),
|
||||
modalidade: v.union(
|
||||
v.literal('dispensa'),
|
||||
@@ -341,6 +342,7 @@ export const addItem = mutation({
|
||||
args: {
|
||||
pedidoId: v.id('pedidos'),
|
||||
objetoId: v.id('objetos'),
|
||||
ataId: v.optional(v.id('atas')),
|
||||
acaoId: v.optional(v.id('acoes')),
|
||||
modalidade: v.union(
|
||||
v.literal('dispensa'),
|
||||
@@ -369,6 +371,7 @@ export const addItem = mutation({
|
||||
q.eq(q.field('objetoId'), args.objetoId),
|
||||
q.eq(q.field('adicionadoPor'), user.funcionarioId),
|
||||
q.eq(q.field('acaoId'), args.acaoId),
|
||||
q.eq(q.field('ataId'), args.ataId),
|
||||
q.eq(q.field('modalidade'), args.modalidade)
|
||||
)
|
||||
)
|
||||
@@ -395,6 +398,7 @@ export const addItem = mutation({
|
||||
await ctx.db.insert('objetoItems', {
|
||||
pedidoId: args.pedidoId,
|
||||
objetoId: args.objetoId,
|
||||
ataId: args.ataId,
|
||||
acaoId: args.acaoId,
|
||||
modalidade: args.modalidade,
|
||||
valorEstimado: args.valorEstimado,
|
||||
@@ -412,6 +416,7 @@ export const addItem = mutation({
|
||||
valor: args.valorEstimado,
|
||||
quantidade: args.quantidade,
|
||||
acaoId: args.acaoId,
|
||||
ataId: args.ataId,
|
||||
modalidade: args.modalidade
|
||||
}),
|
||||
data: Date.now()
|
||||
|
||||
@@ -15,5 +15,12 @@ export const atasTables = {
|
||||
})
|
||||
.index('by_numero', ['numero'])
|
||||
.index('by_empresaId', ['empresaId'])
|
||||
.index('by_numeroSei', ['numeroSei'])
|
||||
.index('by_numeroSei', ['numeroSei']),
|
||||
|
||||
atasObjetos: defineTable({
|
||||
ataId: v.id('atas'),
|
||||
objetoId: v.id('objetos')
|
||||
})
|
||||
.index('by_ataId', ['ataId'])
|
||||
.index('by_objetoId', ['objetoId'])
|
||||
};
|
||||
|
||||
@@ -24,6 +24,7 @@ export const pedidosTables = {
|
||||
objetoItems: defineTable({
|
||||
pedidoId: v.id('pedidos'),
|
||||
objetoId: v.id('objetos'), // was produtoId
|
||||
ataId: v.optional(v.id('atas')),
|
||||
acaoId: v.optional(v.id('acoes')), // Moved from pedidos
|
||||
modalidade: v.union(
|
||||
v.literal('dispensa'),
|
||||
|
||||
Reference in New Issue
Block a user