chore: prep platform improvements

This commit is contained in:
Esdras Renan 2025-11-09 21:09:38 -03:00
parent a62f3d5283
commit c5ddd54a3e
24 changed files with 777 additions and 649 deletions

View file

@ -37,6 +37,15 @@ function validateOptions(type: FieldType, options: { value: string; label: strin
}
}
async function validateCompanyScope(ctx: AnyCtx, tenantId: string, companyId?: Id<"companies"> | null) {
if (!companyId) return undefined;
const company = await ctx.db.get(companyId);
if (!company || company.tenantId !== tenantId) {
throw new ConvexError("Empresa inválida para o campo");
}
return companyId;
}
export const list = query({
args: { tenantId: v.string(), viewerId: v.id("users"), scope: v.optional(v.string()) },
handler: async (ctx, { tenantId, viewerId, scope }) => {
@ -64,6 +73,7 @@ export const list = query({
options: field.options ?? [],
order: field.order,
scope: field.scope ?? "all",
companyId: field.companyId ?? null,
createdAt: field.createdAt,
updatedAt: field.updatedAt,
}));
@ -97,6 +107,7 @@ export const listForTenant = query({
options: field.options ?? [],
order: field.order,
scope: field.scope ?? "all",
companyId: field.companyId ?? null,
}));
},
});
@ -118,8 +129,9 @@ export const create = mutation({
)
),
scope: v.optional(v.string()),
companyId: v.optional(v.id("companies")),
},
handler: async (ctx, { tenantId, actorId, label, description, type, required, options, scope }) => {
handler: async (ctx, { tenantId, actorId, label, description, type, required, options, scope, companyId }) => {
await requireAdmin(ctx, actorId, tenantId);
const normalizedLabel = label.trim();
if (normalizedLabel.length < 2) {
@ -140,6 +152,7 @@ export const create = mutation({
}
return safe;
})();
const companyRef = await validateCompanyScope(ctx, tenantId, companyId ?? undefined);
const existing = await ctx.db
.query("ticketFields")
@ -158,6 +171,7 @@ export const create = mutation({
options,
order: maxOrder + 1,
scope: normalizedScope,
companyId: companyRef,
createdAt: now,
updatedAt: now,
});
@ -183,8 +197,9 @@ export const update = mutation({
)
),
scope: v.optional(v.string()),
companyId: v.optional(v.id("companies")),
},
handler: async (ctx, { tenantId, fieldId, actorId, label, description, type, required, options, scope }) => {
handler: async (ctx, { tenantId, fieldId, actorId, label, description, type, required, options, scope, companyId }) => {
await requireAdmin(ctx, actorId, tenantId);
const field = await ctx.db.get(fieldId);
if (!field || field.tenantId !== tenantId) {
@ -208,6 +223,7 @@ export const update = mutation({
}
return safe;
})();
const companyRef = await validateCompanyScope(ctx, tenantId, companyId ?? undefined);
let key = field.key;
if (field.label !== normalizedLabel) {
@ -223,6 +239,7 @@ export const update = mutation({
required,
options,
scope: normalizedScope,
companyId: companyRef,
updatedAt: Date.now(),
});
},