@feedmepos/order-plugin-gallery 0.0.10-beta.0 → 0.0.10-beta.1
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.
|
@@ -3,6 +3,12 @@ const formatQty = (qty) => {
|
|
|
3
3
|
return `${qty}`;
|
|
4
4
|
return qty.toFixed(2).replace(/\.?0+$/, "");
|
|
5
5
|
};
|
|
6
|
+
const normalizeLookupKey = (value) => {
|
|
7
|
+
if (typeof value !== "string")
|
|
8
|
+
return null;
|
|
9
|
+
const normalizedValue = value.trim().replace(/\s+/g, " ").toLowerCase();
|
|
10
|
+
return normalizedValue || null;
|
|
11
|
+
};
|
|
6
12
|
const addQuantity = (quantityMap, itemId, quantity) => {
|
|
7
13
|
if (!itemId || typeof itemId !== "string")
|
|
8
14
|
return;
|
|
@@ -12,10 +18,21 @@ const addQuantity = (quantityMap, itemId, quantity) => {
|
|
|
12
18
|
return;
|
|
13
19
|
quantityMap.set(itemId, (quantityMap.get(itemId) || 0) + quantity);
|
|
14
20
|
};
|
|
15
|
-
const
|
|
21
|
+
const getResolvedItemId = (item, fallbackItemIdByName) => {
|
|
22
|
+
const productId = typeof item.productId === "string" ? item.productId.trim() : "";
|
|
23
|
+
if (productId)
|
|
24
|
+
return productId;
|
|
25
|
+
if (!fallbackItemIdByName)
|
|
26
|
+
return null;
|
|
27
|
+
const itemName = normalizeLookupKey(item.name || item.title);
|
|
28
|
+
if (!itemName)
|
|
29
|
+
return null;
|
|
30
|
+
return fallbackItemIdByName.get(itemName) || null;
|
|
31
|
+
};
|
|
32
|
+
const getQuantityMap = (items, fallbackItemIdByName) => {
|
|
16
33
|
const quantityMap = new Map();
|
|
17
34
|
for (const item of items) {
|
|
18
|
-
addQuantity(quantityMap, item
|
|
35
|
+
addQuantity(quantityMap, getResolvedItemId(item, fallbackItemIdByName), item.quantity);
|
|
19
36
|
}
|
|
20
37
|
return quantityMap;
|
|
21
38
|
};
|
|
@@ -63,6 +80,26 @@ const getItemTitleMap = (sdk) => {
|
|
|
63
80
|
}
|
|
64
81
|
return titleMap;
|
|
65
82
|
};
|
|
83
|
+
const getFallbackRuleItemIdByNameMap = (rules, itemTitleMap) => {
|
|
84
|
+
const fallbackItemIdByName = new Map();
|
|
85
|
+
const ambiguousNames = new Set();
|
|
86
|
+
for (const rule of rules) {
|
|
87
|
+
const itemTitle = itemTitleMap.get(rule.itemId);
|
|
88
|
+
const normalizedTitle = normalizeLookupKey(itemTitle);
|
|
89
|
+
if (!normalizedTitle || ambiguousNames.has(normalizedTitle))
|
|
90
|
+
continue;
|
|
91
|
+
const existingItemId = fallbackItemIdByName.get(normalizedTitle);
|
|
92
|
+
if (!existingItemId) {
|
|
93
|
+
fallbackItemIdByName.set(normalizedTitle, rule.itemId);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (existingItemId !== rule.itemId) {
|
|
97
|
+
fallbackItemIdByName.delete(normalizedTitle);
|
|
98
|
+
ambiguousNames.add(normalizedTitle);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return fallbackItemIdByName;
|
|
102
|
+
};
|
|
66
103
|
const compulsoryItemsPlugin = ({ sdk, onBeforeSubmitOrder, pluginParams, }) => {
|
|
67
104
|
onBeforeSubmitOrder(() => {
|
|
68
105
|
const params = (pluginParams || {});
|
|
@@ -72,9 +109,10 @@ const compulsoryItemsPlugin = ({ sdk, onBeforeSubmitOrder, pluginParams, }) => {
|
|
|
72
109
|
const submittedItems = (sdk.orderManager.state.value.slotActiveBills?.[0]?.items ||
|
|
73
110
|
[]);
|
|
74
111
|
const draftItems = (sdk.orderManager.state.value.order?.draft || []);
|
|
75
|
-
const submittedQtyByItemId = getQuantityMap(submittedItems);
|
|
76
|
-
const draftQtyByItemId = getQuantityMap(draftItems);
|
|
77
112
|
const itemTitleMap = getItemTitleMap(sdk);
|
|
113
|
+
const fallbackRuleItemIdByName = getFallbackRuleItemIdByNameMap(rules, itemTitleMap);
|
|
114
|
+
const submittedQtyByItemId = getQuantityMap(submittedItems, fallbackRuleItemIdByName);
|
|
115
|
+
const draftQtyByItemId = getQuantityMap(draftItems);
|
|
78
116
|
const violationMessages = [];
|
|
79
117
|
for (const rule of rules) {
|
|
80
118
|
const submittedQty = submittedQtyByItemId.get(rule.itemId) || 0;
|