Files
flyer-crawler.projectium.com/src/routes/stats.routes.ts
Torben Sorensen 3912139273
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 16m24s
adr-028 and int tests
2026-01-09 12:47:41 -08:00

50 lines
1.7 KiB
TypeScript

// src/routes/stats.routes.ts
import { Router, Request, Response, NextFunction } from 'express';
import { z } from 'zod';
import * as db from '../services/db/index.db';
import { validateRequest } from '../middleware/validation.middleware';
import { optionalNumeric } from '../utils/zodUtils';
import { publicReadLimiter } from '../config/rateLimiters';
import { sendSuccess } from '../utils/apiResponse';
const router = Router();
// --- Zod Schema for Stats Routes (as per ADR-003) ---
// Define the query schema separately so we can use it to parse req.query in the handler
const statsQuerySchema = z.object({
days: optionalNumeric({ default: 30, min: 1, max: 365, integer: true }),
limit: optionalNumeric({ default: 10, min: 1, max: 50, integer: true }),
});
const mostFrequentSalesSchema = z.object({
query: statsQuerySchema,
});
/**
* GET /api/stats/most-frequent-sales - Get a list of items that have been on sale most frequently.
* This is a public endpoint for data analysis.
*/
router.get(
'/most-frequent-sales',
publicReadLimiter,
validateRequest(mostFrequentSalesSchema),
async (req: Request, res: Response, next: NextFunction) => {
try {
// The `validateRequest` middleware ensures `req.query` is valid.
// We parse it here to apply Zod's coercions (string to number) and defaults.
const { days, limit } = statsQuerySchema.parse(req.query);
const items = await db.adminRepo.getMostFrequentSaleItems(days!, limit!, req.log);
sendSuccess(res, items);
} catch (error) {
req.log.error(
{ error },
'Error fetching most frequent sale items in /api/stats/most-frequent-sales:',
);
next(error);
}
},
);
export default router;