fix: avoid broken font and speed up backlog overview
This commit is contained in:
parent
38e4bbea7f
commit
d92c817e7b
4 changed files with 44 additions and 2164 deletions
|
|
@ -81,6 +81,44 @@ async function fetchScopedTickets(
|
|||
return fetchTickets(ctx, tenantId);
|
||||
}
|
||||
|
||||
async function fetchScopedTicketsByCreatedRange(
|
||||
ctx: QueryCtx,
|
||||
tenantId: string,
|
||||
viewer: Awaited<ReturnType<typeof requireStaff>>,
|
||||
startMs: number,
|
||||
endMs: number,
|
||||
companyId?: Id<"companies">,
|
||||
) {
|
||||
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_created", (q) =>
|
||||
q.eq("tenantId", tenantId).eq("companyId", viewer.user.companyId!).gte("createdAt", startMs),
|
||||
)
|
||||
.filter((q) => q.lt(q.field("createdAt"), endMs))
|
||||
.collect();
|
||||
}
|
||||
|
||||
if (companyId) {
|
||||
return ctx.db
|
||||
.query("tickets")
|
||||
.withIndex("by_tenant_company_created", (q) =>
|
||||
q.eq("tenantId", tenantId).eq("companyId", companyId).gte("createdAt", startMs),
|
||||
)
|
||||
.filter((q) => q.lt(q.field("createdAt"), endMs))
|
||||
.collect();
|
||||
}
|
||||
|
||||
return ctx.db
|
||||
.query("tickets")
|
||||
.withIndex("by_tenant_created", (q) => q.eq("tenantId", tenantId).gte("createdAt", startMs))
|
||||
.filter((q) => q.lt(q.field("createdAt"), endMs))
|
||||
.collect();
|
||||
}
|
||||
|
||||
async function fetchQueues(ctx: QueryCtx, tenantId: string) {
|
||||
return ctx.db
|
||||
.query("queues")
|
||||
|
|
@ -306,16 +344,13 @@ export const backlogOverview = query({
|
|||
args: { tenantId: v.string(), viewerId: v.id("users"), range: v.optional(v.string()), companyId: v.optional(v.id("companies")) },
|
||||
handler: async (ctx, { tenantId, viewerId, range, companyId }) => {
|
||||
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
|
||||
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 inRange = await fetchScopedTicketsByCreatedRange(ctx, tenantId, viewer, startMs, endMs, companyId);
|
||||
|
||||
const statusCounts = inRange.reduce<Record<TicketStatusNormalized, number>>((acc, ticket) => {
|
||||
const status = normalizeStatus(ticket.status);
|
||||
|
|
|
|||
|
|
@ -181,7 +181,9 @@ export default defineSchema({
|
|||
.index("by_tenant_requester", ["tenantId", "requesterId"])
|
||||
.index("by_tenant_company", ["tenantId", "companyId"])
|
||||
.index("by_tenant_machine", ["tenantId", "machineId"])
|
||||
.index("by_tenant", ["tenantId"]),
|
||||
.index("by_tenant", ["tenantId"])
|
||||
.index("by_tenant_created", ["tenantId", "createdAt"])
|
||||
.index("by_tenant_company_created", ["tenantId", "companyId", "createdAt"]),
|
||||
|
||||
ticketComments: defineTable({
|
||||
ticketId: v.id("tickets"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue