@darkpos/pricing 1.0.66 → 1.0.68

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,53 +1,94 @@
1
- module.exports = ({ actions, _, itemActions }) => {
2
- const updateRelatedItems = (items, parentId) =>
3
- items.map(each => {
4
- const item = { ...each };
5
- if (item.properties && item.properties.parentId === parentId)
6
- item.properties.includeParent = false;
7
- return item;
8
- });
9
-
10
- return function removeItem({ order, item, checkQuantity, hard = true }) {
11
- if (!item) return order;
12
-
1
+ module.exports = ({ actions, itemActions }) => {
2
+ const remove = ({ order, item, checkQuantity }) => {
13
3
  const orderItemIdx = actions.getItemIndex({ order, item });
14
4
  if (orderItemIdx < 0) return order;
15
5
 
16
- let { items } = order;
17
- const orderItem = items[orderItemIdx];
18
- const relatedItems = actions.getRelatedItems({ order, item });
19
- const hasRelatedItem = !_.isEmpty(relatedItems);
6
+ const items = [...order.items];
20
7
 
21
- if (!hard || hasRelatedItem) {
22
- items[orderItemIdx] = { ...orderItem, price: 0, modifiers: [] };
23
- if (hasRelatedItem) {
24
- items = updateRelatedItems(items, item._id);
25
- }
26
- }
27
- // Decrease the quantity
28
- else if (
8
+ if (
29
9
  checkQuantity &&
30
10
  items[orderItemIdx] &&
31
11
  items[orderItemIdx].quantity > 1
32
- )
33
- items[orderItemIdx] = { ...orderItem, quantity: orderItem.quantity - 1 };
34
- // Remove
35
- else items.splice(orderItemIdx, 1);
36
-
37
- // if it is the last relatedItem
38
- if (
39
- itemActions.isRelatedItem(item) &&
40
- !itemActions.isParentIncluded(item)
41
12
  ) {
42
- const parent = itemActions.getParentItem(items, orderItem);
43
- const parentIndex = actions.getItemIndex({ order, item: parent });
44
- const siblings = actions.getRelatedItems({ order, item: parent });
45
- if (siblings <= 1) items.splice(parentIndex, 1);
13
+ items[orderItemIdx] = { ...item, quantity: item.quantity - 1 };
14
+ return {
15
+ ...order,
16
+ items,
17
+ };
46
18
  }
47
19
 
48
- return {
20
+ items.splice(orderItemIdx, 1);
21
+
22
+ let resultedOrder = {
49
23
  ...order,
50
24
  items,
51
25
  };
26
+
27
+ if (itemActions.isRelatedItem(item) && itemActions.hasAddModifiers(item)) {
28
+ const parentItem = itemActions.getParentItem(order.items, item);
29
+
30
+ if (parentItem) {
31
+ resultedOrder = item.properties.addModifiers.reduce(
32
+ (prevOrder, addModifier) => {
33
+ if (
34
+ !itemActions.hasModifier({
35
+ item: parentItem,
36
+ modifier: addModifier,
37
+ relatedItems: actions.getRelatedItems({
38
+ order: prevOrder,
39
+ item: parentItem,
40
+ }),
41
+ originalModifier: addModifier,
42
+ })
43
+ ) {
44
+ const updatedItem = itemActions.removeModifier({
45
+ item: parentItem,
46
+ modifier: addModifier,
47
+ originalItem: parentItem,
48
+ });
49
+ const itemIndexToUpdate = actions.getItemIndex({
50
+ order: prevOrder,
51
+ item: parentItem,
52
+ });
53
+ if (itemIndexToUpdate >= 0) {
54
+ const itemsToUpdate = [...prevOrder.items];
55
+
56
+ itemsToUpdate.splice(itemIndexToUpdate, 1, updatedItem);
57
+
58
+ return {
59
+ ...resultedOrder,
60
+ items: itemsToUpdate,
61
+ };
62
+ }
63
+ }
64
+ return resultedOrder;
65
+ },
66
+ resultedOrder
67
+ );
68
+ }
69
+ }
70
+
71
+ const relatedItems = actions.getRelatedItems({ order, item });
72
+ if (relatedItems.length > 0) {
73
+ return relatedItems.reduce(
74
+ (prevOrder, relatedItem) =>
75
+ remove({
76
+ order: prevOrder,
77
+ item: relatedItem,
78
+ checkQuantity,
79
+ }),
80
+ resultedOrder
81
+ );
82
+ }
83
+
84
+ return resultedOrder;
85
+ };
86
+
87
+ return function removeItem({ order, item, checkQuantity }) {
88
+ if (!order || !Array.isArray(order.items) || !item) return order;
89
+
90
+ const nextOrder = remove({ order, item, checkQuantity });
91
+
92
+ return nextOrder;
52
93
  };
53
94
  };
@@ -16,6 +16,7 @@ module.exports = ({ modifierActions, itemActions }) =>
16
16
  itemActions.removeModifier({
17
17
  item: each,
18
18
  modifier: modifiers[index],
19
+ order: nextOrder,
19
20
  })
20
21
  );
21
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/pricing",
3
- "version": "1.0.66",
3
+ "version": "1.0.68",
4
4
  "description": "Pricing calculator",
5
5
  "author": "Dark POS",
6
6
  "license": "ISC",
@@ -20,10 +20,12 @@
20
20
  "test:split": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/split.test.js",
21
21
  "test:getModifierTags": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/item/getModifierTags.test.js",
22
22
  "test:createIndirectModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/createIndirectModifier.test.js",
23
+ "test:getInvalidRequiredModifiers": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/getInvalidRequiredModifiers",
23
24
  "test:conditionsNotMet": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/conditionsNotMet.test.js",
24
25
  "test:pickEndDate": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/pickEndDate.test.js",
25
26
  "test:modifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier.test.js",
26
27
  "test:paymentModifiers": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/order-payment-modifier.test.js",
28
+ "test:addItem": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/addItem.test.js",
27
29
  "lint": "eslint --quiet lib/"
28
30
  },
29
31
  "publishConfig": {
@@ -47,5 +49,5 @@
47
49
  "supertest": "^6.2.3",
48
50
  "supervisor": "^0.12.0"
49
51
  },
50
- "gitHead": "d007d8455487fed168f127c6b338dc4301a7da79"
52
+ "gitHead": "ac976fa76195b19e32645c5defe5df41c867609c"
51
53
  }
@@ -1,15 +0,0 @@
1
- module.exports = ({ actions, modifierActions }) =>
2
- function removePaymentModifiers({ items }) {
3
- const paymentModifiers = items.modifiers.filter(mod =>
4
- modifierActions.isPaymentModifier(mod)
5
- );
6
-
7
- return paymentModifiers.map(paymentMod =>
8
- items.map(item =>
9
- actions.removeModifier({
10
- item,
11
- modifier: paymentMod,
12
- })
13
- )
14
- );
15
- };
@@ -1,9 +0,0 @@
1
- module.exports = ({ actions }) =>
2
- function addDiscountModifier({ order, modifier }) {
3
- if (!order || !modifier) return order;
4
- const clearedOrder = actions.removeDiscountModifier(order);
5
- return actions.addModifier({
6
- order: clearedOrder,
7
- modifier,
8
- });
9
- };
@@ -1,16 +0,0 @@
1
- module.exports = ({ modifierActions, itemActions }) =>
2
- function removeDiscountModifiers(order) {
3
- if (!order || !Array.isArray(order.modifiers)) return order;
4
- const nextOrder = { ...order };
5
- let { modifiers, items } = nextOrder;
6
- const discountModifiers = modifiers.filter(modifierActions.isDiscount);
7
- modifiers = modifiers.filter(each => !modifierActions.isDiscount(each));
8
- if (items)
9
- items = items.map(each =>
10
- itemActions.removeModifiers({
11
- item: each,
12
- modifiers: discountModifiers,
13
- })
14
- );
15
- return { ...order, modifiers, items };
16
- };