Files
sgse-app/packages/backend/convex/atas.ts

78 lines
1.7 KiB
TypeScript

import { v } from 'convex/values';
import { mutation, query } from './_generated/server';
import { getCurrentUserFunction } from './auth';
export const list = query({
args: {},
handler: async (ctx) => {
return await ctx.db.query('atas').collect();
}
});
export const get = query({
args: { id: v.id('atas') },
handler: async (ctx, args) => {
return await ctx.db.get(args.id);
}
});
export const create = mutation({
args: {
numero: v.string(),
dataInicio: v.optional(v.string()),
dataFim: v.optional(v.string()),
empresaId: v.id('empresas'),
pdf: v.optional(v.string()),
numeroSei: v.string()
},
handler: async (ctx, args) => {
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
return await ctx.db.insert('atas', {
...args,
criadoPor: user._id,
criadoEm: Date.now(),
atualizadoEm: Date.now()
});
}
});
export const update = mutation({
args: {
id: v.id('atas'),
numero: v.string(),
dataInicio: v.optional(v.string()),
dataFim: v.optional(v.string()),
empresaId: v.id('empresas'),
pdf: v.optional(v.string()),
numeroSei: v.string()
},
handler: async (ctx, args) => {
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
await ctx.db.patch(args.id, {
numero: args.numero,
dataInicio: args.dataInicio,
dataFim: args.dataFim,
empresaId: args.empresaId,
pdf: args.pdf,
numeroSei: args.numeroSei,
atualizadoEm: Date.now()
});
}
});
export const remove = mutation({
args: {
id: v.id('atas')
},
handler: async (ctx, args) => {
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
await ctx.db.delete(args.id);
}
});