expressCart/routes/order.js

284 lines
8.9 KiB
JavaScript
Raw Normal View History

2018-02-05 23:20:44 +10:00
const express = require('express');
const {
clearSessionValue,
emptyCart,
getCountryList,
getId,
sendEmail,
getEmailTemplate,
clearCustomer
} = 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-11-09 12:29:28 +10:00
router.get('/admin/orders', restrict, async (req, res, next) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
// Top 10 products
2019-11-09 12:29:28 +10:00
const orders = await db.orders.find({}).sort({ orderDate: -1 }).limit(10).toArray();
2019-11-09 12:29:28 +10:00
// If API request, return json
if(req.apiAuthenticated){
res.status(200).json({
orders
2018-02-05 23:20:44 +10:00
});
2019-11-09 12:29:28 +10:00
return;
}
res.render('orders', {
title: 'Cart',
orders: orders,
admin: true,
config: req.app.config,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
2019-11-09 12:29:28 +10:00
helpers: req.handlebars.helpers
2018-02-05 23:20:44 +10:00
});
});
// Admin section
2019-11-09 12:29:28 +10:00
router.get('/admin/orders/bystatus/:orderstatus', restrict, async (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');
2019-11-09 12:29:28 +10:00
const orders = await db.orders.find({ orderStatus: regex }).sort({ orderDate: -1 }).limit(10).toArray();
2019-11-09 12:29:28 +10:00
// If API request, return json
if(req.apiAuthenticated){
res.status(200).json({
orders
2018-02-05 23:20:44 +10:00
});
2019-11-09 12:29:28 +10:00
return;
}
res.render('orders', {
title: 'Cart',
orders: orders,
admin: true,
filteredOrders: true,
filteredStatus: req.params.orderstatus,
config: req.app.config,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
2019-11-09 12:29:28 +10:00
helpers: req.handlebars.helpers
2018-02-05 23:20:44 +10:00
});
});
// render the editor
2019-11-09 12:29:28 +10:00
router.get('/admin/order/view/:id', restrict, async (req, res) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
const order = await db.orders.findOne({ _id: getId(req.params.id) });
2019-11-09 12:29:28 +10:00
res.render('order', {
title: 'View order',
result: order,
config: req.app.config,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
2019-11-09 12:29:28 +10:00
editor: true,
admin: true,
helpers: req.handlebars.helpers
2018-02-05 23:20:44 +10:00
});
});
// render the editor
router.get('/admin/order/create', restrict, async (req, res) => {
res.render('order-create', {
title: 'Create order',
config: req.app.config,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
countryList: getCountryList(),
editor: true,
admin: true,
helpers: req.handlebars.helpers
});
});
router.post('/admin/order/create', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
// Check if cart is empty
if(!req.session.cart){
res.status(400).json({
message: 'The cart is empty. You will need to add items to the cart first.'
});
}
const orderDoc = {
orderPaymentId: getId(),
orderPaymentGateway: 'Instore',
orderPaymentMessage: 'Your payment was successfully completed',
orderTotal: req.session.totalCartAmount,
orderItemCount: req.session.totalCartItems,
orderProductCount: req.session.totalCartProducts,
orderEmail: req.body.email || req.session.customerEmail,
orderFirstname: req.body.firstName || req.session.customerFirstname,
orderLastname: req.body.lastName || req.session.customerLastname,
orderAddr1: req.body.address1 || req.session.customerAddress1,
orderAddr2: req.body.address2 || req.session.customerAddress2,
orderCountry: req.body.country || req.session.customerCountry,
orderState: req.body.state || req.session.customerState,
orderPostcode: req.body.postcode || req.session.customerPostcode,
orderPhoneNumber: req.body.phone || req.session.customerPhone,
orderComment: req.body.orderComment || req.session.orderComment,
orderStatus: req.body.orderStatus,
orderDate: new Date(),
orderProducts: req.session.cart,
orderType: 'Single'
};
// insert order into DB
try{
const newDoc = await db.orders.insertOne(orderDoc);
// get the new ID
const orderId = newDoc.insertedId;
// add to lunr index
indexOrders(req.app)
.then(() => {
// set the results
req.session.messageType = 'success';
req.session.message = 'Your order was successfully placed. Payment for your order will be completed instore.';
req.session.paymentEmailAddr = newDoc.ops[0].orderEmail;
req.session.paymentApproved = true;
req.session.paymentDetails = `<p><strong>Order ID: </strong>${orderId}</p>
<p><strong>Transaction ID: </strong>${orderDoc.orderPaymentId}</p>`;
// set payment results for email
const paymentResults = {
message: req.session.message,
messageType: req.session.messageType,
paymentEmailAddr: req.session.paymentEmailAddr,
paymentApproved: true,
paymentDetails: req.session.paymentDetails
};
// clear the cart
if(req.session.cart){
emptyCart(req, res, 'function');
}
// Clear customer session
clearCustomer(req);
// send the email with the response
// TODO: Should fix this to properly handle result
sendEmail(req.session.paymentEmailAddr, `Your order with ${config.cartTitle}`, getEmailTemplate(paymentResults));
// redirect to outcome
res.status(200).json({
message: 'Order created successfully',
orderId
});
});
}catch(ex){
res.status(400).json({ err: 'Your order declined. Please try again' });
}
});
2018-02-05 23:20:44 +10:00
// Admin section
2019-11-09 12:29:28 +10:00
router.get('/admin/orders/filter/:search', restrict, async (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(getId(id.ref));
2018-02-05 23:20:44 +10:00
});
// we search on the lunr indexes
2019-11-09 12:29:28 +10:00
const orders = await db.orders.find({ _id: { $in: lunrIdArray } }).toArray();
2019-11-09 12:29:28 +10:00
// If API request, return json
if(req.apiAuthenticated){
res.status(200).json({
orders
2018-02-05 23:20:44 +10:00
});
2019-11-09 12:29:28 +10:00
return;
}
res.render('orders', {
title: 'Order results',
orders: orders,
admin: true,
config: req.app.config,
session: req.session,
searchTerm: searchTerm,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
2019-11-09 12:29:28 +10:00
helpers: req.handlebars.helpers
2018-02-05 23:20:44 +10:00
});
});
// order product
2019-11-09 12:29:28 +10:00
router.get('/admin/order/delete/:id', restrict, async(req, res) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
// remove the order
2019-11-09 12:29:28 +10:00
try{
await db.orders.deleteOne({ _id: getId(req.params.id) });
2019-11-09 12:29:28 +10:00
2018-02-05 23:20:44 +10:00
// remove the index
2019-06-15 15:56:51 +10:00
indexOrders(req.app)
2018-02-05 23:20:44 +10:00
.then(() => {
2019-11-09 12:29:28 +10:00
if(req.apiAuthenticated){
res.status(200).json({
message: 'Order successfully deleted'
});
return;
}
2018-02-05 23:20:44 +10:00
// redirect home
req.session.message = 'Order successfully deleted';
req.session.messageType = 'success';
res.redirect('/admin/orders');
});
2019-11-09 12:29:28 +10:00
}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');
}
2018-02-05 23:20:44 +10:00
});
// update order status
2019-11-09 12:29:28 +10:00
router.post('/admin/order/statusupdate', restrict, checkAccess, async (req, res) => {
2018-02-05 23:20:44 +10:00
const db = req.app.db;
2019-11-09 12:29:28 +10:00
try{
await db.orders.updateOne({
_id: getId(req.body.order_id) },
2019-11-09 12:29:28 +10:00
{ $set: { orderStatus: req.body.status }
}, { multi: false });
return res.status(200).json({ message: 'Status successfully updated' });
2019-11-09 12:29:28 +10:00
}catch(ex){
console.info('Error updating status', ex);
return res.status(400).json({ message: 'Failed to update the order status' });
}
2018-02-05 23:20:44 +10:00
});
module.exports = router;