1413 lines
47 KiB
TypeScript
1413 lines
47 KiB
TypeScript
import { query } from "./_generated/server";
|
|
import type { QueryCtx } from "./_generated/server";
|
|
import { ConvexError, v } from "convex/values";
|
|
import type { Doc, Id } from "./_generated/dataModel";
|
|
|
|
import { requireStaff } from "./rbac";
|
|
import { getOfflineThresholdMs, getStaleThresholdMs } from "./machines";
|
|
|
|
export type TicketStatusNormalized = "PENDING" | "AWAITING_ATTENDANCE" | "PAUSED" | "RESOLVED";
|
|
type QueryFilterBuilder = { lt: (field: unknown, value: number) => unknown; field: (name: string) => unknown };
|
|
export const STATUS_NORMALIZE_MAP: Record<string, TicketStatusNormalized> = {
|
|
NEW: "PENDING",
|
|
PENDING: "PENDING",
|
|
OPEN: "AWAITING_ATTENDANCE",
|
|
AWAITING_ATTENDANCE: "AWAITING_ATTENDANCE",
|
|
ON_HOLD: "PAUSED",
|
|
PAUSED: "PAUSED",
|
|
RESOLVED: "RESOLVED",
|
|
CLOSED: "RESOLVED",
|
|
};
|
|
|
|
export function normalizeStatus(status: string | null | undefined): TicketStatusNormalized {
|
|
if (!status) return "PENDING";
|
|
const normalized = STATUS_NORMALIZE_MAP[status.toUpperCase()];
|
|
return normalized ?? "PENDING";
|
|
}
|
|
|
|
function average(values: number[]) {
|
|
if (values.length === 0) return null;
|
|
return values.reduce((sum, value) => sum + value, 0) / values.length;
|
|
}
|
|
|
|
function resolveCategoryName(
|
|
categoryId: string | null,
|
|
snapshot: { categoryName?: string } | null,
|
|
categories: Map<string, Doc<"ticketCategories">>
|
|
) {
|
|
if (categoryId) {
|
|
const category = categories.get(categoryId)
|
|
if (category?.name) {
|
|
return category.name
|
|
}
|
|
}
|
|
if (snapshot?.categoryName && snapshot.categoryName.trim().length > 0) {
|
|
return snapshot.categoryName.trim()
|
|
}
|
|
return "Sem categoria"
|
|
}
|
|
|
|
export const OPEN_STATUSES = new Set<TicketStatusNormalized>(["PENDING", "AWAITING_ATTENDANCE", "PAUSED"]);
|
|
export const ONE_DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
|
function percentageChange(current: number, previous: number) {
|
|
if (previous === 0) {
|
|
return current === 0 ? 0 : null;
|
|
}
|
|
return ((current - previous) / previous) * 100;
|
|
}
|
|
|
|
function extractScore(payload: unknown): number | null {
|
|
if (typeof payload === "number") return payload;
|
|
if (payload && typeof payload === "object" && "score" in payload) {
|
|
const value = (payload as { score: unknown }).score;
|
|
if (typeof value === "number") {
|
|
return value;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function extractMaxScore(payload: unknown): number | null {
|
|
if (payload && typeof payload === "object" && "maxScore" in payload) {
|
|
const value = (payload as { maxScore: unknown }).maxScore;
|
|
if (typeof value === "number" && Number.isFinite(value) && value > 0) {
|
|
return value;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function extractComment(payload: unknown): string | null {
|
|
if (payload && typeof payload === "object" && "comment" in payload) {
|
|
const value = (payload as { comment: unknown }).comment;
|
|
if (typeof value === "string") {
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function extractAssignee(payload: unknown): { id: string | null; name: string | null } {
|
|
if (!payload || typeof payload !== "object") {
|
|
return { id: null, name: null }
|
|
}
|
|
const record = payload as Record<string, unknown>
|
|
const rawId = record["assigneeId"]
|
|
const rawName = record["assigneeName"]
|
|
const id = typeof rawId === "string" && rawId.trim().length > 0 ? rawId.trim() : null
|
|
const name = typeof rawName === "string" && rawName.trim().length > 0 ? rawName.trim() : null
|
|
return { id, name }
|
|
}
|
|
|
|
function isNotNull<T>(value: T | null): value is T {
|
|
return value !== null;
|
|
}
|
|
|
|
export async function fetchTickets(ctx: QueryCtx, tenantId: string) {
|
|
return ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
|
|
.collect();
|
|
}
|
|
|
|
async function fetchCategoryMap(ctx: QueryCtx, tenantId: string) {
|
|
const categories = await ctx.db
|
|
.query("ticketCategories")
|
|
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
|
|
.collect();
|
|
const map = new Map<string, Doc<"ticketCategories">>();
|
|
for (const category of categories) {
|
|
map.set(String(category._id), category);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
export async function fetchScopedTickets(
|
|
ctx: QueryCtx,
|
|
tenantId: string,
|
|
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
|
) {
|
|
if (viewer.role === "MANAGER") {
|
|
if (!viewer.user.companyId) {
|
|
throw new ConvexError("Gestor não possui empresa vinculada");
|
|
}
|
|
return ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_company", (q) =>
|
|
q.eq("tenantId", tenantId).eq("companyId", viewer.user.companyId!)
|
|
)
|
|
.collect();
|
|
}
|
|
return fetchTickets(ctx, tenantId);
|
|
}
|
|
|
|
export async function fetchScopedTicketsByCreatedRange(
|
|
ctx: QueryCtx,
|
|
tenantId: string,
|
|
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
|
startMs: number,
|
|
endMs: number,
|
|
companyId?: Id<"companies">,
|
|
) {
|
|
const collectRange = async (buildQuery: (chunkStart: number) => unknown) => {
|
|
const results: Doc<"tickets">[] = [];
|
|
const chunkSize = 7 * ONE_DAY_MS;
|
|
for (let chunkStart = startMs; chunkStart < endMs; chunkStart += chunkSize) {
|
|
const chunkEnd = Math.min(chunkStart + chunkSize, endMs);
|
|
const baseQuery = buildQuery(chunkStart);
|
|
const filterFn = (baseQuery as { filter?: (fn: (q: QueryFilterBuilder) => unknown) => unknown }).filter;
|
|
const queryForChunk =
|
|
typeof filterFn === "function"
|
|
? filterFn.call(baseQuery, (q: QueryFilterBuilder) => q.lt(q.field("createdAt"), chunkEnd))
|
|
: baseQuery;
|
|
const collectFn = (queryForChunk as { collect?: () => Promise<Doc<"tickets">[]> }).collect;
|
|
if (typeof collectFn !== "function") {
|
|
throw new ConvexError("Indexed query does not support collect (createdAt)");
|
|
}
|
|
const page = await collectFn.call(queryForChunk);
|
|
if (!page || page.length === 0) continue;
|
|
for (const ticket of page) {
|
|
const createdAt = typeof ticket.createdAt === "number" ? ticket.createdAt : null;
|
|
if (createdAt === null) continue;
|
|
if (createdAt < chunkStart || createdAt >= endMs) continue;
|
|
results.push(ticket);
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
|
|
if (viewer.role === "MANAGER") {
|
|
if (!viewer.user.companyId) {
|
|
throw new ConvexError("Gestor não possui empresa vinculada");
|
|
}
|
|
return collectRange((chunkStart) =>
|
|
ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_company_created", (q) =>
|
|
q.eq("tenantId", tenantId).eq("companyId", viewer.user.companyId!).gte("createdAt", chunkStart)
|
|
)
|
|
);
|
|
}
|
|
|
|
if (companyId) {
|
|
return collectRange((chunkStart) =>
|
|
ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_company_created", (q) =>
|
|
q.eq("tenantId", tenantId).eq("companyId", companyId).gte("createdAt", chunkStart)
|
|
)
|
|
);
|
|
}
|
|
|
|
return collectRange((chunkStart) =>
|
|
ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_created", (q) => q.eq("tenantId", tenantId).gte("createdAt", chunkStart))
|
|
);
|
|
}
|
|
|
|
export async function fetchScopedTicketsByResolvedRange(
|
|
ctx: QueryCtx,
|
|
tenantId: string,
|
|
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
|
startMs: number,
|
|
endMs: number,
|
|
companyId?: Id<"companies">,
|
|
) {
|
|
const collectRange = async (buildQuery: (chunkStart: number) => unknown) => {
|
|
const results: Doc<"tickets">[] = [];
|
|
const chunkSize = 7 * ONE_DAY_MS;
|
|
for (let chunkStart = startMs; chunkStart < endMs; chunkStart += chunkSize) {
|
|
const chunkEnd = Math.min(chunkStart + chunkSize, endMs);
|
|
const baseQuery = buildQuery(chunkStart);
|
|
const filterFn = (baseQuery as { filter?: (fn: (q: QueryFilterBuilder) => unknown) => unknown }).filter;
|
|
const queryForChunk =
|
|
typeof filterFn === "function"
|
|
? filterFn.call(baseQuery, (q: QueryFilterBuilder) => q.lt(q.field("resolvedAt"), chunkEnd))
|
|
: baseQuery;
|
|
const collectFn = (queryForChunk as { collect?: () => Promise<Doc<"tickets">[]> }).collect;
|
|
if (typeof collectFn !== "function") {
|
|
throw new ConvexError("Indexed query does not support collect (resolvedAt)");
|
|
}
|
|
const page = await collectFn.call(queryForChunk);
|
|
if (!page || page.length === 0) continue;
|
|
for (const ticket of page) {
|
|
const resolvedAt = typeof ticket.resolvedAt === "number" ? ticket.resolvedAt : null;
|
|
if (resolvedAt === null) continue;
|
|
if (resolvedAt < chunkStart || resolvedAt >= endMs) continue;
|
|
results.push(ticket);
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
|
|
if (viewer.role === "MANAGER") {
|
|
if (!viewer.user.companyId) {
|
|
throw new ConvexError("Gestor não possui empresa vinculada");
|
|
}
|
|
return collectRange((chunkStart) =>
|
|
ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_company_resolved", (q) =>
|
|
q.eq("tenantId", tenantId).eq("companyId", viewer.user.companyId!).gte("resolvedAt", chunkStart)
|
|
)
|
|
);
|
|
}
|
|
|
|
if (companyId) {
|
|
return collectRange((chunkStart) =>
|
|
ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_company_resolved", (q) =>
|
|
q.eq("tenantId", tenantId).eq("companyId", companyId).gte("resolvedAt", chunkStart)
|
|
)
|
|
);
|
|
}
|
|
|
|
return collectRange((chunkStart) =>
|
|
ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_resolved", (q) => q.eq("tenantId", tenantId).gte("resolvedAt", chunkStart))
|
|
);
|
|
}
|
|
|
|
async function fetchQueues(ctx: QueryCtx, tenantId: string) {
|
|
return ctx.db
|
|
.query("queues")
|
|
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
|
|
.collect();
|
|
}
|
|
|
|
function deriveMachineStatus(machine: Doc<"machines">, now: number) {
|
|
if (machine.isActive === false) {
|
|
return "deactivated";
|
|
}
|
|
const manualStatus = (machine.status ?? "").toLowerCase();
|
|
if (manualStatus === "maintenance" || manualStatus === "blocked") {
|
|
return manualStatus;
|
|
}
|
|
const offlineMs = getOfflineThresholdMs();
|
|
const staleMs = getStaleThresholdMs(offlineMs);
|
|
if (machine.lastHeartbeatAt) {
|
|
const age = now - machine.lastHeartbeatAt;
|
|
if (age <= offlineMs) return "online";
|
|
if (age <= staleMs) return "offline";
|
|
return "stale";
|
|
}
|
|
return (machine.status ?? "unknown") || "unknown";
|
|
}
|
|
|
|
function formatOsLabel(osName?: string | null, osVersion?: string | null) {
|
|
const name = osName?.trim();
|
|
if (!name) return "Desconhecido";
|
|
const version = osVersion?.trim();
|
|
if (!version) return name;
|
|
const conciseVersion = version.split(" ")[0];
|
|
if (!conciseVersion) return name;
|
|
return `${name} ${conciseVersion}`.trim();
|
|
}
|
|
|
|
type CsatSurvey = {
|
|
ticketId: Id<"tickets">;
|
|
reference: number;
|
|
score: number;
|
|
maxScore: number;
|
|
comment: string | null;
|
|
receivedAt: number;
|
|
assigneeId: string | null;
|
|
assigneeName: string | null;
|
|
};
|
|
|
|
async function collectCsatSurveys(ctx: QueryCtx, tickets: Doc<"tickets">[]): Promise<CsatSurvey[]> {
|
|
const perTicket = await Promise.all(
|
|
tickets.map(async (ticket) => {
|
|
if (typeof ticket.csatScore === "number") {
|
|
const snapshot = (ticket.csatAssigneeSnapshot ?? null) as {
|
|
name?: string
|
|
email?: string
|
|
} | null
|
|
const assigneeId =
|
|
ticket.csatAssigneeId && typeof ticket.csatAssigneeId === "string"
|
|
? ticket.csatAssigneeId
|
|
: ticket.csatAssigneeId
|
|
? String(ticket.csatAssigneeId)
|
|
: null
|
|
const assigneeName =
|
|
snapshot && typeof snapshot.name === "string" && snapshot.name.trim().length > 0
|
|
? snapshot.name.trim()
|
|
: null
|
|
return [
|
|
{
|
|
ticketId: ticket._id,
|
|
reference: ticket.reference,
|
|
score: ticket.csatScore,
|
|
maxScore: ticket.csatMaxScore && Number.isFinite(ticket.csatMaxScore) ? (ticket.csatMaxScore as number) : 5,
|
|
comment:
|
|
typeof ticket.csatComment === "string" && ticket.csatComment.trim().length > 0
|
|
? ticket.csatComment.trim()
|
|
: null,
|
|
receivedAt: ticket.csatRatedAt ?? ticket.updatedAt ?? ticket.createdAt,
|
|
assigneeId,
|
|
assigneeName,
|
|
} satisfies CsatSurvey,
|
|
];
|
|
}
|
|
const events = await ctx.db
|
|
.query("ticketEvents")
|
|
.withIndex("by_ticket", (q) => q.eq("ticketId", ticket._id))
|
|
.collect();
|
|
return events
|
|
.filter((event) => event.type === "CSAT_RECEIVED" || event.type === "CSAT_RATED")
|
|
.map((event) => {
|
|
const score = extractScore(event.payload);
|
|
if (score === null) return null;
|
|
const assignee = extractAssignee(event.payload)
|
|
return {
|
|
ticketId: ticket._id,
|
|
reference: ticket.reference,
|
|
score,
|
|
maxScore: extractMaxScore(event.payload) ?? 5,
|
|
comment: extractComment(event.payload),
|
|
receivedAt: event.createdAt,
|
|
assigneeId: assignee.id,
|
|
assigneeName: assignee.name,
|
|
} as CsatSurvey;
|
|
})
|
|
.filter(isNotNull);
|
|
})
|
|
);
|
|
return perTicket.flat();
|
|
}
|
|
|
|
function formatDateKey(timestamp: number) {
|
|
const date = new Date(timestamp);
|
|
const year = date.getUTCFullYear();
|
|
const month = `${date.getUTCMonth() + 1}`.padStart(2, "0");
|
|
const day = `${date.getUTCDate()}`.padStart(2, "0");
|
|
return `${year}-${month}-${day}`;
|
|
}
|
|
|
|
export async function slaOverviewHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range, companyId }: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
let tickets = await fetchScopedTickets(ctx, tenantId, viewer);
|
|
if (companyId) tickets = tickets.filter((t) => t.companyId === companyId)
|
|
// Optional range filter (createdAt) for reporting purposes, similar ao backlog/csat
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90;
|
|
const end = new Date();
|
|
end.setUTCHours(0, 0, 0, 0);
|
|
const endMs = end.getTime() + ONE_DAY_MS;
|
|
const startMs = endMs - days * ONE_DAY_MS;
|
|
const inRange = tickets.filter((t) => t.createdAt >= startMs && t.createdAt < endMs);
|
|
const queues = await fetchQueues(ctx, tenantId);
|
|
const categoriesMap = await fetchCategoryMap(ctx, tenantId);
|
|
|
|
const now = Date.now();
|
|
const openTickets = inRange.filter((ticket) => OPEN_STATUSES.has(normalizeStatus(ticket.status)));
|
|
const resolvedTickets = inRange.filter((ticket) => {
|
|
const status = normalizeStatus(ticket.status);
|
|
return status === "RESOLVED";
|
|
});
|
|
const overdueTickets = openTickets.filter((ticket) => ticket.dueAt && ticket.dueAt < now);
|
|
|
|
const firstResponseTimes = inRange
|
|
.filter((ticket) => ticket.firstResponseAt)
|
|
.map((ticket) => (ticket.firstResponseAt! - ticket.createdAt) / 60000);
|
|
const resolutionTimes = resolvedTickets
|
|
.filter((ticket) => ticket.resolvedAt)
|
|
.map((ticket) => (ticket.resolvedAt! - ticket.createdAt) / 60000);
|
|
|
|
const queueBreakdown = queues.map((queue) => {
|
|
const count = openTickets.filter((ticket) => ticket.queueId === queue._id).length;
|
|
return {
|
|
id: queue._id,
|
|
name: queue.name,
|
|
open: count,
|
|
};
|
|
});
|
|
|
|
const categoryStats = new Map<
|
|
string,
|
|
{
|
|
categoryId: string | null
|
|
categoryName: string
|
|
priority: string
|
|
total: number
|
|
responseMet: number
|
|
solutionMet: number
|
|
}
|
|
>()
|
|
|
|
for (const ticket of inRange) {
|
|
const snapshot = (ticket.slaSnapshot ?? null) as { categoryId?: Id<"ticketCategories">; categoryName?: string; priority?: string } | null
|
|
const rawCategoryId = ticket.categoryId ? String(ticket.categoryId) : snapshot?.categoryId ? String(snapshot.categoryId) : null
|
|
const categoryName = resolveCategoryName(rawCategoryId, snapshot, categoriesMap)
|
|
const priority = (snapshot?.priority ?? ticket.priority ?? "MEDIUM").toUpperCase()
|
|
const key = `${rawCategoryId ?? "uncategorized"}::${priority}`
|
|
let stat = categoryStats.get(key)
|
|
if (!stat) {
|
|
stat = {
|
|
categoryId: rawCategoryId,
|
|
categoryName,
|
|
priority,
|
|
total: 0,
|
|
responseMet: 0,
|
|
solutionMet: 0,
|
|
}
|
|
categoryStats.set(key, stat)
|
|
}
|
|
stat.total += 1
|
|
if (ticket.slaResponseStatus === "met") {
|
|
stat.responseMet += 1
|
|
}
|
|
if (ticket.slaSolutionStatus === "met") {
|
|
stat.solutionMet += 1
|
|
}
|
|
}
|
|
|
|
const categoryBreakdown = Array.from(categoryStats.values())
|
|
.map((entry) => ({
|
|
...entry,
|
|
responseRate: entry.total > 0 ? entry.responseMet / entry.total : null,
|
|
solutionRate: entry.total > 0 ? entry.solutionMet / entry.total : null,
|
|
}))
|
|
.sort((a, b) => b.total - a.total)
|
|
|
|
return {
|
|
totals: {
|
|
total: inRange.length,
|
|
open: openTickets.length,
|
|
resolved: resolvedTickets.length,
|
|
overdue: overdueTickets.length,
|
|
},
|
|
response: {
|
|
averageFirstResponseMinutes: average(firstResponseTimes),
|
|
responsesRegistered: firstResponseTimes.length,
|
|
},
|
|
resolution: {
|
|
averageResolutionMinutes: average(resolutionTimes),
|
|
resolvedCount: resolutionTimes.length,
|
|
},
|
|
queueBreakdown,
|
|
categoryBreakdown,
|
|
rangeDays: days,
|
|
};
|
|
}
|
|
|
|
export const slaOverview = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()), companyId: v.optional(v.id("companies")) },
|
|
handler: slaOverviewHandler,
|
|
});
|
|
|
|
export async function csatOverviewHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range, companyId }: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
let tickets = await fetchScopedTickets(ctx, tenantId, viewer);
|
|
if (companyId) tickets = tickets.filter((t) => t.companyId === companyId)
|
|
const surveysAll = await collectCsatSurveys(ctx, tickets);
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90;
|
|
const end = new Date();
|
|
end.setUTCHours(0, 0, 0, 0);
|
|
const endMs = end.getTime() + ONE_DAY_MS;
|
|
const startMs = endMs - days * ONE_DAY_MS;
|
|
const surveys = surveysAll.filter((s) => s.receivedAt >= startMs && s.receivedAt < endMs);
|
|
|
|
const normalizeToFive = (value: CsatSurvey) => {
|
|
if (!value.maxScore || value.maxScore <= 0) return value.score;
|
|
return Math.min(5, Math.max(1, (value.score / value.maxScore) * 5));
|
|
};
|
|
|
|
const averageScore = average(surveys.map((item) => normalizeToFive(item)));
|
|
const distribution = [1, 2, 3, 4, 5].map((score) => ({
|
|
score,
|
|
total: surveys.filter((item) => Math.round(normalizeToFive(item)) === score).length,
|
|
}));
|
|
|
|
const positiveThreshold = 4;
|
|
const positiveCount = surveys.filter((item) => normalizeToFive(item) >= positiveThreshold).length;
|
|
const positiveRate = surveys.length > 0 ? positiveCount / surveys.length : null;
|
|
|
|
const agentStats = new Map<
|
|
string,
|
|
{ id: string; name: string; total: number; sum: number; positive: number }
|
|
>();
|
|
|
|
for (const survey of surveys) {
|
|
const normalizedScore = normalizeToFive(survey);
|
|
const key = survey.assigneeId ?? "unassigned";
|
|
const existing = agentStats.get(key) ?? {
|
|
id: key,
|
|
name: survey.assigneeName ?? "Sem responsável",
|
|
total: 0,
|
|
sum: 0,
|
|
positive: 0,
|
|
};
|
|
existing.total += 1;
|
|
existing.sum += normalizedScore;
|
|
if (normalizedScore >= positiveThreshold) {
|
|
existing.positive += 1;
|
|
}
|
|
if (survey.assigneeName && survey.assigneeName.trim().length > 0) {
|
|
existing.name = survey.assigneeName.trim();
|
|
}
|
|
agentStats.set(key, existing);
|
|
}
|
|
|
|
const byAgent = Array.from(agentStats.values())
|
|
.map((entry) => ({
|
|
agentId: entry.id === "unassigned" ? null : entry.id,
|
|
agentName: entry.id === "unassigned" ? "Sem responsável" : entry.name,
|
|
totalResponses: entry.total,
|
|
averageScore: entry.total > 0 ? entry.sum / entry.total : null,
|
|
positiveRate: entry.total > 0 ? entry.positive / entry.total : null,
|
|
}))
|
|
.sort((a, b) => {
|
|
const diff = (b.averageScore ?? 0) - (a.averageScore ?? 0);
|
|
if (Math.abs(diff) > 0.0001) return diff;
|
|
return (b.totalResponses ?? 0) - (a.totalResponses ?? 0);
|
|
});
|
|
|
|
return {
|
|
totalSurveys: surveys.length,
|
|
averageScore,
|
|
distribution,
|
|
recent: surveys
|
|
.slice()
|
|
.sort((a, b) => b.receivedAt - a.receivedAt)
|
|
.slice(0, 10)
|
|
.map((item) => ({
|
|
ticketId: item.ticketId,
|
|
reference: item.reference,
|
|
score: item.score,
|
|
maxScore: item.maxScore,
|
|
comment: item.comment,
|
|
receivedAt: item.receivedAt,
|
|
assigneeId: item.assigneeId,
|
|
assigneeName: item.assigneeName,
|
|
})),
|
|
rangeDays: days,
|
|
positiveRate,
|
|
byAgent,
|
|
};
|
|
}
|
|
|
|
export const csatOverview = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()), companyId: v.optional(v.id("companies")) },
|
|
handler: csatOverviewHandler,
|
|
});
|
|
|
|
export async function openedResolvedByDayHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range, companyId }: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90;
|
|
const end = new Date();
|
|
end.setUTCHours(0, 0, 0, 0);
|
|
const endMs = end.getTime() + ONE_DAY_MS;
|
|
const startMs = endMs - days * ONE_DAY_MS;
|
|
|
|
const openedTickets = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
|
const resolvedTickets = await fetchScopedTicketsByResolvedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
|
|
|
const opened: Record<string, number> = {}
|
|
const resolved: Record<string, number> = {}
|
|
|
|
for (let i = days - 1; i >= 0; i--) {
|
|
const d = new Date(endMs - (i + 1) * ONE_DAY_MS)
|
|
const key = formatDateKey(d.getTime())
|
|
opened[key] = 0
|
|
resolved[key] = 0
|
|
}
|
|
|
|
for (const ticket of openedTickets) {
|
|
if (ticket.createdAt >= startMs && ticket.createdAt < endMs) {
|
|
const key = formatDateKey(ticket.createdAt)
|
|
opened[key] = (opened[key] ?? 0) + 1
|
|
}
|
|
}
|
|
|
|
for (const ticket of resolvedTickets) {
|
|
if (typeof ticket.resolvedAt !== "number") {
|
|
continue
|
|
}
|
|
const key = formatDateKey(ticket.resolvedAt)
|
|
resolved[key] = (resolved[key] ?? 0) + 1
|
|
}
|
|
|
|
const series: Array<{ date: string; opened: number; resolved: number }> = []
|
|
for (let i = days - 1; i >= 0; i--) {
|
|
const d = new Date(endMs - (i + 1) * ONE_DAY_MS)
|
|
const key = formatDateKey(d.getTime())
|
|
series.push({ date: key, opened: opened[key] ?? 0, resolved: resolved[key] ?? 0 })
|
|
}
|
|
|
|
return { rangeDays: days, series }
|
|
}
|
|
|
|
export const openedResolvedByDay = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()), companyId: v.optional(v.id("companies")) },
|
|
handler: openedResolvedByDayHandler,
|
|
})
|
|
|
|
export async function backlogOverviewHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range, companyId }: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
// Optional range filter (createdAt) for reporting purposes
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90;
|
|
const end = new Date();
|
|
end.setUTCHours(0, 0, 0, 0);
|
|
const endMs = end.getTime() + ONE_DAY_MS;
|
|
const startMs = endMs - days * ONE_DAY_MS;
|
|
const inRange = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
|
|
|
const statusCounts = inRange.reduce<Record<TicketStatusNormalized, number>>((acc, ticket) => {
|
|
const status = normalizeStatus(ticket.status);
|
|
acc[status] = (acc[status] ?? 0) + 1;
|
|
return acc;
|
|
}, {} as Record<TicketStatusNormalized, number>);
|
|
|
|
const priorityCounts = inRange.reduce<Record<string, number>>((acc, ticket) => {
|
|
acc[ticket.priority] = (acc[ticket.priority] ?? 0) + 1;
|
|
return acc;
|
|
}, {});
|
|
|
|
const openTickets = inRange.filter((ticket) => OPEN_STATUSES.has(normalizeStatus(ticket.status)));
|
|
|
|
const queueMap = new Map<string, { name: string; count: number }>();
|
|
for (const ticket of openTickets) {
|
|
const queueId = ticket.queueId ? ticket.queueId : "sem-fila";
|
|
const current = queueMap.get(queueId) ?? { name: queueId === "sem-fila" ? "Sem fila" : "", count: 0 };
|
|
current.count += 1;
|
|
queueMap.set(queueId, current);
|
|
}
|
|
|
|
const queues = await fetchQueues(ctx, tenantId);
|
|
|
|
for (const queue of queues) {
|
|
const entry = queueMap.get(queue._id) ?? { name: queue.name, count: 0 };
|
|
entry.name = queue.name;
|
|
queueMap.set(queue._id, entry);
|
|
}
|
|
|
|
return {
|
|
rangeDays: days,
|
|
statusCounts,
|
|
priorityCounts,
|
|
queueCounts: Array.from(queueMap.entries()).map(([id, data]) => ({
|
|
id,
|
|
name: data.name,
|
|
total: data.count,
|
|
})),
|
|
totalOpen: openTickets.length,
|
|
};
|
|
}
|
|
|
|
export const backlogOverview = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()), companyId: v.optional(v.id("companies")) },
|
|
handler: backlogOverviewHandler,
|
|
});
|
|
|
|
// Touch to ensure CI convex_deploy runs and that agentProductivity is deployed
|
|
export async function agentProductivityHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range, companyId }: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId)
|
|
let tickets = await fetchScopedTickets(ctx, tenantId, viewer)
|
|
if (companyId) tickets = tickets.filter((t) => t.companyId === companyId)
|
|
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90
|
|
const end = new Date()
|
|
end.setUTCHours(0, 0, 0, 0)
|
|
const endMs = end.getTime() + ONE_DAY_MS
|
|
const startMs = endMs - days * ONE_DAY_MS
|
|
|
|
const inRange = tickets.filter((t) => t.createdAt >= startMs && t.createdAt < endMs)
|
|
type Acc = {
|
|
agentId: Id<"users">
|
|
name: string | null
|
|
email: string | null
|
|
open: number
|
|
resolved: number
|
|
avgFirstResponseMinValues: number[]
|
|
avgResolutionMinValues: number[]
|
|
workedMs: number
|
|
}
|
|
const map = new Map<string, Acc>()
|
|
|
|
for (const t of inRange) {
|
|
const assigneeId = t.assigneeId ?? null
|
|
if (!assigneeId) continue
|
|
let acc = map.get(assigneeId)
|
|
if (!acc) {
|
|
const user = await ctx.db.get(assigneeId)
|
|
acc = {
|
|
agentId: assigneeId,
|
|
name: user?.name ?? null,
|
|
email: user?.email ?? null,
|
|
open: 0,
|
|
resolved: 0,
|
|
avgFirstResponseMinValues: [],
|
|
avgResolutionMinValues: [],
|
|
workedMs: 0,
|
|
}
|
|
map.set(assigneeId, acc)
|
|
}
|
|
const status = normalizeStatus(t.status)
|
|
if (OPEN_STATUSES.has(status)) acc.open += 1
|
|
if (status === "RESOLVED") acc.resolved += 1
|
|
if (t.firstResponseAt) acc.avgFirstResponseMinValues.push((t.firstResponseAt - t.createdAt) / 60000)
|
|
if (t.resolvedAt) acc.avgResolutionMinValues.push((t.resolvedAt - t.createdAt) / 60000)
|
|
}
|
|
|
|
for (const [agentId, acc] of map) {
|
|
const sessions = await ctx.db
|
|
.query("ticketWorkSessions")
|
|
.withIndex("by_agent", (q) => q.eq("agentId", agentId as Id<"users">))
|
|
.collect()
|
|
let total = 0
|
|
for (const s of sessions) {
|
|
const started = s.startedAt
|
|
const ended = s.stoppedAt ?? s.startedAt
|
|
if (ended < startMs || started >= endMs) continue
|
|
total += s.durationMs ?? Math.max(0, (s.stoppedAt ?? Date.now()) - s.startedAt)
|
|
}
|
|
acc.workedMs = total
|
|
}
|
|
|
|
const items = Array.from(map.values()).map((acc) => ({
|
|
agentId: acc.agentId,
|
|
name: acc.name,
|
|
email: acc.email,
|
|
open: acc.open,
|
|
resolved: acc.resolved,
|
|
avgFirstResponseMinutes: average(acc.avgFirstResponseMinValues),
|
|
avgResolutionMinutes: average(acc.avgResolutionMinValues),
|
|
workedHours: Math.round((acc.workedMs / 3600000) * 100) / 100,
|
|
}))
|
|
items.sort((a, b) => b.resolved - a.resolved)
|
|
return { rangeDays: days, items }
|
|
}
|
|
|
|
export const agentProductivity = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()), companyId: v.optional(v.id("companies")) },
|
|
handler: agentProductivityHandler,
|
|
})
|
|
|
|
type CategoryAgentAccumulator = {
|
|
id: Id<"ticketCategories"> | null
|
|
name: string
|
|
total: number
|
|
resolved: number
|
|
agents: Map<string, { agentId: Id<"users"> | null; name: string | null; total: number }>
|
|
}
|
|
|
|
export async function ticketCategoryInsightsHandler(
|
|
ctx: QueryCtx,
|
|
{
|
|
tenantId,
|
|
viewerId,
|
|
range,
|
|
companyId,
|
|
}: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId)
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90
|
|
const end = new Date()
|
|
end.setUTCHours(0, 0, 0, 0)
|
|
const endMs = end.getTime() + ONE_DAY_MS
|
|
const startMs = endMs - days * ONE_DAY_MS
|
|
|
|
const inRange = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId)
|
|
const categories = await ctx.db
|
|
.query("ticketCategories")
|
|
.withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
|
|
.collect()
|
|
|
|
const categoriesById = new Map<Id<"ticketCategories">, Doc<"ticketCategories">>()
|
|
for (const category of categories) {
|
|
categoriesById.set(category._id, category)
|
|
}
|
|
|
|
const stats = new Map<string, CategoryAgentAccumulator>()
|
|
|
|
for (const ticket of inRange) {
|
|
const categoryKey = ticket.categoryId ? String(ticket.categoryId) : "uncategorized"
|
|
let stat = stats.get(categoryKey)
|
|
if (!stat) {
|
|
const categoryDoc = ticket.categoryId ? categoriesById.get(ticket.categoryId) : null
|
|
stat = {
|
|
id: ticket.categoryId ?? null,
|
|
name: categoryDoc?.name ?? (ticket.categoryId ? "Categoria removida" : "Sem categoria"),
|
|
total: 0,
|
|
resolved: 0,
|
|
agents: new Map(),
|
|
}
|
|
stats.set(categoryKey, stat)
|
|
}
|
|
stat.total += 1
|
|
if (typeof ticket.resolvedAt === "number" && ticket.resolvedAt >= startMs && ticket.resolvedAt < endMs) {
|
|
stat.resolved += 1
|
|
}
|
|
|
|
const agentKey = ticket.assigneeId ? String(ticket.assigneeId) : "unassigned"
|
|
let agent = stat.agents.get(agentKey)
|
|
if (!agent) {
|
|
const snapshotName = ticket.assigneeSnapshot?.name ?? null
|
|
const fallbackName = ticket.assigneeId ? null : "Sem responsável"
|
|
agent = {
|
|
agentId: ticket.assigneeId ?? null,
|
|
name: snapshotName ?? fallbackName ?? "Agente",
|
|
total: 0,
|
|
}
|
|
stat.agents.set(agentKey, agent)
|
|
}
|
|
agent.total += 1
|
|
}
|
|
|
|
const categoriesData = Array.from(stats.values())
|
|
.map((stat) => {
|
|
const agents = Array.from(stat.agents.values()).sort((a, b) => b.total - a.total)
|
|
const topAgent = agents[0] ?? null
|
|
return {
|
|
id: stat.id ? String(stat.id) : null,
|
|
name: stat.name,
|
|
total: stat.total,
|
|
resolved: stat.resolved,
|
|
topAgent: topAgent
|
|
? {
|
|
id: topAgent.agentId ? String(topAgent.agentId) : null,
|
|
name: topAgent.name,
|
|
total: topAgent.total,
|
|
}
|
|
: null,
|
|
agents: agents.slice(0, 5).map((agent) => ({
|
|
id: agent.agentId ? String(agent.agentId) : null,
|
|
name: agent.name,
|
|
total: agent.total,
|
|
})),
|
|
}
|
|
})
|
|
.sort((a, b) => b.total - a.total)
|
|
|
|
const spotlight = categoriesData.reduce<
|
|
| null
|
|
| {
|
|
categoryId: string | null
|
|
categoryName: string
|
|
agentId: string | null
|
|
agentName: string | null
|
|
tickets: number
|
|
}
|
|
>((best, current) => {
|
|
if (!current.topAgent) return best
|
|
if (!best || current.topAgent.total > best.tickets) {
|
|
return {
|
|
categoryId: current.id,
|
|
categoryName: current.name,
|
|
agentId: current.topAgent.id,
|
|
agentName: current.topAgent.name,
|
|
tickets: current.topAgent.total,
|
|
}
|
|
}
|
|
return best
|
|
}, null)
|
|
|
|
return {
|
|
rangeDays: days,
|
|
totalTickets: inRange.length,
|
|
categories: categoriesData,
|
|
spotlight,
|
|
}
|
|
}
|
|
|
|
export const categoryInsights = query({
|
|
args: {
|
|
tenantId: v.string(),
|
|
viewerId: v.id("users"),
|
|
range: v.optional(v.string()),
|
|
companyId: v.optional(v.id("companies")),
|
|
},
|
|
handler: ticketCategoryInsightsHandler,
|
|
})
|
|
|
|
export async function dashboardOverviewHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId }: { tenantId: string; viewerId: Id<"users"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
const tickets = await fetchScopedTickets(ctx, tenantId, viewer);
|
|
const now = Date.now();
|
|
|
|
const lastDayStart = now - ONE_DAY_MS;
|
|
const previousDayStart = now - 2 * ONE_DAY_MS;
|
|
|
|
const newTickets = tickets.filter((ticket) => ticket.createdAt >= lastDayStart);
|
|
const previousTickets = tickets.filter(
|
|
(ticket) => ticket.createdAt >= previousDayStart && ticket.createdAt < lastDayStart
|
|
);
|
|
|
|
const trend = percentageChange(newTickets.length, previousTickets.length);
|
|
|
|
const inProgressCurrent = tickets.filter((ticket) => {
|
|
if (!ticket.firstResponseAt) return false;
|
|
const status = normalizeStatus(ticket.status);
|
|
if (status === "RESOLVED") return false;
|
|
return !ticket.resolvedAt;
|
|
});
|
|
|
|
const inProgressPrevious = tickets.filter((ticket) => {
|
|
if (!ticket.firstResponseAt || ticket.firstResponseAt >= lastDayStart) return false;
|
|
if (ticket.resolvedAt && ticket.resolvedAt < lastDayStart) return false;
|
|
const status = normalizeStatus(ticket.status);
|
|
return status !== "RESOLVED" || !ticket.resolvedAt;
|
|
});
|
|
|
|
const inProgressTrend = percentageChange(inProgressCurrent.length, inProgressPrevious.length);
|
|
|
|
const lastWindowStart = now - 7 * ONE_DAY_MS;
|
|
const previousWindowStart = now - 14 * ONE_DAY_MS;
|
|
|
|
const firstResponseWindow = tickets
|
|
.filter(
|
|
(ticket) =>
|
|
ticket.createdAt >= lastWindowStart &&
|
|
ticket.createdAt < now &&
|
|
ticket.firstResponseAt
|
|
)
|
|
.map((ticket) => (ticket.firstResponseAt! - ticket.createdAt) / 60000);
|
|
const firstResponsePrevious = tickets
|
|
.filter(
|
|
(ticket) =>
|
|
ticket.createdAt >= previousWindowStart &&
|
|
ticket.createdAt < lastWindowStart &&
|
|
ticket.firstResponseAt
|
|
)
|
|
.map((ticket) => (ticket.firstResponseAt! - ticket.createdAt) / 60000);
|
|
|
|
const averageWindow = average(firstResponseWindow);
|
|
const averagePrevious = average(firstResponsePrevious);
|
|
const deltaMinutes =
|
|
averageWindow !== null && averagePrevious !== null ? averageWindow - averagePrevious : null;
|
|
|
|
const awaitingTickets = tickets.filter((ticket) => OPEN_STATUSES.has(normalizeStatus(ticket.status)));
|
|
const atRiskTickets = awaitingTickets.filter((ticket) => ticket.dueAt && ticket.dueAt < now);
|
|
|
|
const resolvedLastWindow = tickets.filter(
|
|
(ticket) => ticket.resolvedAt && ticket.resolvedAt >= lastWindowStart && ticket.resolvedAt < now
|
|
);
|
|
const resolvedPreviousWindow = tickets.filter(
|
|
(ticket) =>
|
|
ticket.resolvedAt &&
|
|
ticket.resolvedAt >= previousWindowStart &&
|
|
ticket.resolvedAt < lastWindowStart
|
|
);
|
|
const resolutionRate = tickets.length > 0 ? (resolvedLastWindow.length / tickets.length) * 100 : null;
|
|
const resolutionDelta =
|
|
resolvedPreviousWindow.length > 0
|
|
? ((resolvedLastWindow.length - resolvedPreviousWindow.length) / resolvedPreviousWindow.length) * 100
|
|
: null;
|
|
|
|
return {
|
|
newTickets: {
|
|
last24h: newTickets.length,
|
|
previous24h: previousTickets.length,
|
|
trendPercentage: trend,
|
|
},
|
|
inProgress: {
|
|
current: inProgressCurrent.length,
|
|
previousSnapshot: inProgressPrevious.length,
|
|
trendPercentage: inProgressTrend,
|
|
},
|
|
firstResponse: {
|
|
averageMinutes: averageWindow,
|
|
previousAverageMinutes: averagePrevious,
|
|
deltaMinutes,
|
|
responsesCount: firstResponseWindow.length,
|
|
},
|
|
awaitingAction: {
|
|
total: awaitingTickets.length,
|
|
atRisk: atRiskTickets.length,
|
|
},
|
|
resolution: {
|
|
resolvedLast7d: resolvedLastWindow.length,
|
|
previousResolved: resolvedPreviousWindow.length,
|
|
rate: resolutionRate,
|
|
deltaPercentage: resolutionDelta,
|
|
},
|
|
};
|
|
}
|
|
|
|
export const dashboardOverview = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users") },
|
|
handler: dashboardOverviewHandler,
|
|
});
|
|
|
|
export async function ticketsByChannelHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range, companyId }: { tenantId: string; viewerId: Id<"users">; range?: string; companyId?: Id<"companies"> }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
let tickets = await fetchScopedTickets(ctx, tenantId, viewer);
|
|
if (companyId) tickets = tickets.filter((t) => t.companyId === companyId)
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90;
|
|
|
|
const end = new Date();
|
|
end.setUTCHours(0, 0, 0, 0);
|
|
const endMs = end.getTime() + ONE_DAY_MS;
|
|
const startMs = endMs - days * ONE_DAY_MS;
|
|
|
|
const timeline = new Map<string, Map<string, number>>();
|
|
for (let ts = startMs; ts < endMs; ts += ONE_DAY_MS) {
|
|
timeline.set(formatDateKey(ts), new Map());
|
|
}
|
|
|
|
const channels = new Set<string>();
|
|
|
|
for (const ticket of tickets) {
|
|
if (ticket.createdAt < startMs || ticket.createdAt >= endMs) continue;
|
|
const dateKey = formatDateKey(ticket.createdAt);
|
|
const channelKey = ticket.channel ?? "OUTRO";
|
|
channels.add(channelKey);
|
|
const dayMap = timeline.get(dateKey) ?? new Map<string, number>();
|
|
dayMap.set(channelKey, (dayMap.get(channelKey) ?? 0) + 1);
|
|
timeline.set(dateKey, dayMap);
|
|
}
|
|
|
|
const sortedChannels = Array.from(channels).sort();
|
|
|
|
const points = Array.from(timeline.entries())
|
|
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
.map(([date, map]) => {
|
|
const values: Record<string, number> = {};
|
|
for (const channel of sortedChannels) {
|
|
values[channel] = map.get(channel) ?? 0;
|
|
}
|
|
return { date, values };
|
|
});
|
|
|
|
return {
|
|
rangeDays: days,
|
|
channels: sortedChannels,
|
|
points,
|
|
};
|
|
}
|
|
|
|
export const ticketsByChannel = query({
|
|
args: {
|
|
tenantId: v.string(),
|
|
viewerId: v.id("users"),
|
|
range: v.optional(v.string()),
|
|
companyId: v.optional(v.id("companies")),
|
|
},
|
|
handler: ticketsByChannelHandler,
|
|
});
|
|
|
|
export async function hoursByClientHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, viewerId, range }: { tenantId: string; viewerId: Id<"users">; range?: string }
|
|
) {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId)
|
|
const tickets = await fetchScopedTickets(ctx, tenantId, viewer)
|
|
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90
|
|
const end = new Date()
|
|
end.setUTCHours(0, 0, 0, 0)
|
|
const endMs = end.getTime() + ONE_DAY_MS
|
|
const startMs = endMs - days * ONE_DAY_MS
|
|
|
|
type Acc = {
|
|
companyId: Id<"companies">
|
|
name: string
|
|
isAvulso: boolean
|
|
internalMs: number
|
|
externalMs: number
|
|
totalMs: number
|
|
contractedHoursPerMonth?: number | null
|
|
}
|
|
const map = new Map<string, Acc>()
|
|
|
|
for (const t of tickets) {
|
|
if (t.updatedAt < startMs || t.updatedAt >= endMs) continue
|
|
const companyId = t.companyId ?? null
|
|
if (!companyId) continue
|
|
|
|
let acc = map.get(companyId)
|
|
if (!acc) {
|
|
const company = await ctx.db.get(companyId)
|
|
acc = {
|
|
companyId,
|
|
name: company?.name ?? "Sem empresa",
|
|
isAvulso: Boolean(company?.isAvulso ?? false),
|
|
internalMs: 0,
|
|
externalMs: 0,
|
|
totalMs: 0,
|
|
contractedHoursPerMonth: company?.contractedHoursPerMonth ?? null,
|
|
}
|
|
map.set(companyId, acc)
|
|
}
|
|
const internal = t.internalWorkedMs ?? 0
|
|
const external = t.externalWorkedMs ?? 0
|
|
acc.internalMs += internal
|
|
acc.externalMs += external
|
|
acc.totalMs += internal + external
|
|
}
|
|
|
|
const items = Array.from(map.values()).sort((a, b) => b.totalMs - a.totalMs)
|
|
return {
|
|
rangeDays: days,
|
|
items: items.map((i) => ({
|
|
companyId: i.companyId,
|
|
name: i.name,
|
|
isAvulso: i.isAvulso,
|
|
internalMs: i.internalMs,
|
|
externalMs: i.externalMs,
|
|
totalMs: i.totalMs,
|
|
contractedHoursPerMonth: i.contractedHoursPerMonth ?? null,
|
|
})),
|
|
}
|
|
}
|
|
|
|
export const hoursByClient = query({
|
|
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()) },
|
|
handler: hoursByClientHandler,
|
|
})
|
|
|
|
// Internal variant used by scheduled jobs: skips viewer scoping and aggregates for the whole tenant
|
|
export async function hoursByClientInternalHandler(
|
|
ctx: QueryCtx,
|
|
{ tenantId, range }: { tenantId: string; range?: string }
|
|
) {
|
|
const tickets = await fetchTickets(ctx, tenantId)
|
|
|
|
const days = range === "7d" ? 7 : range === "30d" ? 30 : 90
|
|
const end = new Date()
|
|
end.setUTCHours(0, 0, 0, 0)
|
|
const endMs = end.getTime() + ONE_DAY_MS
|
|
const startMs = endMs - days * ONE_DAY_MS
|
|
|
|
type Acc = {
|
|
companyId: Id<"companies">
|
|
name: string
|
|
isAvulso: boolean
|
|
internalMs: number
|
|
externalMs: number
|
|
totalMs: number
|
|
contractedHoursPerMonth?: number | null
|
|
}
|
|
const map = new Map<string, Acc>()
|
|
|
|
for (const t of tickets) {
|
|
if (t.updatedAt < startMs || t.updatedAt >= endMs) continue
|
|
const companyId = t.companyId ?? null
|
|
if (!companyId) continue
|
|
|
|
let acc = map.get(companyId)
|
|
if (!acc) {
|
|
const company = await ctx.db.get(companyId)
|
|
acc = {
|
|
companyId,
|
|
name: company?.name ?? "Sem empresa",
|
|
isAvulso: Boolean(company?.isAvulso ?? false),
|
|
internalMs: 0,
|
|
externalMs: 0,
|
|
totalMs: 0,
|
|
contractedHoursPerMonth: company?.contractedHoursPerMonth ?? null,
|
|
}
|
|
map.set(companyId, acc)
|
|
}
|
|
const internal = t.internalWorkedMs ?? 0
|
|
const external = t.externalWorkedMs ?? 0
|
|
acc.internalMs += internal
|
|
acc.externalMs += external
|
|
acc.totalMs += internal + external
|
|
}
|
|
|
|
const items = Array.from(map.values()).sort((a, b) => b.totalMs - a.totalMs)
|
|
return {
|
|
rangeDays: days,
|
|
items: items.map((i) => ({
|
|
companyId: i.companyId,
|
|
name: i.name,
|
|
isAvulso: i.isAvulso,
|
|
internalMs: i.internalMs,
|
|
externalMs: i.externalMs,
|
|
totalMs: i.totalMs,
|
|
contractedHoursPerMonth: i.contractedHoursPerMonth ?? null,
|
|
})),
|
|
}
|
|
}
|
|
|
|
export const hoursByClientInternal = query({
|
|
args: { tenantId: v.string(), range: v.optional(v.string()) },
|
|
handler: hoursByClientInternalHandler,
|
|
})
|
|
|
|
|
|
export const companyOverview = query({
|
|
args: {
|
|
tenantId: v.string(),
|
|
viewerId: v.id("users"),
|
|
companyId: v.id("companies"),
|
|
range: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, { tenantId, viewerId, companyId, range }) => {
|
|
const viewer = await requireStaff(ctx, viewerId, tenantId);
|
|
if (viewer.role === "MANAGER" && viewer.user.companyId && viewer.user.companyId !== companyId) {
|
|
throw new ConvexError("Gestores só podem consultar relatórios da própria empresa");
|
|
}
|
|
|
|
const company = await ctx.db.get(companyId);
|
|
if (!company || company.tenantId !== tenantId) {
|
|
throw new ConvexError("Empresa não encontrada");
|
|
}
|
|
|
|
const normalizedRange = (range ?? "30d").toLowerCase();
|
|
const rangeDays = normalizedRange === "90d" ? 90 : normalizedRange === "7d" ? 7 : 30;
|
|
const now = Date.now();
|
|
const startMs = now - rangeDays * ONE_DAY_MS;
|
|
|
|
const tickets = await ctx.db
|
|
.query("tickets")
|
|
.withIndex("by_tenant_company", (q) => q.eq("tenantId", tenantId).eq("companyId", companyId))
|
|
.collect();
|
|
|
|
const machines = await ctx.db
|
|
.query("machines")
|
|
.withIndex("by_tenant_company", (q) => q.eq("tenantId", tenantId).eq("companyId", companyId))
|
|
.collect();
|
|
|
|
const users = await ctx.db
|
|
.query("users")
|
|
.withIndex("by_tenant_company", (q) => q.eq("tenantId", tenantId).eq("companyId", companyId))
|
|
.collect();
|
|
|
|
const statusCounts = {} as Record<string, number>;
|
|
const priorityCounts = {} as Record<string, number>;
|
|
const channelCounts = {} as Record<string, number>;
|
|
const trendMap = new Map<string, { opened: number; resolved: number }>();
|
|
const openTickets: Doc<"tickets">[] = [];
|
|
|
|
tickets.forEach((ticket) => {
|
|
const normalizedStatus = normalizeStatus(ticket.status);
|
|
statusCounts[normalizedStatus] = (statusCounts[normalizedStatus] ?? 0) + 1;
|
|
|
|
const priorityKey = (ticket.priority ?? "MEDIUM").toUpperCase();
|
|
priorityCounts[priorityKey] = (priorityCounts[priorityKey] ?? 0) + 1;
|
|
|
|
const channelKey = (ticket.channel ?? "MANUAL").toUpperCase();
|
|
channelCounts[channelKey] = (channelCounts[channelKey] ?? 0) + 1;
|
|
|
|
if (normalizedStatus !== "RESOLVED") {
|
|
openTickets.push(ticket);
|
|
}
|
|
|
|
if (ticket.createdAt >= startMs) {
|
|
const key = formatDateKey(ticket.createdAt);
|
|
if (!trendMap.has(key)) {
|
|
trendMap.set(key, { opened: 0, resolved: 0 });
|
|
}
|
|
trendMap.get(key)!.opened += 1;
|
|
}
|
|
|
|
if (typeof ticket.resolvedAt === "number" && ticket.resolvedAt >= startMs) {
|
|
const key = formatDateKey(ticket.resolvedAt);
|
|
if (!trendMap.has(key)) {
|
|
trendMap.set(key, { opened: 0, resolved: 0 });
|
|
}
|
|
trendMap.get(key)!.resolved += 1;
|
|
}
|
|
});
|
|
|
|
const machineStatusCounts: Record<string, number> = {};
|
|
const machineOsCounts: Record<string, number> = {};
|
|
const machineLookup = new Map<string, Doc<"machines">>();
|
|
|
|
machines.forEach((machine) => {
|
|
machineLookup.set(String(machine._id), machine);
|
|
const status = deriveMachineStatus(machine, now);
|
|
machineStatusCounts[status] = (machineStatusCounts[status] ?? 0) + 1;
|
|
const osLabel = formatOsLabel(machine.osName ?? null, machine.osVersion ?? null);
|
|
machineOsCounts[osLabel] = (machineOsCounts[osLabel] ?? 0) + 1;
|
|
});
|
|
|
|
const roleCounts: Record<string, number> = {};
|
|
users.forEach((user) => {
|
|
const roleKey = (user.role ?? "COLLABORATOR").toUpperCase();
|
|
roleCounts[roleKey] = (roleCounts[roleKey] ?? 0) + 1;
|
|
});
|
|
|
|
const trend = Array.from(trendMap.entries())
|
|
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
.map(([date, value]) => ({ date, opened: value.opened, resolved: value.resolved }));
|
|
|
|
const machineOsDistribution = Object.entries(machineOsCounts)
|
|
.map(([label, value]) => ({ label, value }))
|
|
.sort((a, b) => b.value - a.value);
|
|
|
|
const openTicketSummaries = openTickets
|
|
.sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0))
|
|
.slice(0, 20)
|
|
.map((ticket) => {
|
|
const machineSnapshot = ticket.machineSnapshot as { hostname?: string } | undefined;
|
|
const machine = ticket.machineId ? machineLookup.get(String(ticket.machineId)) : null;
|
|
return {
|
|
id: ticket._id,
|
|
reference: ticket.reference,
|
|
subject: ticket.subject,
|
|
status: normalizeStatus(ticket.status),
|
|
priority: ticket.priority,
|
|
updatedAt: ticket.updatedAt,
|
|
createdAt: ticket.createdAt,
|
|
machine: ticket.machineId
|
|
? {
|
|
id: ticket.machineId,
|
|
hostname: machine?.hostname ?? machineSnapshot?.hostname ?? null,
|
|
}
|
|
: null,
|
|
assignee: ticket.assigneeSnapshot
|
|
? {
|
|
name: (ticket.assigneeSnapshot as { name?: string })?.name ?? null,
|
|
email: (ticket.assigneeSnapshot as { email?: string })?.email ?? null,
|
|
}
|
|
: null,
|
|
};
|
|
});
|
|
|
|
return {
|
|
company: {
|
|
id: company._id,
|
|
name: company.name,
|
|
isAvulso: company.isAvulso ?? false,
|
|
},
|
|
rangeDays,
|
|
generatedAt: now,
|
|
tickets: {
|
|
total: tickets.length,
|
|
byStatus: statusCounts,
|
|
byPriority: priorityCounts,
|
|
byChannel: channelCounts,
|
|
trend,
|
|
open: openTicketSummaries,
|
|
},
|
|
machines: {
|
|
total: machines.length,
|
|
byStatus: machineStatusCounts,
|
|
byOs: machineOsDistribution,
|
|
},
|
|
users: {
|
|
total: users.length,
|
|
byRole: roleCounts,
|
|
},
|
|
};
|
|
},
|
|
});
|