feat: improve ticket export and navigation

This commit is contained in:
Esdras Renan 2025-10-13 00:08:18 -03:00
parent 0731c5d1ea
commit 7d6f3bea01
28 changed files with 1612 additions and 609 deletions

View file

@ -50,20 +50,24 @@ export const list = query({
args: {
tenantId: v.string(),
viewerId: v.id("users"),
kind: v.optional(v.string()),
},
handler: async (ctx, { tenantId, viewerId }) => {
handler: async (ctx, { tenantId, viewerId, kind }) => {
await requireStaff(ctx, viewerId, tenantId)
const normalizedKind = (kind ?? "comment").toLowerCase()
const templates = await ctx.db
.query("commentTemplates")
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
.collect()
return templates
.filter((template) => (template.kind ?? "comment") === normalizedKind)
.sort((a, b) => a.title.localeCompare(b.title, "pt-BR", { sensitivity: "base" }))
.map((template) => ({
id: template._id,
title: template.title,
body: template.body,
kind: template.kind ?? "comment",
createdAt: template.createdAt,
updatedAt: template.updatedAt,
createdBy: template.createdBy,
@ -78,8 +82,9 @@ export const create = mutation({
actorId: v.id("users"),
title: v.string(),
body: v.string(),
kind: v.optional(v.string()),
},
handler: async (ctx, { tenantId, actorId, title, body }) => {
handler: async (ctx, { tenantId, actorId, title, body, kind }) => {
await requireStaff(ctx, actorId, tenantId)
const normalizedTitle = normalizeTitle(title)
if (!normalizedTitle || normalizedTitle.length < 3) {
@ -89,19 +94,21 @@ export const create = mutation({
if (!sanitizedBody) {
throw new ConvexError("Informe o conteúdo do template")
}
const normalizedKind = (kind ?? "comment").toLowerCase()
const existing = await ctx.db
.query("commentTemplates")
.withIndex("by_tenant_title", (q) => q.eq("tenantId", tenantId).eq("title", normalizedTitle))
.first()
if (existing) {
if (existing && (existing.kind ?? "comment") === normalizedKind) {
throw new ConvexError("Já existe um template com este título")
}
const now = Date.now()
const id = await ctx.db.insert("commentTemplates", {
tenantId,
kind: normalizedKind,
title: normalizedTitle,
body: sanitizedBody,
createdBy: actorId,
@ -121,8 +128,9 @@ export const update = mutation({
actorId: v.id("users"),
title: v.string(),
body: v.string(),
kind: v.optional(v.string()),
},
handler: async (ctx, { templateId, tenantId, actorId, title, body }) => {
handler: async (ctx, { templateId, tenantId, actorId, title, body, kind }) => {
await requireStaff(ctx, actorId, tenantId)
const template = await ctx.db.get(templateId)
if (!template || template.tenantId !== tenantId) {
@ -137,17 +145,19 @@ export const update = mutation({
if (!sanitizedBody) {
throw new ConvexError("Informe o conteúdo do template")
}
const normalizedKind = (kind ?? "comment").toLowerCase()
const duplicate = await ctx.db
.query("commentTemplates")
.withIndex("by_tenant_title", (q) => q.eq("tenantId", tenantId).eq("title", normalizedTitle))
.first()
if (duplicate && duplicate._id !== templateId) {
if (duplicate && duplicate._id !== templateId && (duplicate.kind ?? "comment") === normalizedKind) {
throw new ConvexError("Já existe um template com este título")
}
const now = Date.now()
await ctx.db.patch(templateId, {
kind: normalizedKind,
title: normalizedTitle,
body: sanitizedBody,
updatedBy: actorId,