forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
116 lines (95 loc) · 3.24 KB
/
cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const {
getConfig
} = require('./config');
const updateTotalCart = async (req, res) => {
const config = getConfig();
const db = req.app.db;
req.session.totalCartAmount = 0;
req.session.totalCartItems = 0;
req.session.totalCartProducts = 0;
// If cart is empty return zero values
if(!req.session.cart){
return;
}
Object.keys(req.session.cart).forEach((item) => {
req.session.totalCartAmount = req.session.totalCartAmount + req.session.cart[item].totalItemPrice;
req.session.totalCartProducts = req.session.totalCartProducts + req.session.cart[item].quantity;
});
// Update the total items in cart for the badge
req.session.totalCartItems = Object.keys(req.session.cart).length;
// Update the total amount not including shipping/discounts
req.session.totalCartNetAmount = req.session.totalCartAmount;
// Calculate shipping using the loaded module
config.modules.loaded.shipping.calculateShipping(
req.session.totalCartNetAmount,
config,
req
);
// If discount module enabled
if(config.modules.loaded.discount){
// Recalculate discounts
const discount = await db.discounts.findOne({ code: req.session.discountCode });
if(discount){
config.modules.loaded.discount.calculateDiscount(
discount,
req
);
}else{
// If discount code is not found, remove it
delete req.session.discountCode;
req.session.totalCartDiscount = 0;
}
}
// Calculate our total amount removing discount and adding shipping
req.session.totalCartAmount = (req.session.totalCartNetAmount - req.session.totalCartDiscount) + req.session.totalCartShipping;
};
const updateSubscriptionCheck = (req, res) => {
// If cart is empty
if(!req.session.cart || req.session.cart.length === 0){
req.session.cartSubscription = null;
return;
}
Object.keys(req.session.cart).forEach((item) => {
if(item.productSubscription){
req.session.cartSubscription = item.productSubscription;
}else{
req.session.cartSubscription = null;
}
});
};
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;
delete req.session.discountCode;
// Remove cart from DB
await db.cart.deleteOne({ sessionId: req.session.id });
// update total cart
await 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('/');
};
module.exports = {
updateTotalCart,
updateSubscriptionCheck,
emptyCart
};