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