@feedmepos/order-plugin-gallery 0.0.6 → 0.0.8

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,6 +1,6 @@
1
1
  const maxItemInCategoryPlugin = ({ sdk, onAfterComputeMenuState, onBeforeSubmitOrder, pluginParams, }) => {
2
2
  const MIN_ITEMS = 1;
3
- const MAX_ITEMS = 2;
3
+ const MAX_ITEMS = 1;
4
4
  const categoryLimitConfig = pluginParams?.categoryLimitConfig || {};
5
5
  onBeforeSubmitOrder(() => {
6
6
  if (!categoryLimitConfig.categoryId) {
@@ -48,7 +48,7 @@ const maxItemInCategoryPlugin = ({ sdk, onAfterComputeMenuState, onBeforeSubmitO
48
48
  };
49
49
  const toast = (message) => {
50
50
  sdk.ui.toast.show({
51
- title: `Please order one or two "${targetCategory.name}". ${message}`,
51
+ title: message,
52
52
  type: "error",
53
53
  duration: 5000,
54
54
  });
@@ -60,12 +60,12 @@ const maxItemInCategoryPlugin = ({ sdk, onAfterComputeMenuState, onBeforeSubmitO
60
60
  const totalItems = numberOfTotalItems.submitted + numberOfTotalItems.inCart;
61
61
  if (totalItems > MAX_ITEMS) {
62
62
  const extraItems = totalItems - MAX_ITEMS;
63
- toast(`Remove ${extraItems} ${targetCategory.name} from cart to continue. `);
63
+ const extraLabel = extraItems === 1 ? "item" : "items";
64
+ toast(`You can only choose one ${targetCategory.name}. Please remove extra ${targetCategory.name} from cart to continue.`);
64
65
  return false;
65
66
  }
66
67
  if (totalItems < MIN_ITEMS) {
67
- const remainingItems = MIN_ITEMS - totalItems;
68
- toast(`Add at least ${remainingItems} more ${targetCategory.name} to cart to continue. `);
68
+ toast(`Please add one ${targetCategory.name} to cart to continue.`);
69
69
  return false;
70
70
  }
71
71
  return true;
@@ -0,0 +1,3 @@
1
+ import { PluginFunction } from "@feedmepos/ordering-sdk";
2
+ declare const setMealValidationPlugin: PluginFunction;
3
+ export default setMealValidationPlugin;
@@ -0,0 +1,70 @@
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 });
11
+ };
12
+ 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)
17
+ 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);
26
+ });
27
+ if (requiredCatIds.length === 0)
28
+ 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
+ });
35
+ });
36
+ if (requiredIdMap.has(item._id || item.id)) {
37
+ toast(errorMessage || '必須先點主菜');
38
+ return false;
39
+ }
40
+ return true;
41
+ });
42
+ onBeforeSubmitOrder(async () => {
43
+ 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)
47
+ return true;
48
+ const rawMenu = sdk?.menuManager?.state?.value?.overridedMenu;
49
+ if (!rawMenu?.modules)
50
+ 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);
56
+ });
57
+ if (requiredCatIds.length === 0)
58
+ 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 || '必須先點主菜');
65
+ return false;
66
+ }
67
+ return true;
68
+ });
69
+ };
70
+ export default setMealValidationPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feedmepos/order-plugin-gallery",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
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"