@feedmepos/order-plugin-gallery 0.0.7 → 0.0.9-beta.0
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 setMealValidationPlugin = ({ sdk, onBeforeAddToCart, onBeforeSubmitOrder, pluginParams, }) => {
|
|
2
|
+
console.log('[ItemRestriction Plugin] Loaded (Simplified Version)');
|
|
3
|
+
const REQUIRED_CATEGORY_IDS = pluginParams?.requiredCategoryIds || [];
|
|
4
|
+
const RESTRICTED_CATEGORY_IDS = pluginParams?.restrictedCategoryIds || [];
|
|
5
|
+
const errorMessage = pluginParams?.message || 'Please order items from required category first';
|
|
6
|
+
const getMenu = () => sdk?.menuManager?.state?.value?.overridedMenu;
|
|
7
|
+
const getCategoryIdForItem = (item, menu) => {
|
|
8
|
+
// Trust the enriched item from plugin-loader, or check the normalized menu modules
|
|
9
|
+
if (item.categoryId || item.category) {
|
|
10
|
+
return item.categoryId || item.category;
|
|
11
|
+
}
|
|
12
|
+
// Fallback: Check if the item exists in the normalized modules.item list
|
|
13
|
+
// This handles cases where 'item' is a fresh cart object that hasn't been enriched yet,
|
|
14
|
+
// but the menu has the enriched version.
|
|
15
|
+
if (menu?.modules?.item) {
|
|
16
|
+
const itemProductId = item.productId || item.id || item._id;
|
|
17
|
+
const menuItem = menu.modules.item.find((i) => i._id === itemProductId || i.id === itemProductId);
|
|
18
|
+
if (menuItem) {
|
|
19
|
+
return menuItem.categoryId || menuItem.category || null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
};
|
|
24
|
+
onBeforeAddToCart(async (item) => {
|
|
25
|
+
console.log('[ItemRestriction Plugin] Checking item:', item.title || item.name);
|
|
26
|
+
const menu = getMenu();
|
|
27
|
+
if (!menu) {
|
|
28
|
+
console.log('[ItemRestriction Plugin] No menu found');
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
const itemCategoryId = getCategoryIdForItem(item, menu);
|
|
32
|
+
const isRestricted = itemCategoryId && RESTRICTED_CATEGORY_IDS.includes(itemCategoryId);
|
|
33
|
+
console.log('[ItemRestriction Plugin] Category check:', {
|
|
34
|
+
itemName: item.title || item.name,
|
|
35
|
+
itemId: item.id || item._id,
|
|
36
|
+
resolvedCategoryId: itemCategoryId,
|
|
37
|
+
isRestricted
|
|
38
|
+
});
|
|
39
|
+
if (!isRestricted)
|
|
40
|
+
return true;
|
|
41
|
+
const slotActiveBills = sdk?.orderManager?.state?.value?.slotActiveBills || [];
|
|
42
|
+
const orderedItems = slotActiveBills.flatMap
|
|
43
|
+
? slotActiveBills.flatMap((bill) => bill?.items || [])
|
|
44
|
+
: slotActiveBills.reduce((acc, bill) => acc.concat(bill?.items || []), []);
|
|
45
|
+
const draft = sdk?.orderManager?.state?.value?.order?.draft || [];
|
|
46
|
+
const allItems = [...orderedItems, ...draft];
|
|
47
|
+
const requiredCategoryItemsFound = allItems.some((orderItem) => {
|
|
48
|
+
const catId = getCategoryIdForItem(orderItem, menu);
|
|
49
|
+
return catId && REQUIRED_CATEGORY_IDS.includes(catId);
|
|
50
|
+
});
|
|
51
|
+
if (requiredCategoryItemsFound) {
|
|
52
|
+
console.log('[ItemRestriction Plugin] Required category items already ordered, allowing item');
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
console.log('[ItemRestriction Plugin] Blocking item - no required category items found');
|
|
56
|
+
sdk?.ui?.toast?.show?.({ title: errorMessage, type: "error", duration: 5000 });
|
|
57
|
+
return false;
|
|
58
|
+
});
|
|
59
|
+
onBeforeSubmitOrder(async () => {
|
|
60
|
+
const slotActiveBills = sdk?.orderManager?.state?.value?.slotActiveBills || [];
|
|
61
|
+
const orderedItems = slotActiveBills.flatMap
|
|
62
|
+
? slotActiveBills.flatMap((bill) => bill?.items || [])
|
|
63
|
+
: slotActiveBills.reduce((acc, bill) => acc.concat(bill?.items || []), []);
|
|
64
|
+
const draft = sdk?.orderManager?.state?.value?.order?.draft || [];
|
|
65
|
+
if (orderedItems.length === 0 && draft.length === 0)
|
|
66
|
+
return true;
|
|
67
|
+
const menu = getMenu();
|
|
68
|
+
if (!menu)
|
|
69
|
+
return true;
|
|
70
|
+
const allItems = [...orderedItems, ...draft];
|
|
71
|
+
const hasRestrictedCategoryItems = allItems.some((item) => {
|
|
72
|
+
const catId = getCategoryIdForItem(item, menu);
|
|
73
|
+
return catId && RESTRICTED_CATEGORY_IDS.includes(catId);
|
|
74
|
+
});
|
|
75
|
+
if (!hasRestrictedCategoryItems)
|
|
76
|
+
return true;
|
|
77
|
+
const requiredCategoryItemsFound = allItems.some((item) => {
|
|
78
|
+
const catId = getCategoryIdForItem(item, menu);
|
|
79
|
+
return catId && REQUIRED_CATEGORY_IDS.includes(catId);
|
|
80
|
+
});
|
|
81
|
+
if (!requiredCategoryItemsFound) {
|
|
82
|
+
sdk?.ui?.toast?.show?.({ title: errorMessage, type: "error", duration: 5000 });
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
export default setMealValidationPlugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feedmepos/order-plugin-gallery",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9-beta.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
"import": "./dist/plugins/before-submit-guard.js",
|
|
18
18
|
"types": "./dist/plugins/before-submit-guard.d.ts"
|
|
19
19
|
},
|
|
20
|
+
"./set-meal-validation-customer-v3": {
|
|
21
|
+
"import": "./dist/plugins/set-meal-validation-customer-v3.js",
|
|
22
|
+
"types": "./dist/plugins/set-meal-validation-customer-v3.d.ts"
|
|
23
|
+
},
|
|
20
24
|
"./menu-debug-logger": {
|
|
21
25
|
"import": "./dist/plugins/menu-debug-logger.js",
|
|
22
26
|
"types": "./dist/plugins/menu-debug-logger.d.ts"
|