-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart-Controller.js
270 lines (238 loc) · 9.02 KB
/
cart-Controller.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const { reject } = require("bcrypt/promises")
const { response } = require("express")
const async = require("hbs/lib/async")
const cartModel = require("../model/cart-model")
const orderModel = require("../model/order-model")
const productController = require("../controllers/product-Controller")
Helpers = {
addToCart: (productid, userid) => {
console.log(productid);
console.log(userid);
const response = {
duplicate: false,
}
return new Promise(async (res, rej) => {
try {
let usercart = await cartModel.findOne({ userId: userid })
console.log(usercart, 'usercart');
// console.log(usercart);
if (usercart) {
let cartproduct = await cartModel.findOne({
userId: userid,
"cartdata.productId": productid,
})
if (cartproduct) {
console.log(cartproduct, 'cartproduct');
// console.log(cartproduct);
cartModel
.updateOne({ userId: userid, "cartdata.productId": productid },
{ $inc: { "cartdata.$.quantity": 1 } }
)
.then((response) => {
console.log(response, 'response');
response.duplicate = true
res(response)
})
} else {
let cartArray = { productId: productid, quantity: 1 }
cartModel.findOneAndUpdate({ userId: userid }, {
$push: { cartdata: cartArray },
}
)
.then((data) => {
res(response)
})
}
} else {
let body = {
userId: userid,
cartdata: [{ productId: productid, quantity: 1 }],
}
await cartModel.create(body)
}
} catch (error) {
reject(error)
}
})
},
addProductDetails: (userid) => {
try {
return new Promise(async (res, rej) => {
const response = {}
await cartModel
.findOne({ userId: userid })
.populate('cartdata.productId')
.lean().then((cart) => {
if (cart) {
if (cart.cartdata.length > 0) {
response.cartempty = false
response.cart = cart
res(response)
} else {
response.cartempty = true
response.cart = cart
res(response)
}
} else {
response.cartempty = true
res(response)
}
})
})
} catch (error) {
rej(error)
}
},
deleteCart: (userid, productid) => {
return new Promise((res, rej) => {
try {
cartModel.findOneAndUpdate({ userId: userid }, {
$pull: {
cartdata: { productId: productid }
}
}).then((data) => {
res(data)
})
} catch (error) {
rej(error)
}
})
},
changeCartQty: async (data, cb) => {
await cartModel
.updateOne(
{ userId: data.userid, "cartdata.productId": data.productid },
{ "cartdata.$.quantity": data.value }).then((data) => {
console.log("reached");
let taskData = true;
cb(null, data);
}).catch((err) => {
console.log("errror occured in cartQty Change");
cb(err, null);
})
},
decrementQty: (productid, userid) => {
return new Promise(async (res, rej) => {
cartModel.updateOne({ userId: userid, 'cartdata.productId': productid }, { $inc: { 'cartdata.$.quantity': -1 } }).then(async (response) => {
let cart = await cartModel.findOne({ userId: userid })
let quantity;
for (let i = 0; i < cart.cartdata.length; i++) {
if (cart.cartdata[i].productId == productid) {
quantity = cart.cartdata[i].quantity
}
}
response.quantity = quantity
res(response)
})
})
},
totalItem: (userid) => {
return new Promise(async (res, rej) => {
try {
let cart = await cartModel.findOne({ userId: userid })
if (cart) {
let itemCount = 0
itemCount = cart.cartdata.length
res(itemCount)
} else {
itemCount = 0
res(itemCount)
}
} catch (error) {
rej(error)
}
})
},
getTotalAmount: (async (id, cb) => {
cartModel.findOne({ user_id: id }).populate('Cartdata.productId').lean().then((data) => {
let cartlength = data.Cartdata.length
let total = null;
if (cartlength >= 0) {
total = data.Cartdata.reduce((acc, curr) => {
acc += curr.productId.product_price * curr.quantity;
return acc;
}, 0);
cb(null, total);
} else {
cb(null, total);
}
}).catch((err) => {
console.log("error occured in get total", err);
cb(err, null);
})
}),
getTotalAmount: (userid) => {
console.log(userid, 'user keeeeeee iddddd pe cart ki controller');
try {
return new Promise(async (resolve, reject) => {
Helpers.addProductDetails(userid).then((res) => {
console.log(res, 'naaaaaaaaaaaaaaaaaaaajjjjjjjjjjjjjjiiiiiiiiiiiiiii');
let response = {}
cart = res.cart
let total;
if (cart) {
console.log(cart, 'cart ki inside pe');
let cartlength = cart.cartdata.length
console.log(cart.cartdata, 'cartlength-------------------------------------------length');
if (cartlength >= 0) {
total = cart.cartdata.reduce((acc, crr) => {
acc += crr.productId.product_price * crr.quantity
return acc
}, 0)
resolve(total)
} else {
response.cartempty = true
resolve(response)
}
}
})
})
} catch (error) {
reject(error)
}
},
getTotalDiscount: (userid) => {
console.log(userid);
try {
return new Promise(async (res, rej) => {
Helpers.addProductDetails(userid).then((resp) => {
let response = {}
cart = resp.cart
console.log(cart,'its a cart in 555555555555');
let TotalDiscount
if (cart) {
let cartlength = cart.cartdata.length
if (cartlength >= 0) {
TotalDiscount = cart.cartdata.reduce((acc, curr) => {
acc += curr.productId.product_price * curr.quantity
return acc
}, 0)
console.log(TotalDiscount);
res(TotalDiscount)
}
} else {
console.log('error');
(response.cartempty = true),
res(response)
}
})
})
} catch (err) {
rej(err)
}
},
cartCount: (userid) => {
return new Promise(async (res, rej) => {
console.log(userid);
let count = 0
let cart = await cartModel.findOne({ userId: userid })
if (cart) {
// console.log(cart);
count = cart.cartdata.length
}
// console.log(count);
res(count)
})
}
}
module.exports = Helpers;