All files / src/routes upc.routes.ts

92.25% Statements 512/555
70.37% Branches 19/27
100% Functions 11/11
93.3% Lines 460/493

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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 4942x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 24x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 16x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 11x 2x 2x 2x 11x 2x 2x 2x 2x 2x 2x 24x 4x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 2x 2x 12x 2x 2x 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 4x 4x 4x 4x 4x 4x 4x           2x 2x 2x 1x 1x 1x 1x 1x 24x 1x 1x 1x 13x 1x 13x   13x 13x 2x 2x 2x 2x 13x 11x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x         2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x       24x 2x 2x 2x 2x 6x   6x 6x   6x             5x   1x 1x                       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 9x 2x 9x 2x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 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 6x 2x 6x 2x 6x 6x 3x 2x 3x 2x 2x 2x 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 2x 24x 4x 2x 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 24x 2x 2x 2x 2x 5x 2x 5x 2x 5x 5x 2x 2x 2x 2x 5x 2x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 2x 2x 2x 2x 2x  
// src/routes/upc.routes.ts
/**
 * @file UPC Scanning API Routes
 * Provides endpoints for UPC barcode scanning, lookup, and scan history.
 */
import express, { Request, Response, NextFunction } from 'express';
import { z } from 'zod';
import passport, { isAdmin } from '../config/passport';
import type { UserProfile } from '../types';
import { validateRequest } from '../middleware/validation.middleware';
import { numericIdParam, optionalNumeric } from '../utils/zodUtils';
import { sendSuccess, sendNoContent } from '../utils/apiResponse';
import * as upcService from '../services/upcService.server';
 
const router = express.Router();
 
// --- Zod Schemas for UPC Routes ---
 
/**
 * UPC code validation (8-14 digits)
 */
const upcCodeSchema = z.string().regex(/^[0-9]{8,14}$/, 'UPC code must be 8-14 digits.');
 
/**
 * Scan source validation
 */
const scanSourceSchema = z.enum(['image_upload', 'manual_entry', 'phone_app', 'camera_scan']);
 
/**
 * Schema for UPC scan request
 */
const scanUpcSchema = z.object({
  body: z
    .object({
      upc_code: z.string().optional(),
      image_base64: z.string().optional(),
      scan_source: scanSourceSchema,
    })
    .refine((data) => data.upc_code || data.image_base64, {
      message: 'Either upc_code or image_base64 must be provided.',
    }),
});
 
/**
 * Schema for UPC lookup request (without recording scan)
 */
const lookupUpcSchema = z.object({
  query: z.object({
    upc_code: upcCodeSchema,
    include_external: z
      .string()
      .optional()
      .transform((val) => val === 'true'),
    force_refresh: z
      .string()
      .optional()
      .transform((val) => val === 'true'),
  }),
});
 
/**
 * Schema for linking UPC to product (admin)
 */
const linkUpcSchema = z.object({
  body: z.object({
    upc_code: upcCodeSchema,
    product_id: z.number().int().positive('Product ID must be a positive integer.'),
  }),
});
 
/**
 * Schema for scan ID parameter
 */
const scanIdParamSchema = numericIdParam(
  'scanId',
  "Invalid ID for parameter 'scanId'. Must be a number.",
);
 
/**
 * Schema for scan history query
 */
const scanHistoryQuerySchema = z.object({
  query: z.object({
    limit: optionalNumeric({ default: 50, min: 1, max: 100, integer: true }),
    offset: optionalNumeric({ default: 0, min: 0, integer: true }),
    lookup_successful: z
      .string()
      .optional()
      .transform((val) => (val === 'true' ? true : val === 'false' ? false : undefined)),
    scan_source: scanSourceSchema.optional(),
    from_date: z.string().date().optional(),
    to_date: z.string().date().optional(),
  }),
});
 
// Middleware to ensure user is authenticated for all UPC routes
router.use(passport.authenticate('jwt', { session: false }));
 
/**
 * @openapi
 * /upc/scan:
 *   post:
 *     tags: [UPC Scanning]
 *     summary: Scan a UPC barcode
 *     description: >
 *       Scans a UPC barcode either from a manually entered code or from an image.
 *       Records the scan in history and returns product information if found.
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - scan_source
 *             properties:
 *               upc_code:
 *                 type: string
 *                 pattern: '^[0-9]{8,14}$'
 *                 description: UPC code (8-14 digits). Required if image_base64 is not provided.
 *               image_base64:
 *                 type: string
 *                 description: Base64-encoded image containing a barcode. Required if upc_code is not provided.
 *               scan_source:
 *                 type: string
 *                 enum: [image_upload, manual_entry, phone_app, camera_scan]
 *                 description: How the scan was initiated.
 *     responses:
 *       200:
 *         description: Scan completed successfully
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/SuccessResponse'
 *       400:
 *         description: Validation error - invalid UPC code or missing data
 *       401:
 *         description: Unauthorized - invalid or missing token
 */
router.post(
  '/scan',
  validateRequest(scanUpcSchema),
  async (req: Request, res: Response, next: NextFunction) => {
    const userProfile = req.user as UserProfile;
    type ScanUpcRequest = z.infer<typeof scanUpcSchema>;
    const { body } = req as unknown as ScanUpcRequest;

    try {
      req.log.info(
        { userId: userProfile.user.user_id, scanSource: body.scan_source },
        'UPC scan request received',
      );
 
      const result = await upcService.scanUpc(userProfile.user.user_id, body, req.log);
      sendSuccess(res, result);
    } catch (error) {
      req.log.error(
        { error, userId: userProfile.user.user_id, scanSource: body.scan_source },
        'Error processing UPC scan',
      );
      next(error);
    }
  },
);
 
/**
 * @openapi
 * /upc/lookup:
 *   get:
 *     tags: [UPC Scanning]
 *     summary: Look up a UPC code
 *     description: >
 *       Looks up product information for a UPC code without recording in scan history.
 *       Useful for verification or quick lookups.
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: upc_code
 *         required: true
 *         schema:
 *           type: string
 *           pattern: '^[0-9]{8,14}$'
 *         description: UPC code to look up (8-14 digits)
 *       - in: query
 *         name: include_external
 *         schema:
 *           type: boolean
 *           default: true
 *         description: Whether to check external APIs if not found locally
 *       - in: query
 *         name: force_refresh
 *         schema:
 *           type: boolean
 *           default: false
 *         description: Skip cache and perform fresh external lookup
 *     responses:
 *       200:
 *         description: Lookup completed
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/SuccessResponse'
 *       400:
 *         description: Invalid UPC code format
 *       401:
 *         description: Unauthorized - invalid or missing token
 */
router.get(
  '/lookup',
  validateRequest(lookupUpcSchema),
  async (req: Request, res: Response, next: NextFunction) => {
    type LookupUpcRequest = z.infer<typeof lookupUpcSchema>;
    const { query } = req as unknown as LookupUpcRequest;

    try {
      req.log.debug({ upcCode: query.upc_code }, 'UPC lookup request received');

      const result = await upcService.lookupUpc(
        {
          upc_code: query.upc_code,
          force_refresh: query.force_refresh,
        },
        req.log,
      );
      sendSuccess(res, result);
    } catch (error) {
      req.log.error({ error, upcCode: query.upc_code }, 'Error looking up UPC');
      next(error);
    }
  },
);

/**
 * @openapi
 * /upc/history:
 *   get:
 *     tags: [UPC Scanning]
 *     summary: Get scan history
 *     description: Retrieve the authenticated user's UPC scan history with optional filtering.
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 50
 *         description: Maximum number of results
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *           minimum: 0
 *           default: 0
 *         description: Number of results to skip
 *       - in: query
 *         name: lookup_successful
 *         schema:
 *           type: boolean
 *         description: Filter by lookup success status
 *       - in: query
 *         name: scan_source
 *         schema:
 *           type: string
 *           enum: [image_upload, manual_entry, phone_app, camera_scan]
 *         description: Filter by scan source
 *       - in: query
 *         name: from_date
 *         schema:
 *           type: string
 *           format: date
 *         description: Filter scans from this date (YYYY-MM-DD)
 *       - in: query
 *         name: to_date
 *         schema:
 *           type: string
 *           format: date
 *         description: Filter scans until this date (YYYY-MM-DD)
 *     responses:
 *       200:
 *         description: Scan history retrieved
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/SuccessResponse'
 *       401:
 *         description: Unauthorized - invalid or missing token
 */
router.get(
  '/history',
  validateRequest(scanHistoryQuerySchema),
  async (req: Request, res: Response, next: NextFunction) => {
    const userProfile = req.user as UserProfile;
    type ScanHistoryRequest = z.infer<typeof scanHistoryQuerySchema>;
    const { query } = req as unknown as ScanHistoryRequest;
 
    try {
      const result = await upcService.getScanHistory(
        {
          user_id: userProfile.user.user_id,
          limit: query.limit,
          offset: query.offset,
          lookup_successful: query.lookup_successful,
          scan_source: query.scan_source,
          from_date: query.from_date,
          to_date: query.to_date,
        },
        req.log,
      );
      sendSuccess(res, result);
    } catch (error) {
      req.log.error({ error, userId: userProfile.user.user_id }, 'Error fetching scan history');
      next(error);
    }
  },
);
 
/**
 * @openapi
 * /upc/history/{scanId}:
 *   get:
 *     tags: [UPC Scanning]
 *     summary: Get scan by ID
 *     description: Retrieve a specific scan record by its ID.
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: scanId
 *         required: true
 *         schema:
 *           type: integer
 *         description: Scan ID
 *     responses:
 *       200:
 *         description: Scan record retrieved
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/SuccessResponse'
 *       401:
 *         description: Unauthorized - invalid or missing token
 *       404:
 *         description: Scan record not found
 */
router.get(
  '/history/:scanId',
  validateRequest(scanIdParamSchema),
  async (req: Request, res: Response, next: NextFunction) => {
    const userProfile = req.user as UserProfile;
    type GetScanRequest = z.infer<typeof scanIdParamSchema>;
    const { params } = req as unknown as GetScanRequest;
 
    try {
      const scan = await upcService.getScanById(params.scanId, userProfile.user.user_id, req.log);
      sendSuccess(res, scan);
    } catch (error) {
      req.log.error(
        { error, userId: userProfile.user.user_id, scanId: params.scanId },
        'Error fetching scan by ID',
      );
      next(error);
    }
  },
);
 
/**
 * @openapi
 * /upc/stats:
 *   get:
 *     tags: [UPC Scanning]
 *     summary: Get scan statistics
 *     description: Get scanning statistics for the authenticated user.
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Scan statistics retrieved
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                 data:
 *                   type: object
 *                   properties:
 *                     total_scans:
 *                       type: integer
 *                     successful_lookups:
 *                       type: integer
 *                     unique_products:
 *                       type: integer
 *                     scans_today:
 *                       type: integer
 *                     scans_this_week:
 *                       type: integer
 *       401:
 *         description: Unauthorized - invalid or missing token
 */
router.get('/stats', async (req: Request, res: Response, next: NextFunction) => {
  const userProfile = req.user as UserProfile;
 
  try {
    const stats = await upcService.getScanStats(userProfile.user.user_id, req.log);
    sendSuccess(res, stats);
  } catch (error) {
    req.log.error({ error, userId: userProfile.user.user_id }, 'Error fetching scan statistics');
    next(error);
  }
});
 
/**
 * @openapi
 * /upc/link:
 *   post:
 *     tags: [UPC Scanning]
 *     summary: Link UPC to product (Admin)
 *     description: >
 *       Links a UPC code to an existing product in the database.
 *       This is an admin-only operation.
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - upc_code
 *               - product_id
 *             properties:
 *               upc_code:
 *                 type: string
 *                 pattern: '^[0-9]{8,14}$'
 *                 description: UPC code to link (8-14 digits)
 *               product_id:
 *                 type: integer
 *                 description: Product ID to link the UPC to
 *     responses:
 *       204:
 *         description: UPC linked successfully
 *       400:
 *         description: Invalid UPC code or product ID
 *       401:
 *         description: Unauthorized - invalid or missing token
 *       403:
 *         description: Forbidden - user is not an admin
 *       404:
 *         description: Product not found
 *       409:
 *         description: UPC code already linked to another product
 */
router.post(
  '/link',
  isAdmin, // Admin role check - only admins can link UPC codes to products
  validateRequest(linkUpcSchema),
  async (req: Request, res: Response, next: NextFunction) => {
    const userProfile = req.user as UserProfile;
    type LinkUpcRequest = z.infer<typeof linkUpcSchema>;
    const { body } = req as unknown as LinkUpcRequest;
 
    try {
      req.log.info(
        { userId: userProfile.user.user_id, productId: body.product_id, upcCode: body.upc_code },
        'UPC link request received',
      );
 
      await upcService.linkUpcToProduct(body.product_id, body.upc_code, req.log);
      sendNoContent(res);
    } catch (error) {
      req.log.error(
        {
          error,
          userId: userProfile.user.user_id,
          productId: body.product_id,
          upcCode: body.upc_code,
        },
        'Error linking UPC to product',
      );
      next(error);
    }
  },
);
 
export default router;