Permite selecionar solicitante e empresa nos tickets

This commit is contained in:
codex-bot 2025-10-23 17:47:23 -03:00
parent 25321224a6
commit 4aee7d7719
6 changed files with 817 additions and 11 deletions

View file

@ -1550,6 +1550,95 @@ export const changeAssignee = mutation({
},
});
export const changeRequester = mutation({
args: { ticketId: v.id("tickets"), requesterId: v.id("users"), actorId: v.id("users") },
handler: async (ctx, { ticketId, requesterId, actorId }) => {
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 viewerRole = (viewer.role ?? "AGENT").toUpperCase()
const actor = viewer.user
if (String(ticketDoc.requesterId) === String(requesterId)) {
return { status: "unchanged" }
}
const requester = (await ctx.db.get(requesterId)) as Doc<"users"> | null
if (!requester || requester.tenantId !== ticketDoc.tenantId) {
throw new ConvexError("Solicitante inválido")
}
if (viewerRole === "MANAGER") {
if (!actor.companyId) {
throw new ConvexError("Gestor não possui empresa vinculada")
}
if (requester.companyId !== actor.companyId) {
throw new ConvexError("Gestores só podem alterar para usuários da própria empresa")
}
}
const now = Date.now()
const requesterSnapshot = {
name: requester.name,
email: requester.email,
avatarUrl: requester.avatarUrl ?? undefined,
teams: requester.teams ?? undefined,
}
let companyId: Id<"companies"> | undefined
let companySnapshot: { name: string; slug?: string; isAvulso?: boolean } | undefined
if (requester.companyId) {
const company = await ctx.db.get(requester.companyId)
if (company) {
companyId = company._id as Id<"companies">
companySnapshot = {
name: company.name,
slug: company.slug ?? undefined,
isAvulso: company.isAvulso ?? undefined,
}
}
}
const patch: Record<string, unknown> = {
requesterId,
requesterSnapshot,
updatedAt: now,
}
if (companyId) {
patch["companyId"] = companyId
patch["companySnapshot"] = companySnapshot
} else {
patch["companyId"] = undefined
patch["companySnapshot"] = undefined
}
await ctx.db.patch(ticketId, patch)
await ctx.db.insert("ticketEvents", {
ticketId,
type: "REQUESTER_CHANGED",
payload: {
requesterId,
requesterName: requester.name,
requesterEmail: requester.email,
companyId: companyId ?? null,
companyName: companySnapshot?.name ?? null,
actorId,
actorName: actor.name,
actorAvatar: actor.avatarUrl,
},
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 }) => {