-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (86 loc) · 4.06 KB
/
index.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
module.exports = function (config) {
return function(req, res, next) {
req.session.items = {}
// req.session.items[instance] = [
// { id: 3, name: 'mouse', preview: '', price: 8900, quantity: 1, attributes: [] }
// ],
req.cart = {
add: ( options, instance = "default" ) => {
const { id, name, price, quantity, attributes = [] } = options;
if( !id || !name || !price || !quantity ){
return { msg: 'Unable to Add Item, All Params are required!' };
}else{
if(req.session.items[instance]){
if( req.session.items[instance].some(item=> item.id == id ) ){
req.flash('error_msg', "product already in cart" );
}else{
req.session.items[instance] = req.session.items[instance].concat(options);
}
}else{
req.session.items[instance] = []
// adding to cart for the first time
req.session.items[instance] = req.session.items[instance].concat(options);
}
return { id, name, price, quantity, attributes }
}
},
update: ( id, options = {}, instance = 'default' ) => {
if( id ){
delete options.id; // delete id if given
let updatedItem = { ...req.cart.content(instance).filter(item => item.id == id )[0], ...options }
req.session.items[instance] = req.cart.remove(id).concat(updatedItem);
}
},
get: (id, instance = 'default' ) => {
if( id ){
return req.session.items[instance] = req.cart.content(instance).filter( item => item.id === parseInt(id) );
}else{
return { msg: 'Unable to remove Product, id not given' };
}
},
remove: (id, instance = 'default' ) => {
if( id ){
return req.session.items[instance] = req.cart.content(instance).filter( item => item.id != id );
}else{
return { msg: 'Unable to remove Product, id not given' };
}
},
checkout: (instance = 'default') => {
req.session.items[instance] = [];
},
content: (instance = 'default') => {
if( req.session.items[instance] ) return req.session.items[instance];
else return [];
},
subTotal: (instance = 'default') => {
if(req.cart.content(instance).length > 0){
return req.cart.content(instance).map(item => parseInt(item.price) * parseInt(item.quantity) )
.reduce((total, num) => total += parseInt(num) );
}else{
return 0
}
},
tax: (instance = 'default') => {
if(config.tax){
return req.cart.subTotal(instance) * parseInt(config.tax) / 100;
}
return 0;
},
total: (instance = 'default') => {
return req.cart.subTotal(instance) + req.cart.tax(instance);
},
totalQuantity: (instance = 'default') => {
if(req.cart.content(instance).length > 0){
return req.cart.content(instance).map(item => parseInt(item.quantity) )
.reduce((total, num) => total += parseInt(num) );
}else{
return 0
}
},
count: (instance = 'default') => {
return req.cart.content(instance).length
}
}
next();
}
}