@managesome/eposmatic-toolkit 0.2.696 → 0.2.699

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.
@@ -18,20 +18,25 @@ export declare function makeSpaceForFreeOptions(options: IOrderEngineOption[], n
18
18
  * `HALF_HALF` and the caller passed the flag.
19
19
  *
20
20
  * Rule: `price = base + upsell` — the item (option) level upsell is already
21
- * baked into the option price at conversion. choose_expensive compares the
22
- * FINAL segment price (option price + required/price-defining modifier
21
+ * baked into the option price at conversion. choose_expensive compares
22
+ * EVERY segment option (option price + required/price-defining modifier
23
23
  * prices — e.g. a "Pizza Size" group whose Small/Large/X-Large options are
24
- * part of the item's price) and raises every cheaper selected segment to the
25
- * most expensive one. Optional/upsell modifier groups are excluded: they add
26
- * on top and must not influence the comparison. The IHalfHalfSetup level
27
- * upsell is NOT part of this rule: it is the converted item's own price and
28
- * is added once on top of the combined subtotal.
24
+ * part of the item's price) and raises the SELECTED ones so their final
25
+ * segment price matches the setup's most expensive option — no matter which
26
+ * halves the user picks (same semantics as the modern workable converter).
27
+ * The raise is applied to the FIRST PRICED element along the segment's price
28
+ * chain: the segment option's own price when non-zero (like the modern flow),
29
+ * else the first priced required leaf ("First Half -> ROMA -> Pizza Size ->
30
+ * Large 13\"") — see `findRaiseTarget`. Optional/upsell modifier groups are
31
+ * excluded: they add on top and must not influence the comparison. The
32
+ * IHalfHalfSetup level upsell is NOT part of this rule: it is the converted
33
+ * item's own price and is added once on top of the combined subtotal.
29
34
  *
30
35
  * All prices here are plain numbers (engine-priced). Idempotent — it only
31
36
  * ever raises, never lowers — so it can safely be re-applied (e.g. on
32
37
  * submit).
33
38
  *
34
- * Returns the item unchanged when fewer than two segments are selected.
39
+ * Returns the item unchanged when no segment option is selected.
35
40
  */
36
41
  export declare function applyChooseExpensiveSegmentPrice(orderEngineItem: IOrderEngineItem): IOrderEngineItem;
37
42
  export type IOrderEngineItemRewardClaim = {
@@ -296,29 +296,35 @@ function makeSpaceForFreeOptions(options, num_free_options) {
296
296
  * `HALF_HALF` and the caller passed the flag.
297
297
  *
298
298
  * Rule: `price = base + upsell` — the item (option) level upsell is already
299
- * baked into the option price at conversion. choose_expensive compares the
300
- * FINAL segment price (option price + required/price-defining modifier
299
+ * baked into the option price at conversion. choose_expensive compares
300
+ * EVERY segment option (option price + required/price-defining modifier
301
301
  * prices — e.g. a "Pizza Size" group whose Small/Large/X-Large options are
302
- * part of the item's price) and raises every cheaper selected segment to the
303
- * most expensive one. Optional/upsell modifier groups are excluded: they add
304
- * on top and must not influence the comparison. The IHalfHalfSetup level
305
- * upsell is NOT part of this rule: it is the converted item's own price and
306
- * is added once on top of the combined subtotal.
302
+ * part of the item's price) and raises the SELECTED ones so their final
303
+ * segment price matches the setup's most expensive option — no matter which
304
+ * halves the user picks (same semantics as the modern workable converter).
305
+ * The raise is applied to the FIRST PRICED element along the segment's price
306
+ * chain: the segment option's own price when non-zero (like the modern flow),
307
+ * else the first priced required leaf ("First Half -> ROMA -> Pizza Size ->
308
+ * Large 13\"") — see `findRaiseTarget`. Optional/upsell modifier groups are
309
+ * excluded: they add on top and must not influence the comparison. The
310
+ * IHalfHalfSetup level upsell is NOT part of this rule: it is the converted
311
+ * item's own price and is added once on top of the combined subtotal.
307
312
  *
308
313
  * All prices here are plain numbers (engine-priced). Idempotent — it only
309
314
  * ever raises, never lowers — so it can safely be re-applied (e.g. on
310
315
  * submit).
311
316
  *
312
- * Returns the item unchanged when fewer than two segments are selected.
317
+ * Returns the item unchanged when no segment option is selected.
313
318
  */
314
319
  function applyChooseExpensiveSegmentPrice(orderEngineItem) {
315
320
  const segmentGroups = (orderEngineItem.modifiers ?? []).filter((modifier) => modifier.depth === 0);
316
321
  if (segmentGroups.length < 2)
317
322
  return orderEngineItem;
318
- // Prices of all SELECTED options of required (price-defining) modifier
319
- // groups, at any depth — e.g. "Pizza Size" -> "Large 13"" carried under a
320
- // segment option. Optional/upsell modifier groups are excluded: they add on
321
- // top and must not influence the comparison.
323
+ // Prices of ALL options of required (price-defining) modifier groups, at
324
+ // any depth — e.g. "Pizza Size" -> "Large 13"" is part of the item's price
325
+ // whichever size is picked (mirrors `getSegmentItemTotalPrice` used by the
326
+ // modern workable converter). Optional/upsell modifier groups are excluded:
327
+ // they add on top and must not influence the comparison.
322
328
  const getRequiredOptionPrices = (modifiers) => {
323
329
  let total = 0;
324
330
  (modifiers ?? []).forEach((modifier) => {
@@ -327,36 +333,86 @@ function applyChooseExpensiveSegmentPrice(orderEngineItem) {
327
333
  if (!isRequired)
328
334
  return;
329
335
  (modifier.options ?? []).forEach((option) => {
330
- if (!option.default)
331
- return;
332
336
  total += (option.price ?? 0) * (option.quantity || 1);
333
337
  total += getRequiredOptionPrices(option.modifiers);
334
338
  });
335
339
  });
336
340
  return total;
337
341
  };
338
- // FINAL segment price = option price (base + item/option level upsell,
339
- // already merged at conversion) + required (price-defining) modifier prices.
342
+ // Segment price = option price (base + item/option level upsell, already
343
+ // merged at conversion) + required (price-defining) modifier prices.
340
344
  const getSegmentTotal = (option) => (option.price ?? 0) + getRequiredOptionPrices(option.modifiers);
341
- // One selected option per segment group (segments are single-select).
342
- const selections = [];
345
+ // Max over ALL segment options (the setup's most expensive segment), not
346
+ // just the selected ones — otherwise the charged price would depend on
347
+ // which halves the user picks. Free options never drive the comparison.
343
348
  let maxSegmentValue = 0;
344
349
  for (let g = 0; g < segmentGroups.length; g++) {
345
350
  const options = segmentGroups[g].options ?? [];
346
351
  for (let o = 0; o < options.length; o++) {
347
352
  const option = options[o];
348
- if (!option.default || option.is_free)
353
+ if (option.is_free)
349
354
  continue;
350
- selections.push({ groupIndex: g, optionIndex: o });
351
355
  const segmentValue = getSegmentTotal(option);
352
356
  if (segmentValue > maxSegmentValue) {
353
357
  maxSegmentValue = segmentValue;
354
358
  }
359
+ }
360
+ }
361
+ if (maxSegmentValue <= 0)
362
+ return orderEngineItem;
363
+ // One selected option per segment group (segments are single-select).
364
+ const selections = [];
365
+ for (let g = 0; g < segmentGroups.length; g++) {
366
+ const options = segmentGroups[g].options ?? [];
367
+ for (let o = 0; o < options.length; o++) {
368
+ const option = options[o];
369
+ if (!option.default || option.is_free)
370
+ continue;
371
+ selections.push({ groupIndex: g, optionIndex: o });
355
372
  break; // single-select: at most one selected option
356
373
  }
357
374
  }
358
- if (selections.length < 2 || maxSegmentValue <= 0)
375
+ if (selections.length === 0)
359
376
  return orderEngineItem;
377
+ // RULE for where the raise lands: on the FIRST PRICED element along the
378
+ // segment's price chain —
379
+ // 1. the segment option's own price (the item base) when it is non-zero
380
+ // (same as the modern flow, which raises the item price), or
381
+ // 2. otherwise, the first priced required leaf down the chain: the single
382
+ // option of a single-option required group (its price is fixed — e.g.
383
+ // "ROMA -> Pizza Size -> Large 13\""), or the currently selected
384
+ // option of a multi-option required group,
385
+ // 3. otherwise, the segment option's own price (even when 0).
386
+ // This keeps the raise where the price actually lives and never invents a
387
+ // price on an empty slot or an unselected option.
388
+ const findRaiseTarget = (option) => {
389
+ if ((option.price ?? 0) > 0)
390
+ return option;
391
+ for (const modifier of option.modifiers ?? []) {
392
+ const isRequired = (modifier.min_num_options != null && modifier.min_num_options > 0) ||
393
+ (modifier.min_aggregate_options_quantity != null && modifier.min_aggregate_options_quantity > 0);
394
+ if (!isRequired)
395
+ continue;
396
+ const options = modifier.options ?? [];
397
+ const candidate = options.length === 1 ? options[0] : (options.find((opt) => opt.default) ?? null);
398
+ if (candidate != null && (candidate.price ?? 0) > 0)
399
+ return candidate;
400
+ }
401
+ return null;
402
+ };
403
+ const raiseSegmentOption = (option, delta) => {
404
+ const target = findRaiseTarget(option);
405
+ if (target == null || target._id === option._id) {
406
+ return { ...option, price: (option.price ?? 0) + delta };
407
+ }
408
+ return {
409
+ ...option,
410
+ modifiers: option.modifiers.map((modifier) => ({
411
+ ...modifier,
412
+ options: modifier.options.map((opt) => opt._id === target._id ? { ...opt, price: (opt.price ?? 0) + delta } : opt),
413
+ })),
414
+ };
415
+ };
360
416
  const modifiers = (orderEngineItem.modifiers ?? []).map((modifier, g) => {
361
417
  if (modifier.depth !== 0)
362
418
  return modifier;
@@ -365,13 +421,14 @@ function applyChooseExpensiveSegmentPrice(orderEngineItem) {
365
421
  if (selection == null)
366
422
  return option;
367
423
  const segmentValue = getSegmentTotal(option);
368
- // Already at (or above) the most expensive final price: keep as is.
424
+ // Already at (or above) the most expensive segment price: keep as is.
369
425
  if (segmentValue >= maxSegmentValue)
370
426
  return option;
371
- // Raise the option's own price by the difference so the segment's final
372
- // price (base + upsell + required modifiers) equals the expensive one.
373
- // The IHalfHalfSetup upsell (item price) is untouched.
374
- return { ...option, price: (option.price ?? 0) + (maxSegmentValue - segmentValue) };
427
+ // Raise by the difference so the segment's final price (base + upsell +
428
+ // required modifiers) equals the expensive one — applied to the priced
429
+ // leaf when available (see `raiseSegmentOption`). The IHalfHalfSetup
430
+ // upsell (item price) is untouched.
431
+ return raiseSegmentOption(option, maxSegmentValue - segmentValue);
375
432
  });
376
433
  return { ...modifier, options };
377
434
  });
@@ -490,6 +547,24 @@ function convertCatalogItemModifierToOrderEngineItemModifier({ modifiers, orderI
490
547
  const options = makeSpaceForFreeOptions(orderEngineSingleModifier.options, modifier.num_free_options);
491
548
  orderEngineSingleModifier.options = options;
492
549
  }
550
+ // A required modifier with exactly ONE selectable option left has no real
551
+ // choice — auto-select it so the user doesn't have to pick the only
552
+ // option (e.g. "Half/Half Pizza -> Pizza Size -> Large 13\""). Applied
553
+ // recursively: nested required groups under an option get the same
554
+ // treatment; they only reveal once their parent option is selected.
555
+ const isRequiredModifier = (modifier.min_num_options != null && modifier.min_num_options > 0) ||
556
+ (modifier.min_aggregate_options_quantity != null && modifier.min_aggregate_options_quantity > 0);
557
+ if (isRequiredModifier && orderEngineSingleModifier.options.length === 1) {
558
+ const singleOption = orderEngineSingleModifier.options[0];
559
+ if (!singleOption.default) {
560
+ orderEngineSingleModifier.options[0] = {
561
+ ...singleOption,
562
+ default: true,
563
+ quantity: modifier.min_option_choice_quantity || 1,
564
+ };
565
+ totalDefaultOptions = 1;
566
+ }
567
+ }
493
568
  orderEngineSingleModifier.default = totalDefaultOptions > 0;
494
569
  orderEngineModifiers.push(orderEngineSingleModifier);
495
570
  }
@@ -907,10 +982,9 @@ function setCheckFieldInOrderEngineItem(orderEngineItem, identifier, shouldCheck
907
982
  // halves the user actually picked, so it cannot run during
908
983
  // `IHalfHalfSetup -> IItem` conversion — it is applied right here, after
909
984
  // every selection, whenever the caller passes the setup flag. It compares
910
- // the FINAL segment prices (option base + item upsell + required/price-
911
- // defining modifiers — all baked at conversion) and raises the cheaper ones
912
- // to the most expensive. Idempotent: it never lowers a price, so re-running
913
- // is safe.
985
+ // EVERY segment option (option base + item upsell + required/price-defining
986
+ // modifiers) and raises the selected ones to the setup's most expensive
987
+ // segment. Idempotent: it never lowers a price, so re-running is safe.
914
988
  if (orderEngineItem.item_type === constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF && chooseExpensiveSegmentPrice === true) {
915
989
  return applyChooseExpensiveSegmentPrice(updatedOrderEngineItem);
916
990
  }
@@ -56,13 +56,18 @@ describe('convertHalfHalfSetupToSimplifiedHalfHalfSetup', () => {
56
56
  expect(simplified).not.toHaveProperty('items');
57
57
  expect(simplified).not.toHaveProperty('excluded_refs');
58
58
  expect(simplified).not.toHaveProperty('exclusive_refs');
59
- expect(simplified).not.toHaveProperty('choose_expensive_segment_price');
60
59
  expect(simplified).not.toHaveProperty('upsell_price');
61
60
  });
61
+ it('carries choose_expensive_segment_price through', () => {
62
+ const simplified = (0, simplified_halfhalf_1.convertHalfHalfSetupToSimplifiedHalfHalfSetup)(makeHalfHalfSetup({ choose_expensive_segment_price: true }), TENANT_ID);
63
+ expect(simplified.choose_expensive_segment_price).toBe(true);
64
+ });
65
+ it('omits choose_expensive_segment_price when not enabled', () => {
66
+ const simplified = (0, simplified_halfhalf_1.convertHalfHalfSetupToSimplifiedHalfHalfSetup)(makeHalfHalfSetup({ choose_expensive_segment_price: false }), TENANT_ID);
67
+ expect(simplified.choose_expensive_segment_price).toBeUndefined();
68
+ });
62
69
  it('minifies setup prices', () => {
63
- const simplified = (0, simplified_halfhalf_1.convertHalfHalfSetupToSimplifiedHalfHalfSetup)(makeHalfHalfSetup({
64
- discount_amount: { BASE_PRICE: 300 },
65
- }), TENANT_ID);
70
+ const simplified = (0, simplified_halfhalf_1.convertHalfHalfSetupToSimplifiedHalfHalfSetup)(makeHalfHalfSetup({ discount_amount: { BASE_PRICE: 300 } }), TENANT_ID);
66
71
  expect(simplified.discount_amount).toBe(300);
67
72
  });
68
73
  it('handles a halfhalf sold out for the tenant', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.696",
3
+ "version": "0.2.699",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",