Skip to content

Commit

Permalink
Update 2024 (#39)
Browse files Browse the repository at this point in the history
* feat : add preparator user

* fix eslint compliance

* update: change order splitting method (now by needsPreparation)

* Update src/controllers/order/create.ts

Co-authored-by: Zalko <88582103+Zalk0@users.noreply.github.com>

---------

Co-authored-by: Zalko <88582103+Zalk0@users.noreply.github.com>
  • Loading branch information
tuturd and Zalk0 authored Sep 11, 2024
1 parent 57472ba commit b426c15
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 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
2 changes: 1 addition & 1 deletion src/controllers/order/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const create = async (req: BodyRequest<Body>, res: Response) => {
})
.reduce(
(acc, item) => {
if (item.item.category.key !== 'pizzas') {
if (item.item.category.needsPreparation) {
acc[0].push(item);
} else {
acc[1].push(item);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/order/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const dispatch = async (req: BodyRequest<BuckResponse>, res: Response<unknown, O

const separatedItems = items.reduce(
(acc, item) => {
acc[Number(item.category.key === 'pizzas')].push(item);
acc[Number(item.category.needsPreparation === false)].push(item);
return acc;
},
[[], []] as [Partial<Item>[], Partial<Item>[]],
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;
};
33 changes: 33 additions & 0 deletions src/middlewares/hasPermissionInList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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();
}

const hasPermission: boolean = permissions.some((perm) => perm === decoded.permissions)
if (hasPermission) 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 b426c15

Please sign in to comment.