Refactoring customer route

master
Mark Moffat 2019-11-09 14:51:48 +10:30
parent 8c4d1fa199
commit 357e694597
2 changed files with 177 additions and 198 deletions

View File

@ -7,7 +7,7 @@ const common = require('../lib/common');
const { restrict } = require('../lib/auth'); const { restrict } = require('../lib/auth');
// insert a customer // insert a customer
router.post('/customer/create', (req, res) => { router.post('/customer/create', async (req, res) => {
const db = req.app.db; const db = req.app.db;
const doc = { const doc = {
@ -25,93 +25,90 @@ router.post('/customer/create', (req, res) => {
}; };
// check for existing customer // check for existing customer
db.customers.findOne({ email: req.body.email }, (err, customer) => { const customer = await db.customers.findOne({ email: req.body.email });
if(customer){ if(customer){
res.status(400).json({ res.status(400).json({
err: 'A customer already exists with that email address' err: 'A customer already exists with that email address'
}); });
return; return;
} }
// email is ok to be used. // email is ok to be used.
db.customers.insertOne(doc, (err, newCustomer) => { try{
if(err){ await db.customers.insertOne(doc, (err, newCustomer) => {
if(newCustomer){
console.error(colors.red('Failed to insert customer: ' + err));
res.status(400).json({
err: 'A customer already exists with that email address'
});
return;
}
console.error(colors.red('Failed to insert customer: ' + err));
res.status(400).json({
err: 'Customer creation failed.'
});
return;
}
// Customer creation successful // Customer creation successful
req.session.customer = newCustomer.ops[0]; req.session.customer = newCustomer.insertedId;
res.status(200).json({ res.status(200).json({
message: 'Successfully logged in', message: 'Successfully logged in',
customer: newCustomer customer: newCustomer
}); });
}); });
}); }catch(ex){
console.error(colors.red('Failed to insert customer: ', ex));
res.status(400).json({
err: 'Customer creation failed.'
});
}
}); });
// render the customer view // render the customer view
router.get('/admin/customer/view/:id?', restrict, (req, res) => { router.get('/admin/customer/view/:id?', restrict, async (req, res) => {
const db = req.app.db; const db = req.app.db;
db.customers.findOne({ _id: common.getId(req.params.id) }, (err, customer) => { const customer = await db.customers.findOne({ _id: common.getId(req.params.id) });
if(err){
console.info(err.stack);
}
// If API request, return json if(!customer){
// If API request, return json
if(req.apiAuthenticated){ if(req.apiAuthenticated){
return res.status(200).json(customer); 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');
}
return res.render('customer', { // If API request, return json
title: 'View customer', if(req.apiAuthenticated){
result: customer, return res.status(200).json(customer);
admin: true, }
session: req.session,
message: common.clearSessionValue(req.session, 'message'), return res.render('customer', {
messageType: common.clearSessionValue(req.session, 'messageType'), title: 'View customer',
config: req.app.config, result: customer,
editor: true, admin: true,
helpers: req.handlebars.helpers session: req.session,
}); message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
config: req.app.config,
editor: true,
helpers: req.handlebars.helpers
}); });
}); });
// customers list // customers list
router.get('/admin/customers', restrict, (req, res) => { router.get('/admin/customers', restrict, async (req, res) => {
const db = req.app.db; const db = req.app.db;
db.customers.find({}).limit(20).sort({ created: -1 }).toArray((err, customers) => { 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', { // If API request, return json
title: 'Customers - List', if(req.apiAuthenticated){
admin: true, return res.status(200).json(customers);
customers: customers, }
session: req.session,
helpers: req.handlebars.helpers, return res.render('customers', {
message: common.clearSessionValue(req.session, 'message'), title: 'Customers - List',
messageType: common.clearSessionValue(req.session, 'messageType'), admin: true,
config: req.app.config customers: customers,
}); session: req.session,
helpers: req.handlebars.helpers,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
config: req.app.config
}); });
}); });
// Filtered customers list // Filtered customers list
router.get('/admin/customers/filter/:search', restrict, (req, res, next) => { router.get('/admin/customers/filter/:search', restrict, async (req, res, next) => {
const db = req.app.db; const db = req.app.db;
const searchTerm = req.params.search; const searchTerm = req.params.search;
const customersIndex = req.app.customersIndex; const customersIndex = req.app.customersIndex;
@ -122,29 +119,25 @@ router.get('/admin/customers/filter/:search', restrict, (req, res, next) => {
}); });
// we search on the lunr indexes // we search on the lunr indexes
db.customers.find({ _id: { $in: lunrIdArray } }).sort({ created: -1 }).toArray((err, customers) => { const customers = await db.customers.find({ _id: { $in: lunrIdArray } }).sort({ created: -1 }).toArray();
if(err){
console.error(colors.red('Error searching', err));
}
// If API request, return json // If API request, return json
if(req.apiAuthenticated){ if(req.apiAuthenticated){
return res.status(200).json({ return res.status(200).json({
customers customers
});
}
return res.render('customers', {
title: 'Customer results',
customers: customers,
admin: true,
config: req.app.config,
session: req.session,
searchTerm: searchTerm,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
}); });
}
return res.render('customers', {
title: 'Customer results',
customers: customers,
admin: true,
config: req.app.config,
session: req.session,
searchTerm: searchTerm,
message: common.clearSessionValue(req.session, 'message'),
messageType: common.clearSessionValue(req.session, 'messageType'),
helpers: req.handlebars.helpers
}); });
}); });
@ -152,41 +145,35 @@ router.get('/admin/customers/filter/:search', restrict, (req, res, next) => {
router.post('/customer/login_action', async (req, res) => { router.post('/customer/login_action', async (req, res) => {
const db = req.app.db; const db = req.app.db;
db.customers.findOne({email: common.mongoSanitize(req.body.loginEmail)}, (err, customer) => { // eslint-disable-line const customer = await db.customers.findOne({ email: common.mongoSanitize(req.body.loginEmail) });
if(err){ // check if customer exists with that email
// An error accurred if(customer === undefined || customer === null){
return res.status(400).json({ 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.' message: 'Access denied. Check password and try again.'
}); });
return;
} }
// check if customer exists with that email // Customer login successful
if(customer === undefined || customer === null){ req.session.customer = customer;
return res.status(400).json({ res.status(200).json({
message: 'A customer with that email does not exist.' message: 'Successfully logged in',
}); customer: customer
} });
// we have a customer under that email so we compare the password })
bcrypt.compare(req.body.loginPassword, customer.password) .catch((err) => {
.then((result) => { res.status(400).json({
if(!result){ message: 'Access denied. Check password and try again.'
// password is not correct
return res.status(400).json({
message: 'Access denied. Check password and try again.'
});
}
// Customer login successful
req.session.customer = customer;
return res.status(200).json({
message: 'Successfully logged in',
customer: customer
});
})
.catch((err) => {
return res.status(400).json({
message: 'Access denied. Check password and try again.'
});
}); });
}); });
}); });
@ -206,97 +193,105 @@ router.get('/customer/forgotten', (req, res) => {
}); });
// forgotten password // forgotten password
router.post('/customer/forgotten_action', (req, res) => { router.post('/customer/forgotten_action', async (req, res) => {
const db = req.app.db; const db = req.app.db;
const config = req.app.config; const config = req.app.config;
const passwordToken = randtoken.generate(30); const passwordToken = randtoken.generate(30);
// find the user // find the user
db.customers.findOne({ email: req.body.email }, (err, customer) => { const customer = await db.customers.findOne({ email: req.body.email });
// if we have a customer, set a token, expiry and email it // if we have a customer, set a token, expiry and email it
if(customer){ if(!customer){
const tokenExpiry = Date.now() + 3600000; req.session.message = 'Account does not exist';
db.customers.updateOne({ email: req.body.email }, { $set: { resetToken: passwordToken, resetTokenExpiry: tokenExpiry } }, { multi: false }, (err, numReplaced) => { req.session.message_type = 'danger';
// send forgotten password email res.redirect('/customer/forgotten');
const mailOpts = { return;
to: req.body.email, }
subject: 'Forgotten password request', try{
body: `You are receiving this because you (or someone else) have requested the reset of the password for your user account.\n\n const tokenExpiry = Date.now() + 3600000;
Please click on the following link, or paste this into your browser to complete the process:\n\n await db.customers.updateOne({ email: req.body.email }, { $set: { resetToken: passwordToken, resetTokenExpiry: tokenExpiry } }, { multi: false });
${config.baseUrl}/customer/reset/${passwordToken}\n\n // send forgotten password email
If you did not request this, please ignore this email and your password will remain unchanged.\n` 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 // send the email with token to the user
// TODO: Should fix this to properly handle result // TODO: Should fix this to properly handle result
common.sendEmail(mailOpts.to, mailOpts.subject, mailOpts.body); common.sendEmail(mailOpts.to, mailOpts.subject, mailOpts.body);
req.session.message = 'An email has been sent to ' + req.body.email + ' with further instructions'; req.session.message = 'An email has been sent to ' + req.body.email + ' with further instructions';
req.session.message_type = 'success'; req.session.message_type = 'success';
return res.redirect('/customer/forgotten'); res.redirect('/customer/forgotten');
}); }catch(ex){
}else{ req.session.message = 'Account does not exist';
req.session.message = 'Account does not exist'; req.session.message_type = 'danger';
res.redirect('/customer/forgotten'); res.redirect('/customer/forgotten');
} }
});
}); });
// reset password form // reset password form
router.get('/customer/reset/:token', (req, res) => { router.get('/customer/reset/:token', async (req, res) => {
const db = req.app.db; const db = req.app.db;
// Find the customer using the token // Find the customer using the token
db.customers.findOne({ resetToken: req.params.token, resetTokenExpiry: { $gt: Date.now() } }, (err, customer) => { const customer = await db.customers.findOne({ resetToken: req.params.token, resetTokenExpiry: { $gt: Date.now() } });
if(!customer){ if(!customer){
req.session.message = 'Password reset token is invalid or has expired'; req.session.message = 'Password reset token is invalid or has expired';
req.session.message_type = 'danger'; req.session.message_type = 'danger';
res.redirect('/forgot'); res.redirect('/forgot');
return; return;
} }
// show the password reset form // show the password reset form
res.render('reset', { res.render('reset', {
title: 'Reset password', title: 'Reset password',
token: req.params.token, token: req.params.token,
route: 'customer', route: 'customer',
config: req.app.config, config: req.app.config,
message: common.clearSessionValue(req.session, 'message'), message: common.clearSessionValue(req.session, 'message'),
message_type: common.clearSessionValue(req.session, 'message_type'), message_type: common.clearSessionValue(req.session, 'message_type'),
show_footer: 'show_footer', show_footer: 'show_footer',
helpers: req.handlebars.helpers helpers: req.handlebars.helpers
});
}); });
}); });
// reset password action // reset password action
router.post('/customer/reset/:token', (req, res) => { router.post('/customer/reset/:token', async (req, res) => {
const db = req.app.db; const db = req.app.db;
// get the customer // get the customer
db.customers.findOne({ resetToken: req.params.token, resetTokenExpiry: { $gt: Date.now() } }, (err, customer) => { const customer = await db.customers.findOne({ resetToken: req.params.token, resetTokenExpiry: { $gt: Date.now() } });
if(!customer){ if(!customer){
req.session.message = 'Password reset token is invalid or has expired'; req.session.message = 'Password reset token is invalid or has expired';
req.session.message_type = 'danger'; req.session.message_type = 'danger';
return res.redirect('/forgot'); return res.redirect('/forgot');
} }
// update the password and remove the token // update the password and remove the token
const newPassword = bcrypt.hashSync(req.body.password, 10); const newPassword = bcrypt.hashSync(req.body.password, 10);
db.customers.updateOne({ email: customer.email }, { $set: { password: newPassword, resetToken: undefined, resetTokenExpiry: undefined } }, { multi: false }, (err, numReplaced) => { try{
const mailOpts = { await db.customers.updateOne({ email: customer.email }, { $set: { password: newPassword, resetToken: undefined, resetTokenExpiry: undefined } }, { multi: false });
to: customer.email, const mailOpts = {
subject: 'Password successfully reset', to: customer.email,
body: 'This is a confirmation that the password for your account ' + customer.email + ' has just been changed successfully.\n' 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 // TODO: Should fix this to properly handle result
common.sendEmail(mailOpts.to, mailOpts.subject, mailOpts.body); common.sendEmail(mailOpts.to, mailOpts.subject, mailOpts.body);
req.session.message = 'Password successfully updated'; req.session.message = 'Password successfully updated';
req.session.message_type = 'success'; req.session.message_type = 'success';
return res.redirect('/pay'); return res.redirect('/pay');
}); }catch(ex){
return''; 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 // logout the customer

View File

@ -9,14 +9,6 @@ test.before(async () => {
}); });
test('[Success] Create a customer', async t => { test('[Success] Create a customer', async t => {
// Login
await g.request
.post('/admin/login_action')
.send({
email: g.users[0].userEmail,
password: 'test'
});
const customer = { const customer = {
email: 'sarah.jones@test.com', email: 'sarah.jones@test.com',
firstName: 'Sarah', firstName: 'Sarah',
@ -39,14 +31,6 @@ test('[Success] Create a customer', async t => {
}); });
test('[Fail] Try create a duplicate customer', async t => { test('[Fail] Try create a duplicate customer', async t => {
// Login
await g.request
.post('/admin/login_action')
.send({
email: g.users[0].userEmail,
password: 'test'
});
const customer = { const customer = {
email: 'sarah.jones@test.com', email: 'sarah.jones@test.com',
firstName: 'Sarah', firstName: 'Sarah',