From c32b9f77b396483c552b1e2b600ac0a15f6eaf0d Mon Sep 17 00:00:00 2001 From: Mark Moffat Date: Sat, 16 Nov 2019 21:35:02 +1030 Subject: [PATCH] Fixed customer indexing and dodgy test --- routes/customer.js | 37 ++++++++++++++++++++++--------------- test/specs/customers.js | 4 ++-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/routes/customer.js b/routes/customer.js index bb6990e..f3089da 100644 --- a/routes/customer.js +++ b/routes/customer.js @@ -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){ diff --git a/test/specs/customers.js b/test/specs/customers.js index b467131..e27322d 100644 --- a/test/specs/customers.js +++ b/test/specs/customers.js @@ -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 => {