expressCart/routes/order.js

167 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-02-05 23:20:44 +10:00
const express = require('express');
const common = require('../lib/common');
2019-06-15 14:46:08 +10:00
const { restrict, checkAccess } = require('../lib/auth');
2019-06-15 15:56:51 +10:00
const { indexOrders } = require('../lib/indexing');
2018-02-05 23:20:44 +10:00
const router = express.Router();
// Show orders
2019-06-15 14:46:08 +10:00
router.get('/admin/orders', restrict, (req, res, next) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
// Top 10 products
2019-07-12 17:50:07 +10:00
db.orders.find({}).sort({ orderDate: -1 }).limit(10).toArray((err, orders) => {
2018-02-05 23:20:44 +10:00
if(err){
console.info(err.stack);
}
// If API request, return json
if(req.apiAuthenticated){
return res.status(200).json({
orders
});
}
return res.render('orders', {
2018-02-05 23:20:44 +10:00
title: 'Cart',
orders: orders,
admin: true,
2018-02-23 03:41:24 +10:00
config: req.app.config,
2018-02-05 23:20:44 +10:00
session: req.session,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
});
});
});
// Admin section
2019-06-15 14:46:08 +10:00
router.get('/admin/orders/bystatus/:orderstatus', restrict, (req, res, next) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
if(typeof req.params.orderstatus === 'undefined'){
res.redirect('/admin/orders');
return;
}
// case insensitive search
2019-07-12 17:50:07 +10:00
const regex = new RegExp(['^', req.params.orderstatus, '$'].join(''), 'i');
db.orders.find({ orderStatus: regex }).sort({ orderDate: -1 }).limit(10).toArray((err, orders) => {
2018-02-05 23:20:44 +10:00
if(err){
console.info(err.stack);
}
// If API request, return json
if(req.apiAuthenticated){
return res.status(200).json({
orders
});
}
return res.render('orders', {
2018-02-05 23:20:44 +10:00
title: 'Cart',
orders: orders,
admin: true,
filteredOrders: true,
filteredStatus: req.params.orderstatus,
2018-02-23 03:41:24 +10:00
config: req.app.config,
2018-02-05 23:20:44 +10:00
session: req.session,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
});
});
});
// render the editor
2019-06-15 14:46:08 +10:00
router.get('/admin/order/view/:id', restrict, (req, res) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
2019-06-15 14:46:08 +10:00
db.orders.findOne({ _id: common.getId(req.params.id) }, (err, result) => {
2018-02-05 23:20:44 +10:00
if(err){
console.info(err.stack);
}
res.render('order', {
title: 'View order',
result: result,
2018-02-23 03:41:24 +10:00
config: req.app.config,
2018-02-05 23:20:44 +10:00
session: req.session,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
editor: true,
admin: true,
helpers: req.handlebars.helpers
});
});
});
// Admin section
2019-06-15 14:46:08 +10:00
router.get('/admin/orders/filter/:search', restrict, (req, res, next) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
2019-07-12 17:50:07 +10:00
const searchTerm = req.params.search;
const ordersIndex = req.app.ordersIndex;
2018-02-05 23:20:44 +10:00
2019-07-12 17:50:07 +10:00
const lunrIdArray = [];
2018-02-05 23:20:44 +10:00
ordersIndex.search(searchTerm).forEach((id) => {
lunrIdArray.push(common.getId(id.ref));
});
// we search on the lunr indexes
2019-06-15 14:46:08 +10:00
db.orders.find({ _id: { $in: lunrIdArray } }).toArray((err, orders) => {
2018-02-05 23:20:44 +10:00
if(err){
console.info(err.stack);
}
// If API request, return json
if(req.apiAuthenticated){
return res.status(200).json({
orders
});
}
return res.render('orders', {
2018-02-05 23:20:44 +10:00
title: 'Order results',
orders: orders,
admin: true,
2018-02-23 03:41:24 +10:00
config: req.app.config,
2018-02-05 23:20:44 +10:00
session: req.session,
searchTerm: searchTerm,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
});
});
});
// order product
2019-06-15 14:46:08 +10:00
router.get('/admin/order/delete/:id', restrict, (req, res) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
// remove the order
2019-06-15 14:46:08 +10:00
db.orders.remove({ _id: common.getId(req.params.id) }, {}, (err, numRemoved) => {
2018-02-05 23:20:44 +10:00
if(err){
console.info(err.stack);
}
// remove the index
2019-06-15 15:56:51 +10:00
indexOrders(req.app)
2018-02-05 23:20:44 +10:00
.then(() => {
// redirect home
req.session.message = 'Order successfully deleted';
req.session.messageType = 'success';
res.redirect('/admin/orders');
});
});
});
// update order status
2019-06-15 14:46:08 +10:00
router.post('/admin/order/statusupdate', restrict, checkAccess, (req, res) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
2019-06-15 14:46:08 +10:00
db.orders.update({ _id: common.getId(req.body.order_id) }, { $set: { orderStatus: req.body.status } }, { multi: false }, (err, numReplaced) => {
2018-02-05 23:20:44 +10:00
if(err){
console.info(err.stack);
return res.status(400).json({ message: 'Failed to update the order status' });
2018-02-05 23:20:44 +10:00
}
return res.status(200).json({ message: 'Status successfully updated' });
2018-02-05 23:20:44 +10:00
});
});
module.exports = router;