-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: added active contacts middleware
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/tom-server/src/active-contacts-api/middlewares/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Response, NextFunction } from 'express' | ||
import type { AuthRequest } from '../../types' | ||
import type { IActiveContactsApiValidationMiddleware } from '../types' | ||
|
||
export default class ActiveContactsApiValidationMiddleWare | ||
implements IActiveContactsApiValidationMiddleware | ||
{ | ||
/** | ||
* Check the creation requirements of the active contacts API | ||
* | ||
* @param {AuthRequest} req - the request object | ||
* @param {Response} res - the response object | ||
* @param {NextFunction} next - the next function | ||
* @returns {void} | ||
* @example | ||
* router.post('/', checkCreationRequirements, create) | ||
*/ | ||
checkCreationRequirements = ( | ||
req: AuthRequest, | ||
res: Response, | ||
next: NextFunction | ||
): void => { | ||
try { | ||
const { contacts } = req.body | ||
|
||
if (contacts === undefined) { | ||
throw new Error('Missing required fields', { | ||
cause: 'userId or contacts is missing' | ||
}) | ||
} | ||
|
||
next() | ||
} catch (error) { | ||
res.status(400).json({ message: 'Bad Request' }) | ||
} | ||
} | ||
} |