@darkpos/pricing 1.0.162 → 1.0.164
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__/modifier/getGiftModifierSource.test.js +51 -0
- package/__TEST__/order/getGiftModifierSources.test.js +145 -0
- package/__TEST__/order/getPaidGiftModifiers.test.js +111 -0
- package/lib/modifier/getGiftModifierSource.js +15 -0
- package/lib/modifier/index.js +2 -0
- package/lib/order/getGiftModifierSources.js +7 -0
- package/lib/order/getPaidGiftModifiers.js +17 -0
- package/lib/order/index.js +4 -0
- package/package.json +2 -2
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
|
|
3
|
+
const pricingService = usePricing();
|
|
4
|
+
|
|
5
|
+
describe('getGiftModifierSource', () => {
|
|
6
|
+
test('maps gift modifier to source with correct fields', () => {
|
|
7
|
+
const purchasedDate = new Date('2026-05-01T00:00:00.000Z');
|
|
8
|
+
const modifier = {
|
|
9
|
+
properties: {
|
|
10
|
+
gift: {
|
|
11
|
+
number: '111222',
|
|
12
|
+
pin: '9999',
|
|
13
|
+
expMonth: 12,
|
|
14
|
+
expYear: 2028,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
compute: { amount: 75 },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
expect(
|
|
21
|
+
pricingService.modifier.getGiftModifierSource(modifier, purchasedDate)
|
|
22
|
+
).toEqual({
|
|
23
|
+
number: '111222',
|
|
24
|
+
pin: '9999',
|
|
25
|
+
amount: 75,
|
|
26
|
+
expMonth: 12,
|
|
27
|
+
expYear: 2028,
|
|
28
|
+
purchasedDate,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('uses null expiration fields when they are missing', () => {
|
|
33
|
+
const modifier = {
|
|
34
|
+
properties: {
|
|
35
|
+
gift: {
|
|
36
|
+
number: '111222',
|
|
37
|
+
pin: '9999',
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
compute: { amount: 75 },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const result = pricingService.modifier.getGiftModifierSource(
|
|
44
|
+
modifier,
|
|
45
|
+
new Date()
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(result.expMonth).toBeNull();
|
|
49
|
+
expect(result.expYear).toBeNull();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
|
|
3
|
+
const pricingService = usePricing();
|
|
4
|
+
|
|
5
|
+
const makeGiftModifier = ({
|
|
6
|
+
number = '111222',
|
|
7
|
+
pin = '9999',
|
|
8
|
+
amount = 50,
|
|
9
|
+
expMonth = null,
|
|
10
|
+
expYear = null,
|
|
11
|
+
} = {}) => ({
|
|
12
|
+
attributes: ['gift'],
|
|
13
|
+
properties: {
|
|
14
|
+
gift: { number, pin, expMonth, expYear },
|
|
15
|
+
},
|
|
16
|
+
compute: { amount },
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const makePaidItem = (modifiers = []) => ({
|
|
20
|
+
status: { paid: { value: true } },
|
|
21
|
+
modifiers,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const makeOrder = (items = []) => ({ items, orders: [] });
|
|
25
|
+
|
|
26
|
+
describe('getGiftModifierSources', () => {
|
|
27
|
+
test('returns empty array for empty orders', () => {
|
|
28
|
+
expect(pricingService.order.getGiftModifierSources([], new Date())).toEqual(
|
|
29
|
+
[]
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('maps paid gift modifier to source with correct fields', () => {
|
|
34
|
+
const purchasedDate = new Date('2026-05-01T00:00:00.000Z');
|
|
35
|
+
const orders = [
|
|
36
|
+
makeOrder([
|
|
37
|
+
makePaidItem([
|
|
38
|
+
makeGiftModifier({ number: '111222', pin: '9999', amount: 75 }),
|
|
39
|
+
]),
|
|
40
|
+
]),
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
const result = pricingService.order.getGiftModifierSources(
|
|
44
|
+
orders,
|
|
45
|
+
purchasedDate
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(result).toHaveLength(1);
|
|
49
|
+
expect(result[0]).toEqual({
|
|
50
|
+
number: '111222',
|
|
51
|
+
pin: '9999',
|
|
52
|
+
amount: 75,
|
|
53
|
+
expMonth: null,
|
|
54
|
+
expYear: null,
|
|
55
|
+
purchasedDate,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('includes expMonth and expYear when present', () => {
|
|
60
|
+
const purchasedDate = new Date('2026-05-01T00:00:00.000Z');
|
|
61
|
+
const orders = [
|
|
62
|
+
makeOrder([
|
|
63
|
+
makePaidItem([
|
|
64
|
+
makeGiftModifier({
|
|
65
|
+
number: '333444',
|
|
66
|
+
pin: '0000',
|
|
67
|
+
amount: 100,
|
|
68
|
+
expMonth: 12,
|
|
69
|
+
expYear: 2028,
|
|
70
|
+
}),
|
|
71
|
+
]),
|
|
72
|
+
]),
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
const result = pricingService.order.getGiftModifierSources(
|
|
76
|
+
orders,
|
|
77
|
+
purchasedDate
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(result[0].expMonth).toBe(12);
|
|
81
|
+
expect(result[0].expYear).toBe(2028);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('returns null for missing expMonth/expYear', () => {
|
|
85
|
+
const orders = [
|
|
86
|
+
makeOrder([
|
|
87
|
+
makePaidItem([
|
|
88
|
+
makeGiftModifier({ expMonth: undefined, expYear: undefined }),
|
|
89
|
+
]),
|
|
90
|
+
]),
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
const result = pricingService.order.getGiftModifierSources(
|
|
94
|
+
orders,
|
|
95
|
+
new Date()
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(result[0].expMonth).toBeNull();
|
|
99
|
+
expect(result[0].expYear).toBeNull();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('sets purchasedDate from argument', () => {
|
|
103
|
+
const purchasedDate = new Date('2026-01-15T08:00:00.000Z');
|
|
104
|
+
const orders = [makeOrder([makePaidItem([makeGiftModifier()])])];
|
|
105
|
+
|
|
106
|
+
const result = pricingService.order.getGiftModifierSources(
|
|
107
|
+
orders,
|
|
108
|
+
purchasedDate
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(result[0].purchasedDate).toBe(purchasedDate);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('returns multiple sources for multiple gift modifiers', () => {
|
|
115
|
+
const orders = [
|
|
116
|
+
makeOrder([
|
|
117
|
+
makePaidItem([makeGiftModifier({ number: '111111', amount: 50 })]),
|
|
118
|
+
makePaidItem([makeGiftModifier({ number: '222222', amount: 25 })]),
|
|
119
|
+
]),
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
const result = pricingService.order.getGiftModifierSources(
|
|
123
|
+
orders,
|
|
124
|
+
new Date()
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
expect(result).toHaveLength(2);
|
|
128
|
+
expect(result.map(s => s.number)).toEqual(['111111', '222222']);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('skips modifiers from unpaid items', () => {
|
|
132
|
+
const unpaidItem = {
|
|
133
|
+
status: { paid: { value: false } },
|
|
134
|
+
modifiers: [makeGiftModifier()],
|
|
135
|
+
};
|
|
136
|
+
const orders = [makeOrder([unpaidItem])];
|
|
137
|
+
|
|
138
|
+
const result = pricingService.order.getGiftModifierSources(
|
|
139
|
+
orders,
|
|
140
|
+
new Date()
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
expect(result).toHaveLength(0);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
const usePricing = require('../../index');
|
|
2
|
+
|
|
3
|
+
const pricingService = usePricing();
|
|
4
|
+
|
|
5
|
+
const makeGiftModifier = ({
|
|
6
|
+
number = '111222',
|
|
7
|
+
pin = '9999',
|
|
8
|
+
amount = 50,
|
|
9
|
+
expMonth = null,
|
|
10
|
+
expYear = null,
|
|
11
|
+
} = {}) => ({
|
|
12
|
+
attributes: ['gift'],
|
|
13
|
+
properties: {
|
|
14
|
+
gift: { number, pin, expMonth, expYear },
|
|
15
|
+
},
|
|
16
|
+
compute: { amount },
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const makePaidItem = (modifiers = []) => ({
|
|
20
|
+
status: { paid: { value: true } },
|
|
21
|
+
modifiers,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const makeUnpaidItem = (modifiers = []) => ({
|
|
25
|
+
status: { paid: { value: false } },
|
|
26
|
+
modifiers,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('getPaidGiftModifiers', () => {
|
|
30
|
+
test('returns empty array for empty orders', () => {
|
|
31
|
+
expect(pricingService.order.getPaidGiftModifiers([])).toEqual([]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('returns empty array when orders is null or undefined', () => {
|
|
35
|
+
expect(pricingService.order.getPaidGiftModifiers(null)).toEqual([]);
|
|
36
|
+
expect(pricingService.order.getPaidGiftModifiers(undefined)).toEqual([]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('returns gift modifiers from paid items', () => {
|
|
40
|
+
const giftMod = makeGiftModifier({ number: '111222' });
|
|
41
|
+
const orders = [{ items: [makePaidItem([giftMod])], orders: [] }];
|
|
42
|
+
|
|
43
|
+
const result = pricingService.order.getPaidGiftModifiers(orders);
|
|
44
|
+
|
|
45
|
+
expect(result).toHaveLength(1);
|
|
46
|
+
expect(result[0]).toBe(giftMod);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('ignores gift modifiers from unpaid items', () => {
|
|
50
|
+
const giftMod = makeGiftModifier();
|
|
51
|
+
const orders = [{ items: [makeUnpaidItem([giftMod])], orders: [] }];
|
|
52
|
+
|
|
53
|
+
const result = pricingService.order.getPaidGiftModifiers(orders);
|
|
54
|
+
|
|
55
|
+
expect(result).toHaveLength(0);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('picks up gift modifiers from sub-order items', () => {
|
|
59
|
+
const giftMod = makeGiftModifier({ number: '999888' });
|
|
60
|
+
const orders = [
|
|
61
|
+
{
|
|
62
|
+
items: [],
|
|
63
|
+
orders: [{ items: [makePaidItem([giftMod])] }],
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
const result = pricingService.order.getPaidGiftModifiers(orders);
|
|
68
|
+
|
|
69
|
+
expect(result).toHaveLength(1);
|
|
70
|
+
expect(result[0]).toBe(giftMod);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('matches gift modifier by properties.gift when attributes does not include gift', () => {
|
|
74
|
+
const mod = {
|
|
75
|
+
attributes: [],
|
|
76
|
+
properties: { gift: { number: '555444', pin: '1111' } },
|
|
77
|
+
compute: { amount: 30 },
|
|
78
|
+
};
|
|
79
|
+
const orders = [{ items: [makePaidItem([mod])], orders: [] }];
|
|
80
|
+
|
|
81
|
+
const result = pricingService.order.getPaidGiftModifiers(orders);
|
|
82
|
+
|
|
83
|
+
expect(result).toHaveLength(1);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('ignores non-gift modifiers', () => {
|
|
87
|
+
const nonGiftMod = {
|
|
88
|
+
attributes: ['discount'],
|
|
89
|
+
properties: {},
|
|
90
|
+
compute: { amount: 10 },
|
|
91
|
+
};
|
|
92
|
+
const orders = [{ items: [makePaidItem([nonGiftMod])], orders: [] }];
|
|
93
|
+
|
|
94
|
+
const result = pricingService.order.getPaidGiftModifiers(orders);
|
|
95
|
+
|
|
96
|
+
expect(result).toHaveLength(0);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('aggregates gift modifiers across multiple orders and items', () => {
|
|
100
|
+
const mod1 = makeGiftModifier({ number: '111111' });
|
|
101
|
+
const mod2 = makeGiftModifier({ number: '222222' });
|
|
102
|
+
const orders = [
|
|
103
|
+
{ items: [makePaidItem([mod1])], orders: [] },
|
|
104
|
+
{ items: [makePaidItem([mod2])], orders: [] },
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
const result = pricingService.order.getPaidGiftModifiers(orders);
|
|
108
|
+
|
|
109
|
+
expect(result).toHaveLength(2);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = () =>
|
|
2
|
+
function getGiftModifierSource(modifier, purchasedDate) {
|
|
3
|
+
const { gift } = modifier.properties;
|
|
4
|
+
const { number } = gift;
|
|
5
|
+
const giftAmount = modifier.compute.amount;
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
number,
|
|
9
|
+
pin: gift.pin,
|
|
10
|
+
amount: giftAmount,
|
|
11
|
+
expMonth: gift.expMonth || null,
|
|
12
|
+
expYear: gift.expYear || null,
|
|
13
|
+
purchasedDate,
|
|
14
|
+
};
|
|
15
|
+
};
|
package/lib/modifier/index.js
CHANGED
|
@@ -112,6 +112,7 @@ const hasItems = require('./hasItems');
|
|
|
112
112
|
const removeGroupData = require('./removeGroupData');
|
|
113
113
|
const isRelatedModifierById = require('./isRelatedModifierById');
|
|
114
114
|
const getGroupedModifiers = require('./getGroupedModifiers');
|
|
115
|
+
const getGiftModifierSource = require('./getGiftModifierSource');
|
|
115
116
|
const isOptionsOverride = require('./isOptionsOverride');
|
|
116
117
|
const getGroupedModifierLabels = require('./getGroupedModifierLabels');
|
|
117
118
|
const validate = require('./validate');
|
|
@@ -304,6 +305,7 @@ const modifierActions = (deps = {}) => {
|
|
|
304
305
|
removeGroupData: removeGroupData(innerDeps),
|
|
305
306
|
isRelatedModifierById: isRelatedModifierById(innerDeps),
|
|
306
307
|
getGroupedModifiers: getGroupedModifiers(innerDeps),
|
|
308
|
+
getGiftModifierSource: getGiftModifierSource(innerDeps),
|
|
307
309
|
isOptionsOverride: isOptionsOverride(innerDeps),
|
|
308
310
|
getGroupedModifierLabels: getGroupedModifierLabels(innerDeps),
|
|
309
311
|
validate: validate(innerDeps),
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module.exports = ({ actions, modifierActions }) =>
|
|
2
|
+
function getGiftModifierSources(orders, purchasedDate) {
|
|
3
|
+
const paidItemModifiers = actions.getPaidGiftModifiers(orders);
|
|
4
|
+
return (paidItemModifiers || []).map(modifier =>
|
|
5
|
+
modifierActions.getGiftModifierSource(modifier, purchasedDate)
|
|
6
|
+
);
|
|
7
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = () =>
|
|
2
|
+
function getPaidGiftModifiers(orders) {
|
|
3
|
+
if ((orders || []).length === 0) return [];
|
|
4
|
+
|
|
5
|
+
const allItems = orders
|
|
6
|
+
.flatMap(order => [
|
|
7
|
+
...(order.items || []),
|
|
8
|
+
...(order.orders || []).flatMap(subOrder => subOrder.items || []),
|
|
9
|
+
])
|
|
10
|
+
.filter(item => item.status.paid.value);
|
|
11
|
+
|
|
12
|
+
return allItems.flatMap(item =>
|
|
13
|
+
(item.modifiers || []).filter(
|
|
14
|
+
m => m.attributes.includes('gift') || m.properties.gift
|
|
15
|
+
)
|
|
16
|
+
);
|
|
17
|
+
};
|
package/lib/order/index.js
CHANGED
|
@@ -106,6 +106,8 @@ const getOverPaidAmount = require('./getOverPaidAmount');
|
|
|
106
106
|
const getTotalPaidToRedistribute = require('./getTotalPaidToRedistribute');
|
|
107
107
|
const refundOrderItem = require('./refundOrderItem');
|
|
108
108
|
const normalizeEndDate = require('./normalizeEndDate');
|
|
109
|
+
const getPaidGiftModifiers = require('./getPaidGiftModifiers');
|
|
110
|
+
const getGiftModifierSources = require('./getGiftModifierSources');
|
|
109
111
|
|
|
110
112
|
const orderActions = (deps = {}) => {
|
|
111
113
|
const actions = {};
|
|
@@ -223,6 +225,8 @@ const orderActions = (deps = {}) => {
|
|
|
223
225
|
getTotalPaidToRedistribute: getTotalPaidToRedistribute(innerDeps),
|
|
224
226
|
refundOrderItem: refundOrderItem(innerDeps),
|
|
225
227
|
normalizeEndDate: normalizeEndDate(innerDeps),
|
|
228
|
+
getPaidGiftModifiers: getPaidGiftModifiers(innerDeps),
|
|
229
|
+
getGiftModifierSources: getGiftModifierSources(innerDeps),
|
|
226
230
|
});
|
|
227
231
|
|
|
228
232
|
Object.keys(freezedActions).forEach(actionName => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darkpos/pricing",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.164",
|
|
4
4
|
"description": "Pricing calculator",
|
|
5
5
|
"author": "Dark POS",
|
|
6
6
|
"license": "ISC",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"supertest": "^6.2.3",
|
|
55
55
|
"supervisor": "^0.12.0"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "b9786e89438ac14114630c52ecaf5f4770aa2bf8"
|
|
58
58
|
}
|