feat(F-048): completed feature

This commit is contained in:
chattie
2026-08-19 07:17:14 +02:00
parent 8ee1938af9
commit 835ab66eda
187 changed files with 12361 additions and 1065 deletions

View File

@@ -1,19 +1,26 @@
import type { FastifyInstance } from 'fastify';
import type { FastifySchema } from 'fastify';
import type pg from 'pg';
import { z } from 'zod';
import { requireRole, type Authenticate } from '../../../shared/auth.js';
import { parseJson } from '../../../shared/http-input.js';
import { errorSchema } from '../../../shared/swagger.js';
interface AdminStatsDeps {
pool: pg.Pool;
authenticate: Authenticate;
}
const statsSchema: FastifySchema = {
tags: ['Admin'],
summary: 'Admin dashboard stats',
description: 'KPIs del dashboard: pedidos hoy, ingresos, productos sin stock, etc.',
response: { 401: errorSchema, 403: errorSchema },
};
export async function registerAdminStatsRoutes(
app: FastifyInstance,
deps: AdminStatsDeps,
): Promise<void> {
app.get('/admin/stats', async (request, reply) => {
app.get('/admin/stats', { schema: statsSchema }, async (request, reply) => {
const user = await deps.authenticate(request);
requireRole(user, 'admin');
@@ -51,9 +58,7 @@ export async function registerAdminStatsRoutes(
`SELECT state, COUNT(*)::text AS count FROM orders_orders
GROUP BY state ORDER BY count DESC`,
)
.then((r) =>
Object.fromEntries(r.rows.map((row) => [row.state, parseInt(row.count, 10)])),
),
.then((r) => Object.fromEntries(r.rows.map((row) => [row.state, parseInt(row.count, 10)]))),
// Out-of-stock variants
pool

View File

@@ -0,0 +1,2 @@
/** Public API of the admin-stats module. */
export { registerAdminStatsRoutes } from './api/stats.routes.js';