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();
})
.fail(function(msg){
showNotification(msg.responseJSON.err, 'danger');
showNotification(msg.responseJSON.message, 'danger');
});
}
});
@ -484,6 +484,24 @@ $(document).ready(function (){
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){
var thumbnails = $('.thumbnail-image');
var index = 0;
@ -663,7 +681,6 @@ $(document).ready(function (){
data: { permalink: $('#productPermalink').val(), docId: $('#productId').val() }
})
.done(function(msg){
console.log('msg', msg);
showNotification(msg.message, 'success');
})
.fail(function(msg){
@ -850,14 +867,20 @@ function getSelectedOptions(){
}
// show notification popup
function showNotification(msg, type, reloadPage){
function showNotification(msg, type, reloadPage, redirect){
// defaults to false
reloadPage = reloadPage || false;
// defaults to null
redirect = redirect || null;
$('#notify_message').removeClass();
$('#notify_message').addClass('alert-' + type);
$('#notify_message').html(msg);
$('#notify_message').slideDown(600).delay(2500).slideUp(600, function(){
if(redirect){
window.location = redirect;
}
if(reloadPage === true){
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 });
if(customer){
res.status(400).json({
err: 'A customer already exists with that email address'
message: 'A customer already exists with that email address'
});
return;
}
@ -54,7 +54,7 @@ router.post('/customer/create', async (req, res) => {
}catch(ex){
console.error(colors.red('Failed to insert customer: ', ex));
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(req.apiAuthenticated){
res.status(400).json({
err: 'Customer not found'
message: 'Customer not found'
});
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
router.get('/admin/customer/view/:id?', restrict, async (req, res) => {
const db = req.app.db;

View File

@ -50,7 +50,7 @@ test('[Fail] Try create a duplicate customer', async t => {
.send(customer)
.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 => {
@ -157,3 +157,25 @@ test('[Success] Customer login with correct email', async t => {
.expect(200);
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="page-header">
<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>
<h2>Customer</h2>
</div>
@ -77,7 +80,7 @@
</div>
</div>
</div>
<input type="hidden" name="customerId" value="{{result._id}}">
<input type="hidden" name="customerId" id="customerId" value="{{result._id}}">
</form>
</div>