@darkpos/pricing 1.0.161 → 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/__TEST__/order/normalizeEndDate.test.js +125 -0
- package/__TEST__/order/pickEndDate.test.js +23 -12
- package/__TEST__/order/split.test.js +135 -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 +6 -0
- package/lib/order/normalizeEndDate.js +38 -0
- package/lib/order/splitByDepartments.js +3 -6
- package/lib/store/pickEndDate.js +69 -55
- 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,125 @@
|
|
|
1
|
+
const moment = require('moment-timezone');
|
|
2
|
+
const usePricing = require('../../index');
|
|
3
|
+
|
|
4
|
+
const getPricingService = () =>
|
|
5
|
+
usePricing({
|
|
6
|
+
store: {
|
|
7
|
+
_settings: {
|
|
8
|
+
order: {
|
|
9
|
+
schedules: [
|
|
10
|
+
{
|
|
11
|
+
addDays: 0,
|
|
12
|
+
readyHour: { hour: 17, minute: 0 },
|
|
13
|
+
skipDays: [],
|
|
14
|
+
cutHour: { hour: 16, minute: 0 },
|
|
15
|
+
cutDay: 1,
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('normalizeEndDate', () => {
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
jest.restoreAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('keeps end.requestDate sent in the payload', () => {
|
|
29
|
+
const pricingService = getPricingService();
|
|
30
|
+
const requestDate = '2030-01-18T22:00:00.000Z';
|
|
31
|
+
const order = {
|
|
32
|
+
end: {
|
|
33
|
+
requestDate,
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const result = pricingService.order.normalizeEndDate({
|
|
38
|
+
data: order,
|
|
39
|
+
order,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(result.end.requestDate).toBe(requestDate);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('picks end.requestDate when it is not sent in the payload', () => {
|
|
46
|
+
const pricingService = getPricingService();
|
|
47
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
48
|
+
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
49
|
+
|
|
50
|
+
const result = pricingService.order.normalizeEndDate({
|
|
51
|
+
data: {},
|
|
52
|
+
order: {},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
expect(result.end.requestDate).toBe('2025-05-13T21:00:00.000Z');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('picks end.requestDate when requestDate is empty string', () => {
|
|
59
|
+
const pricingService = getPricingService();
|
|
60
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
61
|
+
jest.spyOn(moment, 'now').mockImplementation(() => now.valueOf());
|
|
62
|
+
|
|
63
|
+
const result = pricingService.order.normalizeEndDate({
|
|
64
|
+
data: {
|
|
65
|
+
end: {
|
|
66
|
+
requestDate: '',
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
order: {
|
|
70
|
+
end: {
|
|
71
|
+
requestDate: '',
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(result.end.requestDate).toBe('2025-05-13T21:00:00.000Z');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('preserves previous end.requestDate when an update sends null', () => {
|
|
80
|
+
const pricingService = getPricingService();
|
|
81
|
+
const prevRequestDate = '2030-01-18T22:00:00.000Z';
|
|
82
|
+
|
|
83
|
+
const result = pricingService.order.normalizeEndDate({
|
|
84
|
+
data: {
|
|
85
|
+
end: {
|
|
86
|
+
requestDate: null,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
order: {
|
|
90
|
+
end: {
|
|
91
|
+
requestDate: null,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
prevOrder: {
|
|
95
|
+
end: {
|
|
96
|
+
requestDate: prevRequestDate,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(result.end.requestDate).toBe(prevRequestDate);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('does not touch previous end.requestDate when an update omits end', () => {
|
|
105
|
+
const pricingService = getPricingService();
|
|
106
|
+
const prevRequestDate = '2030-01-18T22:00:00.000Z';
|
|
107
|
+
const order = {
|
|
108
|
+
end: {
|
|
109
|
+
requestDate: prevRequestDate,
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const result = pricingService.order.normalizeEndDate({
|
|
114
|
+
data: {},
|
|
115
|
+
order,
|
|
116
|
+
prevOrder: {
|
|
117
|
+
end: {
|
|
118
|
+
requestDate: prevRequestDate,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(result).toBe(order);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -243,28 +243,39 @@ describe('pickEndDate function', () => {
|
|
|
243
243
|
expect(result).toBe('2025-05-14T15:00:00Z');
|
|
244
244
|
});
|
|
245
245
|
|
|
246
|
-
test('pickEndDate -
|
|
246
|
+
test('pickEndDate - returns latest date across schedules with departments', () => {
|
|
247
247
|
const schedulesWithDepartments = [
|
|
248
248
|
{
|
|
249
|
-
_id: 'schedule
|
|
249
|
+
_id: 'tailoring-schedule',
|
|
250
250
|
dayOfWeek: [2],
|
|
251
251
|
recommended: 'auto_recommended',
|
|
252
|
-
addDays:
|
|
252
|
+
addDays: 4,
|
|
253
253
|
cutDay: 1,
|
|
254
|
-
skipDays: [
|
|
255
|
-
readyHour: { hour:
|
|
256
|
-
cutHour: { hour:
|
|
257
|
-
departments: [{ _id: 'department-id' }],
|
|
254
|
+
skipDays: [],
|
|
255
|
+
readyHour: { hour: 17, minute: 0 },
|
|
256
|
+
cutHour: { hour: 10, minute: 0 },
|
|
257
|
+
departments: [{ _id: 'tailoring-department-id' }],
|
|
258
258
|
},
|
|
259
259
|
{
|
|
260
|
-
_id: 'schedule
|
|
260
|
+
_id: 'default-schedule',
|
|
261
261
|
dayOfWeek: [2],
|
|
262
262
|
recommended: 'auto_recommended',
|
|
263
263
|
addDays: 1,
|
|
264
264
|
cutDay: 1,
|
|
265
|
-
skipDays: [
|
|
266
|
-
readyHour: { hour:
|
|
267
|
-
cutHour: { hour:
|
|
265
|
+
skipDays: [],
|
|
266
|
+
readyHour: { hour: 15, minute: 0 },
|
|
267
|
+
cutHour: { hour: 0, minute: 0 },
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
_id: 'dt-schedule',
|
|
271
|
+
dayOfWeek: [2],
|
|
272
|
+
recommended: 'auto_recommended',
|
|
273
|
+
addDays: 10,
|
|
274
|
+
cutDay: 1,
|
|
275
|
+
skipDays: [],
|
|
276
|
+
readyHour: { hour: 0, minute: 0 },
|
|
277
|
+
cutHour: { hour: 0, minute: 0 },
|
|
278
|
+
departments: [{ _id: 'dt-department-id' }],
|
|
268
279
|
},
|
|
269
280
|
];
|
|
270
281
|
|
|
@@ -276,7 +287,7 @@ describe('pickEndDate function', () => {
|
|
|
276
287
|
|
|
277
288
|
const result = pricingService.store.pickEndDate();
|
|
278
289
|
|
|
279
|
-
expect(result).toBe('2025-05-
|
|
290
|
+
expect(result).toBe('2025-05-24T04:00:00Z');
|
|
280
291
|
});
|
|
281
292
|
|
|
282
293
|
test('pickEndDate - uses provided schedules even with departments', () => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const moment = require('moment-timezone');
|
|
1
2
|
const usePricing = require('../../index');
|
|
2
3
|
const mockStores = require('../mocks/stores');
|
|
3
4
|
const orderSplit1 = require('../mocks/order/split-1.json');
|
|
@@ -80,6 +81,140 @@ describe('Auto Split', () => {
|
|
|
80
81
|
expect(subOrders[0].items[2].quantity).toEqual(1000);
|
|
81
82
|
});
|
|
82
83
|
|
|
84
|
+
test('Auto split keeps incoming end date when department has its own schedule', () => {
|
|
85
|
+
const defaultSchedule = {
|
|
86
|
+
addDays: 0,
|
|
87
|
+
readyHour: { hour: 17, minute: 0 },
|
|
88
|
+
skipDays: [],
|
|
89
|
+
cutHour: { hour: 16, minute: 0 },
|
|
90
|
+
cutDay: 1,
|
|
91
|
+
};
|
|
92
|
+
const departmentSchedule = {
|
|
93
|
+
addDays: 2,
|
|
94
|
+
readyHour: { hour: 18, minute: 0 },
|
|
95
|
+
skipDays: [],
|
|
96
|
+
cutHour: { hour: 16, minute: 0 },
|
|
97
|
+
cutDay: 1,
|
|
98
|
+
departments: [{ _id: 'laundry-department-id' }],
|
|
99
|
+
};
|
|
100
|
+
const pricingServiceWithSchedules = usePricing({
|
|
101
|
+
store: {
|
|
102
|
+
...mockStores[0],
|
|
103
|
+
_settings: {
|
|
104
|
+
...mockStores[0]._settings,
|
|
105
|
+
order: {
|
|
106
|
+
...mockStores[0]._settings.order,
|
|
107
|
+
schedules: [defaultSchedule, departmentSchedule],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
113
|
+
const momentNowSpy = jest
|
|
114
|
+
.spyOn(moment, 'now')
|
|
115
|
+
.mockImplementation(() => now.valueOf());
|
|
116
|
+
|
|
117
|
+
const incomingEndDate = '2025-05-14T21:00:00Z';
|
|
118
|
+
const departmentEndDate = pricingServiceWithSchedules.store.pickEndDate([
|
|
119
|
+
departmentSchedule,
|
|
120
|
+
]);
|
|
121
|
+
const laundryModifier = {
|
|
122
|
+
...laundryModNoAutoSplit,
|
|
123
|
+
modifierId: 'laundry-department-id',
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const subOrders = pricingServiceWithSchedules.order.autoSplit({
|
|
127
|
+
parentOrder: {
|
|
128
|
+
items: [
|
|
129
|
+
{
|
|
130
|
+
_id: '123',
|
|
131
|
+
itemId: '234',
|
|
132
|
+
modifiers: [laundryModifier],
|
|
133
|
+
quantity: 1,
|
|
134
|
+
},
|
|
135
|
+
],
|
|
136
|
+
end: {
|
|
137
|
+
requestDate: incomingEndDate,
|
|
138
|
+
},
|
|
139
|
+
status: {},
|
|
140
|
+
modifiers: [],
|
|
141
|
+
displayId: 1,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
momentNowSpy.mockRestore();
|
|
146
|
+
|
|
147
|
+
expect(departmentEndDate).not.toEqual(incomingEndDate);
|
|
148
|
+
expect(subOrders[0].end.requestDate).toEqual(incomingEndDate);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('Auto split picks department end date when incoming end date is empty', () => {
|
|
152
|
+
const defaultSchedule = {
|
|
153
|
+
addDays: 0,
|
|
154
|
+
readyHour: { hour: 17, minute: 0 },
|
|
155
|
+
skipDays: [],
|
|
156
|
+
cutHour: { hour: 16, minute: 0 },
|
|
157
|
+
cutDay: 1,
|
|
158
|
+
};
|
|
159
|
+
const departmentSchedule = {
|
|
160
|
+
addDays: 2,
|
|
161
|
+
readyHour: { hour: 18, minute: 0 },
|
|
162
|
+
skipDays: [],
|
|
163
|
+
cutHour: { hour: 16, minute: 0 },
|
|
164
|
+
cutDay: 1,
|
|
165
|
+
departments: [{ _id: 'laundry-department-id' }],
|
|
166
|
+
};
|
|
167
|
+
const pricingServiceWithSchedules = usePricing({
|
|
168
|
+
store: {
|
|
169
|
+
...mockStores[0],
|
|
170
|
+
_settings: {
|
|
171
|
+
...mockStores[0]._settings,
|
|
172
|
+
order: {
|
|
173
|
+
...mockStores[0]._settings.order,
|
|
174
|
+
schedules: [defaultSchedule, departmentSchedule],
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
const now = moment('2025-05-13T15:00:00Z').tz('America/New_York');
|
|
180
|
+
const momentNowSpy = jest
|
|
181
|
+
.spyOn(moment, 'now')
|
|
182
|
+
.mockImplementation(() => now.valueOf());
|
|
183
|
+
|
|
184
|
+
const departmentEndDate = pricingServiceWithSchedules.store.pickEndDate([
|
|
185
|
+
departmentSchedule,
|
|
186
|
+
]);
|
|
187
|
+
const laundryModifier = {
|
|
188
|
+
...laundryModNoAutoSplit,
|
|
189
|
+
modifierId: 'laundry-department-id',
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const subOrders = pricingServiceWithSchedules.order.autoSplit({
|
|
193
|
+
parentOrder: {
|
|
194
|
+
items: [
|
|
195
|
+
{
|
|
196
|
+
_id: '123',
|
|
197
|
+
itemId: '234',
|
|
198
|
+
modifiers: [laundryModifier],
|
|
199
|
+
quantity: 1,
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
end: {
|
|
203
|
+
requestDate: '',
|
|
204
|
+
},
|
|
205
|
+
status: {},
|
|
206
|
+
modifiers: [],
|
|
207
|
+
displayId: 1,
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
momentNowSpy.mockRestore();
|
|
212
|
+
|
|
213
|
+
expect(subOrders[0].end.requestDate).toEqual(
|
|
214
|
+
new Date(departmentEndDate).toISOString()
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
83
218
|
test('Auto split by department and Items Quantity', () => {
|
|
84
219
|
const { autoSplit } = pricingService.order;
|
|
85
220
|
|
|
@@ -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
|
@@ -105,6 +105,9 @@ const hasSerial = require('./hasSerial');
|
|
|
105
105
|
const getOverPaidAmount = require('./getOverPaidAmount');
|
|
106
106
|
const getTotalPaidToRedistribute = require('./getTotalPaidToRedistribute');
|
|
107
107
|
const refundOrderItem = require('./refundOrderItem');
|
|
108
|
+
const normalizeEndDate = require('./normalizeEndDate');
|
|
109
|
+
const getPaidGiftModifiers = require('./getPaidGiftModifiers');
|
|
110
|
+
const getGiftModifierSources = require('./getGiftModifierSources');
|
|
108
111
|
|
|
109
112
|
const orderActions = (deps = {}) => {
|
|
110
113
|
const actions = {};
|
|
@@ -221,6 +224,9 @@ const orderActions = (deps = {}) => {
|
|
|
221
224
|
getOverPaidAmount: getOverPaidAmount(innerDeps),
|
|
222
225
|
getTotalPaidToRedistribute: getTotalPaidToRedistribute(innerDeps),
|
|
223
226
|
refundOrderItem: refundOrderItem(innerDeps),
|
|
227
|
+
normalizeEndDate: normalizeEndDate(innerDeps),
|
|
228
|
+
getPaidGiftModifiers: getPaidGiftModifiers(innerDeps),
|
|
229
|
+
getGiftModifierSources: getGiftModifierSources(innerDeps),
|
|
224
230
|
});
|
|
225
231
|
|
|
226
232
|
Object.keys(freezedActions).forEach(actionName => {
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module.exports = ({ _, actions, storeActions }) => {
|
|
2
|
+
const isEmptyEndRequestDate = requestDate =>
|
|
3
|
+
requestDate === undefined || requestDate === null || requestDate === '';
|
|
4
|
+
|
|
5
|
+
const shouldPreservePrevEndDate = ({ data, prevOrder }) =>
|
|
6
|
+
prevOrder &&
|
|
7
|
+
_.has(data, 'end') &&
|
|
8
|
+
isEmptyEndRequestDate(_.get(data, 'end.requestDate')) &&
|
|
9
|
+
!isEmptyEndRequestDate(_.get(prevOrder, 'end.requestDate'));
|
|
10
|
+
|
|
11
|
+
const preservePrevEndDate = ({ order, prevOrder }) => ({
|
|
12
|
+
...order,
|
|
13
|
+
end: {
|
|
14
|
+
...(order.end || {}),
|
|
15
|
+
requestDate: _.get(prevOrder, 'end.requestDate'),
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const shouldPickEndDate = ({ data, order, prevOrder }) => {
|
|
20
|
+
if (prevOrder && !_.has(data, 'end')) return false;
|
|
21
|
+
|
|
22
|
+
const endRequestDate = _.get(order, 'end.requestDate');
|
|
23
|
+
return isEmptyEndRequestDate(endRequestDate);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
return function normalizeEndDate({ data = {}, order, prevOrder }) {
|
|
27
|
+
if (shouldPreservePrevEndDate({ data, prevOrder })) {
|
|
28
|
+
return preservePrevEndDate({ order, prevOrder });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!shouldPickEndDate({ data, order, prevOrder })) return order;
|
|
32
|
+
|
|
33
|
+
return actions.changeEndDate({
|
|
34
|
+
date: storeActions.pickEndDate(),
|
|
35
|
+
order,
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
};
|
|
@@ -59,11 +59,8 @@ module.exports = ({
|
|
|
59
59
|
);
|
|
60
60
|
const itemGroups = Object.values(itemsByDepartments);
|
|
61
61
|
let splitOrders = [];
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
!parentOrderParam.end ||
|
|
65
|
-
!parentOrderParam.end.requestDate ||
|
|
66
|
-
utils.date.isEqual(defaultEndDate, parentOrderParam.end.requestDate);
|
|
62
|
+
const parentEndDate = _.get(parentOrderParam, 'end.requestDate');
|
|
63
|
+
const shouldPickDepartmentEndDate = !parentEndDate;
|
|
67
64
|
|
|
68
65
|
itemGroups.forEach(items => {
|
|
69
66
|
// Assuming there is one department in the item
|
|
@@ -79,7 +76,7 @@ module.exports = ({
|
|
|
79
76
|
if (
|
|
80
77
|
departmentSchedules &&
|
|
81
78
|
departmentSchedules.length &&
|
|
82
|
-
|
|
79
|
+
shouldPickDepartmentEndDate
|
|
83
80
|
) {
|
|
84
81
|
parentOrder = actions.changeEndDate({
|
|
85
82
|
date: storeActions.pickEndDate(departmentSchedules),
|
package/lib/store/pickEndDate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { MAX_ADD_DAYS } = require('../constants/Store');
|
|
2
2
|
|
|
3
|
-
module.exports = ({ _, moment, actions }) =>
|
|
3
|
+
module.exports = ({ _, moment, settings, actions }) =>
|
|
4
4
|
function pickEndDate(
|
|
5
5
|
schedules,
|
|
6
6
|
isoWeekday,
|
|
@@ -8,75 +8,89 @@ module.exports = ({ _, moment, actions }) =>
|
|
|
8
8
|
readyDateCapacityCounts = []
|
|
9
9
|
) {
|
|
10
10
|
const timezone = actions.getStoreTimezone();
|
|
11
|
+
const schedulesToEvaluate =
|
|
12
|
+
schedules && schedules.length
|
|
13
|
+
? schedules
|
|
14
|
+
: _.get(settings, 'order.schedules', []);
|
|
15
|
+
|
|
16
|
+
const getCandidateDate = schedule => {
|
|
17
|
+
const candidateSchedules = schedule ? [schedule] : undefined;
|
|
18
|
+
let candidateDate = actions.pickBaseEndDate(
|
|
19
|
+
candidateSchedules,
|
|
20
|
+
isoWeekday,
|
|
21
|
+
fromDate
|
|
22
|
+
);
|
|
11
23
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
fromDate
|
|
16
|
-
);
|
|
24
|
+
const readyDateCapacityDays = actions.getReadyDateCapacityDays();
|
|
25
|
+
if (!actions.hasReadyDateCapacitySettings(readyDateCapacityDays))
|
|
26
|
+
return candidateDate;
|
|
17
27
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
const countsByDate = actions.getReadyDateCountsByDate(
|
|
29
|
+
readyDateCapacityCounts
|
|
30
|
+
);
|
|
31
|
+
if (!countsByDate.size) return candidateDate;
|
|
21
32
|
|
|
22
|
-
|
|
23
|
-
readyDateCapacityCounts
|
|
24
|
-
);
|
|
25
|
-
if (!countsByDate.size) return candidateDate;
|
|
33
|
+
const inspectedDays = new Set();
|
|
26
34
|
|
|
27
|
-
|
|
35
|
+
for (let index = 0; index < MAX_ADD_DAYS; index += 1) {
|
|
36
|
+
const candidateMomentTZ = moment(candidateDate).tz(timezone);
|
|
37
|
+
const candidateLocalDateKey = candidateMomentTZ.format('YYYY-MM-DD');
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const candidateLocalDateKey = candidateMomentTZ.format('YYYY-MM-DD');
|
|
39
|
+
if (inspectedDays.has(candidateLocalDateKey)) break;
|
|
40
|
+
inspectedDays.add(candidateLocalDateKey);
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
42
|
+
const capacitySettingsForDay = readyDateCapacityDays.find(
|
|
43
|
+
day => Number(_.get(day, 'day')) === candidateMomentTZ.isoWeekday()
|
|
44
|
+
);
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
day => Number(_.get(day, 'day')) === candidateMomentTZ.isoWeekday()
|
|
38
|
-
);
|
|
46
|
+
if (!capacitySettingsForDay) return candidateDate;
|
|
39
47
|
|
|
40
|
-
|
|
48
|
+
const maxOrders = actions.normalizeLimit(
|
|
49
|
+
_.get(capacitySettingsForDay, 'maxOrders')
|
|
50
|
+
);
|
|
51
|
+
const maxItems = actions.normalizeLimit(
|
|
52
|
+
_.get(capacitySettingsForDay, 'maxItems')
|
|
53
|
+
);
|
|
41
54
|
|
|
42
|
-
|
|
43
|
-
_.get(capacitySettingsForDay, 'maxOrders')
|
|
44
|
-
);
|
|
45
|
-
const maxItems = actions.normalizeLimit(
|
|
46
|
-
_.get(capacitySettingsForDay, 'maxItems')
|
|
47
|
-
);
|
|
55
|
+
if (maxOrders <= 0 && maxItems <= 0) return candidateDate;
|
|
48
56
|
|
|
49
|
-
|
|
57
|
+
const candidateUTCDateKey = moment
|
|
58
|
+
.utc(candidateDate)
|
|
59
|
+
.format('YYYY-MM-DD');
|
|
60
|
+
const dayCount = countsByDate.get(candidateUTCDateKey) || {
|
|
61
|
+
totalOrders: 0,
|
|
62
|
+
totalItems: 0,
|
|
63
|
+
};
|
|
50
64
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const dayCount = countsByDate.get(candidateUTCDateKey) || {
|
|
55
|
-
totalOrders: 0,
|
|
56
|
-
totalItems: 0,
|
|
57
|
-
};
|
|
65
|
+
const isUnavailable =
|
|
66
|
+
(maxOrders > 0 && dayCount.totalOrders >= maxOrders) ||
|
|
67
|
+
(maxItems > 0 && dayCount.totalItems >= maxItems);
|
|
58
68
|
|
|
59
|
-
|
|
60
|
-
(maxOrders > 0 && dayCount.totalOrders >= maxOrders) ||
|
|
61
|
-
(maxItems > 0 && dayCount.totalItems >= maxItems);
|
|
69
|
+
if (!isUnavailable) return candidateDate;
|
|
62
70
|
|
|
63
|
-
|
|
71
|
+
const nextDateSeed = candidateMomentTZ
|
|
72
|
+
.clone()
|
|
73
|
+
.add(1, 'day')
|
|
74
|
+
.startOf('day')
|
|
75
|
+
.toISOString();
|
|
64
76
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.startOf('day')
|
|
69
|
-
.toISOString();
|
|
77
|
+
const { readyHour, cutHour, cutDay, skipDays } =
|
|
78
|
+
actions.getSchedule(candidateSchedules, undefined, candidateDate) ||
|
|
79
|
+
{};
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
|
|
81
|
+
candidateDate = actions.pickBaseEndDate(
|
|
82
|
+
[{ readyHour, cutHour, cutDay, skipDays }],
|
|
83
|
+
undefined,
|
|
84
|
+
nextDateSeed
|
|
85
|
+
);
|
|
86
|
+
}
|
|
73
87
|
|
|
74
|
-
candidateDate
|
|
75
|
-
|
|
76
|
-
undefined,
|
|
77
|
-
nextDateSeed
|
|
78
|
-
);
|
|
79
|
-
}
|
|
88
|
+
return candidateDate;
|
|
89
|
+
};
|
|
80
90
|
|
|
81
|
-
return
|
|
91
|
+
return (schedulesToEvaluate.length ? schedulesToEvaluate : [undefined])
|
|
92
|
+
.map(getCandidateDate)
|
|
93
|
+
.reduce((latestDate, candidateDate) =>
|
|
94
|
+
moment(candidateDate).isAfter(latestDate) ? candidateDate : latestDate
|
|
95
|
+
);
|
|
82
96
|
};
|
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
|
}
|