feat(F-048): completed feature
This commit is contained in:
@@ -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
|
||||
|
||||
2
project/src/modules/admin-stats/index.ts
Normal file
2
project/src/modules/admin-stats/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
/** Public API of the admin-stats module. */
|
||||
export { registerAdminStatsRoutes } from './api/stats.routes.js';
|
||||
Reference in New Issue
Block a user