Fix product price and amount global searching

master
Mark Moffat 2020-01-08 18:58:29 +10:30
parent 97b1dd7875
commit fe14f28994
9 changed files with 43 additions and 30 deletions

View File

@ -16,7 +16,7 @@ const addSchemas = () => {
ajv.addFormat('emailAddress', emailRegex);
// Amount format
const amountRegex = /^[1-9]\d*(\.\d+)?$/;
const amountRegex = /^\d+\.\d\d$/;
ajv.addFormat('amount', amountRegex);
ajv.addKeyword('isNotEmpty', {

View File

@ -12,7 +12,7 @@
"type": "string"
},
"productPrice": {
"type": "number",
"type": "string",
"format": "amount"
},
"productDescription": {

View File

@ -9,7 +9,7 @@
"type": "string"
},
"productPrice": {
"type": "number",
"type": "string",
"format": "amount"
},
"productDescription": {

View File

@ -652,8 +652,14 @@ $(document).ready(function (){
$('#global-search-results').empty();
$('#global-search-results').addClass('invisible');
}
let minLength = 3;
if(/^\d*\.?\d*$/.test($('#global-search-value').val())){
minLength = 1;
}
// Search when 3 or more characters are entered
if($('#global-search-value').val().length > 3){
if($('#global-search-value').val().length > minLength){
$('#global-search').html('<span class="fa fa-spinner fa-spin"></span>');
globalSearch();
}
@ -685,34 +691,40 @@ function globalSearch(){
let hasResult = false;
res.customers.forEach((value) => {
hasResult = true;
let result = '<li class="list-group-item global-result text-center" data-url="/admin/customer/view/' + value._id + '">';
result += '<div class="row">';
result += '<div class="col global-result-type gr-click"><i class="fas fa-users"></i> Customer</div>';
result += '<div class="col global-result-detail gr-click">' + value.firstName + ' ' + value.lastName + '</div>';
result += '<div class="col global-result-detail gr-click">' + value.email + '</div>';
result += '</div></li>';
const result = `
<li class="list-group-item global-result text-center" data-url="/admin/customer/view/${value._id}">
<div class="row">
<div class="col global-result-type gr-click"><i class="fas fa-users"></i> Customer</div>
<div class="col global-result-detail gr-click">${value.firstName} ${value.lastName}</div>
<div class="col global-result-detail gr-click">${value.email}</div>
</div>
</li>`;
$('#global-search-results').append(result);
});
res.orders.forEach((value) => {
hasResult = true;
let result = '<li class="list-group-item global-result text-center" data-url="/admin/order/view/' + value._id + '">';
result += '<div class="row">';
result += '<div class="col global-result-type gr-click"><i class="fas fa-cube"></i> Order</div>';
result += '<div class="col global-result-detail gr-click">' + value.orderFirstname + ' ' + value.orderLastname + '</div>';
result += '<div class="col global-result-detail gr-click">' + value.orderEmail + '</div>';
result += '</div></li>';
const result = `
<li class="list-group-item global-result text-center" data-url="/admin/order/view/${value._id}">
<div class="row">
<div class="col global-result-type gr-click"><i class="fas fa-cube"></i> Order</div>
<div class="col global-result-detail gr-click">${value.orderFirstname} ${value.orderLastname}</div>
<div class="col global-result-detail gr-click">${value.orderEmail}</div>
</div>
</li>`;
$('#global-search-results').append(result);
});
res.products.forEach((value) => {
hasResult = true;
let result = '<li class="list-group-item global-result text-center" data-url="/admin/product/edit/' + value._id + '">';
result += '<div class="row">';
result += '<div class="col global-result-type gr-click"><i class="fas fa-box-open"></i> Product</div>';
result += '<div class="col global-result-detail gr-click">' + value.productTitle + '</div>';
result += '<div class="col global-result-detail gr-click">' + numeral(value.productPrice).format('0.00') + '</div>';
result += '</div></li>';
const result =
`<li class="list-group-item global-result text-center" data-url="/admin/product/edit/${value._id}">
<div class="row">
<div class="col global-result-type gr-click"><i class="fas fa-box-open"></i> Product</div>
<div class="col global-result-detail gr-click">${value.productTitle}</div>
<div class="col global-result-detail gr-click">${$('#currencySymbol').val()}${numeral(value.productPrice).format('0.00')}</div>
</div>
</li>`;
$('#global-search-results').append(result);
});

File diff suppressed because one or more lines are too long

View File

@ -555,8 +555,8 @@ router.post('/admin/searchall', restrict, async (req, res, next) => {
orderQuery.orderEmail = searchValue;
}else if(numericRegex.test(req.body.searchValue)){
// If a numeric value is detected
orderQuery.amount = common.cleanAmount(req.body.searchValue);
productQuery.productPrice = common.cleanAmount(req.body.searchValue);
orderQuery.amount = req.body.searchValue;
productQuery.productPrice = req.body.searchValue;
}else{
// String searches
customerQuery.$or = [

View File

@ -93,7 +93,7 @@ router.post('/admin/product/insert', restrict, checkAccess, async (req, res) =>
const doc = {
productPermalink: req.body.productPermalink,
productTitle: common.cleanHtml(req.body.productTitle),
productPrice: common.safeParseInt(req.body.productPrice),
productPrice: req.body.productPrice,
productDescription: common.cleanHtml(req.body.productDescription),
productPublished: common.convertBool(req.body.productPublished),
productTags: req.body.productTags,
@ -237,7 +237,7 @@ router.post('/admin/product/update', restrict, checkAccess, async (req, res) =>
productId: req.body.productId,
productPermalink: req.body.productPermalink,
productTitle: common.cleanHtml(req.body.productTitle),
productPrice: common.safeParseInt(req.body.productPrice),
productPrice: req.body.productPrice,
productDescription: common.cleanHtml(req.body.productDescription),
productPublished: common.convertBool(req.body.productPublished),
productTags: req.body.productTags,

View File

@ -205,7 +205,7 @@ test('[Success] Add a product', async t => {
const product = {
productPermalink: 'test-jacket',
productTitle: 'Test Jacket',
productPrice: 100,
productPrice: '100.00',
productDescription: 'Test desc',
productPublished: true,
productTags: 'organic, jacket',
@ -235,7 +235,7 @@ test('[Fail] Add a product - Duplicate permalink', async t => {
const product = {
productPermalink: 'test-jacket',
productTitle: 'Test Jacket - blue',
productPrice: 100,
productPrice: '100.00',
productDescription: 'Test desc blue',
productPublished: true,
productTags: 'organic, jacket, blue',
@ -265,7 +265,7 @@ test('[Success] Update a product', async t => {
const product = {
productId: g.products[0]._id,
productTitle: 'Test Jacket',
productPrice: 200,
productPrice: '200.00',
productDescription: 'Test desc',
productPublished: true,
productTags: 'organic, jacket',

View File

@ -165,6 +165,7 @@
<input type="hidden" id="input_notify_messageType" value="{{messageType}}">
<input type="hidden" id="cartCheckout" value="{{checkout}}">
<input type="hidden" id="cartTheme" value="{{@root.config.theme}}">
<input type="hidden" id="currencySymbol" value="{{@root.config.currencySymbol}}">
<div id="notify_message"></div>
{{#if showFooter}}
<footer class="footer">