Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 2024 #39

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading