Fixed customer indexing and dodgy test

master
Mark Moffat 2019-11-16 21:35:02 +10:30
parent 4ebd509478
commit c32b9f77b3
2 changed files with 24 additions and 17 deletions

View File

@ -4,6 +4,7 @@ const colors = require('colors');
const randtoken = require('rand-token');
const bcrypt = require('bcryptjs');
const common = require('../lib/common');
const { indexCustomers } = require('../lib/indexing');
const { validateJson } = require('../lib/schema');
const { restrict } = require('../lib/auth');
@ -42,11 +43,14 @@ router.post('/customer/create', async (req, res) => {
// email is ok to be used.
try{
const newCustomer = await db.customers.insertOne(customerObj);
// Customer creation successful
req.session.customer = newCustomer.insertedId;
const customerReturn = newCustomer.ops[0];
delete customerReturn.password;
res.status(200).json(customerReturn);
indexCustomers(req.app)
.then(() => {
// Customer creation successful
req.session.customer = newCustomer.insertedId;
const customerReturn = newCustomer.ops[0];
delete customerReturn.password;
res.status(200).json(customerReturn);
});
}catch(ex){
console.error(colors.red('Failed to insert customer: ', ex));
res.status(400).json({
@ -110,16 +114,19 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
$set: customerObj
}, { multi: false, returnOriginal: false }
);
if(req.apiAuthenticated){
const returnCustomer = updatedCustomer.value;
delete returnCustomer.password;
res.status(200).json({ message: 'Customer updated', customer: updatedCustomer.value });
return;
}
// show the view
req.session.message = 'Customer updated';
req.session.messageType = 'success';
res.redirect('/admin/customer/view/' + req.body.customerId);
indexCustomers(req.app)
.then(() => {
if(req.apiAuthenticated){
const returnCustomer = updatedCustomer.value;
delete returnCustomer.password;
res.status(200).json({ message: 'Customer updated', customer: updatedCustomer.value });
return;
}
// show the view
req.session.message = 'Customer updated';
req.session.messageType = 'success';
res.redirect('/admin/customer/view/' + req.body.customerId);
});
}catch(ex){
console.error(colors.red('Failed updating customer: ' + ex));
if(req.apiAuthenticated){

View File

@ -118,12 +118,12 @@ test('[Success] Get customer list', async t => {
test('[Success] Filter customers', async t => {
const res = await g.request
.get('/admin/customers')
.get('/admin/customers/filter/Testy')
.set('apiKey', g.users[0].apiKey)
.expect(200);
// Check the returned customers length
t.deepEqual(3, res.body.length);
t.deepEqual(1, res.body.customers.length);
});
test('[Success] Get single customer', async t => {