Ajusta placeholders, formulários e widgets

This commit is contained in:
Esdras Renan 2025-11-06 23:13:41 -03:00
parent 343f0c8c64
commit b94cea2f9a
33 changed files with 2122 additions and 462 deletions

View file

@ -4,18 +4,11 @@ import { ConvexError, v } from "convex/values"
import type { Id } from "./_generated/dataModel"
import { requireAdmin } from "./rbac"
import { getTemplateByKey, normalizeFormTemplateKey } from "./ticketFormTemplates"
import { TICKET_FORM_CONFIG } from "./ticketForms.config"
const KNOWN_TEMPLATES = new Set(["admissao", "desligamento"])
const VALID_SCOPES = new Set(["tenant", "company", "user"])
function normalizeTemplate(input: string) {
const normalized = input.trim().toLowerCase()
if (!KNOWN_TEMPLATES.has(normalized)) {
throw new ConvexError("Template desconhecido")
}
return normalized
}
function normalizeScope(input: string) {
const normalized = input.trim().toLowerCase()
if (!VALID_SCOPES.has(normalized)) {
@ -24,6 +17,22 @@ function normalizeScope(input: string) {
return normalized
}
async function ensureTemplateExists(ctx: MutationCtx | QueryCtx, tenantId: string, template: string) {
const normalized = normalizeFormTemplateKey(template)
if (!normalized) {
throw new ConvexError("Template desconhecido")
}
const existing = await getTemplateByKey(ctx, tenantId, normalized)
if (existing && existing.isArchived !== true) {
return normalized
}
const fallback = TICKET_FORM_CONFIG.find((tpl) => tpl.key === normalized)
if (fallback) {
return normalized
}
throw new ConvexError("Template desconhecido")
}
export const list = query({
args: {
tenantId: v.string(),
@ -32,7 +41,7 @@ export const list = query({
},
handler: async (ctx, { tenantId, viewerId, template }) => {
await requireAdmin(ctx, viewerId, tenantId)
const normalizedTemplate = template ? normalizeTemplate(template) : null
const normalizedTemplate = template ? normalizeFormTemplateKey(template) : null
const settings = await ctx.db
.query("ticketFormSettings")
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
@ -65,7 +74,7 @@ export const upsert = mutation({
},
handler: async (ctx, { tenantId, actorId, template, scope, companyId, userId, enabled }) => {
await requireAdmin(ctx, actorId, tenantId)
const normalizedTemplate = normalizeTemplate(template)
const normalizedTemplate = await ensureTemplateExists(ctx, tenantId, template)
const normalizedScope = normalizeScope(scope)
if (normalizedScope === "company" && !companyId) {