All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 6m11s
78 lines
2.5 KiB
TypeScript
78 lines
2.5 KiB
TypeScript
// src/routes/flyer.routes.ts
|
|
import { Router, Request, Response, NextFunction } from 'express';
|
|
import * as db from '../services/db/index.db';
|
|
import { logger } from '../services/logger.server';
|
|
|
|
const router = Router();
|
|
|
|
/**
|
|
* GET /api/flyers - Get a paginated list of all flyers.
|
|
*/
|
|
router.get('/', async (req, res, next: NextFunction) => {
|
|
try {
|
|
const limit = parseInt(req.query.limit as string, 10) || 20;
|
|
const offset = parseInt(req.query.offset as string, 10) || 0;
|
|
const flyers = await db.flyerRepo.getFlyers(limit, offset);
|
|
res.json(flyers);
|
|
} catch (error) {
|
|
logger.error('Error fetching flyers in /api/flyers:', { error });
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/flyers/:id/items - Get all items for a specific flyer.
|
|
*/
|
|
router.get('/:id/items', async (req, res, next: NextFunction) => {
|
|
try {
|
|
const flyerId = parseInt(req.params.id, 10);
|
|
const items = await db.flyerRepo.getFlyerItems(flyerId);
|
|
res.json(items);
|
|
} catch (error) {
|
|
logger.error('Error fetching flyer items in /api/flyers/:id/items:', { error });
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /api/flyers/items/batch-fetch - Get all items for multiple flyers at once.
|
|
*/
|
|
router.post('/items/batch-fetch', async (req, res, next: NextFunction) => {
|
|
const { flyerIds } = req.body;
|
|
if (!Array.isArray(flyerIds)) {
|
|
return res.status(400).json({ message: 'flyerIds must be an array.' });
|
|
}
|
|
try {
|
|
const items = await db.flyerRepo.getFlyerItemsForFlyers(flyerIds);
|
|
res.json(items);
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /api/flyers/items/batch-count - Get the total number of items for multiple flyers.
|
|
*/
|
|
router.post('/items/batch-count', async (req, res, next: NextFunction) => {
|
|
const { flyerIds } = req.body;
|
|
if (!Array.isArray(flyerIds)) {
|
|
return res.status(400).json({ message: 'flyerIds must be an array.' });
|
|
}
|
|
try {
|
|
const count = await db.flyerRepo.countFlyerItemsForFlyers(flyerIds);
|
|
res.json({ count });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /api/flyers/items/:itemId/track - Tracks a user interaction with a flyer item.
|
|
*/
|
|
router.post('/items/:itemId/track', (req: Request, res: Response) => {
|
|
// This is a fire-and-forget endpoint. It's kept simple and doesn't need a full try/catch/next.
|
|
db.flyerRepo.trackFlyerItemInteraction(parseInt(req.params.itemId, 10), req.body.type);
|
|
res.status(202).send();
|
|
});
|
|
|
|
export default router; |