UNPKG

@open-tender/utils

Version:

A library of utils for use with Open Tender applications that utilize our cloud-based Order API.

91 lines (90 loc) 3.5 kB
export const makeRefundCartLookup = (cart) => { const lookup = {}; cart.forEach(item => { var _a; const { line_no, quantity, price_total } = item; lookup[line_no !== null && line_no !== void 0 ? line_no : 0] = { quantity, price_total }; (_a = item === null || item === void 0 ? void 0 : item.groups) === null || _a === void 0 ? void 0 : _a.forEach(group => group.options.forEach(option => { const { line_no, quantity, price } = option; lookup[line_no !== null && line_no !== void 0 ? line_no : 0] = { quantity, price }; })); }); return lookup; }; export const makeAmountsLookup = (items, key = 'id') => { return items.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i[key]]: i.amount })), {}); }; export const makeRefundLookup = (order) => { const { cart, gift_cards, surcharges, discounts, taxes, totals, tenders } = order; const { subtotal, gift_card, surcharge, discount, tax, tip, shipping, total } = totals; return { cart: makeRefundCartLookup(cart), gift_cards: gift_cards ? makeAmountsLookup(gift_cards, 'code') : null, surcharges: makeAmountsLookup(surcharges), discounts: makeAmountsLookup(discounts), taxes: makeAmountsLookup(taxes), tenders: makeAmountsLookup(tenders, 'tender_index'), subtotal, gift_card, surcharge, discount, tax, tip, shipping, total }; }; const adjustAmount = (amount, refundAmount) => { return (parseFloat(amount) + parseFloat(refundAmount)).toFixed(2); }; export const adjustAmounts = (order, lookup, list, key = 'id') => { if (!order[list]) return order; order[list].forEach((i) => { const amount = lookup[list][i[key]]; if (amount) i.amount = adjustAmount(i.amount, amount); }); return order; }; const amounts = [ 'subtotal', 'gift_card', 'surcharge', 'discount', 'tax', 'tip', 'shipping', 'total' ]; export const makeNetOrder = (order, refunds) => { if (!refunds || !refunds.length) return order; let copy = JSON.parse(JSON.stringify(order)); refunds.forEach(refund => { const lookup = makeRefundLookup(refund); copy.cart.forEach((i) => { var _a, _b, _c, _d, _e; const item = lookup.cart[(_a = i.line_no) !== null && _a !== void 0 ? _a : 0]; if (item) { i.quantity = (_c = ((_b = i.quantity) !== null && _b !== void 0 ? _b : 0) - item.quantity) !== null && _c !== void 0 ? _c : 0; i.price_total = adjustAmount(i.price_total, (_d = item.price_total) !== null && _d !== void 0 ? _d : '0.0'); } if (i.quantity === 0) { (_e = i.groups) === null || _e === void 0 ? void 0 : _e.forEach(group => group.options.forEach(option => { option.quantity = 0; option.price_total = '0.00'; })); } }); copy = adjustAmounts(copy, lookup, 'gift_cards', 'code'); copy = adjustAmounts(copy, lookup, 'surcharges'); copy = adjustAmounts(copy, lookup, 'discounts'); copy = adjustAmounts(copy, lookup, 'taxes'); copy = adjustAmounts(copy, lookup, 'tenders', 'tender_index'); amounts.forEach(i => { copy.totals[i] = adjustAmount(copy.totals[i], lookup[i]); }); }); return copy; };