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

View File

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