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

View File

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

View File

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

View File

@ -652,8 +652,14 @@ $(document).ready(function (){
$('#global-search-results').empty(); $('#global-search-results').empty();
$('#global-search-results').addClass('invisible'); $('#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 // 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>'); $('#global-search').html('<span class="fa fa-spinner fa-spin"></span>');
globalSearch(); globalSearch();
} }
@ -685,34 +691,40 @@ function globalSearch(){
let hasResult = false; let hasResult = false;
res.customers.forEach((value) => { res.customers.forEach((value) => {
hasResult = true; hasResult = true;
let result = '<li class="list-group-item global-result text-center" data-url="/admin/customer/view/' + value._id + '">'; const result = `
result += '<div class="row">'; <li class="list-group-item global-result text-center" data-url="/admin/customer/view/${value._id}">
result += '<div class="col global-result-type gr-click"><i class="fas fa-users"></i> Customer</div>'; <div class="row">
result += '<div class="col global-result-detail gr-click">' + value.firstName + ' ' + value.lastName + '</div>'; <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.email + '</div>'; <div class="col global-result-detail gr-click">${value.firstName} ${value.lastName}</div>
result += '</div></li>'; <div class="col global-result-detail gr-click">${value.email}</div>
</div>
</li>`;
$('#global-search-results').append(result); $('#global-search-results').append(result);
}); });
res.orders.forEach((value) => { res.orders.forEach((value) => {
hasResult = true; hasResult = true;
let result = '<li class="list-group-item global-result text-center" data-url="/admin/order/view/' + value._id + '">'; const result = `
result += '<div class="row">'; <li class="list-group-item global-result text-center" data-url="/admin/order/view/${value._id}">
result += '<div class="col global-result-type gr-click"><i class="fas fa-cube"></i> Order</div>'; <div class="row">
result += '<div class="col global-result-detail gr-click">' + value.orderFirstname + ' ' + value.orderLastname + '</div>'; <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.orderEmail + '</div>'; <div class="col global-result-detail gr-click">${value.orderFirstname} ${value.orderLastname}</div>
result += '</div></li>'; <div class="col global-result-detail gr-click">${value.orderEmail}</div>
</div>
</li>`;
$('#global-search-results').append(result); $('#global-search-results').append(result);
}); });
res.products.forEach((value) => { res.products.forEach((value) => {
hasResult = true; hasResult = true;
let result = '<li class="list-group-item global-result text-center" data-url="/admin/product/edit/' + value._id + '">'; const result =
result += '<div class="row">'; `<li class="list-group-item global-result text-center" data-url="/admin/product/edit/${value._id}">
result += '<div class="col global-result-type gr-click"><i class="fas fa-box-open"></i> Product</div>'; <div class="row">
result += '<div class="col global-result-detail gr-click">' + value.productTitle + '</div>'; <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">' + numeral(value.productPrice).format('0.00') + '</div>'; <div class="col global-result-detail gr-click">${value.productTitle}</div>
result += '</div></li>'; <div class="col global-result-detail gr-click">${$('#currencySymbol').val()}${numeral(value.productPrice).format('0.00')}</div>
</div>
</li>`;
$('#global-search-results').append(result); $('#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; orderQuery.orderEmail = searchValue;
}else if(numericRegex.test(req.body.searchValue)){ }else if(numericRegex.test(req.body.searchValue)){
// If a numeric value is detected // If a numeric value is detected
orderQuery.amount = common.cleanAmount(req.body.searchValue); orderQuery.amount = req.body.searchValue;
productQuery.productPrice = common.cleanAmount(req.body.searchValue); productQuery.productPrice = req.body.searchValue;
}else{ }else{
// String searches // String searches
customerQuery.$or = [ customerQuery.$or = [

View File

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

View File

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

View File

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