@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
120 lines (119 loc) • 5.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeFavoriteItems = exports.makeFavorites = exports.makeRecentItems = exports.makeRecents = exports.makeMenuGroupsLookup = exports.makeFeatured = exports.makeMenuItemLookup = void 0;
const cart_1 = require("./cart");
const makeMenuItemLookup = (categories) => {
const cats = categories.reduce((arr, cat) => {
return [...arr, cat, ...(cat.children || [])];
}, []);
return cats.reduce((obj, category) => {
const items = category.items.reduce((o, i) => (Object.assign(Object.assign({}, o), { [i.id]: i })), {});
return Object.assign(Object.assign({}, obj), items);
}, {});
};
exports.makeMenuItemLookup = makeMenuItemLookup;
const makeFeatured = (categories) => {
const flatCategories = categories.reduce((arr, cat) => {
return [...arr, cat, ...(cat.children || [])];
}, []);
const featured = flatCategories.reduce((arr, category) => {
const items = category.items.filter(i => i.featured_position);
return [...arr, ...items];
}, []);
return featured.sort((a, b) => { var _a, _b; return ((_a = a.featured_position) !== null && _a !== void 0 ? _a : 0) - ((_b = b.featured_position) !== null && _b !== void 0 ? _b : 0); });
};
exports.makeFeatured = makeFeatured;
const makeMenuGroupsLookup = (menuItem) => {
if (!menuItem.option_groups)
return {};
return menuItem.option_groups.reduce((grpObj, i) => {
const options = i.option_items.reduce((optObj, o) => {
var _a;
optObj[(_a = o.id) !== null && _a !== void 0 ? _a : 0] = o;
return optObj;
}, {});
grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
return grpObj;
}, {});
};
exports.makeMenuGroupsLookup = makeMenuGroupsLookup;
const validateFavorite = (item, menuItem, soldOut) => {
const groupsLookup = (0, exports.makeMenuGroupsLookup)(menuItem);
const missingGroups = [];
const missingOptions = [];
item.groups.forEach(group => {
var _a;
if (group.id) {
const menuItemGroup = groupsLookup[group.id];
if (menuItemGroup) {
(_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.forEach(option => {
if (option.id) {
const menuItemOption = menuItemGroup.options[option.id];
if (!menuItemOption || soldOut.includes(option.id)) {
missingOptions.push(option);
}
}
});
}
else {
missingGroups.push(group);
}
}
});
return missingGroups.length || missingOptions.length ? false : true;
};
const makeRecents = (recents, itemLookup, soldOut) => {
return recents.reduce((arr, item) => {
if (item.id) {
const menuItem = itemLookup[item.id];
if (!menuItem)
return arr;
const isValid = validateFavorite(item, menuItem, soldOut);
if (!isValid)
return arr;
const favorite = { item };
return isValid
? [
...arr,
Object.assign(Object.assign({}, menuItem), { favorite: { item: Object.assign(Object.assign({}, favorite.item), { quantity: 1 }) } })
]
: arr;
}
else {
return arr;
}
}, []);
};
exports.makeRecents = makeRecents;
const makeRecentItems = (categories, soldOut, orders) => {
if (!orders.length)
return [];
const displayItems = (0, cart_1.makeUniqueDisplayItems)(orders);
const itemLookup = (0, exports.makeMenuItemLookup)(categories);
return (0, exports.makeRecents)(displayItems, itemLookup, soldOut);
};
exports.makeRecentItems = makeRecentItems;
const makeFavorites = (favorites, itemLookup, soldOut) => {
return favorites.reduce((arr, favorite) => {
var _a, _b;
if ((_a = favorite === null || favorite === void 0 ? void 0 : favorite.item) === null || _a === void 0 ? void 0 : _a.id) {
const menuItem = itemLookup[(_b = favorite === null || favorite === void 0 ? void 0 : favorite.item) === null || _b === void 0 ? void 0 : _b.id];
if (!menuItem)
return arr;
const isValid = validateFavorite(favorite.item, menuItem, soldOut);
const favoriteSimple = { item: Object.assign(Object.assign({}, favorite.item), { quantity: 1 }) };
return isValid ? [...arr, Object.assign(Object.assign({}, menuItem), { favorite: favoriteSimple })] : arr;
}
else {
return arr;
}
}, []);
};
exports.makeFavorites = makeFavorites;
const makeFavoriteItems = (categories, soldOut, favorites) => {
if (!favorites.length)
return [];
const itemLookup = (0, exports.makeMenuItemLookup)(categories);
return (0, exports.makeFavorites)(favorites, itemLookup, soldOut);
};
exports.makeFavoriteItems = makeFavoriteItems;