diff --git a/assets/order-history/script.js b/assets/order-history/script.js index 33b26bb..a096b4c 100644 --- a/assets/order-history/script.js +++ b/assets/order-history/script.js @@ -372,7 +372,8 @@ async function searchProducts(query) { .map(p => ({ _id: p.id, name: p.name, - selling_price: p.final_price || p.price || 0, + selling_price: p.price ?? 0, + final_price: p.final_price, available_quantity: p.available_quantity || 0 })); diff --git a/tests/order-base-price.test.js b/tests/order-base-price.test.js new file mode 100644 index 0000000..0e4a590 --- /dev/null +++ b/tests/order-base-price.test.js @@ -0,0 +1,25 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const vm = require('node:vm'); +const source = fs.readFileSync(require('node:path').join(__dirname, '../assets/order-history/script.js'), 'utf8'); +const search = source.slice(source.indexOf('async function searchProducts(query)'), source.indexOf('// Store products for reference')); +const add = source.slice(source.indexOf('function addProductToOrder(productId,'), source.indexOf('// Update order total')); +const save = source.slice(source.indexOf('function linesForSave(items)'), source.indexOf('function confirmRemoveItem()')); +test('search, add and save send the menu base price, never final_price', async () => { + let matches; + const ctx = vm.createContext({ + console, crypto: require('node:crypto'), window: {}, STORE_NAME: 'products', + getData: async () => [{ id: 'paratha', name: 'Paratha', price: 45, final_price: 47.25 }], + document: { getElementById: () => ({}) }, + renderProductSuggestions: products => { matches = products; }, + editingOrder: { items: [] }, renderCurrentOrderItems() {}, updateOrderTotal() {}, + lineIsCancelled: () => false, lineQuantity: item => item.quantity, + }); + vm.runInContext(search + '\n' + add + '\n' + save, ctx); + await ctx.searchProducts('Paratha'); + ctx.addProductToOrder('paratha', matches[0].name, matches[0].selling_price); + const request = ctx.linesForSave(ctx.editingOrder.items); + assert.equal(request[0].price, 45); + assert.equal(matches[0].final_price, 47.25); +});