feat: custom fields improvements

This commit is contained in:
Esdras Renan 2025-11-06 14:05:51 -03:00
parent 9495b54a28
commit 0f0f367b3a
11 changed files with 1290 additions and 12 deletions

View file

@ -3516,6 +3516,74 @@ export const updateSummary = mutation({
},
});
export const updateCustomFields = mutation({
args: {
ticketId: v.id("tickets"),
actorId: v.id("users"),
fields: v.array(
v.object({
fieldId: v.id("ticketFields"),
value: v.any(),
})
),
},
handler: async (ctx, { ticketId, actorId, fields }) => {
const ticket = await ctx.db.get(ticketId)
if (!ticket) {
throw new ConvexError("Ticket não encontrado")
}
const ticketDoc = ticket as Doc<"tickets">
const viewer = await requireTicketStaff(ctx, actorId, ticketDoc)
const normalizedRole = (viewer.role ?? "").toUpperCase()
if (normalizedRole !== "ADMIN" && normalizedRole !== "AGENT") {
throw new ConvexError("Somente administradores e agentes podem editar campos personalizados.")
}
const sanitizedInputs: CustomFieldInput[] = fields
.filter((entry) => entry.value !== undefined)
.map((entry) => ({
fieldId: entry.fieldId,
value: entry.value,
}))
const normalized = await normalizeCustomFieldValues(
ctx,
ticketDoc.tenantId,
sanitizedInputs,
ticketDoc.formTemplate ?? null
)
const now = Date.now()
await ctx.db.patch(ticketId, {
customFields: normalized.length > 0 ? normalized : undefined,
updatedAt: now,
})
await ctx.db.insert("ticketEvents", {
ticketId,
type: "CUSTOM_FIELDS_UPDATED",
payload: {
actorId,
actorName: viewer.user.name,
actorAvatar: viewer.user.avatarUrl ?? undefined,
fields: normalized.map((field) => ({
fieldId: field.fieldId,
fieldKey: field.fieldKey,
label: field.label,
type: field.type,
})),
},
createdAt: now,
})
return {
customFields: mapCustomFieldsToRecord(normalized),
updatedAt: now,
}
},
})
export const playNext = mutation({
args: {
tenantId: v.string(),