Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 10x 2x 10x 2x 10x 9x 1x 1x 2x 2x 2x 2x 2x 2x 2x 24x 4x 4x 4x 2x 2x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 4x 4x 4x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 5x 5x 2x 2x 5x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 6x 6x 2x 2x 6x 5x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 5x 2x 5x 2x 2x 2x 5x 2x 2x 2x 5x 2x 2x 2x 2x 2x 2x 2x 2x | // src/routes/flyer.routes.ts
import { Router } from 'express';
import * as db from '../services/db/index.db';
import { z } from 'zod';
import { validateRequest } from '../middleware/validation.middleware';
import { optionalNumeric } from '../utils/zodUtils';
import { publicReadLimiter, batchLimiter, trackingLimiter } from '../config/rateLimiters';
import { sendSuccess } from '../utils/apiResponse';
const router = Router();
// --- Zod Schemas for Flyer Routes ---
const getFlyersSchema = z.object({
query: z.object({
limit: optionalNumeric({ default: 20, integer: true, positive: true }),
offset: optionalNumeric({ default: 0, integer: true, nonnegative: true }),
}),
});
const flyerIdParamSchema = z.object({
params: z.object({
id: z.coerce.number().int('Invalid flyer ID provided.').positive('Invalid flyer ID provided.'),
}),
});
const batchFetchSchema = z.object({
body: z.object({
flyerIds: z.array(z.number().int().positive()).min(1, 'flyerIds must be a non-empty array.'),
}),
});
const batchCountSchema = z.object({
body: z.object({
flyerIds: z.array(z.number().int().positive()),
}),
});
const trackItemSchema = z.object({
params: z.object({
itemId: z.coerce.number().int().positive('Invalid item ID provided.'),
}),
body: z.object({
type: z.enum(['view', 'click'], {
message: 'A valid interaction type ("view" or "click") is required.',
}),
}),
});
/**
* @openapi
* /flyers:
* get:
* summary: Get all flyers
* description: Returns a paginated list of all flyers.
* tags:
* - Flyers
* parameters:
* - in: query
* name: limit
* schema:
* type: integer
* default: 20
* description: Maximum number of flyers to return
* - in: query
* name: offset
* schema:
* type: integer
* default: 0
* description: Number of flyers to skip
* responses:
* 200:
* description: List of flyers
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: object
* properties:
* flyer_id:
* type: integer
* store_id:
* type: integer
* flyer_name:
* type: string
* start_date:
* type: string
* format: date
* end_date:
* type: string
* format: date
*/
router.get(
'/',
publicReadLimiter,
validateRequest(getFlyersSchema),
async (req, res, next): Promise<void> => {
try {
// The `validateRequest` middleware ensures `req.query` is valid.
// We parse it here to apply Zod's coercions (string to number) and defaults.
const { limit, offset } = getFlyersSchema.shape.query.parse(req.query);
const flyers = await db.flyerRepo.getFlyers(req.log, limit, offset);
sendSuccess(res, flyers);
} catch (error) {
req.log.error({ error }, 'Error fetching flyers in /api/flyers:');
next(error);
}
},
);
/**
* @openapi
* /flyers/{id}:
* get:
* summary: Get flyer by ID
* description: Returns a single flyer by its ID.
* tags:
* - Flyers
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: The flyer ID
* responses:
* 200:
* description: Flyer details
* 404:
* description: Flyer not found
*/
router.get(
'/:id',
publicReadLimiter,
validateRequest(flyerIdParamSchema),
async (req, res, next): Promise<void> => {
try {
// Explicitly parse to get the coerced number type for `id`.
const { id } = flyerIdParamSchema.shape.params.parse(req.params);
const flyer = await db.flyerRepo.getFlyerById(id);
sendSuccess(res, flyer);
} catch (error) {
req.log.error({ error, flyerId: req.params.id }, 'Error fetching flyer by ID:');
next(error);
}
},
);
/**
* @openapi
* /flyers/{id}/items:
* get:
* summary: Get flyer items
* description: Returns all items (deals) for a specific flyer.
* tags:
* - Flyers
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: The flyer ID
* responses:
* 200:
* description: List of flyer items
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: array
* items:
* type: object
* properties:
* item_id:
* type: integer
* item_name:
* type: string
* price:
* type: number
* unit:
* type: string
*/
router.get(
'/:id/items',
publicReadLimiter,
validateRequest(flyerIdParamSchema),
async (req, res, next): Promise<void> => {
try {
// Explicitly parse to get the coerced number type for `id`.
const { id } = flyerIdParamSchema.shape.params.parse(req.params);
const items = await db.flyerRepo.getFlyerItems(id, req.log);
sendSuccess(res, items);
} catch (error) {
req.log.error(
{ error, flyerId: req.params.id },
'Error fetching flyer items in /api/flyers/:id/items:',
);
next(error);
}
},
);
/**
* @openapi
* /flyers/items/batch-fetch:
* post:
* summary: Batch fetch flyer items
* description: Returns all items for multiple flyers in a single request.
* tags:
* - Flyers
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - flyerIds
* properties:
* flyerIds:
* type: array
* items:
* type: integer
* minItems: 1
* example: [1, 2, 3]
* responses:
* 200:
* description: Items for all requested flyers
*/
type BatchFetchRequest = z.infer<typeof batchFetchSchema>;
router.post(
'/items/batch-fetch',
batchLimiter,
validateRequest(batchFetchSchema),
async (req, res, next): Promise<void> => {
const { body } = req as unknown as BatchFetchRequest;
try {
// No re-parsing needed here as `validateRequest` has already ensured the body shape,
// and `express.json()` has parsed it. There's no type coercion to apply.
const items = await db.flyerRepo.getFlyerItemsForFlyers(body.flyerIds, req.log);
sendSuccess(res, items);
} catch (error) {
req.log.error({ error }, 'Error fetching batch flyer items');
next(error);
}
},
);
/**
* @openapi
* /flyers/items/batch-count:
* post:
* summary: Batch count flyer items
* description: Returns the total item count for multiple flyers.
* tags:
* - Flyers
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - flyerIds
* properties:
* flyerIds:
* type: array
* items:
* type: integer
* example: [1, 2, 3]
* responses:
* 200:
* description: Total item count
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: object
* properties:
* count:
* type: integer
* example: 42
*/
type BatchCountRequest = z.infer<typeof batchCountSchema>;
router.post(
'/items/batch-count',
batchLimiter,
validateRequest(batchCountSchema),
async (req, res, next): Promise<void> => {
const { body } = req as unknown as BatchCountRequest;
try {
// The schema ensures flyerIds is an array of numbers.
// The `?? []` was redundant as `validateRequest` would have already caught a missing `flyerIds`.
const count = await db.flyerRepo.countFlyerItemsForFlyers(body.flyerIds, req.log);
sendSuccess(res, { count });
} catch (error) {
req.log.error({ error }, 'Error counting batch flyer items');
next(error);
}
},
);
/**
* @openapi
* /flyers/items/{itemId}/track:
* post:
* summary: Track item interaction
* description: Records a view or click interaction with a flyer item for analytics.
* tags:
* - Flyers
* parameters:
* - in: path
* name: itemId
* required: true
* schema:
* type: integer
* description: The flyer item ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - type
* properties:
* type:
* type: string
* enum: [view, click]
* description: Type of interaction
* responses:
* 202:
* description: Tracking accepted (fire-and-forget)
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* example: true
* data:
* type: object
* properties:
* message:
* type: string
* example: Tracking accepted
*/
router.post(
'/items/:itemId/track',
trackingLimiter,
validateRequest(trackItemSchema),
(req, res, next): void => {
try {
// Explicitly parse to get coerced types.
const { params, body } = trackItemSchema.parse({ params: req.params, body: req.body });
// Fire-and-forget: we don't await the tracking call to avoid delaying the response.
// We add a .catch to log any potential errors without crashing the server process.
db.flyerRepo.trackFlyerItemInteraction(params.itemId, body.type, req.log).catch((error) => {
req.log.error({ error, itemId: params.itemId }, 'Flyer item interaction tracking failed');
});
sendSuccess(res, { message: 'Tracking accepted' }, 202);
} catch (error) {
// This will catch Zod parsing errors if they occur.
next(error);
}
},
);
export default router;
|