Fixed customer indexing and dodgy test
parent
4ebd509478
commit
c32b9f77b3
|
@ -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);
|
||||||
|
indexCustomers(req.app)
|
||||||
|
.then(() => {
|
||||||
// Customer creation successful
|
// Customer creation successful
|
||||||
req.session.customer = newCustomer.insertedId;
|
req.session.customer = newCustomer.insertedId;
|
||||||
const customerReturn = newCustomer.ops[0];
|
const customerReturn = newCustomer.ops[0];
|
||||||
delete customerReturn.password;
|
delete customerReturn.password;
|
||||||
res.status(200).json(customerReturn);
|
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,6 +114,8 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
|
||||||
$set: customerObj
|
$set: customerObj
|
||||||
}, { multi: false, returnOriginal: false }
|
}, { multi: false, returnOriginal: false }
|
||||||
);
|
);
|
||||||
|
indexCustomers(req.app)
|
||||||
|
.then(() => {
|
||||||
if(req.apiAuthenticated){
|
if(req.apiAuthenticated){
|
||||||
const returnCustomer = updatedCustomer.value;
|
const returnCustomer = updatedCustomer.value;
|
||||||
delete returnCustomer.password;
|
delete returnCustomer.password;
|
||||||
|
@ -120,6 +126,7 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
|
||||||
req.session.message = 'Customer updated';
|
req.session.message = 'Customer updated';
|
||||||
req.session.messageType = 'success';
|
req.session.messageType = 'success';
|
||||||
res.redirect('/admin/customer/view/' + req.body.customerId);
|
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){
|
||||||
|
|
|
@ -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 => {
|
||||||
|
|
Loading…
Reference in New Issue