Refactoring order route
parent
f6ab6207eb
commit
87a722d03e
|
@ -5,23 +5,21 @@ const { indexOrders } = require('../lib/indexing');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// Show orders
|
// Show orders
|
||||||
router.get('/admin/orders', restrict, (req, res, next) => {
|
router.get('/admin/orders', restrict, async (req, res, next) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
|
|
||||||
// Top 10 products
|
// Top 10 products
|
||||||
db.orders.find({}).sort({ orderDate: -1 }).limit(10).toArray((err, orders) => {
|
const orders = await db.orders.find({}).sort({ orderDate: -1 }).limit(10).toArray();
|
||||||
if(err){
|
|
||||||
console.info(err.stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
return res.status(200).json({
|
res.status(200).json({
|
||||||
orders
|
orders
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.render('orders', {
|
res.render('orders', {
|
||||||
title: 'Cart',
|
title: 'Cart',
|
||||||
orders: orders,
|
orders: orders,
|
||||||
admin: true,
|
admin: true,
|
||||||
|
@ -32,10 +30,9 @@ router.get('/admin/orders', restrict, (req, res, next) => {
|
||||||
helpers: req.handlebars.helpers
|
helpers: req.handlebars.helpers
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Admin section
|
// Admin section
|
||||||
router.get('/admin/orders/bystatus/:orderstatus', restrict, (req, res, next) => {
|
router.get('/admin/orders/bystatus/:orderstatus', restrict, async (req, res, next) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
|
|
||||||
if(typeof req.params.orderstatus === 'undefined'){
|
if(typeof req.params.orderstatus === 'undefined'){
|
||||||
|
@ -45,19 +42,17 @@ router.get('/admin/orders/bystatus/:orderstatus', restrict, (req, res, next) =>
|
||||||
|
|
||||||
// case insensitive search
|
// case insensitive search
|
||||||
const regex = new RegExp(['^', req.params.orderstatus, '$'].join(''), 'i');
|
const regex = new RegExp(['^', req.params.orderstatus, '$'].join(''), 'i');
|
||||||
db.orders.find({ orderStatus: regex }).sort({ orderDate: -1 }).limit(10).toArray((err, orders) => {
|
const orders = await db.orders.find({ orderStatus: regex }).sort({ orderDate: -1 }).limit(10).toArray();
|
||||||
if(err){
|
|
||||||
console.info(err.stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
return res.status(200).json({
|
res.status(200).json({
|
||||||
orders
|
orders
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.render('orders', {
|
res.render('orders', {
|
||||||
title: 'Cart',
|
title: 'Cart',
|
||||||
orders: orders,
|
orders: orders,
|
||||||
admin: true,
|
admin: true,
|
||||||
|
@ -70,18 +65,15 @@ router.get('/admin/orders/bystatus/:orderstatus', restrict, (req, res, next) =>
|
||||||
helpers: req.handlebars.helpers
|
helpers: req.handlebars.helpers
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// render the editor
|
// render the editor
|
||||||
router.get('/admin/order/view/:id', restrict, (req, res) => {
|
router.get('/admin/order/view/:id', restrict, async (req, res) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
db.orders.findOne({ _id: common.getId(req.params.id) }, (err, result) => {
|
const order = await db.orders.findOne({ _id: common.getId(req.params.id) });
|
||||||
if(err){
|
|
||||||
console.info(err.stack);
|
|
||||||
}
|
|
||||||
res.render('order', {
|
res.render('order', {
|
||||||
title: 'View order',
|
title: 'View order',
|
||||||
result: result,
|
result: order,
|
||||||
config: req.app.config,
|
config: req.app.config,
|
||||||
session: req.session,
|
session: req.session,
|
||||||
message: common.clearSessionValue(req.session, 'message'),
|
message: common.clearSessionValue(req.session, 'message'),
|
||||||
|
@ -91,10 +83,9 @@ router.get('/admin/order/view/:id', restrict, (req, res) => {
|
||||||
helpers: req.handlebars.helpers
|
helpers: req.handlebars.helpers
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Admin section
|
// Admin section
|
||||||
router.get('/admin/orders/filter/:search', restrict, (req, res, next) => {
|
router.get('/admin/orders/filter/:search', restrict, async (req, res, next) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
const searchTerm = req.params.search;
|
const searchTerm = req.params.search;
|
||||||
const ordersIndex = req.app.ordersIndex;
|
const ordersIndex = req.app.ordersIndex;
|
||||||
|
@ -105,19 +96,17 @@ router.get('/admin/orders/filter/:search', restrict, (req, res, next) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// we search on the lunr indexes
|
// we search on the lunr indexes
|
||||||
db.orders.find({ _id: { $in: lunrIdArray } }).toArray((err, orders) => {
|
const orders = await db.orders.find({ _id: { $in: lunrIdArray } }).toArray();
|
||||||
if(err){
|
|
||||||
console.info(err.stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If API request, return json
|
// If API request, return json
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
return res.status(200).json({
|
res.status(200).json({
|
||||||
orders
|
orders
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.render('orders', {
|
res.render('orders', {
|
||||||
title: 'Order results',
|
title: 'Order results',
|
||||||
orders: orders,
|
orders: orders,
|
||||||
admin: true,
|
admin: true,
|
||||||
|
@ -129,38 +118,59 @@ router.get('/admin/orders/filter/:search', restrict, (req, res, next) => {
|
||||||
helpers: req.handlebars.helpers
|
helpers: req.handlebars.helpers
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// order product
|
// order product
|
||||||
router.get('/admin/order/delete/:id', restrict, (req, res) => {
|
router.get('/admin/order/delete/:id', restrict, async(req, res) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
|
|
||||||
// remove the order
|
// remove the order
|
||||||
db.orders.deleteOne({ _id: common.getId(req.params.id) }, {}, (err, numRemoved) => {
|
try{
|
||||||
if(err){
|
await db.orders.deleteOne({ _id: common.getId(req.params.id) });
|
||||||
console.info(err.stack);
|
|
||||||
}
|
|
||||||
// remove the index
|
// remove the index
|
||||||
indexOrders(req.app)
|
indexOrders(req.app)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
if(req.apiAuthenticated){
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Order successfully deleted'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// redirect home
|
// redirect home
|
||||||
req.session.message = 'Order successfully deleted';
|
req.session.message = 'Order successfully deleted';
|
||||||
req.session.messageType = 'success';
|
req.session.messageType = 'success';
|
||||||
res.redirect('/admin/orders');
|
res.redirect('/admin/orders');
|
||||||
});
|
});
|
||||||
|
}catch(ex){
|
||||||
|
console.log('Cannot delete order', ex);
|
||||||
|
if(req.apiAuthenticated){
|
||||||
|
res.status(200).json({
|
||||||
|
message: 'Error deleting order'
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// redirect home
|
||||||
|
req.session.message = 'Error deleting order';
|
||||||
|
req.session.messageType = 'danger';
|
||||||
|
res.redirect('/admin/orders');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// update order status
|
// update order status
|
||||||
router.post('/admin/order/statusupdate', restrict, checkAccess, (req, res) => {
|
router.post('/admin/order/statusupdate', restrict, checkAccess, async (req, res) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
db.orders.updateOne({ _id: common.getId(req.body.order_id) }, { $set: { orderStatus: req.body.status } }, { multi: false }, (err, numReplaced) => {
|
try{
|
||||||
if(err){
|
await db.orders.updateOne({
|
||||||
console.info(err.stack);
|
_id: common.getId(req.body.order_id) },
|
||||||
|
{ $set: { orderStatus: req.body.status }
|
||||||
|
}, { multi: false });
|
||||||
|
return res.status(200).json({ message: 'Status successfully updated' });
|
||||||
|
}catch(ex){
|
||||||
|
console.info('Error updating status', ex);
|
||||||
return res.status(400).json({ message: 'Failed to update the order status' });
|
return res.status(400).json({ message: 'Failed to update the order status' });
|
||||||
}
|
}
|
||||||
return res.status(200).json({ message: 'Status successfully updated' });
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in New Issue