@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
110 lines (109 loc) • 4.58 kB
JavaScript
import { makeUniqueDisplayItems } from './cart';
export 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);
}, {});
};
export 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); });
};
export 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;
}, {});
};
const validateFavorite = (item, menuItem, soldOut) => {
const groupsLookup = 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;
};
export 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;
}
}, []);
};
export const makeRecentItems = (categories, soldOut, orders) => {
if (!orders.length)
return [];
const displayItems = makeUniqueDisplayItems(orders);
const itemLookup = makeMenuItemLookup(categories);
return makeRecents(displayItems, itemLookup, soldOut);
};
export 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;
}
}, []);
};
export const makeFavoriteItems = (categories, soldOut, favorites) => {
if (!favorites.length)
return [];
const itemLookup = makeMenuItemLookup(categories);
return makeFavorites(favorites, itemLookup, soldOut);
};