@feedmepos/order-plugin-gallery 0.0.8 → 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.
@@ -1,67 +1,85 @@
1
1
  const setMealValidationPlugin = ({ sdk, onBeforeAddToCart, onBeforeSubmitOrder, pluginParams, }) => {
2
- if (!pluginParams?.enabled) {
3
- return;
4
- }
5
- const { setMealItemIds, requiredCategoryNames, errorMessage } = pluginParams;
6
- if (!setMealItemIds?.length || !requiredCategoryNames?.length) {
7
- return;
8
- }
9
- const toast = (msg) => {
10
- sdk?.ui?.toast?.show?.({ title: msg, type: "error", duration: 5000 });
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;
11
23
  };
12
24
  onBeforeAddToCart(async (item) => {
13
- const slotActiveBills = sdk?.orderManager?.state?.value?.slotActiveBills || [];
14
- const orderedItems = slotActiveBills[0]?.items || [];
15
- const hasSetMeal = orderedItems.some((i) => setMealItemIds.includes(i._id) || setMealItemIds.includes(i.name));
16
- if (hasSetMeal)
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');
17
29
  return true;
18
- const rawMenu = sdk?.menuManager?.state?.value?.overridedMenu;
19
- if (!rawMenu?.modules)
20
- return true;
21
- const requiredCatIds = [];
22
- requiredCategoryNames.forEach((name) => {
23
- const cat = rawMenu.modules.category.find?.((c) => c.name === name);
24
- if (cat)
25
- requiredCatIds.push(cat._id);
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
26
38
  });
27
- if (requiredCatIds.length === 0)
39
+ if (!isRestricted)
28
40
  return true;
29
- const requiredIdMap = new Map();
30
- requiredCatIds.forEach((id) => {
31
- rawMenu.modules.item.forEach((item) => {
32
- if (item.category === id)
33
- requiredIdMap.set(item._id, item);
34
- });
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);
35
50
  });
36
- if (requiredIdMap.has(item._id || item.id)) {
37
- toast(errorMessage || '必須先點主菜');
38
- return false;
51
+ if (requiredCategoryItemsFound) {
52
+ console.log('[ItemRestriction Plugin] Required category items already ordered, allowing item');
53
+ return true;
39
54
  }
40
- return true;
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;
41
58
  });
42
59
  onBeforeSubmitOrder(async () => {
43
60
  const slotActiveBills = sdk?.orderManager?.state?.value?.slotActiveBills || [];
44
- const orderedItems = slotActiveBills[0]?.items || [];
45
- const hasSetMeal = orderedItems.some((i) => setMealItemIds.includes(i._id) || setMealItemIds.includes(i.name));
46
- if (hasSetMeal)
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)
47
66
  return true;
48
- const rawMenu = sdk?.menuManager?.state?.value?.overridedMenu;
49
- if (!rawMenu?.modules)
67
+ const menu = getMenu();
68
+ if (!menu)
50
69
  return true;
51
- const requiredCatIds = [];
52
- requiredCategoryNames.forEach((name) => {
53
- const cat = rawMenu.modules.category.find?.((c) => c.name === name);
54
- if (cat)
55
- requiredCatIds.push(cat._id);
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);
56
74
  });
57
- if (requiredCatIds.length === 0)
75
+ if (!hasRestrictedCategoryItems)
58
76
  return true;
59
- const cartItems = sdk?.orderManager?.state?.value?.order?.draft || [];
60
- const requiredItemIds = rawMenu.modules.item
61
- .filter?.((i) => requiredCatIds.includes(i.category))
62
- .map?.((i) => i._id) || [];
63
- if (requiredItemIds.length > 0 && cartItems?.some?.((i) => requiredItemIds.includes(i.productId))) {
64
- toast(errorMessage || '必須先點主菜');
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 });
65
83
  return false;
66
84
  }
67
85
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feedmepos/order-plugin-gallery",
3
- "version": "0.0.8",
3
+ "version": "0.0.9-beta.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"