@darkpos/pricing 1.0.108 → 1.0.110
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.test.js +142 -0
- package/__TEST__/payment.test.js +51 -0
- package/lib/index.js +10 -0
- package/lib/item/calculate.js +3 -2
- package/lib/payment/getMaxAmountToRefund.js +13 -0
- package/lib/payment/index.js +23 -0
- package/package.json +3 -2
package/__TEST__/item.test.js
CHANGED
|
@@ -1689,4 +1689,146 @@ describe('Item actions', () => {
|
|
|
1689
1689
|
description: '$7.59 discount (-$7.59)',
|
|
1690
1690
|
});
|
|
1691
1691
|
});
|
|
1692
|
+
|
|
1693
|
+
test('Should use a price override modifier and quantity override modifier in combination accordingly', () => {
|
|
1694
|
+
const overridePriceMod = {
|
|
1695
|
+
_id: '675354ee39a47228afd1f1a4',
|
|
1696
|
+
attributes: ['override'],
|
|
1697
|
+
modifierId: '6751f7eeb60c71cefee138ee',
|
|
1698
|
+
name: 'Override item price fixed 0',
|
|
1699
|
+
tags: ['default', 'all'],
|
|
1700
|
+
direct: true,
|
|
1701
|
+
properties: {
|
|
1702
|
+
override: {
|
|
1703
|
+
type: 'fixed',
|
|
1704
|
+
field: 'price',
|
|
1705
|
+
fixedValue: 0,
|
|
1706
|
+
multiplier: false,
|
|
1707
|
+
},
|
|
1708
|
+
},
|
|
1709
|
+
};
|
|
1710
|
+
const overrideQuantityMod = {
|
|
1711
|
+
_id: '675354ee39a47228afd1f1a5',
|
|
1712
|
+
attributes: ['override'],
|
|
1713
|
+
modifierId: '6751f7eeb60c71cefee138ff',
|
|
1714
|
+
name: 'Override item quantity fixed 20',
|
|
1715
|
+
tags: ['default', 'all'],
|
|
1716
|
+
direct: true,
|
|
1717
|
+
properties: {
|
|
1718
|
+
override: {
|
|
1719
|
+
type: 'fixed',
|
|
1720
|
+
field: 'quantity',
|
|
1721
|
+
fixedValue: 20,
|
|
1722
|
+
multiplier: false,
|
|
1723
|
+
},
|
|
1724
|
+
},
|
|
1725
|
+
};
|
|
1726
|
+
|
|
1727
|
+
const itemWithModifierAmountFixed10 = {
|
|
1728
|
+
name: "Men's 3pc Tuxedo",
|
|
1729
|
+
pieces: 1,
|
|
1730
|
+
price: 10,
|
|
1731
|
+
quantity: 1,
|
|
1732
|
+
modifiers: [overridePriceMod, overrideQuantityMod],
|
|
1733
|
+
_id: '675354e939a47228afd1f199',
|
|
1734
|
+
itemId: '62cdbfd01ee1b400193281ee',
|
|
1735
|
+
};
|
|
1736
|
+
const newItem = pricingService.item.calculate(
|
|
1737
|
+
itemWithModifierAmountFixed10
|
|
1738
|
+
);
|
|
1739
|
+
expect(newItem.price).toBe(0);
|
|
1740
|
+
expect(newItem.total).toBe(0);
|
|
1741
|
+
expect(newItem.quantity).toBe(20);
|
|
1742
|
+
expect(newItem).toHaveProperty('price', 0);
|
|
1743
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
1744
|
+
amount: 0,
|
|
1745
|
+
description: 'Override item price fixed 0 ($0.00/Unit)',
|
|
1746
|
+
});
|
|
1747
|
+
|
|
1748
|
+
expect(newItem.modifiers[1]).toHaveProperty('_computed', {
|
|
1749
|
+
amount: 0,
|
|
1750
|
+
description: 'Override item quantity fixed 20',
|
|
1751
|
+
});
|
|
1752
|
+
|
|
1753
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
1754
|
+
_included: 0,
|
|
1755
|
+
_xincluded: 0,
|
|
1756
|
+
_direct: 0,
|
|
1757
|
+
_xdirect: 0,
|
|
1758
|
+
_simple: 0,
|
|
1759
|
+
_actual: 0,
|
|
1760
|
+
});
|
|
1761
|
+
});
|
|
1762
|
+
|
|
1763
|
+
test('Should use a total override modifier and quantity override modifier in combination accordingly', () => {
|
|
1764
|
+
const overrideTotalMod = {
|
|
1765
|
+
_id: '675354ee39a47228afd1f1a4',
|
|
1766
|
+
attributes: ['override'],
|
|
1767
|
+
modifierId: '6751f7eeb60c71cefee138ee',
|
|
1768
|
+
name: 'Override item total fixed 0',
|
|
1769
|
+
tags: ['default', 'all'],
|
|
1770
|
+
direct: true,
|
|
1771
|
+
properties: {
|
|
1772
|
+
override: {
|
|
1773
|
+
type: 'fixed',
|
|
1774
|
+
field: 'total',
|
|
1775
|
+
fixedValue: 0,
|
|
1776
|
+
multiplier: false,
|
|
1777
|
+
},
|
|
1778
|
+
sort: 2,
|
|
1779
|
+
},
|
|
1780
|
+
};
|
|
1781
|
+
const overrideQuantityMod = {
|
|
1782
|
+
_id: '675354ee39a47228afd1f1a5',
|
|
1783
|
+
attributes: ['override'],
|
|
1784
|
+
modifierId: '6751f7eeb60c71cefee138ff',
|
|
1785
|
+
name: 'Override item quantity fixed 20',
|
|
1786
|
+
tags: ['default', 'all'],
|
|
1787
|
+
direct: true,
|
|
1788
|
+
properties: {
|
|
1789
|
+
override: {
|
|
1790
|
+
type: 'fixed',
|
|
1791
|
+
field: 'quantity',
|
|
1792
|
+
fixedValue: 20,
|
|
1793
|
+
multiplier: false,
|
|
1794
|
+
},
|
|
1795
|
+
sort: 1,
|
|
1796
|
+
},
|
|
1797
|
+
};
|
|
1798
|
+
|
|
1799
|
+
const itemWithModifierAmountFixed10 = {
|
|
1800
|
+
name: "Men's 3pc Tuxedo",
|
|
1801
|
+
pieces: 1,
|
|
1802
|
+
price: 10,
|
|
1803
|
+
quantity: 1,
|
|
1804
|
+
modifiers: [overrideTotalMod, overrideQuantityMod],
|
|
1805
|
+
_id: '675354e939a47228afd1f199',
|
|
1806
|
+
itemId: '62cdbfd01ee1b400193281ee',
|
|
1807
|
+
};
|
|
1808
|
+
const newItem = pricingService.item.calculate(
|
|
1809
|
+
itemWithModifierAmountFixed10
|
|
1810
|
+
);
|
|
1811
|
+
expect(newItem.price).toBe(10);
|
|
1812
|
+
expect(newItem.total).toBe(0);
|
|
1813
|
+
expect(newItem.quantity).toBe(20);
|
|
1814
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
1815
|
+
amount: 0,
|
|
1816
|
+
description: 'Override item quantity fixed 20',
|
|
1817
|
+
});
|
|
1818
|
+
|
|
1819
|
+
expect(newItem.modifiers[1]).toHaveProperty('_computed', {
|
|
1820
|
+
amount: -200,
|
|
1821
|
+
description: 'Override item total fixed 0 (-$200.00)',
|
|
1822
|
+
});
|
|
1823
|
+
|
|
1824
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
1825
|
+
_included: 0,
|
|
1826
|
+
_xincluded: -200,
|
|
1827
|
+
undefined: -200,
|
|
1828
|
+
_direct: -200,
|
|
1829
|
+
_xdirect: 0,
|
|
1830
|
+
_simple: 200,
|
|
1831
|
+
_actual: 200,
|
|
1832
|
+
});
|
|
1833
|
+
});
|
|
1692
1834
|
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const usePricing = require('../index');
|
|
2
|
+
const mockStores = require('./mocks/stores');
|
|
3
|
+
|
|
4
|
+
const session = {
|
|
5
|
+
store: mockStores[0],
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const pricingService = usePricing(session);
|
|
9
|
+
|
|
10
|
+
describe('getMaxAmountToRefund tests', () => {
|
|
11
|
+
test('Returns 0 when payment is undefined', () => {
|
|
12
|
+
expect(
|
|
13
|
+
pricingService.payment.getMaxAmountToRefund({ payment: undefined })
|
|
14
|
+
).toBe(0);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('Returns full payment amount when no deductions', () => {
|
|
18
|
+
const payment = { amount: 100 };
|
|
19
|
+
expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(100);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('Subtracts credit only', () => {
|
|
23
|
+
const payment = { amount: 100, credit: 20 };
|
|
24
|
+
expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(80);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('Subtracts change only', () => {
|
|
28
|
+
const payment = { amount: 100, change: 10 };
|
|
29
|
+
expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(90);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('Subtracts refundedAmount only', () => {
|
|
33
|
+
const payment = { amount: 100, refundedAmount: 25 };
|
|
34
|
+
expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(75);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('Subtracts credit, change, and refundedAmount', () => {
|
|
38
|
+
const payment = {
|
|
39
|
+
amount: 100,
|
|
40
|
+
credit: 10,
|
|
41
|
+
change: 5,
|
|
42
|
+
refundedAmount: 20,
|
|
43
|
+
};
|
|
44
|
+
expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(65);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('Handles all fields missing gracefully', () => {
|
|
48
|
+
const payment = { amount: 50 };
|
|
49
|
+
expect(pricingService.payment.getMaxAmountToRefund({ payment })).toBe(50);
|
|
50
|
+
});
|
|
51
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -8,6 +8,8 @@ const makeStoreActions = require('./store');
|
|
|
8
8
|
const makeItemActions = require('./item');
|
|
9
9
|
const makeOrderActions = require('./order');
|
|
10
10
|
const makeModifierActions = require('./modifier');
|
|
11
|
+
const makePaymentActions = require('./payment');
|
|
12
|
+
|
|
11
13
|
const constants = require('./constants');
|
|
12
14
|
|
|
13
15
|
module.exports = session => {
|
|
@@ -40,12 +42,20 @@ module.exports = session => {
|
|
|
40
42
|
modifierActions,
|
|
41
43
|
storeActions,
|
|
42
44
|
});
|
|
45
|
+
const paymentActions = makePaymentActions({
|
|
46
|
+
...deps,
|
|
47
|
+
itemActions,
|
|
48
|
+
modifierActions,
|
|
49
|
+
orderActions,
|
|
50
|
+
storeActions,
|
|
51
|
+
});
|
|
43
52
|
|
|
44
53
|
return {
|
|
45
54
|
item: itemActions,
|
|
46
55
|
order: orderActions,
|
|
47
56
|
modifier: modifierActions,
|
|
48
57
|
store: storeActions,
|
|
58
|
+
payment: paymentActions,
|
|
49
59
|
constants,
|
|
50
60
|
};
|
|
51
61
|
};
|
package/lib/item/calculate.js
CHANGED
|
@@ -99,7 +99,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
99
99
|
);
|
|
100
100
|
});
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
let price = parseFloat(
|
|
103
103
|
actions.getPriceWithoutModifiers({
|
|
104
104
|
price: item.price,
|
|
105
105
|
modifiers: validatedModifiers,
|
|
@@ -174,6 +174,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
174
174
|
overwrittenPrice: item.price,
|
|
175
175
|
};
|
|
176
176
|
item.price = priceOverride;
|
|
177
|
+
price = priceOverride;
|
|
177
178
|
}
|
|
178
179
|
|
|
179
180
|
if (modifierActions.isQuantityOverride(modifier)) {
|
|
@@ -182,7 +183,7 @@ module.exports = ({ _, utils, actions, modifierActions }) => {
|
|
|
182
183
|
...(item.properties ? item.properties : {}),
|
|
183
184
|
overwrittenQuantity: item.quantity,
|
|
184
185
|
};
|
|
185
|
-
subTotals._simple = math.mul(price, newQuantity);
|
|
186
|
+
subTotals._simple = math.mul(item.price, newQuantity);
|
|
186
187
|
item.quantity = parseInt(newQuantity, 10);
|
|
187
188
|
quantity = parseInt(newQuantity, 10);
|
|
188
189
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ utils }) => {
|
|
2
|
+
const { math } = utils;
|
|
3
|
+
|
|
4
|
+
return function getMaxAmountToRefund({ payment }) {
|
|
5
|
+
if (!payment) return 0;
|
|
6
|
+
return math.sub(
|
|
7
|
+
payment.amount,
|
|
8
|
+
payment.credit || 0,
|
|
9
|
+
payment.change || 0,
|
|
10
|
+
payment.refundedAmount || 0
|
|
11
|
+
);
|
|
12
|
+
};
|
|
13
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//
|
|
2
|
+
const getMaxAmountToRefund = require('./getMaxAmountToRefund');
|
|
3
|
+
|
|
4
|
+
const orderActions = (deps = {}) => {
|
|
5
|
+
const actions = {};
|
|
6
|
+
|
|
7
|
+
const innerDeps = {
|
|
8
|
+
...deps,
|
|
9
|
+
actions,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const freezedActions = Object.freeze({
|
|
13
|
+
getMaxAmountToRefund: getMaxAmountToRefund(innerDeps),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
Object.keys(freezedActions).forEach(actionName => {
|
|
17
|
+
actions[actionName] = freezedActions[actionName];
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return freezedActions;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
module.exports = orderActions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.110",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"test:getParentTotals": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/order/getParentTotals.test.js",
|
|
31
31
|
"test:overrideModifiers": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/overrideModifiers.test.js",
|
|
32
32
|
"test:addItemModifier": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/modifier/addItemModifier.test.js",
|
|
33
|
+
"test:payment": "jest --runInBand --detectOpenHandles --logHeapUsage --forceExit ./__TEST__/payment.test.js",
|
|
33
34
|
"lint": "eslint --quiet lib/"
|
|
34
35
|
},
|
|
35
36
|
"publishConfig": {
|
|
@@ -54,5 +55,5 @@
|
|
|
54
55
|
"supertest": "^6.2.3",
|
|
55
56
|
"supervisor": "^0.12.0"
|
|
56
57
|
},
|
|
57
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "3e47ebde1cb164b785b001a37853316ec3c8e352"
|
|
58
59
|
}
|