Files
flyer-crawler.projectium.com/src/routes/personalization.routes.ts
Torben Sorensen 38eb810e7a
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 11m55s
logging the frontend loop
2025-12-30 23:28:38 -08:00

75 lines
2.4 KiB
TypeScript

// src/routes/personalization.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';
const router = Router();
// --- Zod Schemas for Personalization Routes (as per ADR-003) ---
// These routes do not expect any input, so we define an empty schema
// to maintain a consistent validation pattern across the application.
const emptySchema = z.object({});
/**
* GET /api/personalization/master-items - Get the master list of all grocery items.
*/
router.get(
'/master-items',
validateRequest(emptySchema),
async (req: Request, res: Response, next: NextFunction) => {
try {
// LOGGING: Track how often this heavy DB call is actually made vs served from cache
req.log.info('Fetching master items list from database...');
// Optimization: This list changes rarely. Instruct clients to cache it for 1 hour (3600s).
res.set('Cache-Control', 'public, max-age=3600');
const masterItems = await db.personalizationRepo.getAllMasterItems(req.log);
res.json(masterItems);
} catch (error) {
req.log.error({ error }, 'Error fetching master items in /api/personalization/master-items:');
next(error);
}
},
);
/**
* GET /api/personalization/dietary-restrictions - Get the master list of all dietary restrictions.
*/
router.get(
'/dietary-restrictions',
validateRequest(emptySchema),
async (req: Request, res: Response, next: NextFunction) => {
try {
const restrictions = await db.personalizationRepo.getDietaryRestrictions(req.log);
res.json(restrictions);
} catch (error) {
req.log.error(
{ error },
'Error fetching dietary restrictions in /api/personalization/dietary-restrictions:',
);
next(error);
}
},
);
/**
* GET /api/personalization/appliances - Get the master list of all kitchen appliances.
*/
router.get(
'/appliances',
validateRequest(emptySchema),
async (req: Request, res: Response, next: NextFunction) => {
try {
const appliances = await db.personalizationRepo.getAppliances(req.log);
res.json(appliances);
} catch (error) {
req.log.error({ error }, 'Error fetching appliances in /api/personalization/appliances:');
next(error);
}
},
);
export default router;