Fixed route

master
Mark Moffat 2019-12-07 14:30:45 +10:30
parent b2f58fbe4b
commit 76018ea076
4 changed files with 54 additions and 65 deletions

View File

@ -227,6 +227,35 @@ $(document).ready(function (){
}); });
}); });
// call update settings API
$('#updateCustomer').validator().on('click', function(e){
e.preventDefault();
if($('#customer-form').validator('validate').has('.has-error').length === 0){
$.ajax({
method: 'POST',
url: '/admin/customer/update',
data: {
customerId: $('#customerId').val(),
email: $('#email').val(),
firstName: $('#firstName').val(),
lastName: $('#lastName').val(),
address1: $('#address1').val(),
address2: $('#address2').val(),
country: $('#country').val(),
state: $('#state').val(),
postcode: $('#postcode').val(),
phone: $('#phone').val()
}
})
.done(function(msg){
showNotification(msg.message, 'success');
})
.fail(function(msg){
showNotification(msg.responseJSON.message, 'danger');
});
}
});
// call update settings API // call update settings API
$('#deleteCustomer').on('click', function(e){ $('#deleteCustomer').on('click', function(e){
e.preventDefault(); e.preventDefault();

File diff suppressed because one or more lines are too long

View File

@ -81,29 +81,16 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
const schemaResult = validateJson('editCustomer', customerObj); const schemaResult = validateJson('editCustomer', customerObj);
if(!schemaResult.result){ if(!schemaResult.result){
console.log('errors', schemaResult.errors); console.log('errors', schemaResult.errors);
if(req.apiAuthenticated){ res.status(400).json(schemaResult.errors);
res.status(400).json(schemaResult.errors);
return;
}
req.session.message = 'Unable to update customer. Check input values';
req.session.messageType = 'danger';
res.redirect('/admin/customer/view/' + req.body.customerId);
return; return;
} }
// check for existing customer // check for existing customer
const customer = await db.customers.findOne({ _id: common.getId(req.body.customerId) }); const customer = await db.customers.findOne({ _id: common.getId(req.body.customerId) });
if(!customer){ if(!customer){
if(req.apiAuthenticated){ res.status(400).json({
res.status(400).json({ message: 'Customer not found'
message: 'Customer not found' });
});
return;
}
req.session.message = 'Customer not found';
req.session.messageType = 'danger';
res.redirect('/admin/customer/view/' + req.body.customerId);
return; return;
} }
// Update customer // Update customer
@ -116,26 +103,13 @@ router.post('/admin/customer/update', restrict, async (req, res) => {
); );
indexCustomers(req.app) indexCustomers(req.app)
.then(() => { .then(() => {
if(req.apiAuthenticated){ const returnCustomer = updatedCustomer.value;
const returnCustomer = updatedCustomer.value; delete returnCustomer.password;
delete returnCustomer.password; res.status(200).json({ message: 'Customer updated', customer: updatedCustomer.value });
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){ }catch(ex){
console.error(colors.red('Failed updating customer: ' + ex)); console.error(colors.red('Failed updating customer: ' + ex));
if(req.apiAuthenticated){ res.status(400).json({ message: 'Failed to update customer' });
res.status(400).json({ message: 'Failed to update customer' });
return;
}
req.session.message = 'Failed to update customer';
req.session.messageType = 'danger';
res.redirect('/admin/customer/view/' + req.body.userId);
} }
}); });
@ -146,17 +120,9 @@ router.delete('/admin/customer', restrict, async (req, res) => {
// check for existing customer // check for existing customer
const customer = await db.customers.findOne({ _id: common.getId(req.body.customerId) }); const customer = await db.customers.findOne({ _id: common.getId(req.body.customerId) });
if(!customer){ if(!customer){
if(req.apiAuthenticated){ res.status(400).json({
res.status(400).json({ message: 'Failed to delete customer. Customer not found'
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 // Update customer
try{ try{
@ -167,13 +133,7 @@ router.delete('/admin/customer', restrict, async (req, res) => {
}); });
}catch(ex){ }catch(ex){
console.error(colors.red('Failed deleting customer: ' + ex)); console.error(colors.red('Failed deleting customer: ' + ex));
if(req.apiAuthenticated){ res.status(400).json({ message: 'Failed to delete customer' });
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);
} }
}); });

View File

@ -1,11 +1,11 @@
{{> partials/menu}} {{> partials/menu}}
<div class="col-lg-9"> <div class="col-lg-9">
<div class="row"> <div class="row">
<form method="post" class="form-horizontal" id="update_form" action="/admin/customer/update" data-toggle="validator"> <form method="post" class="form-horizontal" id="customer-form" role="form" data-toggle="validator" novalidate="false">
<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 class="btn btn-success">Save customer <i class="fa fa-floppy-o"></i></button> <button class="btn btn-success" id="updateCustomer">Save customer <i class="fa fa-floppy-o"></i></button>
</div> </div>
<div class="pull-right"> <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> <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>
@ -15,61 +15,61 @@
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">Email *</label> <label class="col-sm-2 control-label">Email *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="email" value={{result.email}} required> <input type="text" class="form-control" name="email" id="email" value={{result.email}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "First name" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "First name" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="firstName" value={{result.firstName}} required> <input type="text" class="form-control" name="firstName" id="firstName" value={{result.firstName}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Last name" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "Last name" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="lastName" value={{result.lastName}} required> <input type="text" class="form-control" name="lastName" id="lastName" value={{result.lastName}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Address 1" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "Address 1" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="address1" value={{result.address1}} required> <input type="text" class="form-control" name="address1" id="address1" value={{result.address1}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Address 2" }}</label> <label class="col-sm-2 control-label">{{ @root.__ "Address 2" }}</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="address2" value={{result.address2}}> <input type="text" class="form-control" name="address2" id="address2" value={{result.address2}}>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Country" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "Country" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="country" value={{result.country}} required> <input type="text" class="form-control" name="country" id="country" value={{result.country}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "State" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "State" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="state" value={{result.state}} required> <input type="text" class="form-control" name="state" id="state" value={{result.state}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Postcode" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "Postcode" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="postcode" value={{result.postcode}} required> <input type="text" class="form-control" name="postcode" id="postcode" value={{result.postcode}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Phone number" }} *</label> <label class="col-sm-2 control-label">{{ @root.__ "Phone number" }} *</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="text" class="form-control" name="phone" value={{result.phone}} required> <input type="text" class="form-control" name="phone" id="phone" value={{result.phone}} required>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label">{{ @root.__ "Password" }}</label> <label class="col-sm-2 control-label">{{ @root.__ "Password" }}</label>
<div class="col-sm-10"> <div class="col-sm-10">
<input type="password" class="form-control" name="password"> <input type="password" class="form-control" name="password" id="password">
<p class="help-block">Only populate if wanting to reset the customers password</p> <p class="help-block">Only populate if wanting to reset the customers password</p>
</div> </div>
</div> </div>