Centralising functions & refactoring

master
Mark Moffat 2020-01-02 15:53:39 +10:30
parent 19268ac412
commit fa477dbdf4
7 changed files with 69 additions and 57 deletions

View File

@ -140,6 +140,60 @@ const updateTotalCart = (req, res) => {
}
};
const emptyCart = async (req, res, type, customMessage) => {
const db = req.app.db;
// Remove from session
delete req.session.cart;
delete req.session.shippingAmount;
delete req.session.orderId;
delete req.session.cartSubscription;
// Remove cart from DB
await db.cart.deleteOne({ sessionId: req.session.id });
// update total cart
updateTotalCart(req, res);
// Update checking cart for subscription
updateSubscriptionCheck(req, res);
// Set returned message
let message = 'Cart successfully emptied';
if(customMessage){
message = customMessage;
}
if(type === 'function'){
return;
}
// If POST, return JSON else redirect nome
if(type === 'json'){
res.status(200).json({ message: message, totalCartItems: 0 });
return;
}
req.session.message = message;
req.session.messageType = 'success';
res.redirect('/');
};
const clearCustomer = (req) => {
// Clear our session
req.session.customerPresent = null;
req.session.customerEmail = null;
req.session.customerFirstname = null;
req.session.customerLastname = null;
req.session.customerAddress1 = null;
req.session.customerAddress2 = null;
req.session.customerCountry = null;
req.session.customerState = null;
req.session.customerPostcode = null;
req.session.customerPhone = null;
req.session.orderComment = null;
};
const updateSubscriptionCheck = (req, res) => {
// If cart is empty
if(!req.session.cart || req.session.cart.length === 0){
@ -595,6 +649,8 @@ module.exports = {
addSitemapProducts,
clearSessionValue,
updateTotalCart,
emptyCart,
clearCustomer,
updateSubscriptionCheck,
checkDirectorySync,
getThemes,

View File

@ -11,6 +11,7 @@ const {
getPaymentConfig,
getImages,
updateTotalCart,
emptyCart,
updateSubscriptionCheck,
getData,
addSitemapProducts,
@ -344,40 +345,6 @@ router.post('/product/emptycart', async (req, res, next) => {
emptyCart(req, res, 'json');
});
const emptyCart = async (req, res, type, customMessage) => {
const db = req.app.db;
// Remove from session
delete req.session.cart;
delete req.session.shippingAmount;
delete req.session.orderId;
// Remove cart from DB
await db.cart.deleteOne({ sessionId: req.session.id });
// update total cart
updateTotalCart(req, res);
// Update checking cart for subscription
updateSubscriptionCheck(req, res);
// Set returned message
let message = 'Cart successfully emptied';
if(customMessage){
message = customMessage;
}
// If POST, return JSON else redirect nome
if(type === 'json'){
res.status(200).json({ message: message, totalCartItems: 0 });
return;
}
req.session.message = message;
req.session.messageType = 'success';
res.redirect('/');
};
// Add item to cart
router.post('/product/addtocart', async (req, res, next) => {
const db = req.app.db;
@ -455,7 +422,6 @@ router.post('/product/addtocart', async (req, res, next) => {
const productPrice = parseFloat(product.productPrice).toFixed(2);
// Doc used to test if existing in the cart with the options. If not found, we add new.
let options = {};
if(req.body.productOptions){
try{

View File

@ -124,9 +124,7 @@ router.post('/checkout_action', async (req, res, next) => {
// clear the cart
if(req.session.cart){
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
common.emptyCart(req, res, 'function');
}
// send the email with the response

View File

@ -74,7 +74,8 @@ router.post('/checkout_action', (req, res, next) => {
orderComment: req.session.orderComment,
orderStatus: orderStatus,
orderDate: new Date(),
orderProducts: req.session.cart
orderProducts: req.session.cart,
orderType: 'Single'
};
// insert order into DB
@ -108,9 +109,7 @@ router.post('/checkout_action', (req, res, next) => {
// clear the cart
if(req.session.cart){
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
common.emptyCart(req, res, 'function');
}
// send the email with the response

View File

@ -28,7 +28,8 @@ router.post('/checkout_action', async (req, res, next) => {
orderComment: req.session.orderComment,
orderStatus: instoreConfig.orderStatus,
orderDate: new Date(),
orderProducts: req.session.cart
orderProducts: req.session.cart,
orderType: 'Single'
};
// insert order into DB
@ -60,9 +61,7 @@ router.post('/checkout_action', async (req, res, next) => {
// clear the cart
if(req.session.cart){
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
common.emptyCart(req, res, 'function');
}
// send the email with the response

View File

@ -53,9 +53,7 @@ router.get('/checkout_return', (req, res, next) => {
// clear the cart
if(req.session.cart){
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
common.emptyCart(req, res, 'function');
}
}
@ -178,7 +176,8 @@ router.post('/checkout_action', (req, res, next) => {
orderComment: req.session.orderComment,
orderStatus: payment.state,
orderDate: new Date(),
orderProducts: req.session.cart
orderProducts: req.session.cart,
orderType: 'Single'
};
if(req.session.orderId){

View File

@ -103,9 +103,7 @@ router.post('/checkout_action', (req, res, next) => {
// clear the cart
if(req.session.cart){
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
common.emptyCart(req, res, 'function');
}
// send the email with the response
@ -274,10 +272,7 @@ router.post('/checkout_action_subscription', async (req, res, next) => {
// clear the cart
if(req.session.cart){
req.session.cartSubscription = null;
req.session.cart = null;
req.session.orderId = null;
req.session.totalCartAmount = 0;
common.emptyCart(req, res, 'function');
}
// send the email with the response