@managesome/eposmatic-toolkit 0.2.718 → 0.2.720
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 +12 -0
- package/lib/helpers/utils/halfhalf-setup-helpers.js +23 -3
- package/lib/helpers/utils/order-engine.d.ts +46 -13
- package/lib/helpers/utils/order-engine.js +96 -23
- package/lib/tests/nested-halfhalf-segment-price.test.js +149 -0
- package/lib/tests/specials-halfhalf-pricing.test.js +507 -5
- package/package.json +1 -1
|
@@ -5,6 +5,18 @@ export interface IOrderEngineOption extends Omit<IModifierOption, 'modifiers' |
|
|
|
5
5
|
loyalty_applied: boolean;
|
|
6
6
|
voucher_applied: boolean;
|
|
7
7
|
price: number;
|
|
8
|
+
/**
|
|
9
|
+
* The price the engine assigned to this option when the item was built
|
|
10
|
+
* (the catalog/definition value — NOT a persisted order value). It is the
|
|
11
|
+
* engine's own memory for reversible price normalization such as
|
|
12
|
+
* `choose_expensive_segment_price`: normalizing re-bases each option from
|
|
13
|
+
* here first, so the rule can raise AND lower across selection changes
|
|
14
|
+
* without any caller having to keep a pristine copy of the item.
|
|
15
|
+
*
|
|
16
|
+
* Optionally absent (e.g. hand-built engine items): callers that normalize
|
|
17
|
+
* prices should then pass an explicit `basePrices` map instead.
|
|
18
|
+
*/
|
|
19
|
+
base_price?: number;
|
|
8
20
|
quantity: number;
|
|
9
21
|
modifiers: IOrderEngineModifier[];
|
|
10
22
|
}
|
|
@@ -539,13 +539,29 @@ function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quant
|
|
|
539
539
|
*/
|
|
540
540
|
function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform, existingOrderItem, orderItemId, chooseExpensiveSegmentPrice, }) {
|
|
541
541
|
let menu_id;
|
|
542
|
-
|
|
542
|
+
/**
|
|
543
|
+
* Definition prices of the SEGMENT ITEM (its own price + nested modifier
|
|
544
|
+
* option prices). The selected per-segment order items can carry prices
|
|
545
|
+
* raised by a previous `choose_expensive_segment_price` normalization (edit
|
|
546
|
+
* flow, de-structured line), so the engine stamps THIS baseline as its own
|
|
547
|
+
* memory `base_price` — re-apply can then raise AND lower.
|
|
548
|
+
*/
|
|
549
|
+
const getSegmentItemBasePrices = (itemId) => {
|
|
550
|
+
const workableItem = halfHalfItem.items.find((item) => item._id === itemId);
|
|
551
|
+
return workableItem == null ? {} : (0, order_engine_1.getOptionPriceMapFromItem)(workableItem, platform);
|
|
552
|
+
};
|
|
553
|
+
const getWorkableItemPrice = (itemId) => {
|
|
554
|
+
const workableItem = halfHalfItem.items.find((item) => item._id === itemId);
|
|
555
|
+
return workableItem == null ? undefined : (0, platform_catalog_1.getPriceEnum)(workableItem.price, platform);
|
|
556
|
+
};
|
|
557
|
+
const toEngineModifiers = (modifiers, depth, basePrices) => (modifiers ?? []).map((modifier) => {
|
|
543
558
|
const options = modifier.options ?? [];
|
|
544
559
|
const engineOptions = options.map((option) => ({
|
|
545
560
|
_id: option._id,
|
|
546
561
|
ref: option.ref,
|
|
547
562
|
name: option.name,
|
|
548
563
|
price: option.price,
|
|
564
|
+
base_price: basePrices[option._id] ?? option.price,
|
|
549
565
|
quantity: option.quantity || 1,
|
|
550
566
|
is_free: option.is_free ?? false,
|
|
551
567
|
default: true,
|
|
@@ -555,7 +571,7 @@ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems,
|
|
|
555
571
|
sold_out: [],
|
|
556
572
|
sort_id: 0,
|
|
557
573
|
excluded_platforms: [],
|
|
558
|
-
modifiers: toEngineModifiers(option.modifiers, depth + 1),
|
|
574
|
+
modifiers: toEngineModifiers(option.modifiers, depth + 1, basePrices),
|
|
559
575
|
}));
|
|
560
576
|
return {
|
|
561
577
|
_id: modifier._id,
|
|
@@ -581,12 +597,16 @@ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems,
|
|
|
581
597
|
menu_id = selectedItem.menu_id;
|
|
582
598
|
}
|
|
583
599
|
const segment = halfHalfItem.segments[s];
|
|
600
|
+
const segmentBasePrices = getSegmentItemBasePrices(selectedItem.item_id);
|
|
584
601
|
const segmentOptions = [
|
|
585
602
|
{
|
|
586
603
|
_id: selectedItem.item_id,
|
|
587
604
|
ref: selectedItem.item_id,
|
|
588
605
|
name: selectedItem.name,
|
|
589
606
|
price: selectedItem.price,
|
|
607
|
+
// Definition price, not the (possibly previously raised) selected
|
|
608
|
+
// order item price — see `getSegmentItemBasePrices`.
|
|
609
|
+
base_price: getWorkableItemPrice(selectedItem.item_id) ?? selectedItem.price,
|
|
590
610
|
quantity: selectedItem.quantity || 1,
|
|
591
611
|
is_free: false,
|
|
592
612
|
default: true,
|
|
@@ -596,7 +616,7 @@ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems,
|
|
|
596
616
|
sold_out: [],
|
|
597
617
|
sort_id: 0,
|
|
598
618
|
excluded_platforms: [],
|
|
599
|
-
modifiers: toEngineModifiers(selectedItem.modifiers, 1),
|
|
619
|
+
modifiers: toEngineModifiers(selectedItem.modifiers, 1, segmentBasePrices),
|
|
600
620
|
},
|
|
601
621
|
];
|
|
602
622
|
const segmentModifier = {
|
|
@@ -7,11 +7,15 @@ export declare function syncOrderEngineItem(orderEngineItem: IOrderEngineItem):
|
|
|
7
7
|
export declare function checkIfModifiersExist(modifiers: IModifier[] | null, platform: PLATFORMS_ENUM | undefined): boolean;
|
|
8
8
|
export declare function makeSpaceForFreeOptions(options: IOrderEngineOption[], num_free_options: number | null | undefined): IOrderEngineOption[];
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
10
|
+
* OPTIONAL override of the pristine-price baseline for
|
|
11
|
+
* `choose_expensive_segment_price`.
|
|
12
|
+
*
|
|
13
|
+
* The engine normally re-bases itself from the `base_price` stamped on every
|
|
14
|
+
* option when the item was built (its own memory — see `IOrderEngineOption`),
|
|
15
|
+
* so re-applying the rule after a selection change can LOWER prices again and
|
|
16
|
+
* callers never have to keep a pristine copy of the item. Pass `basePrices`
|
|
17
|
+
* only when the baseline must come from somewhere else (e.g. a pre-computed
|
|
18
|
+
* `_id -> price` map); it takes precedence over the stamps.
|
|
15
19
|
*/
|
|
16
20
|
export interface IChooseExpensiveSegmentPriceOptions {
|
|
17
21
|
basePrices?: Record<string, number>;
|
|
@@ -25,10 +29,11 @@ export interface IChooseExpensiveSegmentPriceOptions {
|
|
|
25
29
|
* half the user actually picked sets the charged price for every picked half
|
|
26
30
|
* (cheaper halves are raised to it; picking a cheaper combination later
|
|
27
31
|
* lowers them again). It cannot run during `IHalfHalfSetup -> IItem`
|
|
28
|
-
* conversion — it runs at display/submit time on
|
|
29
|
-
*
|
|
30
|
-
* the result
|
|
31
|
-
*
|
|
32
|
+
* conversion — it runs at display/submit time on the engine item, re-basing
|
|
33
|
+
* every price from the engine's own memory (the `base_price` stamped at
|
|
34
|
+
* conversion) so the result NEVER accumulates raises, even when the item is
|
|
35
|
+
* mutated in place across selection changes. `basePrices` can override that
|
|
36
|
+
* baseline (see `IChooseExpensiveSegmentPriceOptions`).
|
|
32
37
|
*
|
|
33
38
|
* Rule: `price = base + upsell` — the item (option) level upsell is already
|
|
34
39
|
* baked into the option price at conversion. choose_expensive compares every
|
|
@@ -68,14 +73,42 @@ export declare function applyNestedChooseExpensiveSegmentPrice(orderEngineItem:
|
|
|
68
73
|
* item (`modifiers -> options -> nested modifiers -> options ...`). Used as
|
|
69
74
|
* the baseline for `choose_expensive_segment_price` so applying the rule never
|
|
70
75
|
* depends on previously raised prices.
|
|
76
|
+
*
|
|
77
|
+
* The flat `_id -> price` shape can only represent ONE value per option
|
|
78
|
+
* `_id`, so callers MUST keep the map to a single price scope. A half-half
|
|
79
|
+
* nested in a special is exactly the case where that fails: its segment
|
|
80
|
+
* options are built FROM the special's items, so they reuse the SAME `_id`s
|
|
81
|
+
* as the special's top-level options while their prices differ (top-level =
|
|
82
|
+
* full upsell, segment = per-segment share). `nestedHalfHalfSegmentsOnly`
|
|
83
|
+
* keeps the map to the subtrees of HALF_HALF options (the segment trees plus
|
|
84
|
+
* their nested modifiers), excluding top-level option prices — the special
|
|
85
|
+
* drawer feeds this map to `resetOrderEngineItemPrices` / the nested
|
|
86
|
+
* normalization, so the walk order of the item can never leak a top-level
|
|
87
|
+
* price into a segment (or the other way around). Standalone half-half items
|
|
88
|
+
* are a single scope: every segment shares its option `_id`s with the SAME
|
|
89
|
+
* price, so the unscoped map is safe there.
|
|
71
90
|
*/
|
|
72
|
-
export declare function getOptionPriceMapFromItem(item: IItem | ISimplifiedCatalogItem, platform: PLATFORMS_ENUM | null | undefined
|
|
91
|
+
export declare function getOptionPriceMapFromItem(item: IItem | ISimplifiedCatalogItem, platform: PLATFORMS_ENUM | null | undefined, options?: {
|
|
92
|
+
nestedHalfHalfSegmentsOnly?: boolean;
|
|
93
|
+
}): Record<string, number>;
|
|
73
94
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
95
|
+
* Re-bases every engine option price from the engine's own memory (the
|
|
96
|
+
* `base_price` stamped at conversion), optionally overridden by an explicit
|
|
97
|
+
* `basePrices` map. Called when a drawer builds the engine item so state
|
|
76
98
|
* (sidebar prices, edit baselines) never carries a previous normalization.
|
|
99
|
+
*
|
|
100
|
+
* `options.nestedHalfHalfOnly` restricts the reset to the subtrees of
|
|
101
|
+
* HALF_HALF options — the exact scope `getOptionPriceMapFromItem` builds when
|
|
102
|
+
* called with `nestedHalfHalfSegmentsOnly`. Only needed when an external
|
|
103
|
+
* `basePrices` map is passed (a special's top-level options reuse their `_id`s
|
|
104
|
+
* at different prices, so a blanket reset with the scoped map would push the
|
|
105
|
+
* segment share onto the top-level option). With the engine's own stamps the
|
|
106
|
+
* scope is inherent — pass the SAME scope to the map builder and to this
|
|
107
|
+
* reset when you do pass a map.
|
|
77
108
|
*/
|
|
78
|
-
export declare function resetOrderEngineItemPrices(orderEngineItem: IOrderEngineItem, basePrices
|
|
109
|
+
export declare function resetOrderEngineItemPrices(orderEngineItem: IOrderEngineItem, basePrices?: Record<string, number>, options?: {
|
|
110
|
+
nestedHalfHalfOnly?: boolean;
|
|
111
|
+
}): IOrderEngineItem;
|
|
79
112
|
export type IOrderEngineItemRewardClaim = {
|
|
80
113
|
type: 'offer';
|
|
81
114
|
offerId: string;
|
|
@@ -287,13 +287,17 @@ function makeSpaceForFreeOptions(options, num_free_options) {
|
|
|
287
287
|
}
|
|
288
288
|
return [...defaultPriceOptions, ...nonDefaultOptions].sort((a, b) => a.sort_id - b.sort_id);
|
|
289
289
|
}
|
|
290
|
-
/**
|
|
290
|
+
/**
|
|
291
|
+
* Restores engine option prices from the pristine baseline: the per-option
|
|
292
|
+
* `base_price` the engine stamped when the item was built, overridden by the
|
|
293
|
+
* optional external `_id -> price` map (see
|
|
294
|
+
* `IChooseExpensiveSegmentPriceOptions`). This is the engine's own memory —
|
|
295
|
+
* re-basing never depends on callers keeping a pristine copy of the item.
|
|
296
|
+
*/
|
|
291
297
|
const resetEngineOptionPrices = (modifiers, basePrices) => {
|
|
292
|
-
if (basePrices == null)
|
|
293
|
-
return modifiers;
|
|
294
298
|
const resetOption = (option) => ({
|
|
295
299
|
...option,
|
|
296
|
-
price: basePrices[option._id] ?? option.price,
|
|
300
|
+
price: basePrices?.[option._id] ?? option.base_price ?? option.price,
|
|
297
301
|
modifiers: (option.modifiers ?? []).map((modifier) => ({
|
|
298
302
|
...modifier,
|
|
299
303
|
options: (modifier.options ?? []).map(resetOption),
|
|
@@ -304,6 +308,24 @@ const resetEngineOptionPrices = (modifiers, basePrices) => {
|
|
|
304
308
|
options: (modifier.options ?? []).map(resetOption),
|
|
305
309
|
}));
|
|
306
310
|
};
|
|
311
|
+
/** True when two modifier trees carry the same option prices at the same positions. */
|
|
312
|
+
const haveSameOptionPrices = (a, b) => {
|
|
313
|
+
if (a.length !== b.length)
|
|
314
|
+
return false;
|
|
315
|
+
for (let i = 0; i < a.length; i++) {
|
|
316
|
+
const optionsA = a[i].options ?? [];
|
|
317
|
+
const optionsB = b[i].options ?? [];
|
|
318
|
+
if (optionsA.length !== optionsB.length)
|
|
319
|
+
return false;
|
|
320
|
+
for (let j = 0; j < optionsA.length; j++) {
|
|
321
|
+
if ((optionsA[j].price ?? 0) !== (optionsB[j].price ?? 0))
|
|
322
|
+
return false;
|
|
323
|
+
if (!haveSameOptionPrices(optionsA[j].modifiers ?? [], optionsB[j].modifiers ?? []))
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return true;
|
|
328
|
+
};
|
|
307
329
|
/**
|
|
308
330
|
* Applies `choose_expensive_segment_price` to an order engine item converted
|
|
309
331
|
* from a half-half setup (top-level modifier groups = segments, single-select
|
|
@@ -313,10 +335,11 @@ const resetEngineOptionPrices = (modifiers, basePrices) => {
|
|
|
313
335
|
* half the user actually picked sets the charged price for every picked half
|
|
314
336
|
* (cheaper halves are raised to it; picking a cheaper combination later
|
|
315
337
|
* lowers them again). It cannot run during `IHalfHalfSetup -> IItem`
|
|
316
|
-
* conversion — it runs at display/submit time on
|
|
317
|
-
*
|
|
318
|
-
* the result
|
|
319
|
-
*
|
|
338
|
+
* conversion — it runs at display/submit time on the engine item, re-basing
|
|
339
|
+
* every price from the engine's own memory (the `base_price` stamped at
|
|
340
|
+
* conversion) so the result NEVER accumulates raises, even when the item is
|
|
341
|
+
* mutated in place across selection changes. `basePrices` can override that
|
|
342
|
+
* baseline (see `IChooseExpensiveSegmentPriceOptions`).
|
|
320
343
|
*
|
|
321
344
|
* Rule: `price = base + upsell` — the item (option) level upsell is already
|
|
322
345
|
* baked into the option price at conversion. choose_expensive compares every
|
|
@@ -357,7 +380,9 @@ function applyChooseExpensiveSegmentPrice(orderEngineItem, options) {
|
|
|
357
380
|
* segment), otherwise the raised segment groups.
|
|
358
381
|
*/
|
|
359
382
|
function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options) {
|
|
360
|
-
|
|
383
|
+
// Re-base from the engine's own memory (or the caller's override) so the
|
|
384
|
+
// rule is always a pure function of the CURRENT selection: switching to a
|
|
385
|
+
// cheaper combination lowers the previously raised halves again.
|
|
361
386
|
const groups = resetEngineOptionPrices(segmentGroups, options?.basePrices);
|
|
362
387
|
// Prices of ALL options of required (price-defining) modifier groups, at
|
|
363
388
|
// any depth — e.g. "Pizza Size" -> "Large 13"" is part of the item's price
|
|
@@ -394,7 +419,7 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options) {
|
|
|
394
419
|
}
|
|
395
420
|
}
|
|
396
421
|
if (selections.length === 0)
|
|
397
|
-
return
|
|
422
|
+
return haveSameOptionPrices(groups, segmentGroups) ? null : groups;
|
|
398
423
|
// Max over the SELECTED halves only — the "expensive segment" is the most
|
|
399
424
|
// expensive half the user actually picked, not the most expensive option in
|
|
400
425
|
// the setup (otherwise picking A+B would charge as if C+D were chosen).
|
|
@@ -408,7 +433,7 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options) {
|
|
|
408
433
|
}
|
|
409
434
|
}
|
|
410
435
|
if (maxSegmentValue <= 0)
|
|
411
|
-
return
|
|
436
|
+
return haveSameOptionPrices(groups, segmentGroups) ? null : groups;
|
|
412
437
|
// RULE for where the raise lands: on the FIRST PRICED element along the
|
|
413
438
|
// segment's price chain —
|
|
414
439
|
// 1. the segment option's own price (the item base) when it is non-zero
|
|
@@ -448,7 +473,7 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options) {
|
|
|
448
473
|
})),
|
|
449
474
|
};
|
|
450
475
|
};
|
|
451
|
-
|
|
476
|
+
const raised = groups.map((modifier, g) => {
|
|
452
477
|
const options = (modifier.options ?? []).map((option, o) => {
|
|
453
478
|
const selection = selections.find((s) => s.groupIndex === g && s.optionIndex === o);
|
|
454
479
|
if (selection == null)
|
|
@@ -465,6 +490,9 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options) {
|
|
|
465
490
|
});
|
|
466
491
|
return { ...modifier, options };
|
|
467
492
|
});
|
|
493
|
+
// Nothing to change (pristine prices for the current selection): report
|
|
494
|
+
// "no change" so callers keep the original engine item object.
|
|
495
|
+
return haveSameOptionPrices(raised, segmentGroups) ? null : raised;
|
|
468
496
|
}
|
|
469
497
|
/**
|
|
470
498
|
* True when the item has a top-level modifier group option that is a nested
|
|
@@ -502,26 +530,67 @@ function applyNestedChooseExpensiveSegmentPrice(orderEngineItem, options) {
|
|
|
502
530
|
* item (`modifiers -> options -> nested modifiers -> options ...`). Used as
|
|
503
531
|
* the baseline for `choose_expensive_segment_price` so applying the rule never
|
|
504
532
|
* depends on previously raised prices.
|
|
533
|
+
*
|
|
534
|
+
* The flat `_id -> price` shape can only represent ONE value per option
|
|
535
|
+
* `_id`, so callers MUST keep the map to a single price scope. A half-half
|
|
536
|
+
* nested in a special is exactly the case where that fails: its segment
|
|
537
|
+
* options are built FROM the special's items, so they reuse the SAME `_id`s
|
|
538
|
+
* as the special's top-level options while their prices differ (top-level =
|
|
539
|
+
* full upsell, segment = per-segment share). `nestedHalfHalfSegmentsOnly`
|
|
540
|
+
* keeps the map to the subtrees of HALF_HALF options (the segment trees plus
|
|
541
|
+
* their nested modifiers), excluding top-level option prices — the special
|
|
542
|
+
* drawer feeds this map to `resetOrderEngineItemPrices` / the nested
|
|
543
|
+
* normalization, so the walk order of the item can never leak a top-level
|
|
544
|
+
* price into a segment (or the other way around). Standalone half-half items
|
|
545
|
+
* are a single scope: every segment shares its option `_id`s with the SAME
|
|
546
|
+
* price, so the unscoped map is safe there.
|
|
505
547
|
*/
|
|
506
|
-
function getOptionPriceMapFromItem(item, platform) {
|
|
548
|
+
function getOptionPriceMapFromItem(item, platform, options) {
|
|
507
549
|
const priceMap = {};
|
|
508
|
-
const
|
|
550
|
+
const nestedHalfHalfSegmentsOnly = options?.nestedHalfHalfSegmentsOnly === true;
|
|
551
|
+
const walk = (modifiers, insideHalfHalf) => {
|
|
509
552
|
(modifiers ?? []).forEach((modifier) => {
|
|
510
553
|
(modifier.options ?? []).forEach((option) => {
|
|
511
|
-
|
|
512
|
-
|
|
554
|
+
const isHalfHalf = option.ref_type === constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF;
|
|
555
|
+
// Inside a half-half subtree every option belongs to the segment
|
|
556
|
+
// scope; at the top level only record when not scoped (or record
|
|
557
|
+
// nothing because the option is the half-half hub itself, whose price
|
|
558
|
+
// is NOT part of the segment scope).
|
|
559
|
+
if (insideHalfHalf || !nestedHalfHalfSegmentsOnly) {
|
|
560
|
+
priceMap[option._id] = (0, platform_catalog_1.getPriceEnum)(option.price, platform);
|
|
561
|
+
}
|
|
562
|
+
walk(option.modifiers, insideHalfHalf || isHalfHalf);
|
|
513
563
|
});
|
|
514
564
|
});
|
|
515
565
|
};
|
|
516
|
-
walk(item.modifiers);
|
|
566
|
+
walk(item.modifiers, false);
|
|
517
567
|
return priceMap;
|
|
518
568
|
}
|
|
519
569
|
/**
|
|
520
|
-
*
|
|
521
|
-
*
|
|
570
|
+
* Re-bases every engine option price from the engine's own memory (the
|
|
571
|
+
* `base_price` stamped at conversion), optionally overridden by an explicit
|
|
572
|
+
* `basePrices` map. Called when a drawer builds the engine item so state
|
|
522
573
|
* (sidebar prices, edit baselines) never carries a previous normalization.
|
|
574
|
+
*
|
|
575
|
+
* `options.nestedHalfHalfOnly` restricts the reset to the subtrees of
|
|
576
|
+
* HALF_HALF options — the exact scope `getOptionPriceMapFromItem` builds when
|
|
577
|
+
* called with `nestedHalfHalfSegmentsOnly`. Only needed when an external
|
|
578
|
+
* `basePrices` map is passed (a special's top-level options reuse their `_id`s
|
|
579
|
+
* at different prices, so a blanket reset with the scoped map would push the
|
|
580
|
+
* segment share onto the top-level option). With the engine's own stamps the
|
|
581
|
+
* scope is inherent — pass the SAME scope to the map builder and to this
|
|
582
|
+
* reset when you do pass a map.
|
|
523
583
|
*/
|
|
524
|
-
function resetOrderEngineItemPrices(orderEngineItem, basePrices) {
|
|
584
|
+
function resetOrderEngineItemPrices(orderEngineItem, basePrices, options) {
|
|
585
|
+
if (options?.nestedHalfHalfOnly === true) {
|
|
586
|
+
const modifiers = (orderEngineItem.modifiers ?? []).map((modifier) => ({
|
|
587
|
+
...modifier,
|
|
588
|
+
options: (modifier.options ?? []).map((option) => option.ref_type !== constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
|
|
589
|
+
? option
|
|
590
|
+
: { ...option, modifiers: resetEngineOptionPrices(option.modifiers ?? [], basePrices) }),
|
|
591
|
+
}));
|
|
592
|
+
return syncOrderEngineItem({ ...orderEngineItem, modifiers });
|
|
593
|
+
}
|
|
525
594
|
const modifiers = resetEngineOptionPrices(orderEngineItem.modifiers ?? [], basePrices);
|
|
526
595
|
return syncOrderEngineItem({ ...orderEngineItem, modifiers });
|
|
527
596
|
}
|
|
@@ -611,13 +680,17 @@ function convertCatalogItemModifierToOrderEngineItemModifier({ modifiers, orderI
|
|
|
611
680
|
depth: depth + 1,
|
|
612
681
|
});
|
|
613
682
|
// If zeroPriceRefs is present then we set the price to 0
|
|
614
|
-
const
|
|
615
|
-
|
|
616
|
-
|
|
683
|
+
const isZeroPricedOption = zeroPriceRefs != null && (zeroPriceRefs.includes(option.ref) || zeroPriceRefs.includes(modifier.ref));
|
|
684
|
+
const optionPrice = isZeroPricedOption ? 0 : price;
|
|
685
|
+
// The engine's own memory records the CATALOG value (`price` above may
|
|
686
|
+
// come from the persisted order item in edit flows) so re-basing never
|
|
687
|
+
// inherits a previously normalized (raised) price.
|
|
688
|
+
const basePrice = isZeroPricedOption ? 0 : (0, platform_catalog_1.getPriceEnum)(option.price, platform);
|
|
617
689
|
const orderEngineSingleOption = {
|
|
618
690
|
...option,
|
|
619
691
|
default: isDefaultOption,
|
|
620
692
|
price: optionPrice,
|
|
693
|
+
base_price: basePrice,
|
|
621
694
|
quantity: qty,
|
|
622
695
|
modifiers: newModifiers,
|
|
623
696
|
is_free,
|
|
@@ -270,10 +270,42 @@ describe('applyNestedChooseExpensiveSegmentPrice — selected-halves max', () =>
|
|
|
270
270
|
function halfHalfOption(item) {
|
|
271
271
|
return item.modifiers[0].options[0];
|
|
272
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
|
+
}
|
|
273
293
|
function segmentPrice(item, segmentId, optionId) {
|
|
274
294
|
const option = halfHalfOption(item).modifiers.find((m) => m._id === segmentId);
|
|
275
295
|
return option.options.find((o) => o._id === optionId).price;
|
|
276
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
|
+
}
|
|
277
309
|
it('a cheap selected pair is NOT raised (max of the SELECTED halves only)', () => {
|
|
278
310
|
// seg-1: milan(500) selected, torent(600) not; seg-2: milan2(500) selected,
|
|
279
311
|
// torent2(600) not.
|
|
@@ -299,4 +331,121 @@ describe('applyNestedChooseExpensiveSegmentPrice — selected-halves max', () =>
|
|
|
299
331
|
expect(segmentPrice(lowered, 'seg-1', 'milan')).toBe(500);
|
|
300
332
|
expect(segmentPrice(lowered, 'seg-2', 'milan2')).toBe(500);
|
|
301
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
|
+
});
|
|
302
451
|
});
|
|
@@ -844,11 +844,13 @@ describe('choose_expensive_segment_price end-to-end (specials with half-half)',
|
|
|
844
844
|
{ item_id: 'torent', sort_id: 2, upsell_price: { BASE_PRICE: 200 } },
|
|
845
845
|
{ item_id: 'halfhalf-1', sort_id: 3, upsell_price: { BASE_PRICE: 100 } },
|
|
846
846
|
];
|
|
847
|
-
function
|
|
848
|
-
|
|
847
|
+
function buildSpecialItem(items = specialItems) {
|
|
848
|
+
return (0, specials_helpers_1.convertSpecialToItem)(modifySpecialItems(makeSpecial(), items), {
|
|
849
849
|
milan: makeItem('milan', { modifiers: makePizzaSizeAndExtra() }),
|
|
850
850
|
torent: makeItem('torent', { price: { BASE_PRICE: 1200 } }),
|
|
851
851
|
}, { 'halfhalf-1': makeHalfHalfSetup() });
|
|
852
|
+
}
|
|
853
|
+
function buildEngine(item = buildSpecialItem()) {
|
|
852
854
|
return (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
853
855
|
item,
|
|
854
856
|
orderItem: undefined,
|
|
@@ -859,6 +861,30 @@ describe('choose_expensive_segment_price end-to-end (specials with half-half)',
|
|
|
859
861
|
soldoutModifiers: [],
|
|
860
862
|
});
|
|
861
863
|
}
|
|
864
|
+
/**
|
|
865
|
+
* Scoped baseline: ONLY the nested half-half segment prices. A special's
|
|
866
|
+
* top-level item options reuse their `_id`s at different prices (full upsell
|
|
867
|
+
* vs per-segment share), so the unscoped map is walk-order dependent.
|
|
868
|
+
*/
|
|
869
|
+
function nestedBasePrices(item = buildSpecialItem()) {
|
|
870
|
+
return (0, order_engine_1.getOptionPriceMapFromItem)(item, PLATFORM, { nestedHalfHalfSegmentsOnly: true });
|
|
871
|
+
}
|
|
872
|
+
/** Real UI flow: pick one option per segment of the nested half-half. */
|
|
873
|
+
function selectNested(engine, picks) {
|
|
874
|
+
let next = engine;
|
|
875
|
+
next = (0, order_engine_1.setCheckFieldInOrderEngineItem)(next, `mod-pizza_halfhalf-1_seg-1_${picks[0]}`, true);
|
|
876
|
+
next = (0, order_engine_1.setCheckFieldInOrderEngineItem)(next, `mod-pizza_halfhalf-1_seg-2_${picks[1]}`, true);
|
|
877
|
+
return next;
|
|
878
|
+
}
|
|
879
|
+
function nestedHalfHalf(engine) {
|
|
880
|
+
return engine.modifiers[0].options.find((o) => o._id === 'halfhalf-1');
|
|
881
|
+
}
|
|
882
|
+
function nestedPrice(engine, segmentIndex, optionId) {
|
|
883
|
+
return nestedHalfHalf(engine).modifiers[segmentIndex].options.find((o) => o._id === optionId).price;
|
|
884
|
+
}
|
|
885
|
+
function topLevelPrice(engine, optionId) {
|
|
886
|
+
return engine.modifiers[0].options.find((o) => o._id === optionId).price;
|
|
887
|
+
}
|
|
862
888
|
function segmentOptions(engine, halfHalfId = 'halfhalf-1') {
|
|
863
889
|
const hh = engine.modifiers[0].options.find((o) => o._id === halfHalfId);
|
|
864
890
|
return hh.modifiers.map((seg) => seg.options.map((o) => ({ id: o._id, price: o.price, default: o.default })));
|
|
@@ -959,6 +985,153 @@ describe('choose_expensive_segment_price end-to-end (specials with half-half)',
|
|
|
959
985
|
expect(hhFlag.modifiers[0].options.find((o) => o._id === 'milan').price).toBe(100);
|
|
960
986
|
expect(hhNoFlag.modifiers[0].options.find((o) => o._id === 'milan').price).toBe(0);
|
|
961
987
|
});
|
|
988
|
+
it('nested base prices are scoped and walk-order independent (top-level price never leaks)', () => {
|
|
989
|
+
// Half-half FIRST in the item order: an unscoped map ends with the
|
|
990
|
+
// top-level torent price (200) instead of the segment share (100).
|
|
991
|
+
const halfHalfFirst = [
|
|
992
|
+
{ item_id: 'halfhalf-1', sort_id: 1, upsell_price: { BASE_PRICE: 100 } },
|
|
993
|
+
{ item_id: 'milan', sort_id: 2, upsell_price: null },
|
|
994
|
+
{ item_id: 'torent', sort_id: 3, upsell_price: { BASE_PRICE: 200 } },
|
|
995
|
+
];
|
|
996
|
+
const specialItem = buildSpecialItem(halfHalfFirst);
|
|
997
|
+
const scoped = nestedBasePrices(specialItem);
|
|
998
|
+
// Segment scope only: the half-half hub and top-level items are absent.
|
|
999
|
+
expect(scoped.torent).toBe(100);
|
|
1000
|
+
expect(scoped.milan).toBe(0);
|
|
1001
|
+
expect(scoped['halfhalf-1']).toBeUndefined();
|
|
1002
|
+
// Unscoped, the LAST walker wins — here the top-level torent (200), which
|
|
1003
|
+
// is exactly why the drawer scopes the map for specials.
|
|
1004
|
+
expect((0, order_engine_1.getOptionPriceMapFromItem)(specialItem, PLATFORM).torent).toBe(200);
|
|
1005
|
+
// With the scoped map the pipeline stays correct: the drawn half is raised
|
|
1006
|
+
// to the segment share (100) and top-level prices are never touched.
|
|
1007
|
+
const engine = (0, order_engine_1.resetOrderEngineItemPrices)(buildEngine(specialItem), scoped, {
|
|
1008
|
+
nestedHalfHalfOnly: true,
|
|
1009
|
+
});
|
|
1010
|
+
expect(topLevelPrice(engine, 'torent')).toBe(200);
|
|
1011
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(engine, ['milan', 'torent']), {
|
|
1012
|
+
basePrices: scoped,
|
|
1013
|
+
});
|
|
1014
|
+
expect(nestedPrice(result, 0, 'milan')).toBe(100);
|
|
1015
|
+
expect(nestedPrice(result, 1, 'torent')).toBe(100);
|
|
1016
|
+
expect(topLevelPrice(result, 'torent')).toBe(200);
|
|
1017
|
+
// Default order (half-half LAST): the scoped map is IDENTICAL, while the
|
|
1018
|
+
// unscoped one flips to the segment share — order must not matter.
|
|
1019
|
+
const defaultItem = buildSpecialItem();
|
|
1020
|
+
expect(nestedBasePrices(defaultItem).torent).toBe(100);
|
|
1021
|
+
expect((0, order_engine_1.getOptionPriceMapFromItem)(defaultItem, PLATFORM).torent).toBe(100);
|
|
1022
|
+
});
|
|
1023
|
+
it('user flow: after milan+torent, changing the second half back to milan re-bases the raised half', () => {
|
|
1024
|
+
const basePrices = nestedBasePrices();
|
|
1025
|
+
const engine = selectNested(buildEngine(), ['milan', 'torent']);
|
|
1026
|
+
// Premium pair: milan (0) is raised to torent's segment share (100).
|
|
1027
|
+
const displayed = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine, { basePrices });
|
|
1028
|
+
expect(nestedPrice(displayed, 0, 'milan')).toBe(100);
|
|
1029
|
+
expect(nestedPrice(displayed, 1, 'torent')).toBe(100);
|
|
1030
|
+
// The user switches the SECOND half to milan (cheap pair). From the
|
|
1031
|
+
// pristine drawer state AND from the raised display: the artificially
|
|
1032
|
+
// upgraded milan goes back to its own price.
|
|
1033
|
+
for (const source of [engine, displayed]) {
|
|
1034
|
+
const switched = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(source, ['milan', 'milan']), {
|
|
1035
|
+
basePrices,
|
|
1036
|
+
});
|
|
1037
|
+
expect(nestedPrice(switched, 0, 'milan')).toBe(0);
|
|
1038
|
+
expect(nestedPrice(switched, 1, 'milan')).toBe(0);
|
|
1039
|
+
// De-selected torent keeps its own segment share everywhere.
|
|
1040
|
+
expect(nestedPrice(switched, 0, 'torent')).toBe(100);
|
|
1041
|
+
expect(nestedPrice(switched, 1, 'torent')).toBe(100);
|
|
1042
|
+
expect(topLevelPrice(switched, 'torent')).toBe(200);
|
|
1043
|
+
}
|
|
1044
|
+
// Switching back to the premium pair raises again.
|
|
1045
|
+
const premiumAgain = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(selectNested(displayed, ['milan', 'milan']), ['torent', 'torent']), { basePrices });
|
|
1046
|
+
expect(nestedPrice(premiumAgain, 0, 'torent')).toBe(100);
|
|
1047
|
+
expect(nestedPrice(premiumAgain, 1, 'torent')).toBe(100);
|
|
1048
|
+
});
|
|
1049
|
+
it('full change matrix (nested): every switch recomputes from the scoped pristine prices', () => {
|
|
1050
|
+
const basePrices = nestedBasePrices();
|
|
1051
|
+
const baseValues = { milan: 0, torent: 100 };
|
|
1052
|
+
const combos = [
|
|
1053
|
+
['milan', 'milan'],
|
|
1054
|
+
['milan', 'torent'],
|
|
1055
|
+
['torent', 'milan'],
|
|
1056
|
+
['torent', 'torent'],
|
|
1057
|
+
];
|
|
1058
|
+
const engine = buildEngine();
|
|
1059
|
+
for (const from of combos) {
|
|
1060
|
+
const fromDisplay = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(engine, from), { basePrices });
|
|
1061
|
+
for (const to of combos) {
|
|
1062
|
+
const expected = Math.max(baseValues[to[0]], baseValues[to[1]]);
|
|
1063
|
+
for (const source of [engine, fromDisplay]) {
|
|
1064
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(source, to), { basePrices });
|
|
1065
|
+
expect(nestedPrice(result, 0, to[0])).toBe(expected);
|
|
1066
|
+
expect(nestedPrice(result, 1, to[1])).toBe(expected);
|
|
1067
|
+
expect(topLevelPrice(result, 'torent')).toBe(200);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
it('is idempotent with basePrices (re-applying never lowers or changes prices)', () => {
|
|
1073
|
+
const basePrices = nestedBasePrices();
|
|
1074
|
+
const once = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(buildEngine(), ['milan', 'torent']), {
|
|
1075
|
+
basePrices,
|
|
1076
|
+
});
|
|
1077
|
+
const twice = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(once, { basePrices });
|
|
1078
|
+
expect(twice).toEqual(once);
|
|
1079
|
+
});
|
|
1080
|
+
it('engine memory: nested raises are reversible in place, top-level prices untouched', () => {
|
|
1081
|
+
// No external maps at all — the engine re-bases from its own stamps.
|
|
1082
|
+
let engine = selectNested(buildEngine(), ['milan', 'torent']);
|
|
1083
|
+
engine = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine);
|
|
1084
|
+
expect(nestedPrice(engine, 0, 'milan')).toBe(100);
|
|
1085
|
+
expect(nestedPrice(engine, 1, 'torent')).toBe(100);
|
|
1086
|
+
// Stamps are per tree position: the nested segment share (100/0) never
|
|
1087
|
+
// collides with the special's own top-level option price (200).
|
|
1088
|
+
expect(nestedHalfHalf(engine).modifiers[0].options.find((o) => o._id === 'torent').base_price).toBe(100);
|
|
1089
|
+
expect(engine.modifiers[0].options.find((o) => o._id === 'torent').base_price).toBe(200);
|
|
1090
|
+
// The user changes the second half to milan on the RAISED engine.
|
|
1091
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-2_milan', true);
|
|
1092
|
+
engine = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine);
|
|
1093
|
+
// Artificially upgraded milan is back to its own price; de-selected torent
|
|
1094
|
+
// keeps its own; the top-level option is never touched.
|
|
1095
|
+
expect(nestedPrice(engine, 0, 'milan')).toBe(0);
|
|
1096
|
+
expect(nestedPrice(engine, 1, 'milan')).toBe(0);
|
|
1097
|
+
expect(nestedPrice(engine, 0, 'torent')).toBe(100);
|
|
1098
|
+
expect(topLevelPrice(engine, 'torent')).toBe(200);
|
|
1099
|
+
});
|
|
1100
|
+
it('edit flow: a persisted nested raise is re-based before the next change', () => {
|
|
1101
|
+
const specialItem = buildSpecialItem();
|
|
1102
|
+
const basePrices = nestedBasePrices(specialItem);
|
|
1103
|
+
// Submit the premium pair: the order item carries the raised segment price.
|
|
1104
|
+
const submitted = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(buildEngine(specialItem), ['milan', 'torent']), { basePrices });
|
|
1105
|
+
const orderItem = (0, order_engine_1.convertOrderEngineItemToOrderItem)(submitted, TENANT_ID);
|
|
1106
|
+
const persistedHalfHalf = (orderItem.modifiers ?? [])
|
|
1107
|
+
.find((m) => m._id === 'mod-pizza')
|
|
1108
|
+
.options.find((o) => o._id === 'halfhalf-1');
|
|
1109
|
+
expect(persistedHalfHalf.modifiers[0].options.find((o) => o._id === 'milan').price).toBe(100);
|
|
1110
|
+
// Reopening the drawer for edit reads those raised prices back...
|
|
1111
|
+
const reopened = (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1112
|
+
item: specialItem,
|
|
1113
|
+
orderItem,
|
|
1114
|
+
platform: PLATFORM,
|
|
1115
|
+
tenantId: TENANT_ID,
|
|
1116
|
+
orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL,
|
|
1117
|
+
defaultMenuId: null,
|
|
1118
|
+
soldoutModifiers: [],
|
|
1119
|
+
});
|
|
1120
|
+
expect(nestedPrice(reopened, 0, 'milan')).toBe(100);
|
|
1121
|
+
// ...and the scoped reset restores the definition baseline (top-level
|
|
1122
|
+
// prices are untouched by it).
|
|
1123
|
+
const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(reopened, basePrices, { nestedHalfHalfOnly: true });
|
|
1124
|
+
expect(nestedPrice(rebased, 0, 'milan')).toBe(0);
|
|
1125
|
+
expect(topLevelPrice(rebased, 'torent')).toBe(200);
|
|
1126
|
+
// Submitting without changes keeps the premium charging...
|
|
1127
|
+
expect(nestedPrice((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(rebased, { basePrices }), 0, 'milan')).toBe(100);
|
|
1128
|
+
// ...while switching the second half lowers to the definition prices.
|
|
1129
|
+
const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(rebased, ['milan', 'milan']), {
|
|
1130
|
+
basePrices,
|
|
1131
|
+
});
|
|
1132
|
+
expect(nestedPrice(cheap, 0, 'milan')).toBe(0);
|
|
1133
|
+
expect(nestedPrice(cheap, 1, 'milan')).toBe(0);
|
|
1134
|
+
});
|
|
962
1135
|
});
|
|
963
1136
|
// ============================================================================
|
|
964
1137
|
// 5) Engine — choose_expensive_segment_price uses the MAX OF THE SELECTED halves
|
|
@@ -983,9 +1156,11 @@ describe('choose_expensive_segment_price: selected-halves max (new semantics)',
|
|
|
983
1156
|
});
|
|
984
1157
|
const converted = (0, halfhalf_setup_helpers_1.convertHalfHalfToItem)(setup4(), itemsMap4);
|
|
985
1158
|
const basePrices = (0, order_engine_1.getOptionPriceMapFromItem)(converted, PLATFORM);
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1159
|
+
// a/b = 1000/2 = 500, c/d = 1200/2 = 600 (the user's $5/$5/$6/$6 example).
|
|
1160
|
+
const baseValues = { a: 500, b: 500, c: 600, d: 600 };
|
|
1161
|
+
function freshEngine(item = converted) {
|
|
1162
|
+
return (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1163
|
+
item,
|
|
989
1164
|
orderItem: undefined,
|
|
990
1165
|
platform: PLATFORM,
|
|
991
1166
|
tenantId: TENANT_ID,
|
|
@@ -993,6 +1168,9 @@ describe('choose_expensive_segment_price: selected-halves max (new semantics)',
|
|
|
993
1168
|
defaultMenuId: null,
|
|
994
1169
|
soldoutModifiers: [],
|
|
995
1170
|
});
|
|
1171
|
+
}
|
|
1172
|
+
function selectEngine(picks) {
|
|
1173
|
+
let engine = freshEngine();
|
|
996
1174
|
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `seg-1_${picks[0]}`, true);
|
|
997
1175
|
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `seg-2_${picks[1]}`, true);
|
|
998
1176
|
return engine;
|
|
@@ -1000,6 +1178,17 @@ describe('choose_expensive_segment_price: selected-halves max (new semantics)',
|
|
|
1000
1178
|
function selectedPrices(engine) {
|
|
1001
1179
|
return engine.modifiers.map((m) => m.options.find((o) => o.default).price);
|
|
1002
1180
|
}
|
|
1181
|
+
function optionPrice(engine, segmentId, optionId) {
|
|
1182
|
+
return engine.modifiers.find((m) => m._id === segmentId).options.find((o) => o._id === optionId).price;
|
|
1183
|
+
}
|
|
1184
|
+
/** The drawer's display/submit pass: recompute on a copy from pristine prices. */
|
|
1185
|
+
function normalize(engine) {
|
|
1186
|
+
return (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine, { basePrices });
|
|
1187
|
+
}
|
|
1188
|
+
/** Real UI flow: pick one option per segment (the radio group clears the sibling). */
|
|
1189
|
+
function pick(engine, segmentIndex, optionId) {
|
|
1190
|
+
return (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `seg-${segmentIndex + 1}_${optionId}`, true);
|
|
1191
|
+
}
|
|
1003
1192
|
it('A+B stays 5+5 (global max must NOT leak)', () => {
|
|
1004
1193
|
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['a', 'b']));
|
|
1005
1194
|
expect(selectedPrices(result)).toEqual([500, 500]);
|
|
@@ -1046,4 +1235,317 @@ describe('choose_expensive_segment_price: selected-halves max (new semantics)',
|
|
|
1046
1235
|
// a/b stay 500, c/d stay 600 — NOT all raised to 600 at conversion.
|
|
1047
1236
|
expect(workable[0].items.map((i) => priceOf(i.price))).toEqual([500, 500, 600, 600]);
|
|
1048
1237
|
});
|
|
1238
|
+
it('user flow: after B+C, switching to C+D returns the de-selected B to 500', () => {
|
|
1239
|
+
const engine = selectEngine(['b', 'c']);
|
|
1240
|
+
const displayed = normalize(engine);
|
|
1241
|
+
expect(selectedPrices(displayed)).toEqual([600, 600]);
|
|
1242
|
+
// From the pristine drawer state AND from the raised display alike.
|
|
1243
|
+
for (const source of [engine, displayed]) {
|
|
1244
|
+
const result = normalize(pick(pick(source, 0, 'c'), 1, 'd'));
|
|
1245
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1246
|
+
// B is no longer selected anywhere -> back to its own 500.
|
|
1247
|
+
expect(optionPrice(result, 'seg-1', 'b')).toBe(500);
|
|
1248
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(500);
|
|
1249
|
+
// D keeps its own 600 and C stays selected at 600.
|
|
1250
|
+
expect(optionPrice(result, 'seg-2', 'd')).toBe(600);
|
|
1251
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
it('user flow: after B+C, switching to C,B keeps 6+6 and de-selected segment-1 B drops to 500', () => {
|
|
1255
|
+
const engine = selectEngine(['b', 'c']);
|
|
1256
|
+
const displayed = normalize(engine);
|
|
1257
|
+
for (const source of [engine, displayed]) {
|
|
1258
|
+
const result = normalize(pick(pick(source, 0, 'c'), 1, 'b'));
|
|
1259
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1260
|
+
// Segment 1 now charges C (600); its B is de-selected -> 500.
|
|
1261
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1262
|
+
expect(optionPrice(result, 'seg-1', 'b')).toBe(500);
|
|
1263
|
+
// Segment 2 charges B raised to C's price.
|
|
1264
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(600);
|
|
1265
|
+
}
|
|
1266
|
+
});
|
|
1267
|
+
it('user flow: after B+C, switching to A+B lowers the previously raised B to 500', () => {
|
|
1268
|
+
const engine = selectEngine(['b', 'c']);
|
|
1269
|
+
const displayed = normalize(engine);
|
|
1270
|
+
expect(selectedPrices(displayed)).toEqual([600, 600]);
|
|
1271
|
+
for (const source of [engine, displayed]) {
|
|
1272
|
+
const result = normalize(pick(pick(source, 0, 'a'), 1, 'b'));
|
|
1273
|
+
expect(selectedPrices(result)).toEqual([500, 500]);
|
|
1274
|
+
// The de-selected C keeps its own 600; nothing left over from the old pair.
|
|
1275
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1276
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(500);
|
|
1277
|
+
expect(optionPrice(result, 'seg-2', 'c')).toBe(600);
|
|
1278
|
+
}
|
|
1279
|
+
});
|
|
1280
|
+
it('a full session (B+C -> C,D -> C,B -> A,B) recomputes from pristine prices at every step', () => {
|
|
1281
|
+
const engine = selectEngine(['b', 'c']);
|
|
1282
|
+
expect(selectedPrices(normalize(engine))).toEqual([600, 600]);
|
|
1283
|
+
const step1 = pick(pick(engine, 0, 'c'), 1, 'd');
|
|
1284
|
+
const displayed1 = normalize(step1);
|
|
1285
|
+
expect(selectedPrices(displayed1)).toEqual([600, 600]);
|
|
1286
|
+
expect(optionPrice(displayed1, 'seg-1', 'b')).toBe(500);
|
|
1287
|
+
const step2 = pick(pick(step1, 0, 'c'), 1, 'b');
|
|
1288
|
+
const displayed2 = normalize(step2);
|
|
1289
|
+
expect(selectedPrices(displayed2)).toEqual([600, 600]);
|
|
1290
|
+
const step3 = pick(pick(step2, 0, 'a'), 1, 'b');
|
|
1291
|
+
const displayed3 = normalize(step3);
|
|
1292
|
+
expect(selectedPrices(displayed3)).toEqual([500, 500]);
|
|
1293
|
+
// Every unselected option is its own price again.
|
|
1294
|
+
expect(optionPrice(displayed3, 'seg-1', 'c')).toBe(600);
|
|
1295
|
+
expect(optionPrice(displayed3, 'seg-1', 'd')).toBe(600);
|
|
1296
|
+
expect(optionPrice(displayed3, 'seg-2', 'c')).toBe(600);
|
|
1297
|
+
});
|
|
1298
|
+
it('full change matrix: every ordered pair -> pair switch recomputes from pristine prices', () => {
|
|
1299
|
+
const combos = [
|
|
1300
|
+
['a', 'b'],
|
|
1301
|
+
['b', 'a'],
|
|
1302
|
+
['a', 'c'],
|
|
1303
|
+
['c', 'a'],
|
|
1304
|
+
['a', 'd'],
|
|
1305
|
+
['d', 'a'],
|
|
1306
|
+
['b', 'c'],
|
|
1307
|
+
['c', 'b'],
|
|
1308
|
+
['b', 'd'],
|
|
1309
|
+
['d', 'b'],
|
|
1310
|
+
['c', 'd'],
|
|
1311
|
+
['d', 'c'],
|
|
1312
|
+
];
|
|
1313
|
+
for (const from of combos) {
|
|
1314
|
+
const fromPristine = selectEngine(from);
|
|
1315
|
+
const fromDisplay = normalize(fromPristine);
|
|
1316
|
+
const expectedFrom = Math.max(baseValues[from[0]], baseValues[from[1]]);
|
|
1317
|
+
expect(selectedPrices(fromDisplay)).toEqual([expectedFrom, expectedFrom]);
|
|
1318
|
+
for (const to of combos) {
|
|
1319
|
+
const expectedTo = Math.max(baseValues[to[0]], baseValues[to[1]]);
|
|
1320
|
+
for (const source of [fromPristine, fromDisplay]) {
|
|
1321
|
+
const result = normalize(pick(pick(source, 0, to[0]), 1, to[1]));
|
|
1322
|
+
expect(selectedPrices(result)).toEqual([expectedTo, expectedTo]);
|
|
1323
|
+
// Every half de-selected by the switch is back to its own price.
|
|
1324
|
+
for (const optionId of from) {
|
|
1325
|
+
if (to.includes(optionId))
|
|
1326
|
+
continue;
|
|
1327
|
+
expect(optionPrice(result, 'seg-1', optionId)).toBe(baseValues[optionId]);
|
|
1328
|
+
expect(optionPrice(result, 'seg-2', optionId)).toBe(baseValues[optionId]);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
});
|
|
1334
|
+
it('edit flow: persisted raised prices are re-based before the next change', () => {
|
|
1335
|
+
// Submit B+C -> the order item stores segment-1 B at 600.
|
|
1336
|
+
const submitted = normalize(selectEngine(['b', 'c']));
|
|
1337
|
+
const orderItem = (0, order_engine_1.convertOrderEngineItemToOrderItem)(submitted, TENANT_ID);
|
|
1338
|
+
expect((orderItem.modifiers ?? []).find((m) => m._id === 'seg-1').options.find((o) => o._id === 'b').price).toBe(600);
|
|
1339
|
+
// Reopen the drawer for edit: the persisted raise reads back into the engine...
|
|
1340
|
+
const reopened = (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1341
|
+
item: converted,
|
|
1342
|
+
orderItem,
|
|
1343
|
+
platform: PLATFORM,
|
|
1344
|
+
tenantId: TENANT_ID,
|
|
1345
|
+
orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
1346
|
+
defaultMenuId: null,
|
|
1347
|
+
soldoutModifiers: [],
|
|
1348
|
+
});
|
|
1349
|
+
expect(optionPrice(reopened, 'seg-1', 'b')).toBe(600);
|
|
1350
|
+
// ...and the pristine reset restores the catalog baseline.
|
|
1351
|
+
const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(reopened, basePrices);
|
|
1352
|
+
expect(optionPrice(rebased, 'seg-1', 'b')).toBe(500);
|
|
1353
|
+
// Submitting without changes keeps B+C at 6+6...
|
|
1354
|
+
expect(selectedPrices(normalize(rebased))).toEqual([600, 600]);
|
|
1355
|
+
// ...while switching to A+B lowers B back to 5.
|
|
1356
|
+
const changed = normalize(pick(pick(rebased, 0, 'a'), 1, 'b'));
|
|
1357
|
+
expect(selectedPrices(changed)).toEqual([500, 500]);
|
|
1358
|
+
expect(optionPrice(changed, 'seg-1', 'b')).toBe(500);
|
|
1359
|
+
expect(optionPrice(changed, 'seg-2', 'b')).toBe(500);
|
|
1360
|
+
});
|
|
1361
|
+
it('single selected half: nothing to raise, the other segment stays untouched', () => {
|
|
1362
|
+
const result = normalize(pick(freshEngine(), 0, 'a'));
|
|
1363
|
+
expect(optionPrice(result, 'seg-1', 'a')).toBe(500);
|
|
1364
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1365
|
+
// Segment 2 has no selection at all — its options keep their own prices.
|
|
1366
|
+
expect(optionPrice(result, 'seg-2', 'a')).toBe(500);
|
|
1367
|
+
expect(optionPrice(result, 'seg-2', 'd')).toBe(600);
|
|
1368
|
+
});
|
|
1369
|
+
it('no selection at all: prices are re-based and nothing is raised or invented', () => {
|
|
1370
|
+
const source = freshEngine();
|
|
1371
|
+
// Legacy call (no basePrices): unchanged item is returned as is.
|
|
1372
|
+
expect((0, order_engine_1.applyChooseExpensiveSegmentPrice)(source)).toBe(source);
|
|
1373
|
+
const result = normalize(source);
|
|
1374
|
+
expect(optionPrice(result, 'seg-1', 'a')).toBe(500);
|
|
1375
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1376
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(500);
|
|
1377
|
+
expect(optionPrice(result, 'seg-2', 'd')).toBe(600);
|
|
1378
|
+
});
|
|
1379
|
+
it('quarters (4 segments): max over the four selected sides; switching sides re-bases', () => {
|
|
1380
|
+
const quarters = (0, halfhalf_setup_helpers_1.convertHalfHalfToItem)(makeHalfHalfSetup({
|
|
1381
|
+
upsell_price: null,
|
|
1382
|
+
segments: [
|
|
1383
|
+
{ _id: 'q-1', name: 'Quarter 1', sort_id: 1 },
|
|
1384
|
+
{ _id: 'q-2', name: 'Quarter 2', sort_id: 2 },
|
|
1385
|
+
{ _id: 'q-3', name: 'Quarter 3', sort_id: 3 },
|
|
1386
|
+
{ _id: 'q-4', name: 'Quarter 4', sort_id: 4 },
|
|
1387
|
+
],
|
|
1388
|
+
items: [
|
|
1389
|
+
{ item_id: 'a', sort_id: 1, upsell_price: null },
|
|
1390
|
+
{ item_id: 'b', sort_id: 2, upsell_price: null },
|
|
1391
|
+
{ item_id: 'c', sort_id: 3, upsell_price: null },
|
|
1392
|
+
{ item_id: 'd', sort_id: 4, upsell_price: null },
|
|
1393
|
+
],
|
|
1394
|
+
}), itemsMap4);
|
|
1395
|
+
const quarterBase = (0, order_engine_1.getOptionPriceMapFromItem)(quarters, PLATFORM);
|
|
1396
|
+
const quarterPrice = (item, segmentId, optionId) => item.modifiers.find((m) => m._id === segmentId).options.find((o) => o._id === optionId).price;
|
|
1397
|
+
let engine = freshEngine(quarters);
|
|
1398
|
+
// a/b = 1000/4 = 250, c/d = 1200/4 = 300.
|
|
1399
|
+
expect(quarterPrice(engine, 'q-1', 'a')).toBe(250);
|
|
1400
|
+
expect(quarterPrice(engine, 'q-1', 'c')).toBe(300);
|
|
1401
|
+
const picks = [
|
|
1402
|
+
['q-1', 'a'],
|
|
1403
|
+
['q-2', 'a'],
|
|
1404
|
+
['q-3', 'd'],
|
|
1405
|
+
['q-4', 'd'],
|
|
1406
|
+
];
|
|
1407
|
+
for (const [segmentId, optionId] of picks) {
|
|
1408
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `${segmentId}_${optionId}`, true);
|
|
1409
|
+
}
|
|
1410
|
+
const mixed = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine, { basePrices: quarterBase });
|
|
1411
|
+
for (const segmentId of ['q-1', 'q-2']) {
|
|
1412
|
+
expect(quarterPrice(mixed, segmentId, 'a')).toBe(300);
|
|
1413
|
+
}
|
|
1414
|
+
for (const segmentId of ['q-3', 'q-4']) {
|
|
1415
|
+
expect(quarterPrice(mixed, segmentId, 'd')).toBe(300);
|
|
1416
|
+
}
|
|
1417
|
+
// All four sides cheap: everything returns to 250 (from the raised state too).
|
|
1418
|
+
for (const source of [engine, mixed]) {
|
|
1419
|
+
let cheap = source;
|
|
1420
|
+
for (const segmentId of ['q-1', 'q-2', 'q-3', 'q-4']) {
|
|
1421
|
+
cheap = (0, order_engine_1.setCheckFieldInOrderEngineItem)(cheap, `${segmentId}_a`, true);
|
|
1422
|
+
}
|
|
1423
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(cheap, { basePrices: quarterBase });
|
|
1424
|
+
for (const segmentId of ['q-1', 'q-2', 'q-3', 'q-4']) {
|
|
1425
|
+
expect(quarterPrice(result, segmentId, 'a')).toBe(250);
|
|
1426
|
+
}
|
|
1427
|
+
// De-selected c/d keep their own 300.
|
|
1428
|
+
expect(quarterPrice(result, 'q-1', 'd')).toBe(300);
|
|
1429
|
+
}
|
|
1430
|
+
});
|
|
1431
|
+
});
|
|
1432
|
+
// ============================================================================
|
|
1433
|
+
// 6) Engine memory — `base_price` (reversible normalization without maps)
|
|
1434
|
+
// ============================================================================
|
|
1435
|
+
describe('engine memory: base_price makes choose_expensive reversible in place', () => {
|
|
1436
|
+
const itemsMap = {
|
|
1437
|
+
a: makeItem('a', { price: { BASE_PRICE: 1000 } }),
|
|
1438
|
+
b: makeItem('b', { price: { BASE_PRICE: 1000 } }),
|
|
1439
|
+
c: makeItem('c', { price: { BASE_PRICE: 1200 } }),
|
|
1440
|
+
d: makeItem('d', { price: { BASE_PRICE: 1200 } }),
|
|
1441
|
+
};
|
|
1442
|
+
const setup = () => makeHalfHalfSetup({
|
|
1443
|
+
upsell_price: null,
|
|
1444
|
+
items: [
|
|
1445
|
+
{ item_id: 'a', sort_id: 1, upsell_price: null },
|
|
1446
|
+
{ item_id: 'b', sort_id: 2, upsell_price: null },
|
|
1447
|
+
{ item_id: 'c', sort_id: 3, upsell_price: null },
|
|
1448
|
+
{ item_id: 'd', sort_id: 4, upsell_price: null },
|
|
1449
|
+
],
|
|
1450
|
+
});
|
|
1451
|
+
function buildEngine() {
|
|
1452
|
+
const converted = (0, halfhalf_setup_helpers_1.convertHalfHalfToItem)(setup(), itemsMap);
|
|
1453
|
+
return (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1454
|
+
item: converted,
|
|
1455
|
+
orderItem: undefined,
|
|
1456
|
+
platform: PLATFORM,
|
|
1457
|
+
tenantId: TENANT_ID,
|
|
1458
|
+
orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
1459
|
+
defaultMenuId: null,
|
|
1460
|
+
soldoutModifiers: [],
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
function priceOf(engine, segmentId, optionId, base = false) {
|
|
1464
|
+
const option = engine.modifiers.find((m) => m._id === segmentId).options.find((o) => o._id === optionId);
|
|
1465
|
+
return base ? option.base_price : option.price;
|
|
1466
|
+
}
|
|
1467
|
+
it('conversion stamps the catalog definition on every option', () => {
|
|
1468
|
+
const engine = buildEngine();
|
|
1469
|
+
const expected = [
|
|
1470
|
+
['a', 500],
|
|
1471
|
+
['b', 500],
|
|
1472
|
+
['c', 600],
|
|
1473
|
+
['d', 600],
|
|
1474
|
+
];
|
|
1475
|
+
for (const [optionId, value] of expected) {
|
|
1476
|
+
expect(priceOf(engine, 'seg-1', optionId, true)).toBe(value);
|
|
1477
|
+
expect(priceOf(engine, 'seg-1', optionId)).toBe(value);
|
|
1478
|
+
expect(priceOf(engine, 'seg-2', optionId, true)).toBe(value);
|
|
1479
|
+
}
|
|
1480
|
+
});
|
|
1481
|
+
it('changing the choice on the SAME (raised) engine lowers again — no external map', () => {
|
|
1482
|
+
let engine = buildEngine();
|
|
1483
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-1_b', true);
|
|
1484
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_c', true);
|
|
1485
|
+
// B+C -> B raised to 600.
|
|
1486
|
+
engine = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine);
|
|
1487
|
+
expect(priceOf(engine, 'seg-1', 'b')).toBe(600);
|
|
1488
|
+
expect(priceOf(engine, 'seg-2', 'c')).toBe(600);
|
|
1489
|
+
// User changes to A+B on the raised engine: B must come back to its 500.
|
|
1490
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-1_a', true);
|
|
1491
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_b', true);
|
|
1492
|
+
engine = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine);
|
|
1493
|
+
expect(priceOf(engine, 'seg-1', 'a')).toBe(500);
|
|
1494
|
+
expect(priceOf(engine, 'seg-1', 'b')).toBe(500);
|
|
1495
|
+
expect(priceOf(engine, 'seg-2', 'b')).toBe(500);
|
|
1496
|
+
// De-selected C keeps its own price; nothing drifts.
|
|
1497
|
+
expect(priceOf(engine, 'seg-1', 'c')).toBe(600);
|
|
1498
|
+
expect(priceOf(engine, 'seg-2', 'c')).toBe(600);
|
|
1499
|
+
});
|
|
1500
|
+
it('de-selecting a raised half on the SAME engine returns it to its own price', () => {
|
|
1501
|
+
let engine = buildEngine();
|
|
1502
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-1_b', true);
|
|
1503
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_c', true);
|
|
1504
|
+
engine = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine);
|
|
1505
|
+
expect(priceOf(engine, 'seg-1', 'b')).toBe(600);
|
|
1506
|
+
// B+C -> C+D
|
|
1507
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-1_c', true);
|
|
1508
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_d', true);
|
|
1509
|
+
engine = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine);
|
|
1510
|
+
expect(priceOf(engine, 'seg-1', 'c')).toBe(600);
|
|
1511
|
+
expect(priceOf(engine, 'seg-2', 'd')).toBe(600);
|
|
1512
|
+
expect(priceOf(engine, 'seg-1', 'b')).toBe(500);
|
|
1513
|
+
expect(priceOf(engine, 'seg-2', 'b')).toBe(500);
|
|
1514
|
+
});
|
|
1515
|
+
it('re-applying without a selection change is stable (no drift)', () => {
|
|
1516
|
+
let engine = buildEngine();
|
|
1517
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-1_a', true);
|
|
1518
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_d', true);
|
|
1519
|
+
const once = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine);
|
|
1520
|
+
const twice = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(once);
|
|
1521
|
+
expect(priceOf(twice, 'seg-1', 'a')).toBe(priceOf(once, 'seg-1', 'a'));
|
|
1522
|
+
expect(priceOf(twice, 'seg-2', 'd')).toBe(priceOf(once, 'seg-2', 'd'));
|
|
1523
|
+
expect(twice).toEqual(once);
|
|
1524
|
+
});
|
|
1525
|
+
it('modern flow: a de-structured line with raised segment prices re-bases from the definition', () => {
|
|
1526
|
+
const workable = (0, halfhalf_setup_helpers_1.convertHalfHalfSetupToWorkableHalfHalfSetup)([setup()], itemsMap, PLATFORM, TENANT_ID)[0];
|
|
1527
|
+
// Persisted edit state: segment 1 carries B at the previously charged 600.
|
|
1528
|
+
const segmentB = { ...(0, order_engine_1.convertItemToOrderItem)(workable.items[1], null, PLATFORM, TENANT_ID), price: 600 };
|
|
1529
|
+
// B + C: the raise is recomputed from the definition (500 -> 600), not
|
|
1530
|
+
// accumulated on the persisted price, and the baseline stays the catalog.
|
|
1531
|
+
const premium = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderEngineItem)({
|
|
1532
|
+
halfHalfItem: workable,
|
|
1533
|
+
selectedHalfHalfItems: [segmentB, (0, order_engine_1.convertItemToOrderItem)(workable.items[2], null, PLATFORM, TENANT_ID)],
|
|
1534
|
+
quantity: 1,
|
|
1535
|
+
platform: PLATFORM,
|
|
1536
|
+
chooseExpensiveSegmentPrice: true,
|
|
1537
|
+
});
|
|
1538
|
+
expect(priceOf(premium, 'seg-1', 'b')).toBe(600);
|
|
1539
|
+
expect(priceOf(premium, 'seg-1', 'b', true)).toBe(500);
|
|
1540
|
+
// User changes to A + B: the persisted 600 must drop to the definition 500.
|
|
1541
|
+
const cheap = (0, halfhalf_setup_helpers_1.convertHalfHalfToOrderEngineItem)({
|
|
1542
|
+
halfHalfItem: workable,
|
|
1543
|
+
selectedHalfHalfItems: [(0, order_engine_1.convertItemToOrderItem)(workable.items[0], null, PLATFORM, TENANT_ID), segmentB],
|
|
1544
|
+
quantity: 1,
|
|
1545
|
+
platform: PLATFORM,
|
|
1546
|
+
chooseExpensiveSegmentPrice: true,
|
|
1547
|
+
});
|
|
1548
|
+
expect(priceOf(cheap, 'seg-1', 'a')).toBe(500);
|
|
1549
|
+
expect(priceOf(cheap, 'seg-2', 'b')).toBe(500);
|
|
1550
|
+
});
|
|
1049
1551
|
});
|