Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/order-history/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}));

Expand Down
25 changes: 25 additions & 0 deletions tests/order-base-price.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
Loading