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

77 lines
1.7 KiB
TypeScript

import { v } from 'convex/values';
import { mutation, query } from './_generated/server';
import { getCurrentUserFunction } from './auth';
import { internal } from './_generated/api';
export const list = query({
args: {},
handler: async (ctx) => {
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
recurso: 'acoes',
acao: 'listar'
});
return await ctx.db.query('acoes').collect();
}
});
export const create = mutation({
args: {
nome: v.string(),
tipo: v.union(v.literal('projeto'), v.literal('lei'))
},
handler: async (ctx, args) => {
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
recurso: 'acoes',
acao: 'criar'
});
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
return await ctx.db.insert('acoes', {
...args,
criadoPor: user._id,
criadoEm: Date.now()
});
}
});
export const update = mutation({
args: {
id: v.id('acoes'),
nome: v.string(),
tipo: v.union(v.literal('projeto'), v.literal('lei'))
},
handler: async (ctx, args) => {
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
recurso: 'acoes',
acao: 'editar'
});
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
await ctx.db.patch(args.id, {
nome: args.nome,
tipo: args.tipo
});
}
});
export const remove = mutation({
args: {
id: v.id('acoes')
},
handler: async (ctx, args) => {
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
recurso: 'acoes',
acao: 'excluir'
});
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
await ctx.db.delete(args.id);
}
});