feat: Implement sector role configuration on the setores page and remove the deprecated TI config page.
This commit is contained in:
@@ -2,36 +2,40 @@ import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
import { getCurrentUserFunction } from './auth';
|
||||
|
||||
export const getComprasSetor = query({
|
||||
export const getConfig = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
return await ctx.db.query('config').first();
|
||||
}
|
||||
});
|
||||
|
||||
export const updateComprasSetor = mutation({
|
||||
export const updateConfig = mutation({
|
||||
args: {
|
||||
setorId: v.id('setores')
|
||||
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');
|
||||
|
||||
// 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();
|
||||
|
||||
const updateData = {
|
||||
...args,
|
||||
atualizadoEm: Date.now()
|
||||
};
|
||||
|
||||
if (existingConfig) {
|
||||
await ctx.db.patch(existingConfig._id, {
|
||||
comprasSetorId: args.setorId,
|
||||
atualizadoEm: Date.now()
|
||||
});
|
||||
await ctx.db.patch(existingConfig._id, updateData);
|
||||
} else {
|
||||
await ctx.db.insert('config', {
|
||||
comprasSetorId: args.setorId,
|
||||
criadoPor: user._id,
|
||||
atualizadoEm: Date.now()
|
||||
...updateData,
|
||||
criadoPor: user._id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user