From ca7043be2ae36a808510aaa8841c98be5660b7da Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Sat, 15 Jun 2019 10:26:03 +0930 Subject: [PATCH] Starting to add ability to make admin API requests --- .gitignore | 2 +- app.js | 2 +- routes/order.js | 30 +++++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e392609..1231622 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules/ data/ -/config/settings-local.json +/config/*-local.json .vscode **.DS_Store diff --git a/app.js b/app.js index a324728..5002b2d 100644 --- a/app.js +++ b/app.js @@ -252,11 +252,11 @@ app.use((req, res, next) => { // update config when modified app.use((req, res, next) => { - next(); if(res.configDirty){ config = common.getConfig(); app.config = config; } + next(); }); // Ran on all routes diff --git a/routes/order.js b/routes/order.js index d26dd08..970aed4 100644 --- a/routes/order.js +++ b/routes/order.js @@ -11,7 +11,15 @@ router.get('/admin/orders', common.restrict, (req, res, next) => { if(err){ console.info(err.stack); } - res.render('orders', { + + // If API request, return json + if(req.apiAuthenticated){ + return res.status(200).json({ + orders + }); + } + + return res.render('orders', { title: 'Cart', orders: orders, admin: true, @@ -39,7 +47,15 @@ router.get('/admin/orders/bystatus/:orderstatus', common.restrict, (req, res, ne if(err){ console.info(err.stack); } - res.render('orders', { + + // If API request, return json + if(req.apiAuthenticated){ + return res.status(200).json({ + orders + }); + } + + return res.render('orders', { title: 'Cart', orders: orders, admin: true, @@ -91,7 +107,15 @@ router.get('/admin/orders/filter/:search', common.restrict, (req, res, next) => if(err){ console.info(err.stack); } - res.render('orders', { + + // If API request, return json + if(req.apiAuthenticated){ + return res.status(200).json({ + orders + }); + } + + return res.render('orders', { title: 'Order results', orders: orders, admin: true,