Skip to content

Commit

Permalink
feat : add preparator user
Browse files Browse the repository at this point in the history
  • Loading branch information
tuturd committed Mar 15, 2024
1 parent 57472ba commit 2d280af
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ APP_PIN_TV=123456
APP_PIN_SELLER=147258
APP_PIN_PIZZA=741852
APP_PIN_ADMIN=159753
APP_PIN_PREPARATOR=000000

# Envoie un message sur Slack si activé à chaque commande
SLACK_ENABLED=false
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/order/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import list from './list';
import dispatch from './dispatch';
import editStatus from './editStatus';
import hasPermission from '../../middlewares/hasPermission';
import hasPermissionInList from '../../middlewares/hasPermissionInList';
import { OrderUpdate } from '../../types';
import isBuck from '../../middlewares/isBuck';
import isAuth from '../../middlewares/isAuth';
Expand All @@ -14,8 +15,8 @@ export default () => {
router.get('/', isAuth(), list);
router.post('/', isAuth(), hasPermission('sell'), create);
router.post('/dispatch', isBuck, dispatch);
router.patch('/:id/upgrade', isAuth(), hasPermission('pizza'), editStatus(OrderUpdate.UPGRADE));
router.patch('/:id/downgrade', isAuth(), hasPermission('pizza'), editStatus(OrderUpdate.DOWNGRADE));
router.patch('/:id/upgrade', isAuth(), hasPermissionInList(['pizza','prepare']), editStatus(OrderUpdate.UPGRADE));
router.patch('/:id/downgrade', isAuth(), hasPermissionInList(['pizza','prepare']), editStatus(OrderUpdate.DOWNGRADE));

return router;
};
35 changes: 35 additions & 0 deletions src/middlewares/hasPermissionInList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Response, NextFunction, Request } from 'express';
import jwt from 'jsonwebtoken';
import getToken from '../utils/getToken';
import { Token, Permission } from '../types';
import { unauthorized, unauthenticated } from '../utils/responses';
import errorHandler from '../utils/errorHandler';

export default (permissions: Array<string>) => async (req: Request, res: Response, next: NextFunction) => {
try {
const token = getToken(req);
if (token) {
const decoded = jwt.verify(token, process.env.APP_TOKEN_SECRET) as Token;

req.user = decoded;

if (!permissions) {
return next();
}

if (decoded.permissions === Permission.ADMIN) {
return next();
}
for (const permission of permissions) {

Check failure on line 23 in src/middlewares/hasPermissionInList.ts

View workflow job for this annotation

GitHub Actions / lint (18)

iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations

Check failure on line 23 in src/middlewares/hasPermissionInList.ts

View workflow job for this annotation

GitHub Actions / lint (18)

iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations
if (decoded.permissions === permission) {
return next();
}
}
return unauthorized(res);
}

return unauthenticated(res);
} catch (err) {
return errorHandler(res, err);
}
};
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum Permission {
ADMIN = 'admin',
SELLER = 'seller',
PIZZA = 'pizza',
PREPARATOR = 'preparator'
}

export interface Token {
Expand Down

0 comments on commit 2d280af

Please sign in to comment.