@feedmepos/order-plugin-gallery 0.0.3 → 0.0.5

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.
@@ -0,0 +1,3 @@
1
+ import { PluginFunction } from "@feedmepos/ordering-sdk";
2
+ declare const maxItemInCategoryPlugin: PluginFunction;
3
+ export default maxItemInCategoryPlugin;
@@ -0,0 +1,92 @@
1
+ const maxItemInCategoryPlugin = ({ sdk, onAfterComputeMenuState, onBeforeSubmitOrder, pluginParams, }) => {
2
+ const MAX_FREE_ITEMS = 1;
3
+ const MIN_ITEMS = 1;
4
+ const MAX_ITEMS = 2;
5
+ const categoryLimitConfig = pluginParams?.categoryLimitConfig || {};
6
+ onBeforeSubmitOrder(() => {
7
+ if (!categoryLimitConfig.categoryId) {
8
+ return true;
9
+ }
10
+ const slotActiveBills = sdk.orderManager.state.value.slotActiveBills;
11
+ const summarizedItems = slotActiveBills?.[0]?.items || [];
12
+ const currentOrder = sdk.orderManager.state.value.order;
13
+ const cartItems = currentOrder?.draft || [];
14
+ const rawMenu = sdk.menuManager.state.value.overridedMenu;
15
+ const targetCategory = rawMenu?.modules?.category.find((c) => {
16
+ return c._id === categoryLimitConfig.categoryId;
17
+ });
18
+ if (!targetCategory) {
19
+ return true;
20
+ }
21
+ const allItemsInCategory = rawMenu?.modules?.item.filter((i) => i.category === categoryLimitConfig.categoryId);
22
+ const allItemsInCategoryIdMap = new Map();
23
+ allItemsInCategory?.forEach((item) => {
24
+ allItemsInCategoryIdMap.set(item._id, item);
25
+ });
26
+ const allItemsInCategoryNameMap = new Map();
27
+ allItemsInCategory?.forEach((item) => {
28
+ allItemsInCategoryNameMap.set(item.name, item);
29
+ });
30
+ const submittedItemsInCategory = summarizedItems
31
+ .map((item) => {
32
+ const menuItem = allItemsInCategoryNameMap.get(item.name || "");
33
+ if (!menuItem)
34
+ return null;
35
+ return { menuItem, quantity: item.quantity };
36
+ })
37
+ .filter((entry) => Boolean(entry));
38
+ const cartItemsInCategory = cartItems
39
+ .map((item) => {
40
+ const menuItem = allItemsInCategoryIdMap.get(item.productId);
41
+ if (!menuItem)
42
+ return null;
43
+ return { menuItem, quantity: item.quantity };
44
+ })
45
+ .filter((entry) => Boolean(entry));
46
+ // const numberOfFreeItems = {
47
+ // submitted: submittedItemsInCategory.reduce((sum, entry) => {
48
+ // return entry.menuItem.price.amount === 0 ? sum + entry.quantity : sum;
49
+ // }, 0),
50
+ // inCart: cartItemsInCategory.reduce((sum, entry) => {
51
+ // return entry.menuItem.price.amount === 0 ? sum + entry.quantity : sum;
52
+ // }, 0),
53
+ // };
54
+ const numberOfTotalItems = {
55
+ submitted: submittedItemsInCategory.reduce((sum, entry) => sum + entry.quantity, 0),
56
+ inCart: cartItemsInCategory.reduce((sum, entry) => sum + entry.quantity, 0),
57
+ };
58
+ const toast = (message) => {
59
+ sdk.ui.toast.show({
60
+ title: `Please order one or two "${targetCategory.name}". ${message}`,
61
+ type: "error",
62
+ duration: 5000,
63
+ });
64
+ };
65
+ // POS already ordered, and customer have not added more target items to cart, we ignore the validation
66
+ // if (numberOfTotalItems.submitted > 0 && numberOfTotalItems.inCart === 0) {
67
+ // return true;
68
+ // }
69
+ // const totalFreeItems =
70
+ // numberOfFreeItems.submitted + numberOfFreeItems.inCart;
71
+ const totalItems = numberOfTotalItems.submitted + numberOfTotalItems.inCart;
72
+ // if (totalFreeItems > MAX_FREE_ITEMS) {
73
+ // const overFreeItems = totalFreeItems - MAX_FREE_ITEMS;
74
+ // toast(
75
+ // `Remove ${overFreeItems} free ${targetCategory.name} from cart to continue. `
76
+ // );
77
+ // return false;
78
+ // }
79
+ if (totalItems > MAX_ITEMS) {
80
+ const extraItems = totalItems - MAX_ITEMS;
81
+ toast(`Remove ${extraItems} ${targetCategory.name} from cart to continue. `);
82
+ return false;
83
+ }
84
+ if (totalItems < MIN_ITEMS) {
85
+ const remainingItems = MIN_ITEMS - totalItems;
86
+ toast(`Add at least ${remainingItems} more ${targetCategory.name} to cart to continue. `);
87
+ return false;
88
+ }
89
+ return true;
90
+ });
91
+ };
92
+ export default maxItemInCategoryPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feedmepos/order-plugin-gallery",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"