@managesome/eposmatic-toolkit 0.2.717 → 0.2.719
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/utils/halfhalf-setup-helpers.js +6 -89
- package/lib/helpers/utils/order-engine.d.ts +74 -24
- package/lib/helpers/utils/order-engine.js +142 -65
- package/lib/tests/nested-halfhalf-segment-price.test.js +190 -0
- package/lib/tests/specials-halfhalf-pricing.test.js +470 -16
- package/package.json +1 -1
|
@@ -144,27 +144,6 @@ function isRequiredModifierGroup(modifier) {
|
|
|
144
144
|
return ((modifier.min_num_options != null && modifier.min_num_options > 0) ||
|
|
145
145
|
(modifier.min_aggregate_options_quantity != null && modifier.min_aggregate_options_quantity > 0));
|
|
146
146
|
}
|
|
147
|
-
/**
|
|
148
|
-
* Total divided base price of a segment item = item price + prices of all
|
|
149
|
-
* required (price-defining) modifier options at any nesting depth. Used by
|
|
150
|
-
* `choose_expensive_segment_price` so the comparison also covers items whose
|
|
151
|
-
* price lives inside a required modifier group (e.g. "Pizza Size").
|
|
152
|
-
*/
|
|
153
|
-
function getSegmentItemTotalPrice(item, platform) {
|
|
154
|
-
let total = (0, platform_catalog_1.getPriceEnum)(item.price, platform);
|
|
155
|
-
const addRequiredPrices = (modifiers) => {
|
|
156
|
-
(modifiers ?? []).forEach((modifier) => {
|
|
157
|
-
if (!isRequiredModifierGroup(modifier))
|
|
158
|
-
return;
|
|
159
|
-
(modifier.options ?? []).forEach((option) => {
|
|
160
|
-
total += (0, platform_catalog_1.getPriceEnum)(option.price, platform);
|
|
161
|
-
addRequiredPrices(option.modifiers);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
};
|
|
165
|
-
addRequiredPrices(item.modifiers);
|
|
166
|
-
return total;
|
|
167
|
-
}
|
|
168
147
|
function convertHalfHalfSetupToWorkableHalfHalfSetup(halfhalfSetups, itemsMap, platform, tenant_id) {
|
|
169
148
|
if (!platform || !tenant_id)
|
|
170
149
|
return [];
|
|
@@ -200,8 +179,11 @@ function convertHalfHalfSetupToWorkableHalfHalfSetup(halfhalfSetups, itemsMap, p
|
|
|
200
179
|
// from the selectable list (same semantics as specials' excluded_refs).
|
|
201
180
|
.filter((setupItem) => !halfhalfSetup.excluded_refs.includes(setupItem.item_id))
|
|
202
181
|
.sort((a, b) => a.sort_id - b.sort_id);
|
|
203
|
-
// Workable segment entries: divided base price
|
|
204
|
-
//
|
|
182
|
+
// Workable segment entries: divided base price + own upsell applied below.
|
|
183
|
+
// `choose_expensive_segment_price` is NOT applied here: it depends on which
|
|
184
|
+
// halves the user picks and must be able to lower prices again, so it runs
|
|
185
|
+
// at selection/display/submit time on the engine item with pristine
|
|
186
|
+
// `basePrices` (see `applyChooseExpensiveSegmentPrice`).
|
|
205
187
|
const entries = [];
|
|
206
188
|
for (let j = 0; j < halfhalfItems.length; j++) {
|
|
207
189
|
const item = halfhalfItems[j];
|
|
@@ -224,76 +206,11 @@ function convertHalfHalfSetupToWorkableHalfHalfSetup(halfhalfSetups, itemsMap, p
|
|
|
224
206
|
entries.push({
|
|
225
207
|
workableItem: segmentItem,
|
|
226
208
|
itemUpsell: item.upsell_price,
|
|
227
|
-
segmentTotal: getSegmentItemTotalPrice(segmentItem, platform),
|
|
228
209
|
});
|
|
229
210
|
}
|
|
230
|
-
// choose_expensive_segment_price: normalize every segment's TOTAL base
|
|
231
|
-
// price (item price + required/price-defining modifier prices, already
|
|
232
|
-
// divided) to the most expensive one; the difference is added at item
|
|
233
|
-
// level so a $5 half and a $6 half both charge $6.
|
|
234
|
-
let expensiveMaxTotal = 0;
|
|
235
|
-
if (halfhalfSetup.choose_expensive_segment_price && entries.length > 0) {
|
|
236
|
-
expensiveMaxTotal = entries.reduce((max, entry) => Math.max(max, entry.segmentTotal), 0);
|
|
237
|
-
}
|
|
238
211
|
const items = [];
|
|
239
|
-
const raisePriceBy = (price, delta) => {
|
|
240
|
-
// Raise BOTH the active platform's key AND the base by `delta` so the
|
|
241
|
-
// effective price (`getPriceEnum` reads `price[platform] ?? BASE_PRICE`)
|
|
242
|
-
// is normalized even for items priced per-platform (e.g. POS_DINE_IN /
|
|
243
|
-
// WEB_DELIVERY keys on the same IPrice object).
|
|
244
|
-
const deltaObj = { BASE_PRICE: delta };
|
|
245
|
-
deltaObj[platform] = delta;
|
|
246
|
-
return (0, platform_num_1.mergePriceObjs)(price, deltaObj);
|
|
247
|
-
};
|
|
248
|
-
// RULE (same as the engine's `findRaiseTarget`): the raise is applied to
|
|
249
|
-
// the FIRST PRICED element along the segment's price chain —
|
|
250
|
-
// 1. the item's own price when non-zero, else
|
|
251
|
-
// 2. the priced required leaf: the single option of a single-option
|
|
252
|
-
// required group (its price is fixed), or the selected/default
|
|
253
|
-
// option of a multi-option group (never an unselected sibling), else
|
|
254
|
-
// 3. fall back to the item price.
|
|
255
|
-
const findRaiseTargetOption = (modifiers) => {
|
|
256
|
-
for (const modifier of modifiers ?? []) {
|
|
257
|
-
const isRequired = (modifier.min_num_options != null && modifier.min_num_options > 0) ||
|
|
258
|
-
(modifier.min_aggregate_options_quantity != null && modifier.min_aggregate_options_quantity > 0);
|
|
259
|
-
if (!isRequired)
|
|
260
|
-
continue;
|
|
261
|
-
const options = modifier.options ?? [];
|
|
262
|
-
if (options.length === 1)
|
|
263
|
-
return options[0];
|
|
264
|
-
const selectedOption = options.find((option) => option.default);
|
|
265
|
-
if (selectedOption != null)
|
|
266
|
-
return selectedOption;
|
|
267
|
-
}
|
|
268
|
-
return null;
|
|
269
|
-
};
|
|
270
212
|
for (const entry of entries) {
|
|
271
|
-
const { workableItem, itemUpsell
|
|
272
|
-
if (halfhalfSetup.choose_expensive_segment_price && segmentTotal < expensiveMaxTotal) {
|
|
273
|
-
const delta = expensiveMaxTotal - segmentTotal;
|
|
274
|
-
if ((0, platform_catalog_1.getPriceEnum)(workableItem.price, platform) > 0) {
|
|
275
|
-
// Rule 1: the item itself carries the price.
|
|
276
|
-
workableItem.price = raisePriceBy(workableItem.price, delta);
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
// Rule 2: the price lives on a required leaf — raise it there
|
|
280
|
-
// (e.g. "ROMA -> Pizza Size -> Large 13\"").
|
|
281
|
-
const targetOption = findRaiseTargetOption(workableItem.modifiers);
|
|
282
|
-
const currentModifiers = workableItem.modifiers ?? [];
|
|
283
|
-
if (targetOption != null && currentModifiers.length > 0) {
|
|
284
|
-
workableItem.modifiers = currentModifiers.map((modifier) => ({
|
|
285
|
-
...modifier,
|
|
286
|
-
options: modifier.options.map((option) => option._id === targetOption._id
|
|
287
|
-
? { ...option, price: raisePriceBy(option.price ?? null, delta) }
|
|
288
|
-
: option),
|
|
289
|
-
}));
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
// Rule 3: fall back to the item price.
|
|
293
|
-
workableItem.price = raisePriceBy(workableItem.price, delta);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
213
|
+
const { workableItem, itemUpsell } = entry;
|
|
297
214
|
// Each half keeps its own item-level upsell, added ON TOP of the divided
|
|
298
215
|
// price. The half-half upsell is a ONE-TIME addition on the order item,
|
|
299
216
|
// not merged per segment.
|
|
@@ -6,39 +6,49 @@ export declare function hasMissedRequiredItemModifier(currentModifiers: IModifie
|
|
|
6
6
|
export declare function syncOrderEngineItem(orderEngineItem: IOrderEngineItem): 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
|
+
/**
|
|
10
|
+
* Optional pristine-price baseline for `choose_expensive_segment_price`.
|
|
11
|
+
* When provided, every option price is first restored from this map (keyed by
|
|
12
|
+
* option `_id`) before the rule runs — so re-applying it after a selection
|
|
13
|
+
* change can LOWER prices again (the rule is a pure function of the current
|
|
14
|
+
* selection, not an accumulating raise).
|
|
15
|
+
*/
|
|
16
|
+
export interface IChooseExpensiveSegmentPriceOptions {
|
|
17
|
+
basePrices?: Record<string, number>;
|
|
18
|
+
}
|
|
9
19
|
/**
|
|
10
20
|
* Applies `choose_expensive_segment_price` to an order engine item converted
|
|
11
21
|
* from a half-half setup (top-level modifier groups = segments, single-select
|
|
12
22
|
* each, only 2 or 4 segments).
|
|
13
23
|
*
|
|
14
|
-
* The
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
24
|
+
* The rule is a pure function of the CURRENT selection: the most expensive
|
|
25
|
+
* half the user actually picked sets the charged price for every picked half
|
|
26
|
+
* (cheaper halves are raised to it; picking a cheaper combination later
|
|
27
|
+
* lowers them again). It cannot run during `IHalfHalfSetup -> IItem`
|
|
28
|
+
* conversion — it runs at display/submit time on a copy of the engine item,
|
|
29
|
+
* optionally with `basePrices` (see `IChooseExpensiveSegmentPriceOptions`) so
|
|
30
|
+
* the result is recomputed from pristine prices instead of accumulating
|
|
31
|
+
* raises in state.
|
|
19
32
|
*
|
|
20
33
|
* Rule: `price = base + upsell` — the item (option) level upsell is already
|
|
21
|
-
* baked into the option price at conversion. choose_expensive compares
|
|
22
|
-
*
|
|
34
|
+
* baked into the option price at conversion. choose_expensive compares every
|
|
35
|
+
* SELECTED segment option (option price + required/price-defining modifier
|
|
23
36
|
* prices — e.g. a "Pizza Size" group whose Small/Large/X-Large options are
|
|
24
|
-
* part of the item's price) and raises the
|
|
25
|
-
* segment price matches the
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* item's own price and is added once on top of the combined subtotal.
|
|
37
|
+
* part of the item's price) and raises the cheaper ones so their final
|
|
38
|
+
* segment price matches the most expensive selected half. The raise is
|
|
39
|
+
* applied to the FIRST PRICED element along the segment's price chain: the
|
|
40
|
+
* segment option's own price when non-zero, else the first priced required
|
|
41
|
+
* leaf ("First Half -> ROMA -> Pizza Size -> Large 13\"") — see
|
|
42
|
+
* `findRaiseTarget`. Optional/upsell modifier groups are excluded: they add
|
|
43
|
+
* on top and must not influence the comparison. The IHalfHalfSetup level
|
|
44
|
+
* upsell is NOT part of this rule: it is the converted item's own price and
|
|
45
|
+
* is added once on top of the combined subtotal.
|
|
34
46
|
*
|
|
35
|
-
* All prices here are plain numbers (engine-priced).
|
|
36
|
-
* ever raises, never lowers — so it can safely be re-applied (e.g. on
|
|
37
|
-
* submit).
|
|
47
|
+
* All prices here are plain numbers (engine-priced).
|
|
38
48
|
*
|
|
39
|
-
* Returns the item unchanged when
|
|
49
|
+
* Returns the item unchanged when nothing changes.
|
|
40
50
|
*/
|
|
41
|
-
export declare function applyChooseExpensiveSegmentPrice(orderEngineItem: IOrderEngineItem): IOrderEngineItem;
|
|
51
|
+
export declare function applyChooseExpensiveSegmentPrice(orderEngineItem: IOrderEngineItem, options?: IChooseExpensiveSegmentPriceOptions): IOrderEngineItem;
|
|
42
52
|
/**
|
|
43
53
|
* True when the item has a top-level modifier group option that is a nested
|
|
44
54
|
* half-half (ref_type HALF_HALF) with its setup's choose_expensive flag.
|
|
@@ -49,9 +59,49 @@ export declare function hasFlaggedHalfHalfOption(orderEngineItem: IOrderEngineIt
|
|
|
49
59
|
* the option's own top-level modifiers are its segment groups, so they get the
|
|
50
60
|
* same raise-to-most-expensive treatment as a standalone half-half item.
|
|
51
61
|
* ITEM options are terminal (their nested modifiers can never contain
|
|
52
|
-
* ITEM/HALF_HALF); normal options pass untouched.
|
|
62
|
+
* ITEM/HALF_HALF); normal options pass untouched. Runs on a copy with pristine
|
|
63
|
+
* `basePrices` when provided (see `IChooseExpensiveSegmentPriceOptions`).
|
|
64
|
+
*/
|
|
65
|
+
export declare function applyNestedChooseExpensiveSegmentPrice(orderEngineItem: IOrderEngineItem, options?: IChooseExpensiveSegmentPriceOptions): IOrderEngineItem;
|
|
66
|
+
/**
|
|
67
|
+
* Builds a pristine `_id -> price` map of every option in a converted catalog
|
|
68
|
+
* item (`modifiers -> options -> nested modifiers -> options ...`). Used as
|
|
69
|
+
* the baseline for `choose_expensive_segment_price` so applying the rule never
|
|
70
|
+
* depends on previously raised prices.
|
|
71
|
+
*
|
|
72
|
+
* The flat `_id -> price` shape can only represent ONE value per option
|
|
73
|
+
* `_id`, so callers MUST keep the map to a single price scope. A half-half
|
|
74
|
+
* nested in a special is exactly the case where that fails: its segment
|
|
75
|
+
* options are built FROM the special's items, so they reuse the SAME `_id`s
|
|
76
|
+
* as the special's top-level options while their prices differ (top-level =
|
|
77
|
+
* full upsell, segment = per-segment share). `nestedHalfHalfSegmentsOnly`
|
|
78
|
+
* keeps the map to the subtrees of HALF_HALF options (the segment trees plus
|
|
79
|
+
* their nested modifiers), excluding top-level option prices — the special
|
|
80
|
+
* drawer feeds this map to `resetOrderEngineItemPrices` / the nested
|
|
81
|
+
* normalization, so the walk order of the item can never leak a top-level
|
|
82
|
+
* price into a segment (or the other way around). Standalone half-half items
|
|
83
|
+
* are a single scope: every segment shares its option `_id`s with the SAME
|
|
84
|
+
* price, so the unscoped map is safe there.
|
|
85
|
+
*/
|
|
86
|
+
export declare function getOptionPriceMapFromItem(item: IItem | ISimplifiedCatalogItem, platform: PLATFORMS_ENUM | null | undefined, options?: {
|
|
87
|
+
nestedHalfHalfSegmentsOnly?: boolean;
|
|
88
|
+
}): Record<string, number>;
|
|
89
|
+
/**
|
|
90
|
+
* Restores every engine option price from the pristine `basePrices` map (by
|
|
91
|
+
* option `_id`). Called when the drawer builds the engine item so state
|
|
92
|
+
* (sidebar prices, edit baselines) never carries a previous normalization.
|
|
93
|
+
*
|
|
94
|
+
* `options.nestedHalfHalfOnly` restricts the reset to the subtrees of
|
|
95
|
+
* HALF_HALF options — the exact scope `getOptionPriceMapFromItem` builds when
|
|
96
|
+
* called with `nestedHalfHalfSegmentsOnly`. A special's top-level options
|
|
97
|
+
* reuse their `_id`s at different prices (full upsell vs per-segment share),
|
|
98
|
+
* so a blanket reset with the scoped map would push the segment share onto
|
|
99
|
+
* the top-level option. ALWAYS pass the SAME scope to the map builder and to
|
|
100
|
+
* this reset.
|
|
53
101
|
*/
|
|
54
|
-
export declare function
|
|
102
|
+
export declare function resetOrderEngineItemPrices(orderEngineItem: IOrderEngineItem, basePrices: Record<string, number>, options?: {
|
|
103
|
+
nestedHalfHalfOnly?: boolean;
|
|
104
|
+
}): IOrderEngineItem;
|
|
55
105
|
export type IOrderEngineItemRewardClaim = {
|
|
56
106
|
type: 'offer';
|
|
57
107
|
offerId: string;
|
|
@@ -10,6 +10,8 @@ exports.makeSpaceForFreeOptions = makeSpaceForFreeOptions;
|
|
|
10
10
|
exports.applyChooseExpensiveSegmentPrice = applyChooseExpensiveSegmentPrice;
|
|
11
11
|
exports.hasFlaggedHalfHalfOption = hasFlaggedHalfHalfOption;
|
|
12
12
|
exports.applyNestedChooseExpensiveSegmentPrice = applyNestedChooseExpensiveSegmentPrice;
|
|
13
|
+
exports.getOptionPriceMapFromItem = getOptionPriceMapFromItem;
|
|
14
|
+
exports.resetOrderEngineItemPrices = resetOrderEngineItemPrices;
|
|
13
15
|
exports.convertOrderEngineItemToOrderItem = convertOrderEngineItemToOrderItem;
|
|
14
16
|
exports.convertItemDiscountToOrderItemDiscount = convertItemDiscountToOrderItemDiscount;
|
|
15
17
|
exports.convertCatalogItemToOrderEngineItem = convertCatalogItemToOrderEngineItem;
|
|
@@ -285,44 +287,60 @@ function makeSpaceForFreeOptions(options, num_free_options) {
|
|
|
285
287
|
}
|
|
286
288
|
return [...defaultPriceOptions, ...nonDefaultOptions].sort((a, b) => a.sort_id - b.sort_id);
|
|
287
289
|
}
|
|
288
|
-
|
|
290
|
+
/** Restores engine option prices from a pristine `_id -> price` map. */
|
|
291
|
+
const resetEngineOptionPrices = (modifiers, basePrices) => {
|
|
292
|
+
if (basePrices == null)
|
|
293
|
+
return modifiers;
|
|
294
|
+
const resetOption = (option) => ({
|
|
295
|
+
...option,
|
|
296
|
+
price: basePrices[option._id] ?? option.price,
|
|
297
|
+
modifiers: (option.modifiers ?? []).map((modifier) => ({
|
|
298
|
+
...modifier,
|
|
299
|
+
options: (modifier.options ?? []).map(resetOption),
|
|
300
|
+
})),
|
|
301
|
+
});
|
|
302
|
+
return modifiers.map((modifier) => ({
|
|
303
|
+
...modifier,
|
|
304
|
+
options: (modifier.options ?? []).map(resetOption),
|
|
305
|
+
}));
|
|
306
|
+
};
|
|
289
307
|
/**
|
|
290
308
|
* Applies `choose_expensive_segment_price` to an order engine item converted
|
|
291
309
|
* from a half-half setup (top-level modifier groups = segments, single-select
|
|
292
310
|
* each, only 2 or 4 segments).
|
|
293
311
|
*
|
|
294
|
-
* The
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
*
|
|
312
|
+
* The rule is a pure function of the CURRENT selection: the most expensive
|
|
313
|
+
* half the user actually picked sets the charged price for every picked half
|
|
314
|
+
* (cheaper halves are raised to it; picking a cheaper combination later
|
|
315
|
+
* lowers them again). It cannot run during `IHalfHalfSetup -> IItem`
|
|
316
|
+
* conversion — it runs at display/submit time on a copy of the engine item,
|
|
317
|
+
* optionally with `basePrices` (see `IChooseExpensiveSegmentPriceOptions`) so
|
|
318
|
+
* the result is recomputed from pristine prices instead of accumulating
|
|
319
|
+
* raises in state.
|
|
299
320
|
*
|
|
300
321
|
* Rule: `price = base + upsell` — the item (option) level upsell is already
|
|
301
|
-
* baked into the option price at conversion. choose_expensive compares
|
|
302
|
-
*
|
|
322
|
+
* baked into the option price at conversion. choose_expensive compares every
|
|
323
|
+
* SELECTED segment option (option price + required/price-defining modifier
|
|
303
324
|
* prices — e.g. a "Pizza Size" group whose Small/Large/X-Large options are
|
|
304
|
-
* part of the item's price) and raises the
|
|
305
|
-
* segment price matches the
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
* item's own price and is added once on top of the combined subtotal.
|
|
325
|
+
* part of the item's price) and raises the cheaper ones so their final
|
|
326
|
+
* segment price matches the most expensive selected half. The raise is
|
|
327
|
+
* applied to the FIRST PRICED element along the segment's price chain: the
|
|
328
|
+
* segment option's own price when non-zero, else the first priced required
|
|
329
|
+
* leaf ("First Half -> ROMA -> Pizza Size -> Large 13\"") — see
|
|
330
|
+
* `findRaiseTarget`. Optional/upsell modifier groups are excluded: they add
|
|
331
|
+
* on top and must not influence the comparison. The IHalfHalfSetup level
|
|
332
|
+
* upsell is NOT part of this rule: it is the converted item's own price and
|
|
333
|
+
* is added once on top of the combined subtotal.
|
|
314
334
|
*
|
|
315
|
-
* All prices here are plain numbers (engine-priced).
|
|
316
|
-
* ever raises, never lowers — so it can safely be re-applied (e.g. on
|
|
317
|
-
* submit).
|
|
335
|
+
* All prices here are plain numbers (engine-priced).
|
|
318
336
|
*
|
|
319
|
-
* Returns the item unchanged when
|
|
337
|
+
* Returns the item unchanged when nothing changes.
|
|
320
338
|
*/
|
|
321
|
-
function applyChooseExpensiveSegmentPrice(orderEngineItem) {
|
|
339
|
+
function applyChooseExpensiveSegmentPrice(orderEngineItem, options) {
|
|
322
340
|
const segmentGroups = (orderEngineItem.modifiers ?? []).filter((modifier) => modifier.depth === 0);
|
|
323
341
|
if (segmentGroups.length < 2)
|
|
324
342
|
return orderEngineItem;
|
|
325
|
-
const raised = applyChooseExpensiveSegmentPriceToModifiers(segmentGroups);
|
|
343
|
+
const raised = applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options);
|
|
326
344
|
if (raised == null)
|
|
327
345
|
return orderEngineItem;
|
|
328
346
|
let segmentGroupIndex = 0;
|
|
@@ -338,7 +356,9 @@ function applyChooseExpensiveSegmentPrice(orderEngineItem) {
|
|
|
338
356
|
* Returns null when nothing needs to change (no selected option / no priced
|
|
339
357
|
* segment), otherwise the raised segment groups.
|
|
340
358
|
*/
|
|
341
|
-
function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups) {
|
|
359
|
+
function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups, options) {
|
|
360
|
+
const hasBasePrices = options?.basePrices != null;
|
|
361
|
+
const groups = resetEngineOptionPrices(segmentGroups, options?.basePrices);
|
|
342
362
|
// Prices of ALL options of required (price-defining) modifier groups, at
|
|
343
363
|
// any depth — e.g. "Pizza Size" -> "Large 13"" is part of the item's price
|
|
344
364
|
// whichever size is picked (mirrors `getSegmentItemTotalPrice` used by the
|
|
@@ -361,28 +381,10 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups) {
|
|
|
361
381
|
// Segment price = option price (base + item/option level upsell, already
|
|
362
382
|
// merged at conversion) + required (price-defining) modifier prices.
|
|
363
383
|
const getSegmentTotal = (option) => (option.price ?? 0) + getRequiredOptionPrices(option.modifiers);
|
|
364
|
-
// Max over ALL segment options (the setup's most expensive segment), not
|
|
365
|
-
// just the selected ones — otherwise the charged price would depend on
|
|
366
|
-
// which halves the user picks. Free options never drive the comparison.
|
|
367
|
-
let maxSegmentValue = 0;
|
|
368
|
-
for (let g = 0; g < segmentGroups.length; g++) {
|
|
369
|
-
const options = segmentGroups[g].options ?? [];
|
|
370
|
-
for (let o = 0; o < options.length; o++) {
|
|
371
|
-
const option = options[o];
|
|
372
|
-
if (option.is_free)
|
|
373
|
-
continue;
|
|
374
|
-
const segmentValue = getSegmentTotal(option);
|
|
375
|
-
if (segmentValue > maxSegmentValue) {
|
|
376
|
-
maxSegmentValue = segmentValue;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
if (maxSegmentValue <= 0)
|
|
381
|
-
return null;
|
|
382
384
|
// One selected option per segment group (segments are single-select).
|
|
383
385
|
const selections = [];
|
|
384
|
-
for (let g = 0; g <
|
|
385
|
-
const options =
|
|
386
|
+
for (let g = 0; g < groups.length; g++) {
|
|
387
|
+
const options = groups[g].options ?? [];
|
|
386
388
|
for (let o = 0; o < options.length; o++) {
|
|
387
389
|
const option = options[o];
|
|
388
390
|
if (!option.default || option.is_free)
|
|
@@ -392,7 +394,21 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups) {
|
|
|
392
394
|
}
|
|
393
395
|
}
|
|
394
396
|
if (selections.length === 0)
|
|
395
|
-
return null;
|
|
397
|
+
return hasBasePrices ? groups : null;
|
|
398
|
+
// Max over the SELECTED halves only — the "expensive segment" is the most
|
|
399
|
+
// expensive half the user actually picked, not the most expensive option in
|
|
400
|
+
// the setup (otherwise picking A+B would charge as if C+D were chosen).
|
|
401
|
+
// Free halves never drive the comparison and are never raised.
|
|
402
|
+
let maxSegmentValue = 0;
|
|
403
|
+
for (const selection of selections) {
|
|
404
|
+
const option = groups[selection.groupIndex].options[selection.optionIndex];
|
|
405
|
+
const segmentValue = getSegmentTotal(option);
|
|
406
|
+
if (segmentValue > maxSegmentValue) {
|
|
407
|
+
maxSegmentValue = segmentValue;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (maxSegmentValue <= 0)
|
|
411
|
+
return hasBasePrices ? groups : null;
|
|
396
412
|
// RULE for where the raise lands: on the FIRST PRICED element along the
|
|
397
413
|
// segment's price chain —
|
|
398
414
|
// 1. the segment option's own price (the item base) when it is non-zero
|
|
@@ -432,7 +448,7 @@ function applyChooseExpensiveSegmentPriceToModifiers(segmentGroups) {
|
|
|
432
448
|
})),
|
|
433
449
|
};
|
|
434
450
|
};
|
|
435
|
-
return
|
|
451
|
+
return groups.map((modifier, g) => {
|
|
436
452
|
const options = (modifier.options ?? []).map((option, o) => {
|
|
437
453
|
const selection = selections.find((s) => s.groupIndex === g && s.optionIndex === o);
|
|
438
454
|
if (selection == null)
|
|
@@ -462,9 +478,10 @@ function hasFlaggedHalfHalfOption(orderEngineItem) {
|
|
|
462
478
|
* the option's own top-level modifiers are its segment groups, so they get the
|
|
463
479
|
* same raise-to-most-expensive treatment as a standalone half-half item.
|
|
464
480
|
* ITEM options are terminal (their nested modifiers can never contain
|
|
465
|
-
* ITEM/HALF_HALF); normal options pass untouched.
|
|
481
|
+
* ITEM/HALF_HALF); normal options pass untouched. Runs on a copy with pristine
|
|
482
|
+
* `basePrices` when provided (see `IChooseExpensiveSegmentPriceOptions`).
|
|
466
483
|
*/
|
|
467
|
-
function applyNestedChooseExpensiveSegmentPrice(orderEngineItem) {
|
|
484
|
+
function applyNestedChooseExpensiveSegmentPrice(orderEngineItem, options) {
|
|
468
485
|
const modifiers = (orderEngineItem.modifiers ?? []).map((modifier) => ({
|
|
469
486
|
...modifier,
|
|
470
487
|
options: (modifier.options ?? []).map((option) => {
|
|
@@ -472,7 +489,7 @@ function applyNestedChooseExpensiveSegmentPrice(orderEngineItem) {
|
|
|
472
489
|
option.choose_expensive_segment_price !== true) {
|
|
473
490
|
return option;
|
|
474
491
|
}
|
|
475
|
-
const raised = applyChooseExpensiveSegmentPriceToModifiers(option.modifiers ?? []);
|
|
492
|
+
const raised = applyChooseExpensiveSegmentPriceToModifiers(option.modifiers ?? [], options);
|
|
476
493
|
if (raised == null)
|
|
477
494
|
return option;
|
|
478
495
|
return { ...option, modifiers: raised };
|
|
@@ -480,6 +497,73 @@ function applyNestedChooseExpensiveSegmentPrice(orderEngineItem) {
|
|
|
480
497
|
}));
|
|
481
498
|
return syncOrderEngineItem({ ...orderEngineItem, modifiers });
|
|
482
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
* Builds a pristine `_id -> price` map of every option in a converted catalog
|
|
502
|
+
* item (`modifiers -> options -> nested modifiers -> options ...`). Used as
|
|
503
|
+
* the baseline for `choose_expensive_segment_price` so applying the rule never
|
|
504
|
+
* depends on previously raised prices.
|
|
505
|
+
*
|
|
506
|
+
* The flat `_id -> price` shape can only represent ONE value per option
|
|
507
|
+
* `_id`, so callers MUST keep the map to a single price scope. A half-half
|
|
508
|
+
* nested in a special is exactly the case where that fails: its segment
|
|
509
|
+
* options are built FROM the special's items, so they reuse the SAME `_id`s
|
|
510
|
+
* as the special's top-level options while their prices differ (top-level =
|
|
511
|
+
* full upsell, segment = per-segment share). `nestedHalfHalfSegmentsOnly`
|
|
512
|
+
* keeps the map to the subtrees of HALF_HALF options (the segment trees plus
|
|
513
|
+
* their nested modifiers), excluding top-level option prices — the special
|
|
514
|
+
* drawer feeds this map to `resetOrderEngineItemPrices` / the nested
|
|
515
|
+
* normalization, so the walk order of the item can never leak a top-level
|
|
516
|
+
* price into a segment (or the other way around). Standalone half-half items
|
|
517
|
+
* are a single scope: every segment shares its option `_id`s with the SAME
|
|
518
|
+
* price, so the unscoped map is safe there.
|
|
519
|
+
*/
|
|
520
|
+
function getOptionPriceMapFromItem(item, platform, options) {
|
|
521
|
+
const priceMap = {};
|
|
522
|
+
const nestedHalfHalfSegmentsOnly = options?.nestedHalfHalfSegmentsOnly === true;
|
|
523
|
+
const walk = (modifiers, insideHalfHalf) => {
|
|
524
|
+
(modifiers ?? []).forEach((modifier) => {
|
|
525
|
+
(modifier.options ?? []).forEach((option) => {
|
|
526
|
+
const isHalfHalf = option.ref_type === constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF;
|
|
527
|
+
// Inside a half-half subtree every option belongs to the segment
|
|
528
|
+
// scope; at the top level only record when not scoped (or record
|
|
529
|
+
// nothing because the option is the half-half hub itself, whose price
|
|
530
|
+
// is NOT part of the segment scope).
|
|
531
|
+
if (insideHalfHalf || !nestedHalfHalfSegmentsOnly) {
|
|
532
|
+
priceMap[option._id] = (0, platform_catalog_1.getPriceEnum)(option.price, platform);
|
|
533
|
+
}
|
|
534
|
+
walk(option.modifiers, insideHalfHalf || isHalfHalf);
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
};
|
|
538
|
+
walk(item.modifiers, false);
|
|
539
|
+
return priceMap;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Restores every engine option price from the pristine `basePrices` map (by
|
|
543
|
+
* option `_id`). Called when the drawer builds the engine item so state
|
|
544
|
+
* (sidebar prices, edit baselines) never carries a previous normalization.
|
|
545
|
+
*
|
|
546
|
+
* `options.nestedHalfHalfOnly` restricts the reset to the subtrees of
|
|
547
|
+
* HALF_HALF options — the exact scope `getOptionPriceMapFromItem` builds when
|
|
548
|
+
* called with `nestedHalfHalfSegmentsOnly`. A special's top-level options
|
|
549
|
+
* reuse their `_id`s at different prices (full upsell vs per-segment share),
|
|
550
|
+
* so a blanket reset with the scoped map would push the segment share onto
|
|
551
|
+
* the top-level option. ALWAYS pass the SAME scope to the map builder and to
|
|
552
|
+
* this reset.
|
|
553
|
+
*/
|
|
554
|
+
function resetOrderEngineItemPrices(orderEngineItem, basePrices, options) {
|
|
555
|
+
if (options?.nestedHalfHalfOnly === true) {
|
|
556
|
+
const modifiers = (orderEngineItem.modifiers ?? []).map((modifier) => ({
|
|
557
|
+
...modifier,
|
|
558
|
+
options: (modifier.options ?? []).map((option) => option.ref_type !== constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
|
|
559
|
+
? option
|
|
560
|
+
: { ...option, modifiers: resetEngineOptionPrices(option.modifiers ?? [], basePrices) }),
|
|
561
|
+
}));
|
|
562
|
+
return syncOrderEngineItem({ ...orderEngineItem, modifiers });
|
|
563
|
+
}
|
|
564
|
+
const modifiers = resetEngineOptionPrices(orderEngineItem.modifiers ?? [], basePrices);
|
|
565
|
+
return syncOrderEngineItem({ ...orderEngineItem, modifiers });
|
|
566
|
+
}
|
|
483
567
|
function convertCatalogItemModifierToOrderEngineItemModifier({ modifiers, orderItemModifiers, platform, tenantId, excludeRefs, zeroPriceRefs, loyaltyRefs, voucherRefs, keepOptionalModifiersAtBottom, soldoutModifiers, depth = 0, }) {
|
|
484
568
|
function getSortedModifiers(modifiers = []) {
|
|
485
569
|
const sortedModifiers = modifiers.sort((a, b) => a.sort_id - b.sort_id);
|
|
@@ -1027,21 +1111,14 @@ function setCheckFieldInOrderEngineItem(orderEngineItem, identifier, shouldCheck
|
|
|
1027
1111
|
const allIds = identifier.split('_').filter((id) => id.trim() !== '');
|
|
1028
1112
|
const modifiers = setUpdateModifiers(orderEngineItem.modifiers, allIds, 0);
|
|
1029
1113
|
const updatedOrderEngineItem = syncOrderEngineItem({ ...orderEngineItem, modifiers });
|
|
1030
|
-
//
|
|
1031
|
-
//
|
|
1032
|
-
//
|
|
1033
|
-
//
|
|
1034
|
-
//
|
|
1035
|
-
//
|
|
1036
|
-
//
|
|
1037
|
-
|
|
1038
|
-
return applyChooseExpensiveSegmentPrice(updatedOrderEngineItem);
|
|
1039
|
-
}
|
|
1040
|
-
// Half-half option nested in a special: normalize its own segment groups
|
|
1041
|
-
// (the option's top-level modifiers) whenever it carries the setup flag.
|
|
1042
|
-
if (orderEngineItem.item_type === constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL && hasFlaggedHalfHalfOption(updatedOrderEngineItem)) {
|
|
1043
|
-
return applyNestedChooseExpensiveSegmentPrice(updatedOrderEngineItem);
|
|
1044
|
-
}
|
|
1114
|
+
// NOTE: `choose_expensive_segment_price` is intentionally NOT applied here.
|
|
1115
|
+
// The rule depends on the current selection and must be able to LOWER prices
|
|
1116
|
+
// again when a cheaper combination is picked, so normalization runs at
|
|
1117
|
+
// display/submit time on a copy of the engine item with pristine
|
|
1118
|
+
// `basePrices` (see `applyChooseExpensiveSegmentPrice` /
|
|
1119
|
+
// `applyNestedChooseExpensiveSegmentPrice`). The parameter is kept for
|
|
1120
|
+
// backwards compatibility.
|
|
1121
|
+
void chooseExpensiveSegmentPrice;
|
|
1045
1122
|
return updatedOrderEngineItem;
|
|
1046
1123
|
}
|
|
1047
1124
|
function setIncrementOrderEngineItemQuantity(orderEngineItem, increment) {
|
|
@@ -222,3 +222,193 @@ 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
|
+
it('a cheap selected pair is NOT raised (max of the SELECTED halves only)', () => {
|
|
298
|
+
// seg-1: milan(500) selected, torent(600) not; seg-2: milan2(500) selected,
|
|
299
|
+
// torent2(600) not.
|
|
300
|
+
const item = withSelections(makeHub(makeSegmentGroups()), {
|
|
301
|
+
'seg-1': 'milan',
|
|
302
|
+
'seg-2': 'milan2',
|
|
303
|
+
});
|
|
304
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
305
|
+
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
306
|
+
expect(segmentPrice(result, 'seg-2', 'milan2')).toBe(500);
|
|
307
|
+
// Unselected expensive options keep their own prices.
|
|
308
|
+
expect(segmentPrice(result, 'seg-1', 'torent')).toBe(600);
|
|
309
|
+
});
|
|
310
|
+
it('LOWERS with basePrices when switching from an expensive to a cheap pair', () => {
|
|
311
|
+
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
312
|
+
// Selected: milan (500) + torent2 (600) -> milan is raised to 600.
|
|
313
|
+
const normalized = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
314
|
+
expect(segmentPrice(normalized, 'seg-1', 'milan')).toBe(600);
|
|
315
|
+
// User switches seg-2 to milan2 (cheap pair: 500+500). Re-applying with
|
|
316
|
+
// pristine basePrices brings the previously raised milan back to 500.
|
|
317
|
+
const switched = withSelections(normalized, { 'seg-1': 'milan', 'seg-2': 'milan2' });
|
|
318
|
+
const lowered = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(switched, { basePrices });
|
|
319
|
+
expect(segmentPrice(lowered, 'seg-1', 'milan')).toBe(500);
|
|
320
|
+
expect(segmentPrice(lowered, 'seg-2', 'milan2')).toBe(500);
|
|
321
|
+
});
|
|
322
|
+
it('changing picks: cheap pair lowers the raised half, swap keeps the max', () => {
|
|
323
|
+
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
324
|
+
const hub = makeHub(makeSegmentGroups());
|
|
325
|
+
// milan (500) + torent2 (600) -> milan raised to 600.
|
|
326
|
+
const premium = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(hub, { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
327
|
+
expect(segmentPrice(premium, 'seg-1', 'milan')).toBe(600);
|
|
328
|
+
// -> cheap pair milan + milan2: the previously raised milan returns to 500.
|
|
329
|
+
const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(premium, { 'seg-1': 'milan', 'seg-2': 'milan2' }), { basePrices });
|
|
330
|
+
expect(segmentPrice(cheap, 'seg-1', 'milan')).toBe(500);
|
|
331
|
+
expect(segmentPrice(cheap, 'seg-2', 'milan2')).toBe(500);
|
|
332
|
+
// De-selected torent2 keeps its own 600.
|
|
333
|
+
expect(segmentPrice(cheap, 'seg-2', 'torent2')).toBe(600);
|
|
334
|
+
// -> swap to torent + milan2: the newly expensive half is raised and the
|
|
335
|
+
// de-selected milan is re-based (no leftover 600).
|
|
336
|
+
const swapped = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(premium, { 'seg-1': 'torent', 'seg-2': 'milan2' }), { basePrices });
|
|
337
|
+
expect(segmentPrice(swapped, 'seg-1', 'torent')).toBe(600);
|
|
338
|
+
expect(segmentPrice(swapped, 'seg-2', 'milan2')).toBe(600);
|
|
339
|
+
expect(segmentPrice(swapped, 'seg-1', 'milan')).toBe(500);
|
|
340
|
+
});
|
|
341
|
+
it('full change matrix: every switch recomputes from base prices (pristine vs raised source)', () => {
|
|
342
|
+
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
343
|
+
const combos = [
|
|
344
|
+
{ 'seg-1': 'milan', 'seg-2': 'milan2' },
|
|
345
|
+
{ 'seg-1': 'milan', 'seg-2': 'torent2' },
|
|
346
|
+
{ 'seg-1': 'torent', 'seg-2': 'milan2' },
|
|
347
|
+
{ 'seg-1': 'torent', 'seg-2': 'torent2' },
|
|
348
|
+
];
|
|
349
|
+
const hub = makeHub(makeSegmentGroups());
|
|
350
|
+
for (const from of combos) {
|
|
351
|
+
const fromDisplay = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(hub, from));
|
|
352
|
+
for (const to of combos) {
|
|
353
|
+
const expected = Math.max(basePrices[to['seg-1']], basePrices[to['seg-2']]);
|
|
354
|
+
for (const source of [hub, fromDisplay]) {
|
|
355
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(source, to), {
|
|
356
|
+
basePrices,
|
|
357
|
+
});
|
|
358
|
+
expect(segmentPrice(result, 'seg-1', to['seg-1'])).toBe(expected);
|
|
359
|
+
expect(segmentPrice(result, 'seg-2', to['seg-2'])).toBe(expected);
|
|
360
|
+
// Every half de-selected by the switch is back to its own price.
|
|
361
|
+
for (const segmentId of ['seg-1', 'seg-2']) {
|
|
362
|
+
const fromOption = from[segmentId];
|
|
363
|
+
if ([to['seg-1'], to['seg-2']].includes(fromOption))
|
|
364
|
+
continue;
|
|
365
|
+
expect(segmentPrice(result, segmentId, fromOption)).toBe(basePrices[fromOption]);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
it('is idempotent when re-applied with basePrices', () => {
|
|
372
|
+
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
373
|
+
const once = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }), { basePrices });
|
|
374
|
+
const twice = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(once, { basePrices });
|
|
375
|
+
expect(twice).toEqual(once);
|
|
376
|
+
});
|
|
377
|
+
it('resetOrderEngineItemPrices restores nested segment prices (edit flow)', () => {
|
|
378
|
+
const basePrices = { milan: 500, torent: 600, milan2: 500, torent2: 600 };
|
|
379
|
+
const raised = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }));
|
|
380
|
+
expect(segmentPrice(raised, 'seg-1', 'milan')).toBe(600);
|
|
381
|
+
// Reopening an edit: the persisted raise is re-based from the pristine map.
|
|
382
|
+
const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(raised, basePrices);
|
|
383
|
+
expect(segmentPrice(rebased, 'seg-1', 'milan')).toBe(500);
|
|
384
|
+
expect(segmentPrice(rebased, 'seg-1', 'torent')).toBe(600);
|
|
385
|
+
expect(segmentPrice(rebased, 'seg-2', 'torent2')).toBe(600);
|
|
386
|
+
// Changing the pick afterwards lowers correctly (no accumulation).
|
|
387
|
+
const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(withSelections(rebased, { 'seg-1': 'milan', 'seg-2': 'milan2' }), { basePrices });
|
|
388
|
+
expect(segmentPrice(cheap, 'seg-1', 'milan')).toBe(500);
|
|
389
|
+
expect(segmentPrice(cheap, 'seg-2', 'milan2')).toBe(500);
|
|
390
|
+
});
|
|
391
|
+
it('single selected segment: nothing to raise', () => {
|
|
392
|
+
const item = withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan' });
|
|
393
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
394
|
+
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
395
|
+
// No selection in segment 2 — its options keep their own prices.
|
|
396
|
+
expect(segmentPrice(result, 'seg-2', 'milan2')).toBe(500);
|
|
397
|
+
expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
|
|
398
|
+
});
|
|
399
|
+
it('no selection at all: nothing is raised or invented', () => {
|
|
400
|
+
const item = withSelections(makeHub(makeSegmentGroups()), {});
|
|
401
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
402
|
+
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
403
|
+
expect(segmentPrice(result, 'seg-1', 'torent')).toBe(600);
|
|
404
|
+
expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
|
|
405
|
+
});
|
|
406
|
+
it('a free selected half neither drives the max nor gets raised', () => {
|
|
407
|
+
// The expensive side (torent2, 600) sits in a free slot: the paid half
|
|
408
|
+
// (milan, 500) must NOT be raised to it.
|
|
409
|
+
const item = markFree(withSelections(makeHub(makeSegmentGroups()), { 'seg-1': 'milan', 'seg-2': 'torent2' }), 'seg-2', 'torent2');
|
|
410
|
+
const result = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(item);
|
|
411
|
+
expect(segmentPrice(result, 'seg-1', 'milan')).toBe(500);
|
|
412
|
+
expect(segmentPrice(result, 'seg-2', 'torent2')).toBe(600);
|
|
413
|
+
});
|
|
414
|
+
});
|
|
@@ -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 })));
|
|
@@ -867,11 +893,12 @@ describe('choose_expensive_segment_price end-to-end (specials with half-half)',
|
|
|
867
893
|
let engine = buildEngine();
|
|
868
894
|
// Real UI flow: selection happens through the special's engine item, so
|
|
869
895
|
// the identifier is the FULL path (group -> half-half option -> segment
|
|
870
|
-
// -> option)
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
engine
|
|
874
|
-
|
|
896
|
+
// -> option). setCheckField keeps prices PRISTINE — normalization is a
|
|
897
|
+
// derived pass applied at display/submit time.
|
|
898
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-1_milan', true);
|
|
899
|
+
expect(segmentOptions(engine)[0].find((o) => o.id === 'milan').price).toBe(0);
|
|
900
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-2_torent', true);
|
|
901
|
+
const after = segmentOptions((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine));
|
|
875
902
|
// seg-1 selected MILAN raised 0 -> 100; seg-2 selected TORENT stays 100.
|
|
876
903
|
// Unselected options untouched; NO 575 from Pizza Size anywhere.
|
|
877
904
|
expect(after[0].find((o) => o.id === 'milan').price).toBe(100);
|
|
@@ -881,10 +908,10 @@ describe('choose_expensive_segment_price end-to-end (specials with half-half)',
|
|
|
881
908
|
});
|
|
882
909
|
it('is idempotent (re-applying never lowers or changes prices)', () => {
|
|
883
910
|
let engine = buildEngine();
|
|
884
|
-
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-1_milan', true
|
|
885
|
-
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-2_torent', true
|
|
886
|
-
const once = segmentOptions(engine);
|
|
887
|
-
const twice = segmentOptions((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine));
|
|
911
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-1_milan', true);
|
|
912
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_halfhalf-1_seg-2_torent', true);
|
|
913
|
+
const once = segmentOptions((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine));
|
|
914
|
+
const twice = segmentOptions((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine)));
|
|
888
915
|
expect(twice).toEqual(once);
|
|
889
916
|
});
|
|
890
917
|
it('does nothing when the half-half flag is off', () => {
|
|
@@ -946,12 +973,439 @@ describe('choose_expensive_segment_price end-to-end (specials with half-half)',
|
|
|
946
973
|
defaultMenuId: null,
|
|
947
974
|
soldoutModifiers: [],
|
|
948
975
|
});
|
|
949
|
-
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_hh-flag_seg-1_milan', true
|
|
950
|
-
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_hh-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
976
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_hh-flag_seg-1_milan', true);
|
|
977
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_hh-flag_seg-2_torent', true);
|
|
978
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_hh-no-flag_seg-1_milan', true);
|
|
979
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'mod-pizza_hh-no-flag_seg-2_torent', true);
|
|
980
|
+
const normalized = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(engine);
|
|
981
|
+
const hhFlag = normalized.modifiers[0].options.find((o) => o._id === 'hh-flag');
|
|
982
|
+
const hhNoFlag = normalized.modifiers[0].options.find((o) => o._id === 'hh-no-flag');
|
|
983
|
+
// Flagged: selected free milan raised to the selected expensive half (+100);
|
|
984
|
+
// un-flagged: stays free.
|
|
954
985
|
expect(hhFlag.modifiers[0].options.find((o) => o._id === 'milan').price).toBe(100);
|
|
955
986
|
expect(hhNoFlag.modifiers[0].options.find((o) => o._id === 'milan').price).toBe(0);
|
|
956
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('edit flow: a persisted nested raise is re-based before the next change', () => {
|
|
1081
|
+
const specialItem = buildSpecialItem();
|
|
1082
|
+
const basePrices = nestedBasePrices(specialItem);
|
|
1083
|
+
// Submit the premium pair: the order item carries the raised segment price.
|
|
1084
|
+
const submitted = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(buildEngine(specialItem), ['milan', 'torent']), { basePrices });
|
|
1085
|
+
const orderItem = (0, order_engine_1.convertOrderEngineItemToOrderItem)(submitted, TENANT_ID);
|
|
1086
|
+
const persistedHalfHalf = (orderItem.modifiers ?? [])
|
|
1087
|
+
.find((m) => m._id === 'mod-pizza')
|
|
1088
|
+
.options.find((o) => o._id === 'halfhalf-1');
|
|
1089
|
+
expect(persistedHalfHalf.modifiers[0].options.find((o) => o._id === 'milan').price).toBe(100);
|
|
1090
|
+
// Reopening the drawer for edit reads those raised prices back...
|
|
1091
|
+
const reopened = (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1092
|
+
item: specialItem,
|
|
1093
|
+
orderItem,
|
|
1094
|
+
platform: PLATFORM,
|
|
1095
|
+
tenantId: TENANT_ID,
|
|
1096
|
+
orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL,
|
|
1097
|
+
defaultMenuId: null,
|
|
1098
|
+
soldoutModifiers: [],
|
|
1099
|
+
});
|
|
1100
|
+
expect(nestedPrice(reopened, 0, 'milan')).toBe(100);
|
|
1101
|
+
// ...and the scoped reset restores the definition baseline (top-level
|
|
1102
|
+
// prices are untouched by it).
|
|
1103
|
+
const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(reopened, basePrices, { nestedHalfHalfOnly: true });
|
|
1104
|
+
expect(nestedPrice(rebased, 0, 'milan')).toBe(0);
|
|
1105
|
+
expect(topLevelPrice(rebased, 'torent')).toBe(200);
|
|
1106
|
+
// Submitting without changes keeps the premium charging...
|
|
1107
|
+
expect(nestedPrice((0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(rebased, { basePrices }), 0, 'milan')).toBe(100);
|
|
1108
|
+
// ...while switching the second half lowers to the definition prices.
|
|
1109
|
+
const cheap = (0, order_engine_1.applyNestedChooseExpensiveSegmentPrice)(selectNested(rebased, ['milan', 'milan']), {
|
|
1110
|
+
basePrices,
|
|
1111
|
+
});
|
|
1112
|
+
expect(nestedPrice(cheap, 0, 'milan')).toBe(0);
|
|
1113
|
+
expect(nestedPrice(cheap, 1, 'milan')).toBe(0);
|
|
1114
|
+
});
|
|
1115
|
+
});
|
|
1116
|
+
// ============================================================================
|
|
1117
|
+
// 5) Engine — choose_expensive_segment_price uses the MAX OF THE SELECTED halves
|
|
1118
|
+
// ============================================================================
|
|
1119
|
+
describe('choose_expensive_segment_price: selected-halves max (new semantics)', () => {
|
|
1120
|
+
// A(500) B(500) C(600) D(600) halves — user example: the expensive half is
|
|
1121
|
+
// at most the most expensive SELECTED one, not the setup's global max.
|
|
1122
|
+
const itemsMap4 = {
|
|
1123
|
+
a: makeItem('a', { price: { BASE_PRICE: 1000 } }),
|
|
1124
|
+
b: makeItem('b', { price: { BASE_PRICE: 1000 } }),
|
|
1125
|
+
c: makeItem('c', { price: { BASE_PRICE: 1200 } }),
|
|
1126
|
+
d: makeItem('d', { price: { BASE_PRICE: 1200 } }),
|
|
1127
|
+
};
|
|
1128
|
+
const setup4 = () => makeHalfHalfSetup({
|
|
1129
|
+
upsell_price: null,
|
|
1130
|
+
items: [
|
|
1131
|
+
{ item_id: 'a', sort_id: 1, upsell_price: null },
|
|
1132
|
+
{ item_id: 'b', sort_id: 2, upsell_price: null },
|
|
1133
|
+
{ item_id: 'c', sort_id: 3, upsell_price: null },
|
|
1134
|
+
{ item_id: 'd', sort_id: 4, upsell_price: null },
|
|
1135
|
+
],
|
|
1136
|
+
});
|
|
1137
|
+
const converted = (0, halfhalf_setup_helpers_1.convertHalfHalfToItem)(setup4(), itemsMap4);
|
|
1138
|
+
const basePrices = (0, order_engine_1.getOptionPriceMapFromItem)(converted, PLATFORM);
|
|
1139
|
+
// a/b = 1000/2 = 500, c/d = 1200/2 = 600 (the user's $5/$5/$6/$6 example).
|
|
1140
|
+
const baseValues = { a: 500, b: 500, c: 600, d: 600 };
|
|
1141
|
+
function freshEngine(item = converted) {
|
|
1142
|
+
return (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1143
|
+
item,
|
|
1144
|
+
orderItem: undefined,
|
|
1145
|
+
platform: PLATFORM,
|
|
1146
|
+
tenantId: TENANT_ID,
|
|
1147
|
+
orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
1148
|
+
defaultMenuId: null,
|
|
1149
|
+
soldoutModifiers: [],
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
function selectEngine(picks) {
|
|
1153
|
+
let engine = freshEngine();
|
|
1154
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `seg-1_${picks[0]}`, true);
|
|
1155
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `seg-2_${picks[1]}`, true);
|
|
1156
|
+
return engine;
|
|
1157
|
+
}
|
|
1158
|
+
function selectedPrices(engine) {
|
|
1159
|
+
return engine.modifiers.map((m) => m.options.find((o) => o.default).price);
|
|
1160
|
+
}
|
|
1161
|
+
function optionPrice(engine, segmentId, optionId) {
|
|
1162
|
+
return engine.modifiers.find((m) => m._id === segmentId).options.find((o) => o._id === optionId).price;
|
|
1163
|
+
}
|
|
1164
|
+
/** The drawer's display/submit pass: recompute on a copy from pristine prices. */
|
|
1165
|
+
function normalize(engine) {
|
|
1166
|
+
return (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine, { basePrices });
|
|
1167
|
+
}
|
|
1168
|
+
/** Real UI flow: pick one option per segment (the radio group clears the sibling). */
|
|
1169
|
+
function pick(engine, segmentIndex, optionId) {
|
|
1170
|
+
return (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `seg-${segmentIndex + 1}_${optionId}`, true);
|
|
1171
|
+
}
|
|
1172
|
+
it('A+B stays 5+5 (global max must NOT leak)', () => {
|
|
1173
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['a', 'b']));
|
|
1174
|
+
expect(selectedPrices(result)).toEqual([500, 500]);
|
|
1175
|
+
});
|
|
1176
|
+
it('B+C raises B to C (6+6)', () => {
|
|
1177
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['b', 'c']));
|
|
1178
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1179
|
+
});
|
|
1180
|
+
it('C+B is order independent (6+6)', () => {
|
|
1181
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['c', 'b']));
|
|
1182
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1183
|
+
});
|
|
1184
|
+
it('C+D needs no raise (6+6)', () => {
|
|
1185
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['c', 'd']));
|
|
1186
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1187
|
+
});
|
|
1188
|
+
it('A+D raises A to D (6+6)', () => {
|
|
1189
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['a', 'd']));
|
|
1190
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1191
|
+
});
|
|
1192
|
+
it('unselected options keep their own prices; item-level upsell untouched', () => {
|
|
1193
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['a', 'b']));
|
|
1194
|
+
const seg1 = result.modifiers.find((m) => m._id === 'seg-1');
|
|
1195
|
+
expect(seg1.options.find((o) => o._id === 'c').price).toBe(600);
|
|
1196
|
+
expect(seg1.options.find((o) => o._id === 'd').price).toBe(600);
|
|
1197
|
+
// Setup upsell is null -> engine item price 0 (added once, never normalized).
|
|
1198
|
+
expect(result.price).toBe(0);
|
|
1199
|
+
});
|
|
1200
|
+
it('LOWERS again when switching from an expensive pair to a cheap pair (basePrices reset)', () => {
|
|
1201
|
+
// B+C -> B is raised to 600...
|
|
1202
|
+
const expensive = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(selectEngine(['b', 'c']));
|
|
1203
|
+
expect(selectedPrices(expensive)).toEqual([600, 600]);
|
|
1204
|
+
// ...then the user switches BOTH halves to the cheap pair A+B. Re-applying
|
|
1205
|
+
// with pristine basePrices must bring B back down to 500.
|
|
1206
|
+
let engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(expensive, 'seg-1_b', false);
|
|
1207
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-1_a', true);
|
|
1208
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_c', false);
|
|
1209
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, 'seg-2_b', true);
|
|
1210
|
+
const lowered = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine, { basePrices });
|
|
1211
|
+
expect(selectedPrices(lowered)).toEqual([500, 500]);
|
|
1212
|
+
});
|
|
1213
|
+
it('workable conversion no longer bakes the global max into option prices', () => {
|
|
1214
|
+
const workable = (0, halfhalf_setup_helpers_1.convertHalfHalfSetupToWorkableHalfHalfSetup)([setup4()], itemsMap4, PLATFORM, TENANT_ID);
|
|
1215
|
+
// a/b stay 500, c/d stay 600 — NOT all raised to 600 at conversion.
|
|
1216
|
+
expect(workable[0].items.map((i) => priceOf(i.price))).toEqual([500, 500, 600, 600]);
|
|
1217
|
+
});
|
|
1218
|
+
it('user flow: after B+C, switching to C+D returns the de-selected B to 500', () => {
|
|
1219
|
+
const engine = selectEngine(['b', 'c']);
|
|
1220
|
+
const displayed = normalize(engine);
|
|
1221
|
+
expect(selectedPrices(displayed)).toEqual([600, 600]);
|
|
1222
|
+
// From the pristine drawer state AND from the raised display alike.
|
|
1223
|
+
for (const source of [engine, displayed]) {
|
|
1224
|
+
const result = normalize(pick(pick(source, 0, 'c'), 1, 'd'));
|
|
1225
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1226
|
+
// B is no longer selected anywhere -> back to its own 500.
|
|
1227
|
+
expect(optionPrice(result, 'seg-1', 'b')).toBe(500);
|
|
1228
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(500);
|
|
1229
|
+
// D keeps its own 600 and C stays selected at 600.
|
|
1230
|
+
expect(optionPrice(result, 'seg-2', 'd')).toBe(600);
|
|
1231
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1232
|
+
}
|
|
1233
|
+
});
|
|
1234
|
+
it('user flow: after B+C, switching to C,B keeps 6+6 and de-selected segment-1 B drops to 500', () => {
|
|
1235
|
+
const engine = selectEngine(['b', 'c']);
|
|
1236
|
+
const displayed = normalize(engine);
|
|
1237
|
+
for (const source of [engine, displayed]) {
|
|
1238
|
+
const result = normalize(pick(pick(source, 0, 'c'), 1, 'b'));
|
|
1239
|
+
expect(selectedPrices(result)).toEqual([600, 600]);
|
|
1240
|
+
// Segment 1 now charges C (600); its B is de-selected -> 500.
|
|
1241
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1242
|
+
expect(optionPrice(result, 'seg-1', 'b')).toBe(500);
|
|
1243
|
+
// Segment 2 charges B raised to C's price.
|
|
1244
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(600);
|
|
1245
|
+
}
|
|
1246
|
+
});
|
|
1247
|
+
it('user flow: after B+C, switching to A+B lowers the previously raised B to 500', () => {
|
|
1248
|
+
const engine = selectEngine(['b', 'c']);
|
|
1249
|
+
const displayed = normalize(engine);
|
|
1250
|
+
expect(selectedPrices(displayed)).toEqual([600, 600]);
|
|
1251
|
+
for (const source of [engine, displayed]) {
|
|
1252
|
+
const result = normalize(pick(pick(source, 0, 'a'), 1, 'b'));
|
|
1253
|
+
expect(selectedPrices(result)).toEqual([500, 500]);
|
|
1254
|
+
// The de-selected C keeps its own 600; nothing left over from the old pair.
|
|
1255
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1256
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(500);
|
|
1257
|
+
expect(optionPrice(result, 'seg-2', 'c')).toBe(600);
|
|
1258
|
+
}
|
|
1259
|
+
});
|
|
1260
|
+
it('a full session (B+C -> C,D -> C,B -> A,B) recomputes from pristine prices at every step', () => {
|
|
1261
|
+
const engine = selectEngine(['b', 'c']);
|
|
1262
|
+
expect(selectedPrices(normalize(engine))).toEqual([600, 600]);
|
|
1263
|
+
const step1 = pick(pick(engine, 0, 'c'), 1, 'd');
|
|
1264
|
+
const displayed1 = normalize(step1);
|
|
1265
|
+
expect(selectedPrices(displayed1)).toEqual([600, 600]);
|
|
1266
|
+
expect(optionPrice(displayed1, 'seg-1', 'b')).toBe(500);
|
|
1267
|
+
const step2 = pick(pick(step1, 0, 'c'), 1, 'b');
|
|
1268
|
+
const displayed2 = normalize(step2);
|
|
1269
|
+
expect(selectedPrices(displayed2)).toEqual([600, 600]);
|
|
1270
|
+
const step3 = pick(pick(step2, 0, 'a'), 1, 'b');
|
|
1271
|
+
const displayed3 = normalize(step3);
|
|
1272
|
+
expect(selectedPrices(displayed3)).toEqual([500, 500]);
|
|
1273
|
+
// Every unselected option is its own price again.
|
|
1274
|
+
expect(optionPrice(displayed3, 'seg-1', 'c')).toBe(600);
|
|
1275
|
+
expect(optionPrice(displayed3, 'seg-1', 'd')).toBe(600);
|
|
1276
|
+
expect(optionPrice(displayed3, 'seg-2', 'c')).toBe(600);
|
|
1277
|
+
});
|
|
1278
|
+
it('full change matrix: every ordered pair -> pair switch recomputes from pristine prices', () => {
|
|
1279
|
+
const combos = [
|
|
1280
|
+
['a', 'b'],
|
|
1281
|
+
['b', 'a'],
|
|
1282
|
+
['a', 'c'],
|
|
1283
|
+
['c', 'a'],
|
|
1284
|
+
['a', 'd'],
|
|
1285
|
+
['d', 'a'],
|
|
1286
|
+
['b', 'c'],
|
|
1287
|
+
['c', 'b'],
|
|
1288
|
+
['b', 'd'],
|
|
1289
|
+
['d', 'b'],
|
|
1290
|
+
['c', 'd'],
|
|
1291
|
+
['d', 'c'],
|
|
1292
|
+
];
|
|
1293
|
+
for (const from of combos) {
|
|
1294
|
+
const fromPristine = selectEngine(from);
|
|
1295
|
+
const fromDisplay = normalize(fromPristine);
|
|
1296
|
+
const expectedFrom = Math.max(baseValues[from[0]], baseValues[from[1]]);
|
|
1297
|
+
expect(selectedPrices(fromDisplay)).toEqual([expectedFrom, expectedFrom]);
|
|
1298
|
+
for (const to of combos) {
|
|
1299
|
+
const expectedTo = Math.max(baseValues[to[0]], baseValues[to[1]]);
|
|
1300
|
+
for (const source of [fromPristine, fromDisplay]) {
|
|
1301
|
+
const result = normalize(pick(pick(source, 0, to[0]), 1, to[1]));
|
|
1302
|
+
expect(selectedPrices(result)).toEqual([expectedTo, expectedTo]);
|
|
1303
|
+
// Every half de-selected by the switch is back to its own price.
|
|
1304
|
+
for (const optionId of from) {
|
|
1305
|
+
if (to.includes(optionId))
|
|
1306
|
+
continue;
|
|
1307
|
+
expect(optionPrice(result, 'seg-1', optionId)).toBe(baseValues[optionId]);
|
|
1308
|
+
expect(optionPrice(result, 'seg-2', optionId)).toBe(baseValues[optionId]);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
});
|
|
1314
|
+
it('edit flow: persisted raised prices are re-based before the next change', () => {
|
|
1315
|
+
// Submit B+C -> the order item stores segment-1 B at 600.
|
|
1316
|
+
const submitted = normalize(selectEngine(['b', 'c']));
|
|
1317
|
+
const orderItem = (0, order_engine_1.convertOrderEngineItemToOrderItem)(submitted, TENANT_ID);
|
|
1318
|
+
expect((orderItem.modifiers ?? []).find((m) => m._id === 'seg-1').options.find((o) => o._id === 'b').price).toBe(600);
|
|
1319
|
+
// Reopen the drawer for edit: the persisted raise reads back into the engine...
|
|
1320
|
+
const reopened = (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
|
|
1321
|
+
item: converted,
|
|
1322
|
+
orderItem,
|
|
1323
|
+
platform: PLATFORM,
|
|
1324
|
+
tenantId: TENANT_ID,
|
|
1325
|
+
orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
1326
|
+
defaultMenuId: null,
|
|
1327
|
+
soldoutModifiers: [],
|
|
1328
|
+
});
|
|
1329
|
+
expect(optionPrice(reopened, 'seg-1', 'b')).toBe(600);
|
|
1330
|
+
// ...and the pristine reset restores the catalog baseline.
|
|
1331
|
+
const rebased = (0, order_engine_1.resetOrderEngineItemPrices)(reopened, basePrices);
|
|
1332
|
+
expect(optionPrice(rebased, 'seg-1', 'b')).toBe(500);
|
|
1333
|
+
// Submitting without changes keeps B+C at 6+6...
|
|
1334
|
+
expect(selectedPrices(normalize(rebased))).toEqual([600, 600]);
|
|
1335
|
+
// ...while switching to A+B lowers B back to 5.
|
|
1336
|
+
const changed = normalize(pick(pick(rebased, 0, 'a'), 1, 'b'));
|
|
1337
|
+
expect(selectedPrices(changed)).toEqual([500, 500]);
|
|
1338
|
+
expect(optionPrice(changed, 'seg-1', 'b')).toBe(500);
|
|
1339
|
+
expect(optionPrice(changed, 'seg-2', 'b')).toBe(500);
|
|
1340
|
+
});
|
|
1341
|
+
it('single selected half: nothing to raise, the other segment stays untouched', () => {
|
|
1342
|
+
const result = normalize(pick(freshEngine(), 0, 'a'));
|
|
1343
|
+
expect(optionPrice(result, 'seg-1', 'a')).toBe(500);
|
|
1344
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1345
|
+
// Segment 2 has no selection at all — its options keep their own prices.
|
|
1346
|
+
expect(optionPrice(result, 'seg-2', 'a')).toBe(500);
|
|
1347
|
+
expect(optionPrice(result, 'seg-2', 'd')).toBe(600);
|
|
1348
|
+
});
|
|
1349
|
+
it('no selection at all: prices are re-based and nothing is raised or invented', () => {
|
|
1350
|
+
const source = freshEngine();
|
|
1351
|
+
// Legacy call (no basePrices): unchanged item is returned as is.
|
|
1352
|
+
expect((0, order_engine_1.applyChooseExpensiveSegmentPrice)(source)).toBe(source);
|
|
1353
|
+
const result = normalize(source);
|
|
1354
|
+
expect(optionPrice(result, 'seg-1', 'a')).toBe(500);
|
|
1355
|
+
expect(optionPrice(result, 'seg-1', 'c')).toBe(600);
|
|
1356
|
+
expect(optionPrice(result, 'seg-2', 'b')).toBe(500);
|
|
1357
|
+
expect(optionPrice(result, 'seg-2', 'd')).toBe(600);
|
|
1358
|
+
});
|
|
1359
|
+
it('quarters (4 segments): max over the four selected sides; switching sides re-bases', () => {
|
|
1360
|
+
const quarters = (0, halfhalf_setup_helpers_1.convertHalfHalfToItem)(makeHalfHalfSetup({
|
|
1361
|
+
upsell_price: null,
|
|
1362
|
+
segments: [
|
|
1363
|
+
{ _id: 'q-1', name: 'Quarter 1', sort_id: 1 },
|
|
1364
|
+
{ _id: 'q-2', name: 'Quarter 2', sort_id: 2 },
|
|
1365
|
+
{ _id: 'q-3', name: 'Quarter 3', sort_id: 3 },
|
|
1366
|
+
{ _id: 'q-4', name: 'Quarter 4', sort_id: 4 },
|
|
1367
|
+
],
|
|
1368
|
+
items: [
|
|
1369
|
+
{ item_id: 'a', sort_id: 1, upsell_price: null },
|
|
1370
|
+
{ item_id: 'b', sort_id: 2, upsell_price: null },
|
|
1371
|
+
{ item_id: 'c', sort_id: 3, upsell_price: null },
|
|
1372
|
+
{ item_id: 'd', sort_id: 4, upsell_price: null },
|
|
1373
|
+
],
|
|
1374
|
+
}), itemsMap4);
|
|
1375
|
+
const quarterBase = (0, order_engine_1.getOptionPriceMapFromItem)(quarters, PLATFORM);
|
|
1376
|
+
const quarterPrice = (item, segmentId, optionId) => item.modifiers.find((m) => m._id === segmentId).options.find((o) => o._id === optionId).price;
|
|
1377
|
+
let engine = freshEngine(quarters);
|
|
1378
|
+
// a/b = 1000/4 = 250, c/d = 1200/4 = 300.
|
|
1379
|
+
expect(quarterPrice(engine, 'q-1', 'a')).toBe(250);
|
|
1380
|
+
expect(quarterPrice(engine, 'q-1', 'c')).toBe(300);
|
|
1381
|
+
const picks = [
|
|
1382
|
+
['q-1', 'a'],
|
|
1383
|
+
['q-2', 'a'],
|
|
1384
|
+
['q-3', 'd'],
|
|
1385
|
+
['q-4', 'd'],
|
|
1386
|
+
];
|
|
1387
|
+
for (const [segmentId, optionId] of picks) {
|
|
1388
|
+
engine = (0, order_engine_1.setCheckFieldInOrderEngineItem)(engine, `${segmentId}_${optionId}`, true);
|
|
1389
|
+
}
|
|
1390
|
+
const mixed = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(engine, { basePrices: quarterBase });
|
|
1391
|
+
for (const segmentId of ['q-1', 'q-2']) {
|
|
1392
|
+
expect(quarterPrice(mixed, segmentId, 'a')).toBe(300);
|
|
1393
|
+
}
|
|
1394
|
+
for (const segmentId of ['q-3', 'q-4']) {
|
|
1395
|
+
expect(quarterPrice(mixed, segmentId, 'd')).toBe(300);
|
|
1396
|
+
}
|
|
1397
|
+
// All four sides cheap: everything returns to 250 (from the raised state too).
|
|
1398
|
+
for (const source of [engine, mixed]) {
|
|
1399
|
+
let cheap = source;
|
|
1400
|
+
for (const segmentId of ['q-1', 'q-2', 'q-3', 'q-4']) {
|
|
1401
|
+
cheap = (0, order_engine_1.setCheckFieldInOrderEngineItem)(cheap, `${segmentId}_a`, true);
|
|
1402
|
+
}
|
|
1403
|
+
const result = (0, order_engine_1.applyChooseExpensiveSegmentPrice)(cheap, { basePrices: quarterBase });
|
|
1404
|
+
for (const segmentId of ['q-1', 'q-2', 'q-3', 'q-4']) {
|
|
1405
|
+
expect(quarterPrice(result, segmentId, 'a')).toBe(250);
|
|
1406
|
+
}
|
|
1407
|
+
// De-selected c/d keep their own 300.
|
|
1408
|
+
expect(quarterPrice(result, 'q-1', 'd')).toBe(300);
|
|
1409
|
+
}
|
|
1410
|
+
});
|
|
957
1411
|
});
|