Centralising functions & refactoring
parent
19268ac412
commit
fa477dbdf4
|
@ -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) => {
|
const updateSubscriptionCheck = (req, res) => {
|
||||||
// If cart is empty
|
// If cart is empty
|
||||||
if(!req.session.cart || req.session.cart.length === 0){
|
if(!req.session.cart || req.session.cart.length === 0){
|
||||||
|
@ -595,6 +649,8 @@ module.exports = {
|
||||||
addSitemapProducts,
|
addSitemapProducts,
|
||||||
clearSessionValue,
|
clearSessionValue,
|
||||||
updateTotalCart,
|
updateTotalCart,
|
||||||
|
emptyCart,
|
||||||
|
clearCustomer,
|
||||||
updateSubscriptionCheck,
|
updateSubscriptionCheck,
|
||||||
checkDirectorySync,
|
checkDirectorySync,
|
||||||
getThemes,
|
getThemes,
|
||||||
|
|
|
@ -11,6 +11,7 @@ const {
|
||||||
getPaymentConfig,
|
getPaymentConfig,
|
||||||
getImages,
|
getImages,
|
||||||
updateTotalCart,
|
updateTotalCart,
|
||||||
|
emptyCart,
|
||||||
updateSubscriptionCheck,
|
updateSubscriptionCheck,
|
||||||
getData,
|
getData,
|
||||||
addSitemapProducts,
|
addSitemapProducts,
|
||||||
|
@ -344,40 +345,6 @@ router.post('/product/emptycart', async (req, res, next) => {
|
||||||
emptyCart(req, res, 'json');
|
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
|
// Add item to cart
|
||||||
router.post('/product/addtocart', async (req, res, next) => {
|
router.post('/product/addtocart', async (req, res, next) => {
|
||||||
const db = req.app.db;
|
const db = req.app.db;
|
||||||
|
@ -455,7 +422,6 @@ router.post('/product/addtocart', async (req, res, next) => {
|
||||||
|
|
||||||
const productPrice = parseFloat(product.productPrice).toFixed(2);
|
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 = {};
|
let options = {};
|
||||||
if(req.body.productOptions){
|
if(req.body.productOptions){
|
||||||
try{
|
try{
|
||||||
|
|
|
@ -124,9 +124,7 @@ router.post('/checkout_action', async (req, res, next) => {
|
||||||
|
|
||||||
// clear the cart
|
// clear the cart
|
||||||
if(req.session.cart){
|
if(req.session.cart){
|
||||||
req.session.cart = null;
|
common.emptyCart(req, res, 'function');
|
||||||
req.session.orderId = null;
|
|
||||||
req.session.totalCartAmount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the email with the response
|
// send the email with the response
|
||||||
|
|
|
@ -74,7 +74,8 @@ router.post('/checkout_action', (req, res, next) => {
|
||||||
orderComment: req.session.orderComment,
|
orderComment: req.session.orderComment,
|
||||||
orderStatus: orderStatus,
|
orderStatus: orderStatus,
|
||||||
orderDate: new Date(),
|
orderDate: new Date(),
|
||||||
orderProducts: req.session.cart
|
orderProducts: req.session.cart,
|
||||||
|
orderType: 'Single'
|
||||||
};
|
};
|
||||||
|
|
||||||
// insert order into DB
|
// insert order into DB
|
||||||
|
@ -108,9 +109,7 @@ router.post('/checkout_action', (req, res, next) => {
|
||||||
|
|
||||||
// clear the cart
|
// clear the cart
|
||||||
if(req.session.cart){
|
if(req.session.cart){
|
||||||
req.session.cart = null;
|
common.emptyCart(req, res, 'function');
|
||||||
req.session.orderId = null;
|
|
||||||
req.session.totalCartAmount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the email with the response
|
// send the email with the response
|
||||||
|
|
|
@ -28,7 +28,8 @@ router.post('/checkout_action', async (req, res, next) => {
|
||||||
orderComment: req.session.orderComment,
|
orderComment: req.session.orderComment,
|
||||||
orderStatus: instoreConfig.orderStatus,
|
orderStatus: instoreConfig.orderStatus,
|
||||||
orderDate: new Date(),
|
orderDate: new Date(),
|
||||||
orderProducts: req.session.cart
|
orderProducts: req.session.cart,
|
||||||
|
orderType: 'Single'
|
||||||
};
|
};
|
||||||
|
|
||||||
// insert order into DB
|
// insert order into DB
|
||||||
|
@ -60,9 +61,7 @@ router.post('/checkout_action', async (req, res, next) => {
|
||||||
|
|
||||||
// clear the cart
|
// clear the cart
|
||||||
if(req.session.cart){
|
if(req.session.cart){
|
||||||
req.session.cart = null;
|
common.emptyCart(req, res, 'function');
|
||||||
req.session.orderId = null;
|
|
||||||
req.session.totalCartAmount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the email with the response
|
// send the email with the response
|
||||||
|
|
|
@ -53,9 +53,7 @@ router.get('/checkout_return', (req, res, next) => {
|
||||||
|
|
||||||
// clear the cart
|
// clear the cart
|
||||||
if(req.session.cart){
|
if(req.session.cart){
|
||||||
req.session.cart = null;
|
common.emptyCart(req, res, 'function');
|
||||||
req.session.orderId = null;
|
|
||||||
req.session.totalCartAmount = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,7 +176,8 @@ router.post('/checkout_action', (req, res, next) => {
|
||||||
orderComment: req.session.orderComment,
|
orderComment: req.session.orderComment,
|
||||||
orderStatus: payment.state,
|
orderStatus: payment.state,
|
||||||
orderDate: new Date(),
|
orderDate: new Date(),
|
||||||
orderProducts: req.session.cart
|
orderProducts: req.session.cart,
|
||||||
|
orderType: 'Single'
|
||||||
};
|
};
|
||||||
|
|
||||||
if(req.session.orderId){
|
if(req.session.orderId){
|
||||||
|
|
|
@ -103,9 +103,7 @@ router.post('/checkout_action', (req, res, next) => {
|
||||||
|
|
||||||
// clear the cart
|
// clear the cart
|
||||||
if(req.session.cart){
|
if(req.session.cart){
|
||||||
req.session.cart = null;
|
common.emptyCart(req, res, 'function');
|
||||||
req.session.orderId = null;
|
|
||||||
req.session.totalCartAmount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the email with the response
|
// send the email with the response
|
||||||
|
@ -274,10 +272,7 @@ router.post('/checkout_action_subscription', async (req, res, next) => {
|
||||||
|
|
||||||
// clear the cart
|
// clear the cart
|
||||||
if(req.session.cart){
|
if(req.session.cart){
|
||||||
req.session.cartSubscription = null;
|
common.emptyCart(req, res, 'function');
|
||||||
req.session.cart = null;
|
|
||||||
req.session.orderId = null;
|
|
||||||
req.session.totalCartAmount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the email with the response
|
// send the email with the response
|
||||||
|
|
Loading…
Reference in New Issue