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

39 lines
987 B
TypeScript

import { v } from 'convex/values';
import { mutation, query } from './_generated/server';
import { getCurrentUserFunction } from './auth';
export const getComprasSetor = query({
args: {},
handler: async (ctx) => {
return await ctx.db.query('config').first();
}
});
export const updateComprasSetor = mutation({
args: {
setorId: v.id('setores')
},
handler: async (ctx, args) => {
const user = await getCurrentUserFunction(ctx);
if (!user) throw new Error('Unauthorized');
// Check if user has permission (e.g., admin or TI) - For now, assuming any auth user can set it,
// but in production should be restricted.
const existingConfig = await ctx.db.query('config').first();
if (existingConfig) {
await ctx.db.patch(existingConfig._id, {
comprasSetorId: args.setorId,
atualizadoEm: Date.now()
});
} else {
await ctx.db.insert('config', {
comprasSetorId: args.setorId,
criadoPor: user._id,
atualizadoEm: Date.now()
});
}
}
});