@feedmepos/order-plugin-gallery 0.0.7 → 0.0.9

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 setMealValidationPlugin: PluginFunction;
3
+ export default setMealValidationPlugin;
@@ -0,0 +1,103 @@
1
+ const setMealValidationPlugin = ({ sdk, onBeforeAddToCart, onBeforeSubmitOrder, pluginParams, }) => {
2
+ console.log('[ItemRestriction Plugin] Loaded');
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
+ if (!menu?.modules?.item)
9
+ return null;
10
+ // Try direct categoryId first
11
+ if (item.categoryId)
12
+ return item.categoryId;
13
+ // Also check category property which exists in some models
14
+ if (item.category && typeof item.category === 'string')
15
+ return item.category;
16
+ // Look up by productId (in cart) or id/_id (in menu/item)
17
+ const itemProductId = item.productId || item.id || item._id;
18
+ if (!itemProductId)
19
+ return null;
20
+ // Find item in menu modules
21
+ const menuItem = menu.modules.item.find((i) => i._id === itemProductId || i.id === itemProductId);
22
+ return menuItem?.category || null;
23
+ };
24
+ onBeforeAddToCart(async (item) => {
25
+ console.log('[ItemRestriction Plugin] Checking item:', item.title || item.name);
26
+ console.log('[ItemRestriction Plugin] Item ID:', item.id || item._id);
27
+ const menu = getMenu();
28
+ if (!menu) {
29
+ console.log('[ItemRestriction Plugin] No menu found');
30
+ return true;
31
+ }
32
+ const itemCategoryId = getCategoryIdForItem(item, menu);
33
+ const isRestricted = itemCategoryId && RESTRICTED_CATEGORY_IDS.includes(itemCategoryId);
34
+ if (!isRestricted) {
35
+ console.log('[ItemRestriction Plugin] Item not in restricted category, allowing');
36
+ return true;
37
+ }
38
+ const slotActiveBills = sdk?.orderManager?.state?.value?.slotActiveBills || [];
39
+ // Use flatMap if available, or fallback to reduce/concat
40
+ const orderedItems = slotActiveBills.flatMap
41
+ ? slotActiveBills.flatMap((bill) => bill?.items || [])
42
+ : slotActiveBills.reduce((acc, bill) => acc.concat(bill?.items || []), []);
43
+ const draft = sdk?.orderManager?.state?.value?.order?.draft || [];
44
+ console.log('[ItemRestriction Plugin] Checking order state:', {
45
+ draftCount: draft.length,
46
+ billsCount: slotActiveBills.length,
47
+ orderedItemsCount: orderedItems.length,
48
+ totalItems: draft.length + orderedItems.length
49
+ });
50
+ const allItems = [...orderedItems, ...draft];
51
+ const requiredCategoryItemsFound = allItems.some((orderItem) => {
52
+ const catId = getCategoryIdForItem(orderItem, menu);
53
+ console.log('[ItemRestriction Plugin] Item check:', {
54
+ productId: orderItem.productId || orderItem.id || orderItem._id,
55
+ foundCategoryId: catId,
56
+ isRequired: catId && REQUIRED_CATEGORY_IDS.includes(catId)
57
+ });
58
+ return catId && REQUIRED_CATEGORY_IDS.includes(catId);
59
+ });
60
+ console.log('[ItemRestriction Plugin] Required category items found?', requiredCategoryItemsFound);
61
+ if (requiredCategoryItemsFound) {
62
+ console.log('[ItemRestriction Plugin] Required category items already ordered, allowing item');
63
+ return true;
64
+ }
65
+ console.log('[ItemRestriction Plugin] Blocking item - no required category items found');
66
+ sdk?.ui?.toast?.show?.({ title: errorMessage, type: "error", duration: 5000 });
67
+ return false;
68
+ });
69
+ onBeforeSubmitOrder(async () => {
70
+ console.log('[ItemRestriction Plugin] onBeforeSubmitOrder called');
71
+ const slotActiveBills = sdk?.orderManager?.state?.value?.slotActiveBills || [];
72
+ const orderedItems = slotActiveBills.flatMap
73
+ ? slotActiveBills.flatMap((bill) => bill?.items || [])
74
+ : slotActiveBills.reduce((acc, bill) => acc.concat(bill?.items || []), []);
75
+ const draft = sdk?.orderManager?.state?.value?.order?.draft || [];
76
+ console.log('[ItemRestriction Plugin] Checking order with', orderedItems.length + draft.length, 'items');
77
+ if (orderedItems.length === 0 && draft.length === 0)
78
+ return true;
79
+ const menu = getMenu();
80
+ if (!menu)
81
+ return true;
82
+ const allItems = [...orderedItems, ...draft];
83
+ const hasRestrictedCategoryItems = allItems.some((item) => {
84
+ const catId = getCategoryIdForItem(item, menu);
85
+ return catId && RESTRICTED_CATEGORY_IDS.includes(catId);
86
+ });
87
+ console.log('[ItemRestriction Plugin] Has restricted category items in order?', hasRestrictedCategoryItems);
88
+ if (!hasRestrictedCategoryItems)
89
+ return true;
90
+ const requiredCategoryItemsFound = allItems.some((item) => {
91
+ const catId = getCategoryIdForItem(item, menu);
92
+ return catId && REQUIRED_CATEGORY_IDS.includes(catId);
93
+ });
94
+ console.log('[ItemRestriction Plugin] Required category items found?', requiredCategoryItemsFound);
95
+ if (!requiredCategoryItemsFound) {
96
+ console.log('[ItemRestriction Plugin] No required category items in order, blocking checkout');
97
+ sdk?.ui?.toast?.show?.({ title: errorMessage, type: "error", duration: 5000 });
98
+ return false;
99
+ }
100
+ return true;
101
+ });
102
+ };
103
+ export default setMealValidationPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feedmepos/order-plugin-gallery",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
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"