@open-tender/utils 0.0.24 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { MenuCategories } from "@open-tender/types";
|
|
1
|
+
import { CartItem, MenuCategories, MenuItem, SoldOut } from "@open-tender/types";
|
|
2
2
|
export declare const makeMenuItemLookup: (categories: MenuCategories) => any;
|
|
3
3
|
export declare const makeFeatured: (categories: MenuCategories) => any;
|
|
4
|
+
export declare const makeMenuGroupsLookup: (menuItem: MenuItem) => any;
|
|
5
|
+
export declare const makeRecents: (recents: Partial<CartItem>[], itemLookup: any, soldOut: SoldOut) => any;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeFeatured = exports.makeMenuItemLookup = void 0;
|
|
3
|
+
exports.makeRecents = exports.makeMenuGroupsLookup = exports.makeFeatured = exports.makeMenuItemLookup = void 0;
|
|
4
4
|
const makeMenuItemLookup = (categories) => {
|
|
5
5
|
const cats = categories.reduce((arr, cat) => {
|
|
6
6
|
return [...arr, cat, ...(cat.children || [])];
|
|
@@ -22,3 +22,62 @@ const makeFeatured = (categories) => {
|
|
|
22
22
|
return featured.sort((a, b) => a.featured_position - b.featured_position);
|
|
23
23
|
};
|
|
24
24
|
exports.makeFeatured = makeFeatured;
|
|
25
|
+
const makeMenuGroupsLookup = (menuItem) => {
|
|
26
|
+
if (!menuItem.option_groups)
|
|
27
|
+
return {};
|
|
28
|
+
return menuItem.option_groups.reduce((grpObj, i) => {
|
|
29
|
+
const options = i.option_items.reduce((optObj, o) => {
|
|
30
|
+
optObj[o.id] = o;
|
|
31
|
+
return optObj;
|
|
32
|
+
}, {});
|
|
33
|
+
grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
|
|
34
|
+
return grpObj;
|
|
35
|
+
}, {});
|
|
36
|
+
};
|
|
37
|
+
exports.makeMenuGroupsLookup = makeMenuGroupsLookup;
|
|
38
|
+
const validateFavorite = (item, menuItem, soldOut) => {
|
|
39
|
+
var _a;
|
|
40
|
+
const groupsLookup = (0, exports.makeMenuGroupsLookup)(menuItem);
|
|
41
|
+
const missingGroups = [];
|
|
42
|
+
const missingOptions = [];
|
|
43
|
+
(_a = item.groups) === null || _a === void 0 ? void 0 : _a.forEach((group) => {
|
|
44
|
+
var _a;
|
|
45
|
+
if (group.id) {
|
|
46
|
+
const menuItemGroup = groupsLookup[group.id];
|
|
47
|
+
if (menuItemGroup) {
|
|
48
|
+
(_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.forEach((option) => {
|
|
49
|
+
if (option.id) {
|
|
50
|
+
const menuItemOption = menuItemGroup.options[option.id];
|
|
51
|
+
if (!menuItemOption || soldOut.includes(option.id)) {
|
|
52
|
+
missingOptions.push(option);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
missingGroups.push(group);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return missingGroups.length || missingOptions.length ? false : true;
|
|
63
|
+
};
|
|
64
|
+
const makeRecents = (recents, itemLookup, soldOut) => {
|
|
65
|
+
return recents.reduce((arr, item) => {
|
|
66
|
+
if (item.id) {
|
|
67
|
+
const menuItem = itemLookup[item.id];
|
|
68
|
+
if (!menuItem)
|
|
69
|
+
return arr;
|
|
70
|
+
const isValid = validateFavorite(item, menuItem, soldOut);
|
|
71
|
+
if (!isValid)
|
|
72
|
+
return arr;
|
|
73
|
+
const favorite = { item };
|
|
74
|
+
return isValid
|
|
75
|
+
? [
|
|
76
|
+
...arr,
|
|
77
|
+
Object.assign(Object.assign({}, menuItem), { favorite: { item: Object.assign(Object.assign({}, favorite.item), { quantity: 1 }) } }),
|
|
78
|
+
]
|
|
79
|
+
: arr;
|
|
80
|
+
}
|
|
81
|
+
}, []);
|
|
82
|
+
};
|
|
83
|
+
exports.makeRecents = makeRecents;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { MenuCategories } from "@open-tender/types";
|
|
1
|
+
import { CartItem, MenuCategories, MenuItem, SoldOut } from "@open-tender/types";
|
|
2
2
|
export declare const makeMenuItemLookup: (categories: MenuCategories) => any;
|
|
3
3
|
export declare const makeFeatured: (categories: MenuCategories) => any;
|
|
4
|
+
export declare const makeMenuGroupsLookup: (menuItem: MenuItem) => any;
|
|
5
|
+
export declare const makeRecents: (recents: Partial<CartItem>[], itemLookup: any, soldOut: SoldOut) => any;
|
|
@@ -17,3 +17,60 @@ export const makeFeatured = (categories) => {
|
|
|
17
17
|
}, []);
|
|
18
18
|
return featured.sort((a, b) => a.featured_position - b.featured_position);
|
|
19
19
|
};
|
|
20
|
+
export const makeMenuGroupsLookup = (menuItem) => {
|
|
21
|
+
if (!menuItem.option_groups)
|
|
22
|
+
return {};
|
|
23
|
+
return menuItem.option_groups.reduce((grpObj, i) => {
|
|
24
|
+
const options = i.option_items.reduce((optObj, o) => {
|
|
25
|
+
optObj[o.id] = o;
|
|
26
|
+
return optObj;
|
|
27
|
+
}, {});
|
|
28
|
+
grpObj[i.id] = Object.assign(Object.assign({}, i), { options });
|
|
29
|
+
return grpObj;
|
|
30
|
+
}, {});
|
|
31
|
+
};
|
|
32
|
+
const validateFavorite = (item, menuItem, soldOut) => {
|
|
33
|
+
var _a;
|
|
34
|
+
const groupsLookup = makeMenuGroupsLookup(menuItem);
|
|
35
|
+
const missingGroups = [];
|
|
36
|
+
const missingOptions = [];
|
|
37
|
+
(_a = item.groups) === null || _a === void 0 ? void 0 : _a.forEach((group) => {
|
|
38
|
+
var _a;
|
|
39
|
+
if (group.id) {
|
|
40
|
+
const menuItemGroup = groupsLookup[group.id];
|
|
41
|
+
if (menuItemGroup) {
|
|
42
|
+
(_a = group === null || group === void 0 ? void 0 : group.options) === null || _a === void 0 ? void 0 : _a.forEach((option) => {
|
|
43
|
+
if (option.id) {
|
|
44
|
+
const menuItemOption = menuItemGroup.options[option.id];
|
|
45
|
+
if (!menuItemOption || soldOut.includes(option.id)) {
|
|
46
|
+
missingOptions.push(option);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
missingGroups.push(group);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return missingGroups.length || missingOptions.length ? false : true;
|
|
57
|
+
};
|
|
58
|
+
export const makeRecents = (recents, itemLookup, soldOut) => {
|
|
59
|
+
return recents.reduce((arr, item) => {
|
|
60
|
+
if (item.id) {
|
|
61
|
+
const menuItem = itemLookup[item.id];
|
|
62
|
+
if (!menuItem)
|
|
63
|
+
return arr;
|
|
64
|
+
const isValid = validateFavorite(item, menuItem, soldOut);
|
|
65
|
+
if (!isValid)
|
|
66
|
+
return arr;
|
|
67
|
+
const favorite = { item };
|
|
68
|
+
return isValid
|
|
69
|
+
? [
|
|
70
|
+
...arr,
|
|
71
|
+
Object.assign(Object.assign({}, menuItem), { favorite: { item: Object.assign(Object.assign({}, favorite.item), { quantity: 1 }) } }),
|
|
72
|
+
]
|
|
73
|
+
: arr;
|
|
74
|
+
}
|
|
75
|
+
}, []);
|
|
76
|
+
};
|
package/package.json
CHANGED