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 | import { Request, Response } from 'express'; import { IdNotController } from '../controllers/idnot.controller'; import { asyncHandler } from '../middleware/error-handler'; import { ValidationError, BusinessRuleError } from '../types/errors'; /** * Route handlers that extract and validate HTTP request data * before calling pure controller methods */ export class IdNotHandlers { /** * GET /user/rattachements * Extract idNot from query params and call controller */ static getUserRattachements = asyncHandler(async (req: Request, res: Response): Promise<void> => { const requestId = req.headers['x-request-id'] as string; // Extract and validate parameters const { idNot } = req.query; if (!idNot || typeof idNot !== 'string') { throw new ValidationError('idNot parameter is required', [ { field: 'idNot', value: idNot, constraints: ['Must be a valid string'] } ], requestId); } // Call pure controller method with extracted parameters const result = await IdNotController.getUserRattachements(idNot); res.json(result); }); /** * GET /office/rattachements * Extract idNot from query params and call controller */ static getOfficeRattachements = asyncHandler(async (req: Request, res: Response): Promise<void> => { const requestId = req.headers['x-request-id'] as string; // Extract and validate parameters const { idNot } = req.query; if (!idNot || typeof idNot !== 'string') { throw new ValidationError('idNot parameter is required', [ { field: 'idNot', value: idNot, constraints: ['Must be a valid string'] } ], requestId); } // Call pure controller method const result = await IdNotController.getOfficeRattachements(idNot); res.json(result); }); /** * POST /auth/:code * Extract code from URL params and call controller */ static authenticate = asyncHandler(async (req: Request, res: Response): Promise<void> => { const requestId = req.headers['x-request-id'] as string; // Extract and validate parameters (support body or URL param) const codeParam = req.params?.code; const codeBody = (req.body && typeof req.body.code === 'string') ? req.body.code : undefined; const code = (codeBody && codeBody.length > 0) ? codeBody : codeParam; if (!code || typeof code !== 'string' || code.length < 10) { throw new ValidationError('Invalid authentication code', [ { field: 'code', value: code, constraints: ['Must be a valid authorization code'] } ], requestId); } // Call pure controller method const result = await IdNotController.authenticate(code); res.json(result); }); /** * GET /user (protected) * Extract user info from middleware and call controller */ static getCurrentUser = asyncHandler(async (req: Request, res: Response): Promise<void> => { const requestId = req.headers['x-request-id'] as string; // Extract user info (set by authenticateIdNot middleware) if (!req.idNotUser) { throw new BusinessRuleError('User authentication required', undefined, requestId); } const { idNot, authToken } = req.idNotUser; // Call pure controller method const result = await IdNotController.getCurrentUser(idNot, authToken); res.json(result); }); /** * POST /logout (protected) * Extract user info and call controller */ static logout = asyncHandler(async (req: Request, res: Response): Promise<void> => { const requestId = req.headers['x-request-id'] as string; if (!req.idNotUser) { throw new BusinessRuleError('User authentication required', undefined, requestId); } const { authToken } = req.idNotUser; // Call pure controller method const result = await IdNotController.logout(authToken); res.json(result); }); /** * GET /validate (protected) * Extract user info and call controller */ static validateToken = asyncHandler(async (req: Request, res: Response): Promise<void> => { const requestId = req.headers['x-request-id'] as string; if (!req.idNotUser) { throw new BusinessRuleError('User authentication required', undefined, requestId); } const { idNot } = req.idNotUser; // Call pure controller method const result = await IdNotController.validateToken(idNot); res.json(result); }); } |