feat: aprimora upload/anexos e regras de atendimento no portal
This commit is contained in:
parent
7e8023ed87
commit
c90e99820f
8 changed files with 218 additions and 74 deletions
|
|
@ -973,6 +973,7 @@ export const changeAssignee = mutation({
|
|||
}
|
||||
const ticketDoc = ticket as Doc<"tickets">
|
||||
const viewer = await requireTicketStaff(ctx, actorId, ticketDoc)
|
||||
const isAdmin = viewer.role === "ADMIN"
|
||||
const assignee = (await ctx.db.get(assigneeId)) as Doc<"users"> | null
|
||||
if (!assignee || assignee.tenantId !== ticketDoc.tenantId) {
|
||||
throw new ConvexError("Responsável inválido")
|
||||
|
|
@ -980,6 +981,11 @@ export const changeAssignee = mutation({
|
|||
if (viewer.role === "MANAGER") {
|
||||
throw new ConvexError("Gestores não podem reatribuir chamados")
|
||||
}
|
||||
const currentAssigneeId = ticketDoc.assigneeId ?? null
|
||||
if (currentAssigneeId && currentAssigneeId !== actorId && !isAdmin) {
|
||||
throw new ConvexError("Somente o responsável atual pode reatribuir este chamado")
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
await ctx.db.patch(ticketId, { assigneeId, updatedAt: now });
|
||||
await ctx.db.insert("ticketEvents", {
|
||||
|
|
@ -1165,12 +1171,28 @@ export const startWork = mutation({
|
|||
if (!ticket) {
|
||||
throw new ConvexError("Ticket não encontrado")
|
||||
}
|
||||
await requireStaff(ctx, actorId, ticket.tenantId)
|
||||
if (ticket.activeSessionId) {
|
||||
return { status: "already_started", sessionId: ticket.activeSessionId }
|
||||
const ticketDoc = ticket as Doc<"tickets">
|
||||
const viewer = await requireTicketStaff(ctx, actorId, ticketDoc)
|
||||
const isAdmin = viewer.role === "ADMIN"
|
||||
const currentAssigneeId = ticketDoc.assigneeId ?? null
|
||||
|
||||
if (currentAssigneeId && currentAssigneeId !== actorId && !isAdmin) {
|
||||
throw new ConvexError("Somente o responsável atual pode iniciar este chamado")
|
||||
}
|
||||
|
||||
if (ticketDoc.activeSessionId) {
|
||||
return { status: "already_started", sessionId: ticketDoc.activeSessionId }
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
let assigneePatched = false
|
||||
|
||||
if (!currentAssigneeId) {
|
||||
await ctx.db.patch(ticketId, { assigneeId: actorId, updatedAt: now })
|
||||
ticketDoc.assigneeId = actorId
|
||||
assigneePatched = true
|
||||
}
|
||||
|
||||
const sessionId = await ctx.db.insert("ticketWorkSessions", {
|
||||
ticketId,
|
||||
agentId: actorId,
|
||||
|
|
@ -1184,11 +1206,25 @@ export const startWork = mutation({
|
|||
updatedAt: now,
|
||||
})
|
||||
|
||||
const actor = (await ctx.db.get(actorId)) as Doc<"users"> | null
|
||||
if (assigneePatched) {
|
||||
await ctx.db.insert("ticketEvents", {
|
||||
ticketId,
|
||||
type: "ASSIGNEE_CHANGED",
|
||||
payload: { assigneeId: actorId, assigneeName: viewer.user.name, actorId },
|
||||
createdAt: now,
|
||||
})
|
||||
}
|
||||
|
||||
await ctx.db.insert("ticketEvents", {
|
||||
ticketId,
|
||||
type: "WORK_STARTED",
|
||||
payload: { actorId, actorName: actor?.name, actorAvatar: actor?.avatarUrl, sessionId, workType: (workType ?? "INTERNAL").toUpperCase() },
|
||||
payload: {
|
||||
actorId,
|
||||
actorName: viewer.user.name,
|
||||
actorAvatar: viewer.user.avatarUrl,
|
||||
sessionId,
|
||||
workType: (workType ?? "INTERNAL").toUpperCase(),
|
||||
},
|
||||
createdAt: now,
|
||||
})
|
||||
|
||||
|
|
@ -1208,8 +1244,14 @@ export const pauseWork = mutation({
|
|||
if (!ticket) {
|
||||
throw new ConvexError("Ticket não encontrado")
|
||||
}
|
||||
await requireStaff(ctx, actorId, ticket.tenantId)
|
||||
if (!ticket.activeSessionId) {
|
||||
const ticketDoc = ticket as Doc<"tickets">
|
||||
const viewer = await requireTicketStaff(ctx, actorId, ticketDoc)
|
||||
const isAdmin = viewer.role === "ADMIN"
|
||||
if (ticketDoc.assigneeId && ticketDoc.assigneeId !== actorId && !isAdmin) {
|
||||
throw new ConvexError("Somente o responsável atual pode pausar este chamado")
|
||||
}
|
||||
|
||||
if (!ticketDoc.activeSessionId) {
|
||||
return { status: "already_paused" }
|
||||
}
|
||||
|
||||
|
|
@ -1217,7 +1259,7 @@ export const pauseWork = mutation({
|
|||
throw new ConvexError("Motivo de pausa inválido")
|
||||
}
|
||||
|
||||
const session = await ctx.db.get(ticket.activeSessionId)
|
||||
const session = await ctx.db.get(ticketDoc.activeSessionId)
|
||||
if (!session) {
|
||||
await ctx.db.patch(ticketId, { activeSessionId: undefined, working: false })
|
||||
return { status: "session_missing" }
|
||||
|
|
@ -1226,7 +1268,7 @@ export const pauseWork = mutation({
|
|||
const now = Date.now()
|
||||
const durationMs = now - session.startedAt
|
||||
|
||||
await ctx.db.patch(ticket.activeSessionId, {
|
||||
await ctx.db.patch(ticketDoc.activeSessionId, {
|
||||
stoppedAt: now,
|
||||
durationMs,
|
||||
pauseReason: reason,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue