diff options
| author | zwlucas <lucas.fariamo08@gmail.com> | 2026-07-25 21:23:29 +0000 |
|---|---|---|
| committer | zwlucas <lucas.fariamo08@gmail.com> | 2026-07-25 21:23:29 +0000 |
| commit | ef55d9bb24430610b60c1b77821307cd72c08c55 (patch) | |
| tree | e6935a810b34370b0a4c821e55f6397aac50c832 /api/src/modules/stats/stats.repository.ts | |
| download | helpdesk-master.tar.gz helpdesk-master.zip | |
Signed-off-by: zwlucas <lucas.fariamo08@gmail.com>
Diffstat (limited to 'api/src/modules/stats/stats.repository.ts')
| -rw-r--r-- | api/src/modules/stats/stats.repository.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/api/src/modules/stats/stats.repository.ts b/api/src/modules/stats/stats.repository.ts new file mode 100644 index 0000000..d5a543d --- /dev/null +++ b/api/src/modules/stats/stats.repository.ts @@ -0,0 +1,41 @@ +/** + * stats.repository.ts + * Queries para obter os totais agrupados. + */ + +import { sql } from 'drizzle-orm'; +import type { Database } from '@/db/connection.js'; +import { tickets } from '@/db/schema.js'; + +export async function getTicketsStats(db: Database) { + // Uma Ășnica query para agrupar as contagens por status + const rows = await db + .select({ + status: tickets.status, + count: sql<number>`count(*)`, + }) + .from(tickets) + .groupBy(tickets.status); + + // Inicializa o mapa com 0 + const stats = { + totalOpen: 0, + totalInProgress: 0, + totalWaiting: 0, + totalResolved: 0, + totalClosed: 0, + }; + + for (const row of rows) { + if (row.status === 'open') stats.totalOpen = row.count; + if (row.status === 'in_progress') stats.totalInProgress = row.count; + if (row.status === 'waiting') stats.totalWaiting = row.count; + if (row.status === 'resolved') stats.totalResolved = row.count; + if (row.status === 'closed') stats.totalClosed = row.count; + } + + return { + ...stats, + totalPending: stats.totalOpen + stats.totalInProgress, + }; +} |