@managesome/eposmatic-toolkit 0.2.700 → 0.2.702

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.
@@ -94,6 +94,7 @@ interface IConvertHalfHalfToOrderEngineItem {
94
94
  platform: PLATFORMS_ENUM;
95
95
  existingOrderItem?: IOrderItem;
96
96
  orderItemId?: string;
97
+ chooseExpensiveSegmentPrice?: boolean;
97
98
  }
98
99
  /**
99
100
  * Converts the legacy half-half selection (per-segment IOrderItems) into a
@@ -110,7 +111,7 @@ interface IConvertHalfHalfToOrderEngineItem {
110
111
  * own modifier tree is carried as nested selected engine modifiers; all
111
112
  * options are kept selected (they were chosen), so sync/error checks pass.
112
113
  */
113
- export declare function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform, existingOrderItem, orderItemId, }: IConvertHalfHalfToOrderEngineItem): IOrderEngineItem;
114
+ export declare function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform, existingOrderItem, orderItemId, chooseExpensiveSegmentPrice, }: IConvertHalfHalfToOrderEngineItem): IOrderEngineItem;
114
115
  /**
115
116
  * De-structures a HALF_HALF IOrderItem back into the legacy per-segment
116
117
  * selection format used by the HalfHalfScreen selection flow.
@@ -213,18 +213,63 @@ function convertHalfHalfSetupToWorkableHalfHalfSetup(halfhalfSetups, itemsMap, p
213
213
  expensiveMaxTotal = entries.reduce((max, entry) => Math.max(max, entry.segmentTotal), 0);
214
214
  }
215
215
  const items = [];
216
+ const raisePriceBy = (price, delta) => {
217
+ // Raise BOTH the active platform's key AND the base by `delta` so the
218
+ // effective price (`getPriceEnum` reads `price[platform] ?? BASE_PRICE`)
219
+ // is normalized even for items priced per-platform (e.g. POS_DINE_IN /
220
+ // WEB_DELIVERY keys on the same IPrice object).
221
+ const deltaObj = { BASE_PRICE: delta };
222
+ deltaObj[platform] = delta;
223
+ return (0, platform_num_1.mergePriceObjs)(price, deltaObj);
224
+ };
225
+ // RULE (same as the engine's `findRaiseTarget`): the raise is applied to
226
+ // the FIRST PRICED element along the segment's price chain —
227
+ // 1. the item's own price when non-zero, else
228
+ // 2. the priced required leaf: the single option of a single-option
229
+ // required group (its price is fixed), or the selected/default
230
+ // option of a multi-option group (never an unselected sibling), else
231
+ // 3. fall back to the item price.
232
+ const findRaiseTargetOption = (modifiers) => {
233
+ for (const modifier of modifiers ?? []) {
234
+ const isRequired = (modifier.min_num_options != null && modifier.min_num_options > 0) ||
235
+ (modifier.min_aggregate_options_quantity != null && modifier.min_aggregate_options_quantity > 0);
236
+ if (!isRequired)
237
+ continue;
238
+ const options = modifier.options ?? [];
239
+ if (options.length === 1)
240
+ return options[0];
241
+ const selectedOption = options.find((option) => option.default);
242
+ if (selectedOption != null)
243
+ return selectedOption;
244
+ }
245
+ return null;
246
+ };
216
247
  for (const entry of entries) {
217
248
  const { workableItem, itemUpsell, segmentTotal } = entry;
218
249
  if (halfhalfSetup.choose_expensive_segment_price && segmentTotal < expensiveMaxTotal) {
219
250
  const delta = expensiveMaxTotal - segmentTotal;
220
- // Raise BOTH the active platform's key AND the base by `delta` so the
221
- // effective price (`getPriceEnum` reads `price[platform] ??
222
- // BASE_PRICE`) is normalized even for items priced per-platform (e.g.
223
- // POS_DINE_IN / WEB_DELIVERY keys on the same IPrice object).
224
- workableItem.price = (0, platform_num_1.mergePriceObjs)(workableItem.price, {
225
- BASE_PRICE: delta,
226
- [platform]: delta,
227
- });
251
+ if ((0, platform_catalog_1.getPriceEnum)(workableItem.price, platform) > 0) {
252
+ // Rule 1: the item itself carries the price.
253
+ workableItem.price = raisePriceBy(workableItem.price, delta);
254
+ }
255
+ else {
256
+ // Rule 2: the price lives on a required leaf — raise it there
257
+ // (e.g. "ROMA -> Pizza Size -> Large 13\"").
258
+ const targetOption = findRaiseTargetOption(workableItem.modifiers);
259
+ const currentModifiers = workableItem.modifiers ?? [];
260
+ if (targetOption != null && currentModifiers.length > 0) {
261
+ workableItem.modifiers = currentModifiers.map((modifier) => ({
262
+ ...modifier,
263
+ options: modifier.options.map((option) => option._id === targetOption._id
264
+ ? { ...option, price: raisePriceBy(option.price ?? null, delta) }
265
+ : option),
266
+ }));
267
+ }
268
+ else {
269
+ // Rule 3: fall back to the item price.
270
+ workableItem.price = raisePriceBy(workableItem.price, delta);
271
+ }
272
+ }
228
273
  }
229
274
  // Each half keeps its own item-level upsell, added ON TOP of the divided
230
275
  // price. The half-half upsell is a ONE-TIME addition on the order item,
@@ -521,7 +566,7 @@ function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quant
521
566
  * own modifier tree is carried as nested selected engine modifiers; all
522
567
  * options are kept selected (they were chosen), so sync/error checks pass.
523
568
  */
524
- function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform, existingOrderItem, orderItemId, }) {
569
+ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform, existingOrderItem, orderItemId, chooseExpensiveSegmentPrice, }) {
525
570
  let menu_id;
526
571
  const toEngineModifiers = (modifiers, depth) => (modifiers ?? []).map((modifier) => {
527
572
  const options = modifier.options ?? [];
@@ -606,10 +651,15 @@ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems,
606
651
  menu_id: menu_id ?? null,
607
652
  item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
608
653
  is_ready: existingOrderItem?.is_ready ?? false,
609
- is_split: existingOrderItem?.is_split ?? false,
610
- moved_to_order_id: existingOrderItem?.moved_to_order_id ?? null,
611
- moved_from_order_id: existingOrderItem?.moved_from_order_id ?? null,
612
- ready_minutes: existingOrderItem?.ready_minutes ?? null,
654
+ // Match the classic engine conversion exactly: these fields are only
655
+ // carried when they already exist on the order item (undefined on create
656
+ // so they're omitted from the payload, same as convertCatalogItemTo-
657
+ // OrderEngineItem). Explicit null/false defaults here caused a shape
658
+ // mismatch (null vs undefined) between classic and modern lines.
659
+ is_split: existingOrderItem?.is_split,
660
+ moved_to_order_id: existingOrderItem?.moved_to_order_id,
661
+ moved_from_order_id: existingOrderItem?.moved_from_order_id,
662
+ ready_minutes: existingOrderItem?.ready_minutes,
613
663
  name: halfHalfItem.name,
614
664
  description: halfHalfItem.description,
615
665
  loyalty_applied: false,
@@ -624,7 +674,11 @@ function convertHalfHalfToOrderEngineItem({ halfHalfItem, selectedHalfHalfItems,
624
674
  item_discount_details: (0, order_engine_1.convertItemDiscountToOrderItemDiscount)(halfHalfItem.discount_amount, halfHalfItem.is_discount_percentage, platform, halfHalfItem._id),
625
675
  modifiers,
626
676
  };
627
- return (0, order_engine_1.syncOrderEngineItem)(orderEngineItem);
677
+ const syncedOrderEngineItem = (0, order_engine_1.syncOrderEngineItem)(orderEngineItem);
678
+ if (chooseExpensiveSegmentPrice === true) {
679
+ return (0, order_engine_1.applyChooseExpensiveSegmentPrice)(syncedOrderEngineItem);
680
+ }
681
+ return syncedOrderEngineItem;
628
682
  }
629
683
  // ====================================================================================================
630
684
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.2.700",
3
+ "version": "0.2.702",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",