@managesome/eposmatic-toolkit 0.2.720 → 0.2.732
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/lib/helpers/types/order-engine.d.ts +40 -12
- package/lib/helpers/utils/cart-helpers.d.ts +2 -0
- package/lib/helpers/utils/halfhalf-setup-helpers.js +75 -52
- package/lib/helpers/utils/order-engine.d.ts +80 -79
- package/lib/helpers/utils/order-engine.js +351 -249
- package/lib/tests/choose-expensive-edge-cases.test.d.ts +1 -0
- package/lib/tests/choose-expensive-edge-cases.test.js +966 -0
- package/lib/tests/choose-expensive-flow-verification.test.d.ts +1 -0
- package/lib/tests/choose-expensive-flow-verification.test.js +702 -0
- package/lib/tests/halfhalf-selection-rules.test.d.ts +1 -0
- package/lib/tests/halfhalf-selection-rules.test.js +200 -0
- package/lib/tests/nested-halfhalf-segment-price.test.js +0 -227
- package/lib/tests/segment-price-floor.test.d.ts +1 -0
- package/lib/tests/segment-price-floor.test.js +143 -0
- package/lib/tests/specials-halfhalf-pricing.test.js +143 -585
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const order_engine_1 = require("../helpers/utils/order-engine");
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// Fixtures
|
|
7
|
+
// ============================================================================
|
|
8
|
+
function makeEngineOption(overrides = {}) {
|
|
9
|
+
return {
|
|
10
|
+
_id: 'opt-1',
|
|
11
|
+
ref: 'opt-1',
|
|
12
|
+
name: 'Option',
|
|
13
|
+
active: true,
|
|
14
|
+
sold_out: [],
|
|
15
|
+
sort_id: 1,
|
|
16
|
+
excluded_platforms: [],
|
|
17
|
+
default: false,
|
|
18
|
+
price: 0,
|
|
19
|
+
quantity: 1,
|
|
20
|
+
modifiers: [],
|
|
21
|
+
is_free: false,
|
|
22
|
+
loyalty_applied: false,
|
|
23
|
+
voucher_applied: false,
|
|
24
|
+
...overrides,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function makeEngineModifier(overrides = {}) {
|
|
28
|
+
return {
|
|
29
|
+
_id: 'mod-1',
|
|
30
|
+
ref: 'mod-1',
|
|
31
|
+
name: 'Modifier',
|
|
32
|
+
description: '',
|
|
33
|
+
active: true,
|
|
34
|
+
sold_out: [],
|
|
35
|
+
sort_id: 1,
|
|
36
|
+
min_num_options: 1,
|
|
37
|
+
max_num_options: 1,
|
|
38
|
+
depth: 0,
|
|
39
|
+
default: false,
|
|
40
|
+
options: [],
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function makeEngineItem(overrides = {}) {
|
|
45
|
+
return {
|
|
46
|
+
_id: 'item-1',
|
|
47
|
+
category_id: 'cat-1',
|
|
48
|
+
item_id: 'item-ref-1',
|
|
49
|
+
menu_id: 'menu-1',
|
|
50
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
51
|
+
name: 'Item',
|
|
52
|
+
description: '',
|
|
53
|
+
loyalty_applied: false,
|
|
54
|
+
voucher_applied: false,
|
|
55
|
+
price: 0,
|
|
56
|
+
gross_total: 0,
|
|
57
|
+
subtotal: 0,
|
|
58
|
+
tax_rate: 0,
|
|
59
|
+
quantity: 1,
|
|
60
|
+
image_url: '',
|
|
61
|
+
special_instructions: '',
|
|
62
|
+
item_discount_details: null,
|
|
63
|
+
modifiers: [],
|
|
64
|
+
...overrides,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function makeOrderItem(itemId) {
|
|
68
|
+
return {
|
|
69
|
+
_id: `order-${itemId}`,
|
|
70
|
+
item_id: itemId,
|
|
71
|
+
menu_id: null,
|
|
72
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
73
|
+
name: itemId,
|
|
74
|
+
price: 0,
|
|
75
|
+
subtotal: 0,
|
|
76
|
+
tax_rate: 0,
|
|
77
|
+
quantity: 1,
|
|
78
|
+
_net_amount: 0,
|
|
79
|
+
_gross_amount: 0,
|
|
80
|
+
_gst_amount: 0,
|
|
81
|
+
_discount_amount: 0,
|
|
82
|
+
special_instructions: '',
|
|
83
|
+
item_discount_details: null,
|
|
84
|
+
modifiers: null,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/** Classic segment group: one group per half, offering roma/milano. */
|
|
88
|
+
function makeSegment(segmentId, selection) {
|
|
89
|
+
return makeEngineModifier({
|
|
90
|
+
_id: segmentId,
|
|
91
|
+
name: segmentId,
|
|
92
|
+
depth: 0,
|
|
93
|
+
default: selection != null,
|
|
94
|
+
options: ['roma', 'milano'].map((optionId) => makeEngineOption({ _id: optionId, ref: optionId, default: selection === optionId })),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const makeClassicHalfHalf = (selections) => makeEngineItem({
|
|
98
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
99
|
+
modifiers: selections.map((selection, index) => makeSegment(`seg-${index + 1}`, selection)),
|
|
100
|
+
});
|
|
101
|
+
// ============================================================================
|
|
102
|
+
// getHalfHalfBlockedItemIds — modern selection list
|
|
103
|
+
// ============================================================================
|
|
104
|
+
describe('getHalfHalfBlockedItemIds (a same item can never make ALL sides)', () => {
|
|
105
|
+
it('2 segments: an item picked for one half is blocked for the other', () => {
|
|
106
|
+
const selections = [makeOrderItem('roma'), null];
|
|
107
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(selections, 1)).toEqual(['roma']);
|
|
108
|
+
// The half that already holds roma can re-pick it (no-op / re-customize).
|
|
109
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(selections, 0)).toEqual([]);
|
|
110
|
+
});
|
|
111
|
+
it('4 segments: an item may fill 3 halves but not the 4th', () => {
|
|
112
|
+
const threeSame = [makeOrderItem('roma'), makeOrderItem('roma'), makeOrderItem('roma'), null];
|
|
113
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(threeSame, 3)).toEqual(['roma']);
|
|
114
|
+
// With only two halves taken, both remaining segments may still pick it.
|
|
115
|
+
const twoSame = [makeOrderItem('roma'), makeOrderItem('roma'), null, null];
|
|
116
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(twoSame, 2)).toEqual([]);
|
|
117
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(twoSame, 3)).toEqual([]);
|
|
118
|
+
});
|
|
119
|
+
it('editing a segment: the item on the other half is blocked, own item is not', () => {
|
|
120
|
+
const selections = [makeOrderItem('roma'), makeOrderItem('milano')];
|
|
121
|
+
// Editing segment 1 (milano): roma would occupy both halves -> blocked.
|
|
122
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(selections, 1)).toEqual(['roma']);
|
|
123
|
+
// Editing segment 0 (roma): milano is the other half -> blocked.
|
|
124
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)(selections, 0)).toEqual(['milano']);
|
|
125
|
+
});
|
|
126
|
+
it('no target segment or a single segment blocks nothing', () => {
|
|
127
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)([makeOrderItem('roma'), null], null)).toEqual([]);
|
|
128
|
+
expect((0, order_engine_1.getHalfHalfBlockedItemIds)([makeOrderItem('roma')], 0)).toEqual([]);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
// ============================================================================
|
|
132
|
+
// getBlockedHalfHalfOptionPaths — classic drawer (engine tree)
|
|
133
|
+
// ============================================================================
|
|
134
|
+
describe('getBlockedHalfHalfOptionPaths (classic drawer)', () => {
|
|
135
|
+
it('2 segments: the picked item is blocked in the OTHER segment, not its own', () => {
|
|
136
|
+
const blocked = (0, order_engine_1.getBlockedHalfHalfOptionPaths)(makeClassicHalfHalf(['roma', null]));
|
|
137
|
+
expect(blocked.has('seg-2_roma')).toBe(true);
|
|
138
|
+
expect(blocked.has('seg-1_roma')).toBe(false);
|
|
139
|
+
expect(blocked.has('seg-2_milano')).toBe(false);
|
|
140
|
+
expect(blocked.has('seg-1_milano')).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
it('4 segments: blocked only when the pick would complete all sides', () => {
|
|
143
|
+
const threeSame = (0, order_engine_1.getBlockedHalfHalfOptionPaths)(makeClassicHalfHalf(['roma', 'roma', 'roma', null]));
|
|
144
|
+
expect(threeSame.has('seg-4_roma')).toBe(true);
|
|
145
|
+
// The segment that already holds roma can re-pick it.
|
|
146
|
+
expect(threeSame.has('seg-3_roma')).toBe(false);
|
|
147
|
+
const twoSame = (0, order_engine_1.getBlockedHalfHalfOptionPaths)(makeClassicHalfHalf(['roma', 'roma', null, null]));
|
|
148
|
+
expect(twoSame.has('seg-3_roma')).toBe(false);
|
|
149
|
+
expect(twoSame.has('seg-4_roma')).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
it('a single half-half SEGMENT item is never treated as segments', () => {
|
|
152
|
+
// Segment items carry their own option groups (e.g. two size groups sharing
|
|
153
|
+
// the same option id); with the flag they are never blocked.
|
|
154
|
+
const segmentItem = makeEngineItem({
|
|
155
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
156
|
+
modifiers: [
|
|
157
|
+
makeEngineModifier({
|
|
158
|
+
_id: 'size-1',
|
|
159
|
+
depth: 0,
|
|
160
|
+
default: true,
|
|
161
|
+
options: [makeEngineOption({ _id: 'l13', default: true })],
|
|
162
|
+
}),
|
|
163
|
+
makeEngineModifier({
|
|
164
|
+
_id: 'size-2',
|
|
165
|
+
depth: 0,
|
|
166
|
+
default: true,
|
|
167
|
+
options: [makeEngineOption({ _id: 'l13', default: true })],
|
|
168
|
+
}),
|
|
169
|
+
],
|
|
170
|
+
});
|
|
171
|
+
expect((0, order_engine_1.getBlockedHalfHalfOptionPaths)(segmentItem).has('size-2_l13')).toBe(true);
|
|
172
|
+
expect((0, order_engine_1.getBlockedHalfHalfOptionPaths)(segmentItem, true).size).toBe(0);
|
|
173
|
+
});
|
|
174
|
+
it('nested flagged half-half in a special: paths carry the full prefix; unflagged options are ignored', () => {
|
|
175
|
+
const makeNestedHalfHalf = (optionId, flagged, selection) => makeEngineOption({
|
|
176
|
+
_id: optionId,
|
|
177
|
+
ref: optionId,
|
|
178
|
+
ref_type: constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF,
|
|
179
|
+
choose_expensive_segment_price: flagged,
|
|
180
|
+
default: true,
|
|
181
|
+
modifiers: [makeSegment('seg-1', selection), makeSegment('seg-2', null)],
|
|
182
|
+
});
|
|
183
|
+
const item = makeEngineItem({
|
|
184
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL,
|
|
185
|
+
modifiers: [
|
|
186
|
+
makeEngineModifier({
|
|
187
|
+
_id: 'mod-pizza',
|
|
188
|
+
depth: 0,
|
|
189
|
+
default: true,
|
|
190
|
+
options: [makeNestedHalfHalf('hh-flag', true, 'roma'), makeNestedHalfHalf('hh-no-flag', false, 'roma')],
|
|
191
|
+
}),
|
|
192
|
+
],
|
|
193
|
+
});
|
|
194
|
+
const blocked = (0, order_engine_1.getBlockedHalfHalfOptionPaths)(item);
|
|
195
|
+
expect(blocked.has('mod-pizza_hh-flag_seg-2_roma')).toBe(true);
|
|
196
|
+
expect(blocked.has('mod-pizza_hh-flag_seg-1_roma')).toBe(false);
|
|
197
|
+
// Un-flagged nested half-half: untouched.
|
|
198
|
+
expect(blocked.has('mod-pizza_hh-no-flag_seg-2_roma')).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -222,230 +222,3 @@ describe('applyChooseExpensiveSegmentPrice (standalone regression)', () => {
|
|
|
222
222
|
expect(segB.options.find((o) => o._id === 'torent2').price).toBe(600);
|
|
223
223
|
});
|
|
224
224
|
});
|
|
225
|
-
describe('applyNestedChooseExpensiveSegmentPrice — selected-halves max', () => {
|
|
226
|
-
/** Nested half-half hub: one flagged HALF_HALF option holding the segments. */
|
|
227
|
-
function makeHub(segmentGroups) {
|
|
228
|
-
return makeEngineItem({
|
|
229
|
-
modifiers: [
|
|
230
|
-
makeEngineModifier({
|
|
231
|
-
_id: 'first-pizza',
|
|
232
|
-
name: 'First Pizza',
|
|
233
|
-
depth: 0,
|
|
234
|
-
options: [
|
|
235
|
-
makeEngineOption({
|
|
236
|
-
_id: 'halfhalf-1',
|
|
237
|
-
ref: 'halfhalf-1',
|
|
238
|
-
ref_type: constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF,
|
|
239
|
-
choose_expensive_segment_price: true,
|
|
240
|
-
default: true,
|
|
241
|
-
price: 0,
|
|
242
|
-
modifiers: segmentGroups,
|
|
243
|
-
}),
|
|
244
|
-
],
|
|
245
|
-
}),
|
|
246
|
-
],
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
/** Marks exactly one option per segment as selected. */
|
|
250
|
-
function withSelections(item, selections) {
|
|
251
|
-
return {
|
|
252
|
-
...item,
|
|
253
|
-
modifiers: item.modifiers.map((modifier) => ({
|
|
254
|
-
...modifier,
|
|
255
|
-
options: modifier.options.map((option) => option.ref_type === constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
|
|
256
|
-
? {
|
|
257
|
-
...option,
|
|
258
|
-
modifiers: option.modifiers.map((segment) => ({
|
|
259
|
-
...segment,
|
|
260
|
-
options: segment.options.map((o) => ({
|
|
261
|
-
...o,
|
|
262
|
-
default: o._id === selections[segment._id],
|
|
263
|
-
})),
|
|
264
|
-
})),
|
|
265
|
-
}
|
|
266
|
-
: option),
|
|
267
|
-
})),
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
function halfHalfOption(item) {
|
|
271
|
-
return item.modifiers[0].options[0];
|
|
272
|
-
}
|
|
273
|
-
/** Marks the given option of a segment as a free slot (`is_free`). */
|
|
274
|
-
function markFree(item, segmentId, optionId) {
|
|
275
|
-
return {
|
|
276
|
-
...item,
|
|
277
|
-
modifiers: item.modifiers.map((modifier) => ({
|
|
278
|
-
...modifier,
|
|
279
|
-
options: modifier.options.map((option) => option.ref_type === constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
|
|
280
|
-
? {
|
|
281
|
-
...option,
|
|
282
|
-
modifiers: option.modifiers.map((segment) => segment._id === segmentId
|
|
283
|
-
? {
|
|
284
|
-
...segment,
|
|
285
|
-
options: segment.options.map((o) => (o._id === optionId ? { ...o, is_free: true } : o)),
|
|
286
|
-
}
|
|
287
|
-
: segment),
|
|
288
|
-
}
|
|
289
|
-
: option),
|
|
290
|
-
})),
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
function segmentPrice(item, segmentId, optionId) {
|
|
294
|
-
const option = halfHalfOption(item).modifiers.find((m) => m._id === segmentId);
|
|
295
|
-
return option.options.find((o) => o._id === optionId).price;
|
|
296
|
-
}
|
|
297
|
-
/** Stamps `base_price = price` on every option (simulates an engine-built item). */
|
|
298
|
-
function withStampedBasePrices(item) {
|
|
299
|
-
const stampModifier = (modifier) => ({
|
|
300
|
-
...modifier,
|
|
301
|
-
options: modifier.options.map((option) => ({
|
|
302
|
-
...option,
|
|
303
|
-
base_price: option.price,
|
|
304
|
-
modifiers: option.modifiers.map(stampModifier),
|
|
305
|
-
})),
|
|
306
|
-
});
|
|
307
|
-
return { ...item, modifiers: item.modifiers.map(stampModifier) };
|
|
308
|
-
}
|
|
309
|
-
it('a cheap selected pair is NOT raised (max of the SELECTED halves only)', () => {
|
|
310
|
-
// seg-1: milan(500) selected, torent(600) not; seg-2: milan2(500) selected,
|
|
311
|
-
// torent2(600) not.
|
|
312
|
-
const item = withSelections(makeHub(makeSegmentGroups()), {
|
|
313
|
-
'seg-1': 'milan',
|
|
314
|
-
'seg-2': 'milan2',
|
|
315
|
-
});
|
|
316
|
-
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
317
|
-
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
318
|
-
expect(segmentPrice(result, 'seg-2', 'milan2')).toBe(500);
|
|
319
|
-
// Unselected expensive options keep their own prices.
|
|
320
|
-
expect(segmentPrice(result, 'seg-1', 'torent')).toBe(600);
|
|
321
|
-
});
|
|
322
|
-
it('LOWERS with basePrices when switching from an expensive to a cheap pair', () => {
|
|
323
|
-
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
324
|
-
// Selected: milan (500) + torent2 (600) -> milan is raised to 600.
|
|
325
|
-
const normalized = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
326
|
-
expect(segmentPrice(normalized, 'seg-1', 'milan')).toBe(600);
|
|
327
|
-
// User switches seg-2 to milan2 (cheap pair: 500+500). Re-applying with
|
|
328
|
-
// pristine basePrices brings the previously raised milan back to 500.
|
|
329
|
-
const switched = withSelections(normalized, { 'seg-1': 'milan', 'seg-2': 'milan2' });
|
|
330
|
-
const lowered = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(switched, { basePrices });
|
|
331
|
-
expect(segmentPrice(lowered, 'seg-1', 'milan')).toBe(500);
|
|
332
|
-
expect(segmentPrice(lowered, 'seg-2', 'milan2')).toBe(500);
|
|
333
|
-
});
|
|
334
|
-
it('changing picks: cheap pair lowers the raised half, swap keeps the max', () => {
|
|
335
|
-
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
336
|
-
const hub = makeHub(makeSegmentGroups());
|
|
337
|
-
// milan (500) + torent2 (600) -> milan raised to 600.
|
|
338
|
-
const premium = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(hub, { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
339
|
-
expect(segmentPrice(premium, 'seg-1', 'milan')).toBe(600);
|
|
340
|
-
// -> cheap pair milan + milan2: the previously raised milan returns to 500.
|
|
341
|
-
const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(premium, { 'seg-1': 'milan', 'seg-2': 'milan2' }), { basePrices });
|
|
342
|
-
expect(segmentPrice(cheap, 'seg-1', 'milan')).toBe(500);
|
|
343
|
-
expect(segmentPrice(cheap, 'seg-2', 'milan2')).toBe(500);
|
|
344
|
-
// De-selected torent2 keeps its own 600.
|
|
345
|
-
expect(segmentPrice(cheap, 'seg-2', 'torent2')).toBe(600);
|
|
346
|
-
// -> swap to torent + milan2: the newly expensive half is raised and the
|
|
347
|
-
// de-selected milan is re-based (no leftover 600).
|
|
348
|
-
const swapped = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(premium, { 'seg-1': 'torent', 'seg-2': 'milan2' }), { basePrices });
|
|
349
|
-
expect(segmentPrice(swapped, 'seg-1', 'torent')).toBe(600);
|
|
350
|
-
expect(segmentPrice(swapped, 'seg-2', 'milan2')).toBe(600);
|
|
351
|
-
expect(segmentPrice(swapped, 'seg-1', 'milan')).toBe(500);
|
|
352
|
-
});
|
|
353
|
-
it('full change matrix: every switch recomputes from base prices (pristine vs raised source)', () => {
|
|
354
|
-
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
355
|
-
const combos = [
|
|
356
|
-
{ 'seg-1': 'milan', 'seg-2': 'milan2' },
|
|
357
|
-
{ 'seg-1': 'milan', 'seg-2': 'torent2' },
|
|
358
|
-
{ 'seg-1': 'torent', 'seg-2': 'milan2' },
|
|
359
|
-
{ 'seg-1': 'torent', 'seg-2': 'torent2' },
|
|
360
|
-
];
|
|
361
|
-
const hub = makeHub(makeSegmentGroups());
|
|
362
|
-
for (const from of combos) {
|
|
363
|
-
const fromDisplay = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(hub, from));
|
|
364
|
-
for (const to of combos) {
|
|
365
|
-
const expected = Math.max(basePrices[to['seg-1']], basePrices[to['seg-2']]);
|
|
366
|
-
for (const source of [hub, fromDisplay]) {
|
|
367
|
-
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(source, to), {
|
|
368
|
-
basePrices,
|
|
369
|
-
});
|
|
370
|
-
expect(segmentPrice(result, 'seg-1', to['seg-1'])).toBe(expected);
|
|
371
|
-
expect(segmentPrice(result, 'seg-2', to['seg-2'])).toBe(expected);
|
|
372
|
-
// Every half de-selected by the switch is back to its own price.
|
|
373
|
-
for (const segmentId of ['seg-1', 'seg-2']) {
|
|
374
|
-
const fromOption = from[segmentId];
|
|
375
|
-
if ([to['seg-1'], to['seg-2']].includes(fromOption))
|
|
376
|
-
continue;
|
|
377
|
-
expect(segmentPrice(result, segmentId, fromOption)).toBe(basePrices[fromOption]);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
it('is idempotent when re-applied with basePrices', () => {
|
|
384
|
-
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
385
|
-
const once = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }), { basePrices });
|
|
386
|
-
const twice = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(once, { basePrices });
|
|
387
|
-
expect(twice).toEqual(once);
|
|
388
|
-
});
|
|
389
|
-
it('resetOrderEngineItemPrices restores nested segment prices (edit flow)', () => {
|
|
390
|
-
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
391
|
-
const raised = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
392
|
-
expect(segmentPrice(raised, 'seg-1', 'milan')).toBe(600);
|
|
393
|
-
// Reopening an edit: the persisted raise is re-based from the pristine map.
|
|
394
|
-
const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(raised, basePrices);
|
|
395
|
-
expect(segmentPrice(rebased, 'seg-1', 'milan')).toBe(500);
|
|
396
|
-
expect(segmentPrice(rebased, 'seg-1', 'torent')).toBe(600);
|
|
397
|
-
expect(segmentPrice(rebased, 'seg-2', 'torent2')).toBe(600);
|
|
398
|
-
// Changing the pick afterwards lowers correctly (no accumulation).
|
|
399
|
-
const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(rebased, { 'seg-1': 'milan', 'seg-2': 'milan2' }), { basePrices });
|
|
400
|
-
expect(segmentPrice(cheap, 'seg-1', 'milan')).toBe(500);
|
|
401
|
-
expect(segmentPrice(cheap, 'seg-2', 'milan2')).toBe(500);
|
|
402
|
-
});
|
|
403
|
-
it('single selected segment: nothing to raise', () => {
|
|
404
|
-
const item = withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan' });
|
|
405
|
-
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
406
|
-
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
407
|
-
// No selection in segment 2 — its options keep their own prices.
|
|
408
|
-
expect(segmentPrice(result, 'seg-2', 'milan2')).toBe(500);
|
|
409
|
-
expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
|
|
410
|
-
});
|
|
411
|
-
it('no selection at all: nothing is raised or invented', () => {
|
|
412
|
-
const item = withSelections(makeHub(makeSegmentGroups()), {});
|
|
413
|
-
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
414
|
-
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
415
|
-
expect(segmentPrice(result, 'seg-1', 'torent')).toBe(600);
|
|
416
|
-
expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
|
|
417
|
-
});
|
|
418
|
-
it('a free selected half neither drives the max nor gets raised', () => {
|
|
419
|
-
// The expensive side (torent2, 600) sits in a free slot: the paid half
|
|
420
|
-
// (milan, 500) must NOT be raised to it.
|
|
421
|
-
const item = markFree(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }), 'seg-2', 'torent2');
|
|
422
|
-
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
423
|
-
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
424
|
-
expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
|
|
425
|
-
});
|
|
426
|
-
it('re-bases from its own base_price memory when no map is passed (in-place changes)', () => {
|
|
427
|
-
// Engine-built item: every option carries its definition price as memory.
|
|
428
|
-
const built = withStampedBasePrices(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
429
|
-
const raised = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(built);
|
|
430
|
-
expect(segmentPrice(raised, 'seg-1', 'milan')).toBe(600);
|
|
431
|
-
// Switching to the cheap pair on the RAISED item, without any external
|
|
432
|
-
// map: the previously raised half goes back to its own 500.
|
|
433
|
-
const switched = withSelections(raised, { 'seg-1': 'milan', 'seg-2': 'milan2' });
|
|
434
|
-
const lowered = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(switched);
|
|
435
|
-
expect(segmentPrice(lowered, 'seg-1', 'milan')).toBe(500);
|
|
436
|
-
expect(segmentPrice(lowered, 'seg-2', 'milan2')).toBe(500);
|
|
437
|
-
// Re-applying without a change is stable.
|
|
438
|
-
expect((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(lowered)).toEqual(lowered);
|
|
439
|
-
});
|
|
440
|
-
it('without stamps and without a map the rule can only raise (legacy behavior)', () => {
|
|
441
|
-
// Hand-built item (no engine memory): the current price is all it knows.
|
|
442
|
-
const raised = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
443
|
-
expect(segmentPrice(raised, 'seg-1', 'milan')).toBe(600);
|
|
444
|
-
const switched = withSelections(raised, { 'seg-1': 'milan', 'seg-2': 'milan2' });
|
|
445
|
-
const still = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(switched);
|
|
446
|
-
// No memory -> the stale 600 is treated as milan's own price, so the cheap
|
|
447
|
-
// pair is NOT lowered — milan2 is even raised to it (legacy limitation).
|
|
448
|
-
expect(segmentPrice(still, 'seg-1', 'milan')).toBe(600);
|
|
449
|
-
expect(segmentPrice(still, 'seg-2', 'milan2')).toBe(600);
|
|
450
|
-
});
|
|
451
|
-
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const order_engine_1 = require("../helpers/utils/order-engine");
|
|
5
|
+
function makeEngineOption(overrides = {}) {
|
|
6
|
+
return {
|
|
7
|
+
_id: 'opt-1',
|
|
8
|
+
ref: 'opt-1',
|
|
9
|
+
name: 'Option',
|
|
10
|
+
active: true,
|
|
11
|
+
sold_out: [],
|
|
12
|
+
sort_id: 1,
|
|
13
|
+
excluded_platforms: [],
|
|
14
|
+
default: false,
|
|
15
|
+
price: 0,
|
|
16
|
+
quantity: 1,
|
|
17
|
+
modifiers: [],
|
|
18
|
+
is_free: false,
|
|
19
|
+
loyalty_applied: false,
|
|
20
|
+
voucher_applied: false,
|
|
21
|
+
...overrides,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function makeEngineModifier(overrides = {}) {
|
|
25
|
+
return {
|
|
26
|
+
_id: 'mod-1',
|
|
27
|
+
ref: 'mod-1',
|
|
28
|
+
name: 'Modifier',
|
|
29
|
+
description: '',
|
|
30
|
+
active: true,
|
|
31
|
+
sold_out: [],
|
|
32
|
+
sort_id: 1,
|
|
33
|
+
min_num_options: 1,
|
|
34
|
+
max_num_options: 1,
|
|
35
|
+
depth: 0,
|
|
36
|
+
default: false,
|
|
37
|
+
options: [],
|
|
38
|
+
...overrides,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function makeEngineItem(overrides = {}) {
|
|
42
|
+
return {
|
|
43
|
+
_id: 'item-1',
|
|
44
|
+
category_id: 'cat-1',
|
|
45
|
+
item_id: 'item-ref-1',
|
|
46
|
+
menu_id: 'menu-1',
|
|
47
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.ITEM,
|
|
48
|
+
name: 'Item',
|
|
49
|
+
description: '',
|
|
50
|
+
loyalty_applied: false,
|
|
51
|
+
voucher_applied: false,
|
|
52
|
+
price: 0,
|
|
53
|
+
gross_total: 0,
|
|
54
|
+
subtotal: 0,
|
|
55
|
+
tax_rate: 0,
|
|
56
|
+
quantity: 1,
|
|
57
|
+
image_url: '',
|
|
58
|
+
special_instructions: '',
|
|
59
|
+
item_discount_details: null,
|
|
60
|
+
modifiers: [],
|
|
61
|
+
...overrides,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A half-half SEGMENT item like ROMA/MILANO in the modern flow: the item price
|
|
66
|
+
* is 0 and the price lives on a single-option required "Pizza Size" group.
|
|
67
|
+
*/
|
|
68
|
+
function makeSegmentItem(sizePrice) {
|
|
69
|
+
return makeEngineItem({
|
|
70
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
71
|
+
modifiers: [
|
|
72
|
+
makeEngineModifier({
|
|
73
|
+
_id: 'mod-size',
|
|
74
|
+
options: [makeEngineOption({ _id: 'size-l', ref: 'size-l', price: sizePrice, default: true })],
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
describe('getOrderEngineOptionSegmentValue', () => {
|
|
80
|
+
it('sums the option price and its required (price-defining) option prices', () => {
|
|
81
|
+
const option = makeEngineOption({
|
|
82
|
+
price: 100,
|
|
83
|
+
modifiers: [
|
|
84
|
+
makeEngineModifier({
|
|
85
|
+
options: [makeEngineOption({ _id: 'leaf', price: 475, default: true })],
|
|
86
|
+
}),
|
|
87
|
+
],
|
|
88
|
+
});
|
|
89
|
+
expect((0, order_engine_1.getOrderEngineOptionSegmentValue)(option)).toBe(575);
|
|
90
|
+
});
|
|
91
|
+
it('ignores optional (non price-defining) modifier groups', () => {
|
|
92
|
+
const option = makeEngineOption({
|
|
93
|
+
price: 100,
|
|
94
|
+
modifiers: [
|
|
95
|
+
makeEngineModifier({
|
|
96
|
+
_id: 'req',
|
|
97
|
+
options: [makeEngineOption({ _id: 'leaf', price: 400 })],
|
|
98
|
+
}),
|
|
99
|
+
makeEngineModifier({
|
|
100
|
+
_id: 'opt',
|
|
101
|
+
min_num_options: 0,
|
|
102
|
+
max_num_options: 0,
|
|
103
|
+
options: [makeEngineOption({ _id: 'extra', price: 300, default: true })],
|
|
104
|
+
}),
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
expect((0, order_engine_1.getOrderEngineOptionSegmentValue)(option)).toBe(500);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe('applySegmentPriceFloor (half-half segment drawers)', () => {
|
|
111
|
+
it('raises the first priced required leaf to the floor (Milano 500 -> Roma 575)', () => {
|
|
112
|
+
const milano = makeSegmentItem(500);
|
|
113
|
+
const romaValue = (0, order_engine_1.getOrderEngineOptionSegmentValue)(makeSegmentItem(575).modifiers[0].options[0]);
|
|
114
|
+
const levelled = (0, order_engine_1.applySegmentPriceFloor)(milano, romaValue);
|
|
115
|
+
expect(levelled.modifiers[0].options[0].price).toBe(575);
|
|
116
|
+
expect((0, order_engine_1.getOrderEngineOptionSegmentValue)(levelled.modifiers[0].options[0])).toBe(575);
|
|
117
|
+
});
|
|
118
|
+
it('keeps the item unchanged when its value is at or above the floor', () => {
|
|
119
|
+
const roma = makeSegmentItem(575);
|
|
120
|
+
expect((0, order_engine_1.applySegmentPriceFloor)(roma, 575)).toBe(roma);
|
|
121
|
+
expect((0, order_engine_1.applySegmentPriceFloor)(roma, 500)).toBe(roma);
|
|
122
|
+
});
|
|
123
|
+
it('is a no-op without a floor', () => {
|
|
124
|
+
const item = makeSegmentItem(500);
|
|
125
|
+
expect((0, order_engine_1.applySegmentPriceFloor)(item, 0)).toBe(item);
|
|
126
|
+
});
|
|
127
|
+
it('raises the item price when the item itself carries the price', () => {
|
|
128
|
+
const item = makeEngineItem({ price: 400 });
|
|
129
|
+
const levelled = (0, order_engine_1.applySegmentPriceFloor)(item, 575);
|
|
130
|
+
expect(levelled.price).toBe(575);
|
|
131
|
+
});
|
|
132
|
+
it('falls back to the item price when no priced required leaf exists', () => {
|
|
133
|
+
const item = makeEngineItem({
|
|
134
|
+
modifiers: [
|
|
135
|
+
makeEngineModifier({
|
|
136
|
+
options: [makeEngineOption({ _id: 'free', price: 0, default: true })],
|
|
137
|
+
}),
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
const levelled = (0, order_engine_1.applySegmentPriceFloor)(item, 575);
|
|
141
|
+
expect(levelled.price).toBe(575);
|
|
142
|
+
});
|
|
143
|
+
});
|