43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { v } from 'convex/values';
|
|
import { mutation, query } from './_generated/server';
|
|
import { getCurrentUserFunction } from './auth';
|
|
|
|
export const getConfig = query({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
return await ctx.db.query('config').first();
|
|
}
|
|
});
|
|
|
|
export const updateConfig = mutation({
|
|
args: {
|
|
comprasSetorId: v.optional(v.id('setores')),
|
|
financeiroSetorId: v.optional(v.id('setores')),
|
|
juridicoSetorId: v.optional(v.id('setores')),
|
|
convenioSetorId: v.optional(v.id('setores')),
|
|
programasEsportivosSetorId: v.optional(v.id('setores')),
|
|
comunicacaoSetorId: v.optional(v.id('setores')),
|
|
tiSetorId: v.optional(v.id('setores'))
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const user = await getCurrentUserFunction(ctx);
|
|
if (!user) throw new Error('Unauthorized');
|
|
|
|
const existingConfig = await ctx.db.query('config').first();
|
|
|
|
const updateData = {
|
|
...args,
|
|
atualizadoEm: Date.now()
|
|
};
|
|
|
|
if (existingConfig) {
|
|
await ctx.db.patch(existingConfig._id, updateData);
|
|
} else {
|
|
await ctx.db.insert('config', {
|
|
...updateData,
|
|
criadoPor: user._id
|
|
});
|
|
}
|
|
}
|
|
});
|