feat: enhance visit scheduling and closing flow

This commit is contained in:
Esdras Renan 2025-11-18 17:42:38 -03:00
parent a7f9191e1d
commit 6473e8d40f
5 changed files with 577 additions and 243 deletions

View file

@ -3469,6 +3469,55 @@ export const purgeTicketsForUsers = mutation({
})
export const updateVisitSchedule = mutation({
args: {
ticketId: v.id("tickets"),
actorId: v.id("users"),
visitDate: v.number(),
},
handler: async (ctx, { ticketId, actorId, visitDate }) => {
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)
if (viewer.role === "MANAGER") {
throw new ConvexError("Gestores não podem alterar a data da visita")
}
if (!Number.isFinite(visitDate)) {
throw new ConvexError("Data da visita inválida")
}
if (!ticketDoc.queueId) {
throw new ConvexError("Este ticket não possui fila configurada")
}
const queue = (await ctx.db.get(ticketDoc.queueId)) as Doc<"queues"> | null
if (!queue) {
throw new ConvexError("Fila não encontrada para este ticket")
}
const queueLabel = (normalizeQueueName(queue) ?? queue.name ?? "").toLowerCase()
const isVisitQueue = VISIT_QUEUE_KEYWORDS.some((keyword) => queueLabel.includes(keyword))
if (!isVisitQueue) {
throw new ConvexError("Somente tickets da fila de visitas possuem data de visita")
}
const now = Date.now()
await ctx.db.patch(ticketId, {
dueAt: visitDate,
updatedAt: now,
})
await ctx.db.insert("ticketEvents", {
ticketId,
type: "VISIT_SCHEDULE_CHANGED",
payload: {
visitDate,
actorId,
},
createdAt: now,
})
return { status: "updated" }
},
})
export const changeQueue = mutation({
args: { ticketId: v.id("tickets"), queueId: v.id("queues"), actorId: v.id("users") },
handler: async (ctx, { ticketId, queueId, actorId }) => {