@darkpos/pricing 1.0.112 → 1.0.114
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 +131 -0
- package/lib/item/calculate.js +1 -0
- package/lib/item/validateModifiers.js +9 -4
- package/lib/modifier/isAvailableSubscription.js +3 -3
- package/lib/order/getOrderItemSubscriptions.js +4 -4
- package/lib/order/hasRemainingSubscription.js +10 -12
- package/package.json +2 -2
package/__TEST__/item.test.js
CHANGED
|
@@ -1831,4 +1831,135 @@ describe('Item actions', () => {
|
|
|
1831
1831
|
_actual: 200,
|
|
1832
1832
|
});
|
|
1833
1833
|
});
|
|
1834
|
+
|
|
1835
|
+
test('Should calculate a price override modifier and a tax modifier properly', () => {
|
|
1836
|
+
const overridePriceMod = {
|
|
1837
|
+
_id: '675354ee39a47228afd1f1a4',
|
|
1838
|
+
attributes: ['override'],
|
|
1839
|
+
modifierId: '6751f7eeb60c71cefee138ee',
|
|
1840
|
+
name: 'Override item price fixed 0',
|
|
1841
|
+
tags: ['default', 'all'],
|
|
1842
|
+
direct: true,
|
|
1843
|
+
properties: {
|
|
1844
|
+
override: {
|
|
1845
|
+
type: 'fixed',
|
|
1846
|
+
field: 'price',
|
|
1847
|
+
fixedValue: 5,
|
|
1848
|
+
multiplier: false,
|
|
1849
|
+
},
|
|
1850
|
+
},
|
|
1851
|
+
};
|
|
1852
|
+
|
|
1853
|
+
const percentageFee10 = {
|
|
1854
|
+
compute: {
|
|
1855
|
+
amount: 10,
|
|
1856
|
+
type: 'percentage',
|
|
1857
|
+
action: 'add',
|
|
1858
|
+
},
|
|
1859
|
+
name: 'percentageFee10',
|
|
1860
|
+
type: 'tax',
|
|
1861
|
+
_id: '234',
|
|
1862
|
+
modifierId: 222,
|
|
1863
|
+
};
|
|
1864
|
+
|
|
1865
|
+
const itemWithModifierAmountFixed10 = {
|
|
1866
|
+
name: "Men's 3pc Tuxedo",
|
|
1867
|
+
pieces: 1,
|
|
1868
|
+
price: 10,
|
|
1869
|
+
quantity: 1,
|
|
1870
|
+
modifiers: [overridePriceMod, percentageFee10],
|
|
1871
|
+
_id: '675354e939a47228afd1f199',
|
|
1872
|
+
itemId: '62cdbfd01ee1b400193281ee',
|
|
1873
|
+
};
|
|
1874
|
+
const newItem = pricingService.item.calculate(
|
|
1875
|
+
itemWithModifierAmountFixed10
|
|
1876
|
+
);
|
|
1877
|
+
expect(newItem.price).toBe(5);
|
|
1878
|
+
expect(newItem.total).toBe(5.5);
|
|
1879
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
1880
|
+
amount: 0,
|
|
1881
|
+
description: 'Override item price fixed 0 ($5.00/Unit)',
|
|
1882
|
+
});
|
|
1883
|
+
|
|
1884
|
+
expect(newItem.modifiers[1]).toHaveProperty('_computed', {
|
|
1885
|
+
amount: 0.5,
|
|
1886
|
+
description: 'percentageFee10 ($0.50)',
|
|
1887
|
+
});
|
|
1888
|
+
|
|
1889
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
1890
|
+
_included: 0,
|
|
1891
|
+
_xincluded: 0.5,
|
|
1892
|
+
_direct: 0,
|
|
1893
|
+
_xdirect: 0.5,
|
|
1894
|
+
_simple: 5,
|
|
1895
|
+
_actual: 5,
|
|
1896
|
+
tax: 0.5,
|
|
1897
|
+
});
|
|
1898
|
+
});
|
|
1899
|
+
|
|
1900
|
+
test('Should calculate a tax modifier and a price override modifier properly', () => {
|
|
1901
|
+
const overridePriceMod = {
|
|
1902
|
+
_id: '675354ee39a47228afd1f1a4',
|
|
1903
|
+
attributes: ['override'],
|
|
1904
|
+
modifierId: '6751f7eeb60c71cefee138ee',
|
|
1905
|
+
name: 'Override item price fixed 0',
|
|
1906
|
+
tags: ['default', 'all'],
|
|
1907
|
+
direct: true,
|
|
1908
|
+
properties: {
|
|
1909
|
+
override: {
|
|
1910
|
+
type: 'fixed',
|
|
1911
|
+
field: 'price',
|
|
1912
|
+
fixedValue: 5,
|
|
1913
|
+
multiplier: false,
|
|
1914
|
+
},
|
|
1915
|
+
},
|
|
1916
|
+
};
|
|
1917
|
+
|
|
1918
|
+
const percentageFee10 = {
|
|
1919
|
+
compute: {
|
|
1920
|
+
amount: 10,
|
|
1921
|
+
type: 'percentage',
|
|
1922
|
+
action: 'add',
|
|
1923
|
+
},
|
|
1924
|
+
name: 'percentageFee10',
|
|
1925
|
+
type: 'tax',
|
|
1926
|
+
_id: '234',
|
|
1927
|
+
modifierId: 222,
|
|
1928
|
+
};
|
|
1929
|
+
|
|
1930
|
+
const itemWithModifierAmountFixed10 = {
|
|
1931
|
+
name: "Men's 3pc Tuxedo",
|
|
1932
|
+
pieces: 1,
|
|
1933
|
+
price: 10,
|
|
1934
|
+
quantity: 1,
|
|
1935
|
+
modifiers: [percentageFee10, overridePriceMod],
|
|
1936
|
+
_id: '675354e939a47228afd1f199',
|
|
1937
|
+
itemId: '62cdbfd01ee1b400193281ee',
|
|
1938
|
+
};
|
|
1939
|
+
const newItem = pricingService.item.calculate(
|
|
1940
|
+
itemWithModifierAmountFixed10
|
|
1941
|
+
);
|
|
1942
|
+
expect(newItem.price).toBe(5);
|
|
1943
|
+
expect(newItem.total).toBe(6);
|
|
1944
|
+
|
|
1945
|
+
expect(newItem.modifiers[0]).toHaveProperty('_computed', {
|
|
1946
|
+
amount: 1,
|
|
1947
|
+
description: 'percentageFee10 ($1.00)',
|
|
1948
|
+
});
|
|
1949
|
+
|
|
1950
|
+
expect(newItem.modifiers[1]).toHaveProperty('_computed', {
|
|
1951
|
+
amount: 0,
|
|
1952
|
+
description: 'Override item price fixed 0 ($5.00/Unit)',
|
|
1953
|
+
});
|
|
1954
|
+
|
|
1955
|
+
expect(newItem).toHaveProperty('subTotals', {
|
|
1956
|
+
_included: 0,
|
|
1957
|
+
_xincluded: 1,
|
|
1958
|
+
_direct: 0,
|
|
1959
|
+
_xdirect: 1,
|
|
1960
|
+
_simple: 5,
|
|
1961
|
+
_actual: 5,
|
|
1962
|
+
tax: 1,
|
|
1963
|
+
});
|
|
1964
|
+
});
|
|
1834
1965
|
});
|
package/lib/item/calculate.js
CHANGED
|
@@ -24,20 +24,25 @@ module.exports = ({ modifierActions }) =>
|
|
|
24
24
|
})
|
|
25
25
|
);
|
|
26
26
|
|
|
27
|
+
const isIdMatch = (mod, modifierId) => {
|
|
28
|
+
if (typeof mod === 'string') return mod === modifierId;
|
|
29
|
+
return mod._id === modifierId;
|
|
30
|
+
};
|
|
31
|
+
|
|
27
32
|
firstValidatedModifiers.forEach(validModifier => {
|
|
28
33
|
const parentAddModifier = firstValidatedModifiers.find(
|
|
29
34
|
validMod =>
|
|
30
35
|
validMod.addModifiers &&
|
|
31
|
-
validMod.addModifiers.some(
|
|
32
|
-
addMod
|
|
36
|
+
validMod.addModifiers.some(addMod =>
|
|
37
|
+
isIdMatch(addMod, validModifier.modifierId)
|
|
33
38
|
)
|
|
34
39
|
);
|
|
35
40
|
|
|
36
41
|
const parentDelModifier = firstValidatedModifiers.find(
|
|
37
42
|
validMod =>
|
|
38
43
|
validMod.delModifiers &&
|
|
39
|
-
validMod.delModifiers.some(
|
|
40
|
-
delMod
|
|
44
|
+
validMod.delModifiers.some(delMod =>
|
|
45
|
+
isIdMatch(delMod, validModifier.modifierId)
|
|
41
46
|
)
|
|
42
47
|
);
|
|
43
48
|
|
|
@@ -14,9 +14,9 @@ module.exports = ({ utils, actions }) => {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
if (
|
|
17
|
-
properties.
|
|
18
|
-
properties.
|
|
19
|
-
math.gte(properties.
|
|
17
|
+
properties.uses &&
|
|
18
|
+
properties.usesLimit &&
|
|
19
|
+
math.gte(properties.uses, properties.usesLimit)
|
|
20
20
|
)
|
|
21
21
|
return false;
|
|
22
22
|
|
|
@@ -26,15 +26,15 @@ module.exports = ({ actions, itemActions, modifierActions, utils, _ }) => {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
const { quantity, price } = orderItem;
|
|
29
|
-
let
|
|
29
|
+
let uses = quantity;
|
|
30
30
|
if (remaining) {
|
|
31
31
|
const left = remaining - (used || 0);
|
|
32
|
-
if (quantity > left)
|
|
32
|
+
if (quantity > left) uses = left;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const modifier = modifierActions.createSubscriptionModifier({
|
|
36
|
-
amount: math.mul(
|
|
37
|
-
properties: { subscription: {
|
|
36
|
+
amount: math.mul(uses, price),
|
|
37
|
+
properties: { subscription: { uses } },
|
|
38
38
|
});
|
|
39
39
|
orderItem.modifiers.push(modifier);
|
|
40
40
|
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
module.exports = ({ actions, modifierActions }) => {
|
|
2
2
|
const getRemaining = (subscriptions, orderId) => {
|
|
3
3
|
const remaining = subscriptions.reduce((total, subscription) => {
|
|
4
|
-
const {
|
|
5
|
-
numberOfUsesLimit = 0,
|
|
6
|
-
numberOfUses = 0,
|
|
7
|
-
history = {},
|
|
8
|
-
} = subscription;
|
|
4
|
+
const { usesLimit = 0, uses = 0, history = {} } = subscription;
|
|
9
5
|
const orderHistory = history[orderId] || 0;
|
|
10
|
-
return total +
|
|
6
|
+
return total + usesLimit + orderHistory - uses;
|
|
11
7
|
}, 0);
|
|
12
8
|
|
|
13
9
|
return remaining;
|
|
@@ -27,16 +23,16 @@ module.exports = ({ actions, modifierActions }) => {
|
|
|
27
23
|
],
|
|
28
24
|
[]
|
|
29
25
|
);
|
|
30
|
-
const
|
|
26
|
+
const uses = modifiers.reduce(
|
|
31
27
|
(sum, each) =>
|
|
32
28
|
sum +
|
|
33
29
|
((each.properties &&
|
|
34
30
|
each.properties.subscription &&
|
|
35
|
-
each.properties.subscription.
|
|
31
|
+
each.properties.subscription.uses) ||
|
|
36
32
|
0),
|
|
37
33
|
0
|
|
38
34
|
);
|
|
39
|
-
return
|
|
35
|
+
return uses;
|
|
40
36
|
};
|
|
41
37
|
|
|
42
38
|
const getSubscriptions = subscriptions =>
|
|
@@ -56,12 +52,14 @@ module.exports = ({ actions, modifierActions }) => {
|
|
|
56
52
|
|
|
57
53
|
subscriptions = getSubscriptions(subscriptions);
|
|
58
54
|
|
|
59
|
-
const isUnlimited = subscriptions.some(
|
|
55
|
+
const isUnlimited = subscriptions.some(
|
|
56
|
+
each => !each.usesLimit || each.renewalType === 'limit_reached'
|
|
57
|
+
);
|
|
60
58
|
if (isUnlimited) return [true];
|
|
61
59
|
|
|
62
60
|
const remaining = getRemaining(subscriptions, order.parentId || order._id);
|
|
63
|
-
const
|
|
61
|
+
const uses = getNumberOfUses(order.items, item);
|
|
64
62
|
|
|
65
|
-
return [remaining >
|
|
63
|
+
return [remaining > uses, remaining, uses];
|
|
66
64
|
};
|
|
67
65
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.114",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"supertest": "^6.2.3",
|
|
56
56
|
"supervisor": "^0.12.0"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "cf91c373155b5fe5207ec9234abffd772fd3df40"
|
|
59
59
|
}
|