@darkpos/pricing 1.0.44 → 1.0.45
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.
- package/__TEST__/item/getItemsModifierDescription.test.js +75 -85
- package/__TEST__/mocks/scripts/calculate-partially-paid/index.js +2 -2
- package/__TEST__/mocks/scripts/calculate-unpaid/index.js +2 -2
- package/__TEST__/order/manualSplit.test.js +104 -0
- package/__TEST__/order/order-payment-modifier.test.js +1117 -0
- package/__TEST__/order/order.test.js +50 -104
- package/__TEST__/order/pickEndDate.test.js +7 -153
- package/__TEST__/order/validateConditionsCalculate.test.js +42 -43
- package/lib/item/calculate.js +67 -9
- package/lib/item/getAmounts.js +14 -0
- package/lib/item/getBalance.js +0 -1
- package/lib/item/getItemsBalance.js +9 -0
- package/lib/item/getItemsTotals.js +27 -0
- package/lib/item/getTotal.js +3 -1
- package/lib/item/hasPaymentMethodType.js +17 -0
- package/lib/item/hasPaymentModifierWithPaymentId.js +9 -0
- package/lib/item/index.js +15 -8
- package/lib/item/removePaymentModifiersByPaymentId.js +13 -0
- package/lib/modifier/areConditionsMet.js +3 -13
- package/lib/modifier/calculatePaymentDiscountModifier.js +31 -0
- package/lib/modifier/calculatePaymentFeeModifier.js +27 -0
- package/lib/modifier/calculatePaymentModifier.js +67 -0
- package/lib/modifier/createDiscountModifier.js +2 -1
- package/lib/modifier/createFeeModifier.js +2 -1
- package/lib/modifier/getChildren.js +8 -0
- package/lib/modifier/getComputedAmount.js +9 -0
- package/lib/modifier/hasPaymentMethodType.js +14 -0
- package/lib/modifier/index.js +19 -4
- package/lib/modifier/isCalculatedPaymentModifier.js +4 -0
- package/lib/modifier/isChild.js +6 -0
- package/lib/modifier/isDiscount.js +6 -2
- package/lib/modifier/isFee.js +5 -2
- package/lib/modifier/isPaymentMethodModifier.js +4 -1
- package/lib/modifier/isPaymentTypeModifier.js +4 -1
- package/lib/modifier/isPercentage.js +10 -0
- package/lib/modifier/sort.js +28 -0
- package/lib/order/applyPayment.js +61 -0
- package/lib/order/calculate.js +34 -16
- package/lib/order/getBalance.js +0 -1
- package/lib/order/getOrdersBalance.js +8 -0
- package/lib/order/index.js +9 -8
- package/lib/order/manualSplit.js +34 -0
- package/lib/order/manualSplitByQuantity.js +50 -0
- package/lib/store/pickEndDate.js +2 -2
- package/package.json +6 -4
- package/lib/item/getBalanceToPay.js +0 -12
- package/lib/item/getTotals.js +0 -40
- package/lib/item/markModifiersAsLocked.js +0 -13
- package/lib/modifier/createPaymentModifier.js +0 -12
- package/lib/modifier/getLockedModifiers.js +0 -5
- package/lib/order/markModifiersAsLocked.js +0 -14
- package/lib/order/removeModifiersWithPaymentMethods.js +0 -28
- package/lib/order/removeModifiersWithPaymentTypes.js +0 -26
package/lib/modifier/sort.js
CHANGED
|
@@ -6,6 +6,34 @@ module.exports = ({ actions }) => {
|
|
|
6
6
|
const aVal = actions.getProperty(a, key);
|
|
7
7
|
const bVal = actions.getProperty(b, key);
|
|
8
8
|
|
|
9
|
+
const aIsPaymentModifier = actions.isPaymentModifier(a);
|
|
10
|
+
const bIsPaymentModifier = actions.isPaymentModifier(b);
|
|
11
|
+
const aIsCalculatedPaymentModifier =
|
|
12
|
+
actions.isCalculatedPaymentModifier(a);
|
|
13
|
+
const bIsCalculatedPaymentModifier =
|
|
14
|
+
actions.isCalculatedPaymentModifier(b);
|
|
15
|
+
|
|
16
|
+
if (aIsCalculatedPaymentModifier && bIsPaymentModifier && aVal === null) {
|
|
17
|
+
return -1;
|
|
18
|
+
}
|
|
19
|
+
if (bIsCalculatedPaymentModifier && aIsPaymentModifier && bVal === null) {
|
|
20
|
+
return -1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (
|
|
24
|
+
(aIsPaymentModifier || aIsCalculatedPaymentModifier) &&
|
|
25
|
+
aVal === null
|
|
26
|
+
) {
|
|
27
|
+
return 1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
(bIsPaymentModifier || bIsCalculatedPaymentModifier) &&
|
|
32
|
+
bVal === null
|
|
33
|
+
) {
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
9
37
|
if (aVal == null && bVal === null) {
|
|
10
38
|
return 0;
|
|
11
39
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
const options = {
|
|
3
|
+
"5d9f80ae6065e52e04686e03": {
|
|
4
|
+
amount: 10,
|
|
5
|
+
items: {
|
|
6
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
7
|
+
status: { picked: { value: true } },
|
|
8
|
+
total: 0,
|
|
9
|
+
totalPaid: 5,
|
|
10
|
+
},
|
|
11
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
12
|
+
status: { picked: { value: true } },
|
|
13
|
+
total: 0,
|
|
14
|
+
totalPaid: 5,
|
|
15
|
+
},
|
|
16
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
17
|
+
status: { picked: { value: true } },
|
|
18
|
+
total: 0,
|
|
19
|
+
totalPaid: 5,
|
|
20
|
+
},
|
|
21
|
+
"5d9f80dc6065e52e04686e3c": {
|
|
22
|
+
status: { picked: { value: true } },
|
|
23
|
+
total: 0,
|
|
24
|
+
totalPaid: 5,
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
*/
|
|
30
|
+
module.exports = ({ utils, itemActions }) =>
|
|
31
|
+
function applyPayment(options, amountToProcess = 0) {
|
|
32
|
+
const optionsWithAmount = options;
|
|
33
|
+
let amount = amountToProcess;
|
|
34
|
+
|
|
35
|
+
Object.keys(options).forEach(orderId => {
|
|
36
|
+
const order = options[orderId];
|
|
37
|
+
const { items } = order;
|
|
38
|
+
|
|
39
|
+
Object.keys(items).forEach(itemId => {
|
|
40
|
+
const item = items[itemId];
|
|
41
|
+
|
|
42
|
+
const { amountToAssign, remainingAmount, isPaid } =
|
|
43
|
+
itemActions.getAmounts(item, amount);
|
|
44
|
+
amount = remainingAmount;
|
|
45
|
+
|
|
46
|
+
// assign amount to item
|
|
47
|
+
items[itemId] = {
|
|
48
|
+
...item,
|
|
49
|
+
orderId,
|
|
50
|
+
amount: amountToAssign,
|
|
51
|
+
totalPaid: utils.math.add(item.totalPaid, amountToAssign),
|
|
52
|
+
status: {
|
|
53
|
+
...item.status,
|
|
54
|
+
paid: { value: isPaid, date: new Date() },
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return optionsWithAmount;
|
|
61
|
+
};
|
package/lib/order/calculate.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* return calculated Order
|
|
4
4
|
*/
|
|
5
|
-
module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
5
|
+
module.exports = ({ _, actions, itemActions, modifierActions, utils }) =>
|
|
6
6
|
function calculateorder(inputOrder, opts = {}) {
|
|
7
7
|
if (!inputOrder) return inputOrder;
|
|
8
8
|
const order = _.cloneDeep(inputOrder);
|
|
@@ -20,24 +20,29 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
20
20
|
startRequestDate,
|
|
21
21
|
endRequestDate,
|
|
22
22
|
};
|
|
23
|
-
const sortedOrderModifiers = modifierActions.sort(
|
|
24
|
-
modifierActions.removeLocked(order.modifiers)
|
|
25
|
-
);
|
|
23
|
+
const sortedOrderModifiers = modifierActions.sort(order.modifiers || []);
|
|
26
24
|
|
|
27
25
|
let itemsWNIM = items.map(item =>
|
|
28
26
|
itemActions.removeModifiers({ item, modifiers: sortedOrderModifiers })
|
|
29
27
|
);
|
|
30
28
|
|
|
31
|
-
itemsWNIM = itemActions.calculate(itemsWNIM,
|
|
29
|
+
itemsWNIM = itemActions.calculate(itemsWNIM, {
|
|
30
|
+
...options,
|
|
31
|
+
lockPaymentModifiers: !!(
|
|
32
|
+
options.lockPaymentModifiers && !sortedOrderModifiers.length
|
|
33
|
+
),
|
|
34
|
+
});
|
|
32
35
|
|
|
33
36
|
const newOrder = {
|
|
34
37
|
...order,
|
|
35
|
-
...itemActions.
|
|
38
|
+
...itemActions.getItemsTotals(itemsWNIM),
|
|
36
39
|
items: itemsWNIM,
|
|
37
40
|
};
|
|
38
41
|
|
|
39
42
|
if (!sortedOrderModifiers.length) return newOrder;
|
|
40
43
|
|
|
44
|
+
itemsWNIM = itemActions.calculate(itemsWNIM, {});
|
|
45
|
+
|
|
41
46
|
let tempItems = itemsWNIM || items;
|
|
42
47
|
let itemsWIM = _.cloneDeep(tempItems);
|
|
43
48
|
let prvSort = null;
|
|
@@ -65,21 +70,14 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
65
70
|
|
|
66
71
|
if (isCredit) {
|
|
67
72
|
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
68
|
-
computedTotal = itemActions.
|
|
73
|
+
computedTotal = itemActions.getItemsTotals(calculatedItemWIM).total;
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
if (modifierActions.hasItems({ modifier })) {
|
|
72
77
|
tempItems = itemActions.getItems(tempItems, modifier.items);
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
|
|
76
|
-
itemActions.removeModifiers({
|
|
77
|
-
item: tempItem,
|
|
78
|
-
modifiers: modifierActions.getLockedModifiers(tempItem.modifiers),
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
computedTotal = itemActions.getTotals(tempItems).total;
|
|
80
|
+
computedTotal = itemActions.getItemsTotals(tempItems).total;
|
|
83
81
|
|
|
84
82
|
const itemsModifiers = itemActions.addIndirectModifier({
|
|
85
83
|
orderTotal: computedTotal,
|
|
@@ -99,9 +97,29 @@ module.exports = ({ _, actions, itemActions, modifierActions }) =>
|
|
|
99
97
|
|
|
100
98
|
const calculatedItemWIM = itemActions.calculate(itemsWIM, options);
|
|
101
99
|
|
|
100
|
+
if (calculatedItemWIM.length > 1) {
|
|
101
|
+
const { total, subTotalsTotal } = calculatedItemWIM.reduce(
|
|
102
|
+
(acc, item) => ({
|
|
103
|
+
total: utils.math.add(acc.total, item.total),
|
|
104
|
+
subTotalsTotal: utils.math.add(
|
|
105
|
+
acc.subTotalsTotal,
|
|
106
|
+
utils.math.add(item.subTotals._actual, item.subTotals._xincluded)
|
|
107
|
+
),
|
|
108
|
+
}),
|
|
109
|
+
{ total: 0, subTotalsTotal: 0 }
|
|
110
|
+
);
|
|
111
|
+
const difference = utils.math.sub(subTotalsTotal, total);
|
|
112
|
+
|
|
113
|
+
if (difference > 0) {
|
|
114
|
+
calculatedItemWIM[0].total = utils.math.toDecimalPlaces(
|
|
115
|
+
utils.math.add(calculatedItemWIM[0].total, difference)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
102
120
|
return {
|
|
103
121
|
...order,
|
|
104
|
-
...itemActions.
|
|
122
|
+
...itemActions.getItemsTotals(calculatedItemWIM),
|
|
105
123
|
items: calculatedItemWIM,
|
|
106
124
|
};
|
|
107
125
|
};
|
package/lib/order/getBalance.js
CHANGED
package/lib/order/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//
|
|
2
2
|
const getTotalPieces = require('./getTotalPieces');
|
|
3
3
|
const getStartDate = require('./getStartDate');
|
|
4
|
-
const
|
|
4
|
+
const getOrdersBalance = require('./getOrdersBalance');
|
|
5
5
|
const getEndDate = require('./getEndDate');
|
|
6
6
|
const isNew = require('./isNew');
|
|
7
7
|
const getSelectedItem = require('./getSelectedItem');
|
|
@@ -57,8 +57,6 @@ const removeApplyModifier = require('./removeApplyModifier');
|
|
|
57
57
|
const getCustomerSubscriptions = require('./getCustomerSubscriptions');
|
|
58
58
|
const getCustomerSubscriptionsByItem = require('./getCustomerSubscriptionsByItem');
|
|
59
59
|
const hasRemainingSubscription = require('./hasRemainingSubscription');
|
|
60
|
-
const removeModifiersWithPaymentMethods = require('./removeModifiersWithPaymentMethods');
|
|
61
|
-
const removeModifiersWithPaymentTypes = require('./removeModifiersWithPaymentTypes');
|
|
62
60
|
const getItemsToPay = require('./getItemsToPay');
|
|
63
61
|
const hasItem = require('./hasItem');
|
|
64
62
|
const getNumberOfItems = require('./getNumberOfItems');
|
|
@@ -82,11 +80,14 @@ const splitAndCalculate = require('./splitAndCalculate');
|
|
|
82
80
|
const spreadModifiers = require('./spreadModifiers');
|
|
83
81
|
const validateCreditModifiersTotal = require('./validateCreditModifiersTotal');
|
|
84
82
|
const validateFixedModifiersTotal = require('./validateFixedModifiersTotal');
|
|
85
|
-
const markModifiersAsLocked = require('./markModifiersAsLocked');
|
|
86
83
|
const removeModifiers = require('./removeModifiers');
|
|
87
84
|
const addModifiers = require('./addModifiers');
|
|
88
85
|
const createSuborder = require('./createSuborder');
|
|
89
86
|
const getSuborder = require('./getSuborder');
|
|
87
|
+
const manualSplit = require('./manualSplit');
|
|
88
|
+
const manualSplitByQuantity = require('./manualSplitByQuantity');
|
|
89
|
+
const applyPayment = require('./applyPayment');
|
|
90
|
+
const getBalance = require('./getBalance');
|
|
90
91
|
|
|
91
92
|
const orderActions = (deps = {}) => {
|
|
92
93
|
const actions = {};
|
|
@@ -156,9 +157,6 @@ const orderActions = (deps = {}) => {
|
|
|
156
157
|
getCustomerSubscriptions: getCustomerSubscriptions(innerDeps),
|
|
157
158
|
getCustomerSubscriptionsByItem: getCustomerSubscriptionsByItem(innerDeps),
|
|
158
159
|
hasRemainingSubscription: hasRemainingSubscription(innerDeps),
|
|
159
|
-
removeModifiersWithPaymentMethods:
|
|
160
|
-
removeModifiersWithPaymentMethods(innerDeps),
|
|
161
|
-
removeModifiersWithPaymentTypes: removeModifiersWithPaymentTypes(innerDeps),
|
|
162
160
|
hasItem: hasItem(innerDeps),
|
|
163
161
|
getNumberOfItems: getNumberOfItems(innerDeps),
|
|
164
162
|
isOpen: isOpen(innerDeps),
|
|
@@ -181,11 +179,14 @@ const orderActions = (deps = {}) => {
|
|
|
181
179
|
spreadModifiers: spreadModifiers(innerDeps),
|
|
182
180
|
validateCreditModifiersTotal: validateCreditModifiersTotal(innerDeps),
|
|
183
181
|
validateFixedModifiersTotal: validateFixedModifiersTotal(innerDeps),
|
|
184
|
-
markModifiersAsLocked: markModifiersAsLocked(innerDeps),
|
|
185
182
|
removeModifiers: removeModifiers(innerDeps),
|
|
186
183
|
addModifiers: addModifiers(innerDeps),
|
|
187
184
|
createSuborder: createSuborder(innerDeps),
|
|
188
185
|
getSuborder: getSuborder(innerDeps),
|
|
186
|
+
manualSplit: manualSplit(innerDeps),
|
|
187
|
+
manualSplitByQuantity: manualSplitByQuantity(innerDeps),
|
|
188
|
+
applyPayment: applyPayment(innerDeps),
|
|
189
|
+
getOrdersBalance: getOrdersBalance(innerDeps),
|
|
189
190
|
});
|
|
190
191
|
|
|
191
192
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module.exports = ({ actions }) =>
|
|
2
|
+
function manualSplit(
|
|
3
|
+
{ orderIndex, itemIndex },
|
|
4
|
+
destinationIndex,
|
|
5
|
+
currentOrder,
|
|
6
|
+
subOrders,
|
|
7
|
+
itemId,
|
|
8
|
+
splitQuantity
|
|
9
|
+
) {
|
|
10
|
+
let _subOrders = subOrders;
|
|
11
|
+
|
|
12
|
+
if (
|
|
13
|
+
orderIndex == null ||
|
|
14
|
+
itemIndex == null ||
|
|
15
|
+
subOrders == null ||
|
|
16
|
+
destinationIndex == null
|
|
17
|
+
)
|
|
18
|
+
return _subOrders;
|
|
19
|
+
if (!subOrders[destinationIndex] || !subOrders[orderIndex])
|
|
20
|
+
return _subOrders;
|
|
21
|
+
if (splitQuantity) {
|
|
22
|
+
_subOrders = actions.manualSplitByQuantity(
|
|
23
|
+
{ orderIndex, itemIndex },
|
|
24
|
+
destinationIndex,
|
|
25
|
+
currentOrder,
|
|
26
|
+
subOrders,
|
|
27
|
+
itemId
|
|
28
|
+
);
|
|
29
|
+
} else {
|
|
30
|
+
const item = _subOrders[orderIndex].items.splice(itemIndex, 1)[0];
|
|
31
|
+
if (item) _subOrders[destinationIndex].items.unshift(item);
|
|
32
|
+
}
|
|
33
|
+
return _subOrders.map(subOrder => actions.calculate(subOrder));
|
|
34
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module.exports = () =>
|
|
2
|
+
function splitByQuantity(
|
|
3
|
+
{ orderIndex, itemIndex },
|
|
4
|
+
destinationIndex,
|
|
5
|
+
currentOrder,
|
|
6
|
+
subOrders,
|
|
7
|
+
itemId
|
|
8
|
+
) {
|
|
9
|
+
const _subOrders = subOrders;
|
|
10
|
+
const currentItem = currentOrder.items.find(item => item._id === itemId);
|
|
11
|
+
const currentSuborder = subOrders[Number(orderIndex)];
|
|
12
|
+
const currentSuborderItem = (currentSuborder.items || []).find(
|
|
13
|
+
item => item._id === itemId
|
|
14
|
+
);
|
|
15
|
+
const destinationSuborder = subOrders[Number(destinationIndex)];
|
|
16
|
+
let destinationItem = (destinationSuborder.items || []).find(
|
|
17
|
+
item => item._id === itemId
|
|
18
|
+
);
|
|
19
|
+
if (orderIndex !== destinationIndex) {
|
|
20
|
+
for (let i = 0; i <= subOrders.length; i += 1) {
|
|
21
|
+
if (i === orderIndex) {
|
|
22
|
+
if (currentSuborderItem && currentSuborder && currentSuborder.items) {
|
|
23
|
+
// react breaks if I don't create a clone to reassign the quantity
|
|
24
|
+
const _currentSuborderItem = { ...currentSuborderItem };
|
|
25
|
+
_currentSuborderItem.quantity =
|
|
26
|
+
Number(_currentSuborderItem.quantity) - 1;
|
|
27
|
+
currentSuborder.items[Number(itemIndex)] = _currentSuborderItem;
|
|
28
|
+
if (Number(_currentSuborderItem.quantity) <= 0) {
|
|
29
|
+
currentSuborder.items.splice(Number(itemIndex), 1);
|
|
30
|
+
}
|
|
31
|
+
_subOrders[Number(orderIndex)] = currentSuborder;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (i === destinationIndex) {
|
|
36
|
+
// case 1: if the suborder has no items
|
|
37
|
+
if (!destinationItem) {
|
|
38
|
+
destinationItem = { ...currentItem, quantity: 1 };
|
|
39
|
+
destinationSuborder.items =
|
|
40
|
+
destinationSuborder.items.concat(destinationItem);
|
|
41
|
+
} else {
|
|
42
|
+
// case 2: if the suborder has items
|
|
43
|
+
destinationItem.quantity = Number(destinationItem.quantity) + 1;
|
|
44
|
+
}
|
|
45
|
+
_subOrders[Number(destinationIndex)] = destinationSuborder;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return _subOrders;
|
|
50
|
+
};
|
package/lib/store/pickEndDate.js
CHANGED
|
@@ -46,8 +46,6 @@ module.exports = ({ settings, _, moment }) => {
|
|
|
46
46
|
/**
|
|
47
47
|
* setting the hour and minute so we can compare with closedDays
|
|
48
48
|
*/
|
|
49
|
-
if (hour !== undefined) endDateTZ.set('hour', hour);
|
|
50
|
-
if (minute !== undefined) endDateTZ.set('minute', minute);
|
|
51
49
|
closedDays.forEach(closed => {
|
|
52
50
|
const closedDay = moment(closed.date);
|
|
53
51
|
closedDay.set('hour', hour);
|
|
@@ -56,6 +54,8 @@ module.exports = ({ settings, _, moment }) => {
|
|
|
56
54
|
endDateTZ.add(1, 'days');
|
|
57
55
|
}
|
|
58
56
|
});
|
|
57
|
+
if (hour !== undefined) endDateTZ.set('hour', hour);
|
|
58
|
+
if (minute !== undefined) endDateTZ.set('minute', minute);
|
|
59
59
|
|
|
60
60
|
return moment.utc(endDateTZ).format();
|
|
61
61
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.45",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -12,8 +12,10 @@
|
|
|
12
12
|
"main": "index.js",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"test": "jest --runInBand --detectOpenHandles --logHeapUsage --verbose --forceExit ./__TEST__",
|
|
15
|
-
"test:me": "jest --runInBand --detectOpenHandles --logHeapUsage
|
|
16
|
-
"
|
|
15
|
+
"test:me": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/order-payment-modifier.test.js",
|
|
16
|
+
"test:order": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/order.test.js",
|
|
17
|
+
"test:validateConditions": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/validateConditionsCalculate.test.js",
|
|
18
|
+
"lint": "eslint --quiet lib/"
|
|
17
19
|
},
|
|
18
20
|
"publishConfig": {
|
|
19
21
|
"access": "public"
|
|
@@ -36,5 +38,5 @@
|
|
|
36
38
|
"supertest": "^6.2.3",
|
|
37
39
|
"supervisor": "^0.12.0"
|
|
38
40
|
},
|
|
39
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "3bfaa3655ef26d0dc0fb60bdc40caee1e3693ccb"
|
|
40
42
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module.exports = ({ utils, actions }) =>
|
|
2
|
-
function getBalanceToPay({ orderItems = [] }) {
|
|
3
|
-
const total = orderItems.reduce(
|
|
4
|
-
(t, item) => utils.math.add(actions.getTotal(item), t),
|
|
5
|
-
0
|
|
6
|
-
);
|
|
7
|
-
const totalPaid = orderItems.reduce(
|
|
8
|
-
(t, item) => utils.math.add(t, item && item.totalPaid),
|
|
9
|
-
0
|
|
10
|
-
);
|
|
11
|
-
return utils.math.toDecimalPlaces(utils.math.sub(total, totalPaid));
|
|
12
|
-
};
|
package/lib/item/getTotals.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-syntax */
|
|
2
|
-
|
|
3
|
-
module.exports = ({ utils }) => {
|
|
4
|
-
const { math } = utils;
|
|
5
|
-
return function getTotals(items) {
|
|
6
|
-
let subTotals = {};
|
|
7
|
-
let subTotal = 0;
|
|
8
|
-
|
|
9
|
-
for (const item of items) {
|
|
10
|
-
subTotal = math.add(subTotal, item.subTotals._actual);
|
|
11
|
-
const keys = Object.keys(item.subTotals).filter(
|
|
12
|
-
key => !key.includes('_')
|
|
13
|
-
);
|
|
14
|
-
for (const key of keys) {
|
|
15
|
-
subTotals[key] = math.add(subTotals[key] || 0, item.subTotals[key]);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const total = math.toDecimalPlaces(
|
|
20
|
-
Object.keys(subTotals).reduce(
|
|
21
|
-
(acc, key) => math.add(acc, subTotals[key]),
|
|
22
|
-
subTotal
|
|
23
|
-
)
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
subTotals = Object.keys(subTotals).reduce(
|
|
27
|
-
(acc, key) => ({
|
|
28
|
-
...acc,
|
|
29
|
-
[key]: math.toDecimalPlaces(subTotals[key]),
|
|
30
|
-
}),
|
|
31
|
-
{}
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
subTotals,
|
|
36
|
-
subTotal,
|
|
37
|
-
total,
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
module.exports = () =>
|
|
2
|
-
function markModifiersAsLocked({ item, modifierIds }) {
|
|
3
|
-
if (!item || !Array.isArray(item.modifiers)) return {};
|
|
4
|
-
|
|
5
|
-
return {
|
|
6
|
-
...item,
|
|
7
|
-
modifiers: item.modifiers.map(mod =>
|
|
8
|
-
modifierIds.includes(mod._parentId || mod._id)
|
|
9
|
-
? { ...mod, locked: true }
|
|
10
|
-
: mod
|
|
11
|
-
),
|
|
12
|
-
};
|
|
13
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
module.exports = ({ itemActions }) =>
|
|
2
|
-
function markModifiersAsLocked({ order, modifierIds }) {
|
|
3
|
-
if (!order || !Array.isArray(order.modifiers)) return {};
|
|
4
|
-
|
|
5
|
-
return {
|
|
6
|
-
...order,
|
|
7
|
-
modifiers: order.modifiers.map(mod =>
|
|
8
|
-
modifierIds.includes(mod._id) ? { ...mod, locked: true } : mod
|
|
9
|
-
),
|
|
10
|
-
items: (order.items || []).map(item =>
|
|
11
|
-
itemActions.markModifiersAsLocked({ item, modifierIds })
|
|
12
|
-
),
|
|
13
|
-
};
|
|
14
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
module.exports = () => {
|
|
2
|
-
const hasPaymentMethods = modifier =>
|
|
3
|
-
modifier.conditions &&
|
|
4
|
-
modifier.conditions.some(condition => condition.key === 'paymentMethods');
|
|
5
|
-
|
|
6
|
-
return function removeModifiersWithPaymentMethods({
|
|
7
|
-
order,
|
|
8
|
-
applyItems = false,
|
|
9
|
-
}) {
|
|
10
|
-
if (!order || !Array.isArray(order.modifiers)) return order;
|
|
11
|
-
|
|
12
|
-
const newOrder = { ...order };
|
|
13
|
-
newOrder.modifiers = order.modifiers.filter(
|
|
14
|
-
each => !hasPaymentMethods(each)
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
if (applyItems && Array.isArray(order.items)) {
|
|
18
|
-
newOrder.items = order.items.map(item => {
|
|
19
|
-
if (!Array.isArray(item.modifiers)) return item;
|
|
20
|
-
return {
|
|
21
|
-
...item,
|
|
22
|
-
modifiers: item.modifiers.filter(each => !hasPaymentMethods(each)),
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
return newOrder;
|
|
27
|
-
};
|
|
28
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
module.exports = () => {
|
|
2
|
-
const hasPaymentTypes = modifier =>
|
|
3
|
-
modifier.conditions &&
|
|
4
|
-
modifier.conditions.some(condition => condition.key === 'paymentMethods');
|
|
5
|
-
|
|
6
|
-
return function removeModifiersWithPaymentTypes({
|
|
7
|
-
order,
|
|
8
|
-
applyItems = false,
|
|
9
|
-
}) {
|
|
10
|
-
if (!order || !Array.isArray(order.modifiers)) return order;
|
|
11
|
-
|
|
12
|
-
const newOrder = { ...order };
|
|
13
|
-
newOrder.modifiers = order.modifiers.filter(each => !hasPaymentTypes(each));
|
|
14
|
-
|
|
15
|
-
if (applyItems && Array.isArray(order.items)) {
|
|
16
|
-
newOrder.items = order.items.map(item => {
|
|
17
|
-
if (!Array.isArray(item.modifiers)) return item;
|
|
18
|
-
return {
|
|
19
|
-
...item,
|
|
20
|
-
modifiers: item.modifiers.filter(each => !hasPaymentTypes(each)),
|
|
21
|
-
};
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
return newOrder;
|
|
25
|
-
};
|
|
26
|
-
};
|