forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer.js
631 lines (565 loc) · 21.1 KB
/
customer.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
const express = require('express');
const router = express.Router();
const colors = require('colors');
const randtoken = require('rand-token');
const bcrypt = require('bcryptjs');
const {
getId,
clearSessionValue,
getCountryList,
mongoSanitize,
sendEmail,
clearCustomer
} = require('../lib/common');
const rateLimit = require('express-rate-limit');
const { indexCustomers } = require('../lib/indexing');
const { validateJson } = require('../lib/schema');
const { restrict } = require('../lib/auth');
const apiLimiter = rateLimit({
windowMs: 300000, // 5 minutes
max: 5
});
// insert a customer
router.post('/customer/create', async (req, res) => {
const db = req.app.db;
const customerObj = {
email: req.body.email,
company: req.body.company,
firstName: req.body.firstName,
lastName: req.body.lastName,
address1: req.body.address1,
address2: req.body.address2,
country: req.body.country,
state: req.body.state,
postcode: req.body.postcode,
phone: req.body.phone,
password: bcrypt.hashSync(req.body.password, 10),
created: new Date()
};
const schemaResult = validateJson('newCustomer', customerObj);
if(!schemaResult.result){
res.status(400).json(schemaResult.errors);
return;
}
// check for existing customer
const customer = await db.customers.findOne({ email: req.body.email });
if(customer){
res.status(400).json({
message: 'A customer already exists with that email address'
});
return;
}
// email is ok to be used.
try{
const newCustomer = await db.customers.insertOne(customerObj);
indexCustomers(req.app)
.then(() => {
// Return the new customer
const customerReturn = newCustomer.ops[0];
delete customerReturn.password;
// Set the customer into the session
req.session.customerPresent = true;
req.session.customerId = customerReturn._id;
req.session.customerEmail = customerReturn.email;
req.session.customerCompany = customerReturn.company;
req.session.customerFirstname = customerReturn.firstName;
req.session.customerLastname = customerReturn.lastName;
req.session.customerAddress1 = customerReturn.address1;
req.session.customerAddress2 = customerReturn.address2;
req.session.customerCountry = customerReturn.country;
req.session.customerState = customerReturn.state;
req.session.customerPostcode = customerReturn.postcode;
req.session.customerPhone = customerReturn.phone;
req.session.orderComment = req.body.orderComment;
// Return customer oject
res.status(200).json(customerReturn);
});
}catch(ex){
console.error(colors.red('Failed to insert customer: ', ex));
res.status(400).json({
message: 'Customer creation failed.'
});
}
});
router.post('/customer/save', async (req, res) => {
const customerObj = {
email: req.body.email,
company: req.body.company,
firstName: req.body.firstName,
lastName: req.body.lastName,
address1: req.body.address1,
address2: req.body.address2,
country: req.body.country,
state: req.body.state,
postcode: req.body.postcode,
phone: req.body.phone
};
const schemaResult = validateJson('saveCustomer', customerObj);
if(!schemaResult.result){
res.status(400).json(schemaResult.errors);
return;
}
// Set the customer into the session
req.session.customerPresent = true;
req.session.customerEmail = customerObj.email;
req.session.customerCompany = customerObj.company;
req.session.customerFirstname = customerObj.firstName;
req.session.customerLastname = customerObj.lastName;
req.session.customerAddress1 = customerObj.address1;
req.session.customerAddress2 = customerObj.address2;
req.session.customerCountry = customerObj.country;
req.session.customerState = customerObj.state;
req.session.customerPostcode = customerObj.postcode;
req.session.customerPhone = customerObj.phone;
req.session.orderComment = req.body.orderComment;
res.status(200).json(customerObj);
});
// Get customer orders
router.get('/customer/account', async (req, res) => {
const db = req.app.db;
const config = req.app.config;
if(!req.session.customerPresent){
res.redirect('/customer/login');
return;
}
const orders = await db.orders.find({
orderCustomer: getId(req.session.customerId)
})
.sort({ orderDate: -1 })
.toArray();
res.render(`${config.themeViews}customer-account`, {
title: 'Orders',
session: req.session,
orders,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
countryList: getCountryList(),
config: req.app.config,
helpers: req.handlebars.helpers
});
});
// Update a customer
router.post('/customer/update', async (req, res) => {
const db = req.app.db;
if(!req.session.customerPresent){
res.redirect('/customer/login');
return;
}
const customerObj = {
company: req.body.company,
email: req.body.email,
firstName: req.body.firstName,
lastName: req.body.lastName,
address1: req.body.address1,
address2: req.body.address2,
country: req.body.country,
state: req.body.state,
postcode: req.body.postcode,
phone: req.body.phone
};
const schemaResult = validateJson('editCustomer', customerObj);
if(!schemaResult.result){
console.log('errors', schemaResult.errors);
res.status(400).json(schemaResult.errors);
return;
}
// check for existing customer
const customer = await db.customers.findOne({ _id: getId(req.session.customerId) });
if(!customer){
res.status(400).json({
message: 'Customer not found'
});
return;
}
// Update customer
try{
const updatedCustomer = await db.customers.findOneAndUpdate(
{ _id: getId(req.session.customerId) },
{
$set: customerObj
}, { multi: false, returnOriginal: false }
);
indexCustomers(req.app)
.then(() => {
// Set the customer into the session
req.session.customerEmail = customerObj.email;
req.session.customerCompany = customerObj.company;
req.session.customerFirstname = customerObj.firstName;
req.session.customerLastname = customerObj.lastName;
req.session.customerAddress1 = customerObj.address1;
req.session.customerAddress2 = customerObj.address2;
req.session.customerCountry = customerObj.country;
req.session.customerState = customerObj.state;
req.session.customerPostcode = customerObj.postcode;
req.session.customerPhone = customerObj.phone;
req.session.orderComment = req.body.orderComment;
res.status(200).json({ message: 'Customer updated', customer: updatedCustomer.value });
});
}catch(ex){
console.error(colors.red(`Failed updating customer: ${ex}`));
res.status(400).json({ message: 'Failed to update customer' });
}
});
// Update a customer
router.post('/admin/customer/update', restrict, async (req, res) => {
const db = req.app.db;
const customerObj = {
company: req.body.company,
email: req.body.email,
firstName: req.body.firstName,
lastName: req.body.lastName,
address1: req.body.address1,
address2: req.body.address2,
country: req.body.country,
state: req.body.state,
postcode: req.body.postcode,
phone: req.body.phone
};
// Handle optional values
if(req.body.password){ customerObj.password = bcrypt.hashSync(req.body.password, 10); }
const schemaResult = validateJson('editCustomer', customerObj);
if(!schemaResult.result){
console.log('errors', schemaResult.errors);
res.status(400).json(schemaResult.errors);
return;
}
// check for existing customer
const customer = await db.customers.findOne({ _id: getId(req.body.customerId) });
if(!customer){
res.status(400).json({
message: 'Customer not found'
});
return;
}
// Update customer
try{
const updatedCustomer = await db.customers.findOneAndUpdate(
{ _id: getId(req.body.customerId) },
{
$set: customerObj
}, { multi: false, returnOriginal: false }
);
indexCustomers(req.app)
.then(() => {
const returnCustomer = updatedCustomer.value;
delete returnCustomer.password;
res.status(200).json({ message: 'Customer updated', customer: updatedCustomer.value });
});
}catch(ex){
console.error(colors.red(`Failed updating customer: ${ex}`));
res.status(400).json({ message: 'Failed to update customer' });
}
});
// Delete a customer
router.delete('/admin/customer', restrict, async (req, res) => {
const db = req.app.db;
// check for existing customer
const customer = await db.customers.findOne({ _id: getId(req.body.customerId) });
if(!customer){
res.status(400).json({
message: 'Failed to delete customer. Customer not found'
});
return;
}
// Update customer
try{
await db.customers.deleteOne({ _id: getId(req.body.customerId) });
indexCustomers(req.app)
.then(() => {
res.status(200).json({ message: 'Customer deleted' });
});
}catch(ex){
console.error(colors.red(`Failed deleting customer: ${ex}`));
res.status(400).json({ message: 'Failed to delete customer' });
}
});
// render the customer view
router.get('/admin/customer/view/:id?', restrict, async (req, res) => {
const db = req.app.db;
const customer = await db.customers.findOne({ _id: getId(req.params.id) });
if(!customer){
// If API request, return json
if(req.apiAuthenticated){
return res.status(400).json({ message: 'Customer not found' });
}
req.session.message = 'Customer not found';
req.session.message_type = 'danger';
return res.redirect('/admin/customers');
}
// If API request, return json
if(req.apiAuthenticated){
return res.status(200).json(customer);
}
return res.render('customer', {
title: 'View customer',
result: customer,
admin: true,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
countryList: getCountryList(),
config: req.app.config,
editor: true,
helpers: req.handlebars.helpers
});
});
// customers list
router.get('/admin/customers', restrict, async (req, res) => {
const db = req.app.db;
const customers = await db.customers.find({}).limit(20).sort({ created: -1 }).toArray();
// If API request, return json
if(req.apiAuthenticated){
return res.status(200).json(customers);
}
return res.render('customers', {
title: 'Customers - List',
admin: true,
customers: customers,
session: req.session,
helpers: req.handlebars.helpers,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
config: req.app.config
});
});
// Filtered customers list
router.get('/admin/customers/filter/:search', restrict, async (req, res, next) => {
const db = req.app.db;
const searchTerm = req.params.search;
const customersIndex = req.app.customersIndex;
const lunrIdArray = [];
customersIndex.search(searchTerm).forEach((id) => {
lunrIdArray.push(getId(id.ref));
});
// we search on the lunr indexes
const customers = await db.customers.find({ _id: { $in: lunrIdArray } }).sort({ created: -1 }).toArray();
// If API request, return json
if(req.apiAuthenticated){
return res.status(200).json({
customers
});
}
return res.render('customers', {
title: 'Customer results',
customers: customers,
admin: true,
config: req.app.config,
session: req.session,
searchTerm: searchTerm,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
});
});
router.post('/admin/customer/lookup', restrict, async (req, res, next) => {
const db = req.app.db;
const customerEmail = req.body.customerEmail;
// Search for a customer
const customer = await db.customers.findOne({ email: customerEmail });
if(customer){
req.session.customerPresent = true;
req.session.customerId = customer._id;
req.session.customerEmail = customer.email;
req.session.customerCompany = customer.company;
req.session.customerFirstname = customer.firstName;
req.session.customerLastname = customer.lastName;
req.session.customerAddress1 = customer.address1;
req.session.customerAddress2 = customer.address2;
req.session.customerCountry = customer.country;
req.session.customerState = customer.state;
req.session.customerPostcode = customer.postcode;
req.session.customerPhone = customer.phone;
return res.status(200).json({
message: 'Customer found',
customer
});
}
return res.status(400).json({
message: 'No customers found'
});
});
router.get('/customer/login', async (req, res, next) => {
const config = req.app.config;
res.render(`${config.themeViews}customer-login`, {
title: 'Customer login',
config: req.app.config,
session: req.session,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
});
});
// login the customer and check the password
router.post('/customer/login_action', async (req, res) => {
const db = req.app.db;
const customer = await db.customers.findOne({ email: mongoSanitize(req.body.loginEmail) });
// check if customer exists with that email
if(customer === undefined || customer === null){
res.status(400).json({
message: 'A customer with that email does not exist.'
});
return;
}
// we have a customer under that email so we compare the password
bcrypt.compare(req.body.loginPassword, customer.password)
.then((result) => {
if(!result){
// password is not correct
res.status(400).json({
message: 'Access denied. Check password and try again.'
});
return;
}
// Customer login successful
req.session.customerPresent = true;
req.session.customerId = customer._id;
req.session.customerEmail = customer.email;
req.session.customerCompany = customer.company;
req.session.customerFirstname = customer.firstName;
req.session.customerLastname = customer.lastName;
req.session.customerAddress1 = customer.address1;
req.session.customerAddress2 = customer.address2;
req.session.customerCountry = customer.country;
req.session.customerState = customer.state;
req.session.customerPostcode = customer.postcode;
req.session.customerPhone = customer.phone;
res.status(200).json({
message: 'Successfully logged in',
customer: customer
});
})
.catch((err) => {
res.status(400).json({
message: 'Access denied. Check password and try again.'
});
});
});
// customer forgotten password
router.get('/customer/forgotten', (req, res) => {
res.render('forgotten', {
title: 'Forgotten',
route: 'customer',
forgotType: 'customer',
config: req.app.config,
helpers: req.handlebars.helpers,
message: clearSessionValue(req.session, 'message'),
messageType: clearSessionValue(req.session, 'messageType'),
showFooter: 'showFooter'
});
});
// forgotten password
router.post('/customer/forgotten_action', apiLimiter, async (req, res) => {
const db = req.app.db;
const config = req.app.config;
const passwordToken = randtoken.generate(30);
// find the user
const customer = await db.customers.findOne({ email: req.body.email });
try{
if(!customer){
// if don't have an email on file, silently fail
res.status(200).json({
message: 'If your account exists, a password reset has been sent to your email'
});
return;
}
const tokenExpiry = Date.now() + 3600000;
await db.customers.updateOne({ email: req.body.email }, { $set: { resetToken: passwordToken, resetTokenExpiry: tokenExpiry } }, { multi: false });
// send forgotten password email
const mailOpts = {
to: req.body.email,
subject: 'Forgotten password request',
body: `You are receiving this because you (or someone else) have requested the reset of the password for your user account.\n\n
Please click on the following link, or paste this into your browser to complete the process:\n\n
${config.baseUrl}/customer/reset/${passwordToken}\n\n
If you did not request this, please ignore this email and your password will remain unchanged.\n`
};
// send the email with token to the user
// TODO: Should fix this to properly handle result
sendEmail(mailOpts.to, mailOpts.subject, mailOpts.body);
res.status(200).json({
message: 'If your account exists, a password reset has been sent to your email'
});
}catch(ex){
res.status(400).json({
message: 'Password reset failed.'
});
}
});
// reset password form
router.get('/customer/reset/:token', async (req, res) => {
const db = req.app.db;
// Find the customer using the token
const customer = await db.customers.findOne({ resetToken: req.params.token, resetTokenExpiry: { $gt: Date.now() } });
if(!customer){
req.session.message = 'Password reset token is invalid or has expired';
req.session.message_type = 'danger';
res.redirect('/forgot');
return;
}
// show the password reset form
res.render('reset', {
title: 'Reset password',
token: req.params.token,
route: 'customer',
config: req.app.config,
message: clearSessionValue(req.session, 'message'),
message_type: clearSessionValue(req.session, 'message_type'),
show_footer: 'show_footer',
helpers: req.handlebars.helpers
});
});
// reset password action
router.post('/customer/reset/:token', async (req, res) => {
const db = req.app.db;
// get the customer
const customer = await db.customers.findOne({ resetToken: req.params.token, resetTokenExpiry: { $gt: Date.now() } });
if(!customer){
req.session.message = 'Password reset token is invalid or has expired';
req.session.message_type = 'danger';
return res.redirect('/forgot');
}
// update the password and remove the token
const newPassword = bcrypt.hashSync(req.body.password, 10);
try{
await db.customers.updateOne({ email: customer.email }, { $set: { password: newPassword, resetToken: undefined, resetTokenExpiry: undefined } }, { multi: false });
const mailOpts = {
to: customer.email,
subject: 'Password successfully reset',
body: `This is a confirmation that the password for your account ${customer.email} has just been changed successfully.\n`
};
// TODO: Should fix this to properly handle result
sendEmail(mailOpts.to, mailOpts.subject, mailOpts.body);
req.session.message = 'Password successfully updated';
req.session.message_type = 'success';
return res.redirect('/checkout/payment');
}catch(ex){
console.log('Unable to reset password', ex);
req.session.message = 'Unable to reset password';
req.session.message_type = 'danger';
return res.redirect('/forgot');
}
});
// logout the customer
router.post('/customer/check', (req, res) => {
if(!req.session.customerPresent){
return res.status(400).json({
message: 'Not logged in'
});
}
return res.status(200).json({
message: 'Customer logged in'
});
});
// logout the customer
router.post('/customer/logout', (req, res) => {
// Clear our session
clearCustomer(req);
res.status(200).json({});
});
// logout the customer
router.get('/customer/logout', (req, res) => {
// Clear our session
clearCustomer(req);
res.redirect('/customer/login');
});
module.exports = router;