diff --git a/lib/common.js b/lib/common.js index 080f776..0adebca 100755 --- a/lib/common.js +++ b/lib/common.js @@ -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, diff --git a/routes/index.js b/routes/index.js index 5fa3465..594d4f8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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{ diff --git a/routes/payments/adyen.js b/routes/payments/adyen.js index b15c855..19de602 100644 --- a/routes/payments/adyen.js +++ b/routes/payments/adyen.js @@ -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 diff --git a/routes/payments/authorizenet.js b/routes/payments/authorizenet.js index 9ef9902..b96e06a 100644 --- a/routes/payments/authorizenet.js +++ b/routes/payments/authorizenet.js @@ -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 diff --git a/routes/payments/instore.js b/routes/payments/instore.js index 6f95bdf..5719a85 100644 --- a/routes/payments/instore.js +++ b/routes/payments/instore.js @@ -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 diff --git a/routes/payments/paypal.js b/routes/payments/paypal.js index ed9ffe1..e4fc656 100644 --- a/routes/payments/paypal.js +++ b/routes/payments/paypal.js @@ -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){ diff --git a/routes/payments/stripe.js b/routes/payments/stripe.js index b7eea31..4e12e91 100644 --- a/routes/payments/stripe.js +++ b/routes/payments/stripe.js @@ -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