Allow deleting of customers

master
Mark Moffat 2019-11-23 09:05:19 +10:30
parent c32b9f77b3
commit 67eb6c9014
5 changed files with 96 additions and 10 deletions

View File

@ -425,7 +425,7 @@ $(document).ready(function (){
location.reload(); location.reload();
}) })
.fail(function(msg){ .fail(function(msg){
showNotification(msg.responseJSON.err, 'danger'); showNotification(msg.responseJSON.message, 'danger');
}); });
} }
}); });
@ -484,6 +484,24 @@ $(document).ready(function (){
e.preventDefault(); e.preventDefault();
}); });
// call update settings API
$('#deleteCustomer').on('click', function(e){
e.preventDefault();
$.ajax({
method: 'DELETE',
url: '/admin/customer',
data: {
customerId: $('#customerId').val()
}
})
.done(function(msg){
showNotification(msg.message, 'success', false, '/admin/customers');
})
.fail(function(msg){
showNotification(msg.responseJSON.message, 'danger');
});
});
$(document).on('click', '.image-next', function(e){ $(document).on('click', '.image-next', function(e){
var thumbnails = $('.thumbnail-image'); var thumbnails = $('.thumbnail-image');
var index = 0; var index = 0;
@ -663,7 +681,6 @@ $(document).ready(function (){
data: { permalink: $('#productPermalink').val(), docId: $('#productId').val() } data: { permalink: $('#productPermalink').val(), docId: $('#productId').val() }
}) })
.done(function(msg){ .done(function(msg){
console.log('msg', msg);
showNotification(msg.message, 'success'); showNotification(msg.message, 'success');
}) })
.fail(function(msg){ .fail(function(msg){
@ -850,14 +867,20 @@ function getSelectedOptions(){
} }
// show notification popup // show notification popup
function showNotification(msg, type, reloadPage){ function showNotification(msg, type, reloadPage, redirect){
// defaults to false // defaults to false
reloadPage = reloadPage || false; reloadPage = reloadPage || false;
// defaults to null
redirect = redirect || null;
$('#notify_message').removeClass(); $('#notify_message').removeClass();
$('#notify_message').addClass('alert-' + type); $('#notify_message').addClass('alert-' + type);
$('#notify_message').html(msg); $('#notify_message').html(msg);
$('#notify_message').slideDown(600).delay(2500).slideUp(600, function(){ $('#notify_message').slideDown(600).delay(2500).slideUp(600, function(){
if(redirect){
window.location = redirect;
}
if(reloadPage === true){ if(reloadPage === true){
location.reload(); location.reload();
} }

File diff suppressed because one or more lines are too long

View File

@ -36,7 +36,7 @@ router.post('/customer/create', async (req, res) => {
const customer = await db.customers.findOne({ email: req.body.email }); 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' message: 'A customer already exists with that email address'
}); });
return; return;
} }
@ -54,7 +54,7 @@ router.post('/customer/create', async (req, res) => {
}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({
err: 'Customer creation failed.' message: 'Customer creation failed.'
}); });
} }
}); });
@ -96,7 +96,7 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
if(!customer){ if(!customer){
if(req.apiAuthenticated){ if(req.apiAuthenticated){
res.status(400).json({ res.status(400).json({
err: 'Customer not found' message: 'Customer not found'
}); });
return; return;
} }
@ -139,6 +139,44 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
} }
}); });
// Delete a customer
router.delete('/admin/customer', restrict, async (req, res) => {
const db = req.app.db;
// check for existing customer
const customer = await db.customers.findOne({ _id: common.getId(req.body.customerId) });
if(!customer){
if(req.apiAuthenticated){
res.status(400).json({
message: 'Failed to delete customer. Customer not found'
});
return;
}
req.session.message = 'Failed to delete customer. Customer not found';
req.session.messageType = 'danger';
res.redirect('/admin/customer/view/' + req.body.customerId);
return;
}
// Update customer
try{
await db.customers.deleteOne({ _id: common.getId(req.body.customerId) });
indexCustomers(req.app)
.then(() => {
res.status(200).json({ message: 'Customer deleted' });
});
}catch(ex){
console.error(colors.red('Failed deleting customer: ' + ex));
if(req.apiAuthenticated){
res.status(400).json({ message: 'Failed to delete customer' });
return;
}
req.session.message = 'Failed to delete customer';
req.session.messageType = 'danger';
res.redirect('/admin/customer/view/' + req.body.userId);
}
});
// render the customer view // render the customer view
router.get('/admin/customer/view/:id?', restrict, async (req, res) => { router.get('/admin/customer/view/:id?', restrict, async (req, res) => {
const db = req.app.db; const db = req.app.db;

View File

@ -50,7 +50,7 @@ test('[Fail] Try create a duplicate customer', async t => {
.send(customer) .send(customer)
.expect(400); .expect(400);
t.deepEqual(res.body.err, 'A customer already exists with that email address'); t.deepEqual(res.body.message, 'A customer already exists with that email address');
}); });
test('[Fail] Create with invalid email address', async t => { test('[Fail] Create with invalid email address', async t => {
@ -157,3 +157,25 @@ test('[Success] Customer login with correct email', async t => {
.expect(200); .expect(200);
t.deepEqual(res.body.message, 'Successfully logged in'); t.deepEqual(res.body.message, 'Successfully logged in');
}); });
test('[Success] Delete a customer', async t => {
const res = await g.request
.delete('/admin/customer')
.send({
customerId: g.customers[0]._id
})
.set('apiKey', g.users[0].apiKey)
.expect(200);
t.deepEqual(res.body.message, 'Customer deleted');
});
test('[Success] Failed deleting an incorrect customer', async t => {
const res = await g.request
.delete('/admin/customer')
.send({
customerId: g.customers[0]._id
})
.set('apiKey', g.users[0].apiKey)
.expect(400);
t.deepEqual(res.body.message, 'Failed to delete customer. Customer not found');
});

View File

@ -5,7 +5,10 @@
<div class="col-xs-12 col-md-12"> <div class="col-xs-12 col-md-12">
<div class="page-header"> <div class="page-header">
<div class="pull-right"> <div class="pull-right">
<button id="frm_edit_product_save" class="btn btn-success">Save customer <i class="fa fa-floppy-o"></i></button> <button class="btn btn-success">Save customer <i class="fa fa-floppy-o"></i></button>
</div>
<div class="pull-right">
<button id="deleteCustomer" onclick="return confirm('Are you sure you want to delete this customer?');" class="btn btn-info">Delete customer <i class="fa fa-trash"></i></button>
</div> </div>
<h2>Customer</h2> <h2>Customer</h2>
</div> </div>
@ -77,7 +80,7 @@
</div> </div>
</div> </div>
</div> </div>
<input type="hidden" name="customerId" value="{{result._id}}"> <input type="hidden" name="customerId" id="customerId" value="{{result._id}}">
</form> </form>
</div> </div>