@managesome/eposmatic-toolkit 0.2.659 → 0.2.674

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.
@@ -21,6 +21,9 @@ export interface IOrderEngineItem {
21
21
  item_type: ORDER_ITEM_TYPE_ENUM;
22
22
  is_ready?: boolean;
23
23
  is_split?: boolean;
24
+ moved_to_order_id?: string | null;
25
+ moved_from_order_id?: string | null;
26
+ ready_minutes?: number | null;
24
27
  name: string;
25
28
  description: string;
26
29
  loyalty_applied: boolean;
@@ -38,20 +38,23 @@ interface IConvertHalfHalfToOrderItem {
38
38
  quantity: number;
39
39
  platform: PLATFORMS_ENUM;
40
40
  existingOrderItem?: IOrderItem;
41
+ orderItemId?: string;
41
42
  }
42
43
  /**
43
44
  * Converts the legacy half-half selection (per-segment IOrderItems) into a
44
45
  * single HALF_HALF IOrderItem. Each chosen segment becomes an IOrderModifier
45
- * (stable id `${halfHalfItem._id}_segment_${s}`) with the chosen item as its
46
- * single option, so it can be de-structured back on edit.
46
+ * whose `_id` is a fresh BSON ObjectId (generated on create, like regular
47
+ * items) and whose `ref` is the segment's deterministic `_id` — so it is the
48
+ * same every time this half-half is punched. Each option's `ref` is the
49
+ * chosen item's `_id`, allowing the item to be de-structured back on edit.
47
50
  */
48
- export declare function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform: _platform, existingOrderItem, }: IConvertHalfHalfToOrderItem): IOrderItem;
51
+ export declare function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform: _platform, existingOrderItem, orderItemId, }: IConvertHalfHalfToOrderItem): IOrderItem;
49
52
  /**
50
53
  * De-structures a HALF_HALF IOrderItem back into the legacy per-segment
51
54
  * selection format used by the HalfHalfScreen selection flow.
52
55
  *
53
- * Matches each order modifier to its segment via the stable
54
- * `${halfHalfItem._id}_segment_${s}` id/ref. Returns null if the order item
56
+ * Segment identity: the modifier's ref = the segment's real `_id`. Position
57
+ * and first-empty-slot are kept as fallbacks. Returns null if the order item
55
58
  * doesn't belong to the provided halfhalf setup.
56
59
  */
57
60
  export declare function deStructureHalfHalfOrderItem(orderItem: IOrderItem, halfHalfItem: IWorkableHalfHalfSetup<IItem>): {
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.getHalfHalfPostedData = getHalfHalfPostedData;
4
7
  exports.renderBulkHalfHalfUpdate = renderBulkHalfHalfUpdate;
@@ -9,6 +12,7 @@ exports.convertHalfHalfSetupToWorkableHalfHalfSetup = convertHalfHalfSetupToWork
9
12
  exports.convertHalfHalfToItem = convertHalfHalfToItem;
10
13
  exports.convertHalfHalfToOrderItem = convertHalfHalfToOrderItem;
11
14
  exports.deStructureHalfHalfOrderItem = deStructureHalfHalfOrderItem;
15
+ const bson_objectid_1 = __importDefault(require("bson-objectid"));
12
16
  const constants_1 = require("../../constants");
13
17
  const store_1 = require("../store");
14
18
  const platform_catalog_1 = require("./platform-catalog");
@@ -103,7 +107,7 @@ function convertHalfHalfSetupToWorkableHalfHalfSetup(halfhalfSetups, itemsMap, p
103
107
  if (!workableItem)
104
108
  continue;
105
109
  if (upsell_price != null) {
106
- workableItem.price = (0, platform_num_1.mergePriceObjs)(workableItem.price, item.upsell_price);
110
+ workableItem.price = (0, platform_num_1.mergePriceObjs)(workableItem.price, upsell_price);
107
111
  }
108
112
  if (discount_amount != null) {
109
113
  workableItem.discount_amount = null;
@@ -112,9 +116,9 @@ function convertHalfHalfSetupToWorkableHalfHalfSetup(halfhalfSetups, itemsMap, p
112
116
  workableItem.price = (0, platform_num_1.mergePriceObjs)(workableItem.price, item.upsell_price);
113
117
  }
114
118
  // we divide the price by the number of segments
115
- workableItem.price = (0, platform_num_1.convertPriceObjToSegmentPrice)(workableItem.price, halfhalfSetup.segments);
119
+ workableItem.price = (0, platform_num_1.convertPriceObjToSegmentPrice)(workableItem.price, halfhalfSetup.segments.length);
116
120
  // we divide the discount_amount by the number of segments
117
- workableItem.discount_amount = (0, platform_num_1.convertPriceObjToSegmentPrice)(workableItem.discount_amount, halfhalfSetup.segments);
121
+ workableItem.discount_amount = (0, platform_num_1.convertPriceObjToSegmentPrice)(workableItem.discount_amount, halfhalfSetup.segments.length);
118
122
  items.push(workableItem);
119
123
  }
120
124
  if (items.length === 0)
@@ -169,6 +173,11 @@ function convertHalfHalfToItem(halfhalfSetup, itemsMap, platform, tenant_id) {
169
173
  }
170
174
  // ── Build shared option list (segment-neutral ids) ─────────────
171
175
  const halfHalfItems = [...halfhalfSetup.items].sort((a, b) => a.sort_id - b.sort_id);
176
+ // Aggregate the union of keywords/caveats/dietary_preferences across all
177
+ // constituent items so the half-half shows them too.
178
+ const allKeywords = [];
179
+ const allCaveats = [];
180
+ const allDietaryPreferences = [];
172
181
  const baseOptions = [];
173
182
  for (let i = 0; i < halfHalfItems.length; i++) {
174
183
  const halfHalfItem = halfHalfItems[i];
@@ -178,15 +187,20 @@ function convertHalfHalfToItem(halfhalfSetup, itemsMap, platform, tenant_id) {
178
187
  const workableItem = (0, platform_catalog_1.convertItemToWorkableItem)(catalogItem, platform, tenant_id);
179
188
  if (!workableItem)
180
189
  continue;
190
+ allKeywords.push(...(workableItem.keywords ?? []));
191
+ allCaveats.push(...(workableItem.caveats ?? []));
192
+ allDietaryPreferences.push(...(workableItem.dietary_preferences ?? []));
181
193
  let optionPrice = workableItem.price;
182
194
  if (upsell_price != null)
183
195
  optionPrice = (0, platform_num_1.mergePriceObjs)(optionPrice, upsell_price);
184
196
  if (halfHalfItem.upsell_price != null) {
185
197
  optionPrice = (0, platform_num_1.mergePriceObjs)(optionPrice, halfHalfItem.upsell_price);
186
198
  }
187
- optionPrice = (0, platform_num_1.convertPriceObjToSegmentPrice)(optionPrice, halfhalfSetup.segments);
199
+ optionPrice = (0, platform_num_1.convertPriceObjToSegmentPrice)(optionPrice, halfhalfSetup.segments.length);
188
200
  baseOptions.push({
189
- _id: `${halfhalfSetup._id}_${catalogItem._id}`,
201
+ // `_id` is a generated BSON ObjectId (unique); `ref` is the actual
202
+ // item `_id` (deterministic, used for reporting).
203
+ _id: (0, bson_objectid_1.default)().toHexString(),
190
204
  ref: catalogItem._id,
191
205
  name: catalogItem.name,
192
206
  active: catalogItem.active,
@@ -201,21 +215,27 @@ function convertHalfHalfToItem(halfhalfSetup, itemsMap, platform, tenant_id) {
201
215
  return null;
202
216
  // ── Build one single-select modifier per segment ───────────────
203
217
  const modifiers = [];
204
- for (let s = 0; s < halfhalfSetup.segments; s++) {
218
+ for (let s = 0; s < halfhalfSetup.segments.length; s++) {
219
+ const segment = halfhalfSetup.segments[s];
205
220
  modifiers.push({
206
- _id: `${halfhalfSetup._id}_segment_${s}`,
207
- ref: `${halfhalfSetup._id}_segment_${s}`,
208
- name: `Segment ${s + 1}`,
221
+ // `_id` is a generated BSON ObjectId (unique per creation); `ref` is the
222
+ // segment's real `_id` — deterministic, unique per portion and the same
223
+ // every time this half-half is punched.
224
+ _id: (0, bson_objectid_1.default)().toHexString(),
225
+ ref: segment._id,
226
+ name: segment.name || `Segment ${s + 1}`,
209
227
  description: '',
210
228
  active: true,
211
229
  sold_out: [],
212
- sort_id: s,
230
+ sort_id: segment.sort_id ?? s,
213
231
  min_num_options: 1,
214
232
  max_num_options: 1,
215
233
  num_free_options: null,
216
234
  options: baseOptions.map((option) => ({
217
235
  ...option,
218
- _id: `${halfhalfSetup._id}_segment_${s}_${option.ref}`,
236
+ // `_id` is a generated BSON ObjectId (unique); `ref` stays the item
237
+ // `_id` (deterministic, used for reporting).
238
+ _id: (0, bson_objectid_1.default)().toHexString(),
219
239
  })),
220
240
  });
221
241
  }
@@ -235,10 +255,10 @@ function convertHalfHalfToItem(halfhalfSetup, itemsMap, platform, tenant_id) {
235
255
  price: null,
236
256
  discount_amount,
237
257
  is_discount_percentage: halfhalfSetup.is_discount_percentage,
238
- caveats: [],
239
- dietary_preferences: [],
258
+ caveats: Array.from(new Set(allCaveats)),
259
+ dietary_preferences: Array.from(new Set(allDietaryPreferences)),
240
260
  tax_rate: null,
241
- keywords: [],
261
+ keywords: Array.from(new Set(allKeywords)),
242
262
  image_url: halfhalfSetup.image_url,
243
263
  ingredients: null,
244
264
  item_special_hours: null, // already resolved into upsell_price / discount_amount
@@ -256,23 +276,32 @@ function convertHalfHalfToItem(halfhalfSetup, itemsMap, platform, tenant_id) {
256
276
  /**
257
277
  * Converts the legacy half-half selection (per-segment IOrderItems) into a
258
278
  * single HALF_HALF IOrderItem. Each chosen segment becomes an IOrderModifier
259
- * (stable id `${halfHalfItem._id}_segment_${s}`) with the chosen item as its
260
- * single option, so it can be de-structured back on edit.
279
+ * whose `_id` is a fresh BSON ObjectId (generated on create, like regular
280
+ * items) and whose `ref` is the segment's deterministic `_id` — so it is the
281
+ * same every time this half-half is punched. Each option's `ref` is the
282
+ * chosen item's `_id`, allowing the item to be de-structured back on edit.
261
283
  */
262
- function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform: _platform, existingOrderItem, }) {
284
+ function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quantity, platform: _platform, existingOrderItem, orderItemId, }) {
263
285
  const modifiers = [];
264
286
  let menu_id;
265
287
  for (let s = 0; s < selectedHalfHalfItems.length; s++) {
266
288
  const selectedItem = selectedHalfHalfItems[s];
267
289
  if (!selectedItem)
268
290
  continue;
291
+ // The selected menu id (each selection item carries it) — the half-half
292
+ // belongs to the selected menu; only its constituent items (segments)
293
+ // bypass the menu filter.
269
294
  if (selectedItem.menu_id != null && menu_id == null) {
270
295
  menu_id = selectedItem.menu_id;
271
296
  }
272
297
  modifiers.push({
273
- _id: `${halfHalfItem._id}_segment_${s}`,
274
- ref: `${halfHalfItem._id}_segment_${s}`,
275
- name: `Segment ${s + 1}`,
298
+ // `_id` must be a BSON ObjectId — generated on create, same as regular
299
+ // order items (convertItemToOrderItem).
300
+ _id: (0, bson_objectid_1.default)().toHexString(),
301
+ // Deterministic ref = the segment's real `_id` — unique per portion and
302
+ // the same every time this half-half is punched.
303
+ ref: halfHalfItem.segments[s]?._id ?? halfHalfItem._id,
304
+ name: halfHalfItem.segments[s]?.name || `Segment ${s + 1}`,
276
305
  options: [
277
306
  {
278
307
  _id: selectedItem._id,
@@ -294,8 +323,13 @@ function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quant
294
323
  _gst_amount: 0,
295
324
  _discount_amount: 0,
296
325
  ...existingOrderItem,
297
- _id: halfHalfItem._id,
326
+ // Keep the original order-item id on edit (otherwise getCartItemsObj would
327
+ // treat it as removed + new); generate a fresh ObjectId on create, same as
328
+ // regular items.
329
+ _id: existingOrderItem?._id ?? orderItemId ?? (0, bson_objectid_1.default)().toHexString(),
298
330
  item_id: halfHalfItem._id,
331
+ // The half-half belongs to the selected menu; only its constituent items
332
+ // (segments) bypass the menu filter.
299
333
  menu_id,
300
334
  item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
301
335
  name: halfHalfItem.name,
@@ -315,20 +349,24 @@ function convertHalfHalfToOrderItem({ halfHalfItem, selectedHalfHalfItems, quant
315
349
  * De-structures a HALF_HALF IOrderItem back into the legacy per-segment
316
350
  * selection format used by the HalfHalfScreen selection flow.
317
351
  *
318
- * Matches each order modifier to its segment via the stable
319
- * `${halfHalfItem._id}_segment_${s}` id/ref. Returns null if the order item
352
+ * Segment identity: the modifier's ref = the segment's real `_id`. Position
353
+ * and first-empty-slot are kept as fallbacks. Returns null if the order item
320
354
  * doesn't belong to the provided halfhalf setup.
321
355
  */
322
356
  function deStructureHalfHalfOrderItem(orderItem, halfHalfItem) {
323
357
  if (orderItem.item_id !== halfHalfItem._id)
324
358
  return null;
325
- const selectedHalfHalfItems = new Array(halfHalfItem.segments).fill(null);
359
+ const selectedHalfHalfItems = new Array(halfHalfItem.segments.length).fill(null);
326
360
  const orderModifiers = orderItem.modifiers ?? [];
327
361
  for (let i = 0; i < orderModifiers.length; i++) {
328
362
  const orderModifier = orderModifiers[i];
329
- const segmentMatch = (orderModifier.ref ?? orderModifier._id ?? '').match(/_segment_(\d+)$/);
330
- let segmentIndex = segmentMatch != null ? Number(segmentMatch[1]) : NaN;
331
- if (Number.isNaN(segmentIndex) || segmentIndex < 0 || segmentIndex >= selectedHalfHalfItems.length) {
363
+ // Segment identity: the modifier ref = the segment's real `_id`; position
364
+ // and first-empty-slot are fallbacks for edge cases.
365
+ const modifierRef = orderModifier.ref ?? orderModifier._id ?? '';
366
+ let segmentIndex = halfHalfItem.segments.findIndex((segment) => segment._id === modifierRef);
367
+ if (segmentIndex === -1)
368
+ segmentIndex = i;
369
+ if (segmentIndex < 0 || segmentIndex >= selectedHalfHalfItems.length) {
332
370
  // Fallback: assign to the first empty slot
333
371
  segmentIndex = selectedHalfHalfItems.findIndex((item) => item === null);
334
372
  if (segmentIndex === -1)
@@ -487,6 +487,9 @@ function convertOrderEngineItemToOrderItem(orderItem, tenantId, offers, giftCard
487
487
  item_type: orderItem.item_type,
488
488
  is_ready: orderItem.is_ready,
489
489
  is_split: orderItem.is_split,
490
+ moved_to_order_id: orderItem.moved_to_order_id,
491
+ moved_from_order_id: orderItem.moved_from_order_id,
492
+ ready_minutes: orderItem.ready_minutes,
490
493
  name: orderItem.name,
491
494
  price: orderItem.price,
492
495
  subtotal: orderItem.price,
@@ -570,7 +573,7 @@ function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenant
570
573
  }
571
574
  const { _id = (0, bson_objectid_1.default)().toHexString(), menu_id = defaultMenuId,
572
575
  // For New Item, If zeroPriceRefs is present then we set the price to 0
573
- price = defaultItemPrice, tax_rate = (0, platform_catalog_1.getPriceEnum)(item.tax_rate, platform), quantity = 1, subtotal = 0, special_instructions = '', item_discount_details = convertItemDiscountToOrderItemDiscount(discount_amount, is_special_discount_percentage, platform, item._id), item_type = orderItemType, is_ready, is_split, } = orderItem ?? {};
576
+ price = defaultItemPrice, tax_rate = (0, platform_catalog_1.getPriceEnum)(item.tax_rate, platform), quantity = 1, subtotal = 0, special_instructions = '', item_discount_details = convertItemDiscountToOrderItemDiscount(discount_amount, is_special_discount_percentage, platform, item._id), item_type = orderItemType, is_ready, is_split, moved_to_order_id, moved_from_order_id, ready_minutes, } = orderItem ?? {};
574
577
  const loyaltyApplied = applyLoyalty != null && applyLoyalty.refs.includes(item._id);
575
578
  const voucherApplied = applyVoucher != null && applyVoucher.refs.includes(item._id);
576
579
  let mergedItemDiscountDetails = item_discount_details;
@@ -616,6 +619,9 @@ function convertCatalogItemToOrderEngineItem({ item, orderItem, platform, tenant
616
619
  item_type,
617
620
  is_ready,
618
621
  is_split,
622
+ moved_to_order_id,
623
+ moved_from_order_id,
624
+ ready_minutes,
619
625
  price,
620
626
  tax_rate,
621
627
  quantity,
@@ -1,5 +1,5 @@
1
1
  import { PLATFORMS_ENUM } from '../../constants';
2
- import { IItem, IOrderItem, ISpecial } from '../../schemas';
2
+ import { IItem, IModifier, IOrderItem, ISpecial } from '../../schemas';
3
3
  import { ISimplifiedCatalogItem } from '../types';
4
4
  export declare function getSpecialsPostedData(data: ISpecial[], payload: ISpecial): ISpecial[];
5
5
  export declare function renderBulkSpecialsUpdate(data: ISpecial[], payload: Partial<ISpecial>[]): ISpecial[];
@@ -29,6 +29,16 @@ export type IWorkableSpecialMenu<T extends IItem | ISimplifiedCatalogItem> = {
29
29
  show_starting_from: ISpecial['show_starting_from'];
30
30
  modifier_specials: IWorkableModifierSpecial<T>[];
31
31
  };
32
+ /**
33
+ * Applies a modifier-special group's reference rules to a constituent item's
34
+ * own modifiers:
35
+ * - excluded_refs: modifiers/options whose ref is listed are REMOVED.
36
+ * - exclusive_refs: modifiers/options whose ref is listed have their PRICE
37
+ * REMOVED (set to null → treated as free) but stay selectable.
38
+ *
39
+ * Returns null when nothing is left after exclusion.
40
+ */
41
+ export declare function applyModifierSpecialRefRules(modifiers: IModifier[] | null | undefined, exclusiveRefs: string[], excludedRefs: string[]): IModifier[] | null;
32
42
  export declare function convertSpecialsToWorkableSpecials<T extends IItem | ISimplifiedCatalogItem>(specials: ISpecial[], itemsMap: Record<string, T>, platform: PLATFORMS_ENUM | undefined, tenant_id: string | undefined): IWorkableSpecialMenu<T>[];
33
43
  interface IModifierSpecials {
34
44
  _id: string;
@@ -40,8 +50,9 @@ interface IConvertSpecialItemToOrderItem {
40
50
  quantity: number;
41
51
  platform: PLATFORMS_ENUM;
42
52
  existingOrderItem?: IOrderItem;
53
+ orderItemId?: string;
43
54
  }
44
- export declare function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity, platform, existingOrderItem, }: IConvertSpecialItemToOrderItem): IOrderItem;
55
+ export declare function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity, platform, existingOrderItem, orderItemId, }: IConvertSpecialItemToOrderItem): IOrderItem;
45
56
  /**
46
57
  * De-structures a special IOrderItem back into the legacy selection format
47
58
  * used by the SpecialsScreen selection flow (edit without classic preview).
@@ -8,6 +8,7 @@ exports.renderBulkSpecialsUpdate = renderBulkSpecialsUpdate;
8
8
  exports.getSpecialsUpdatedData = getSpecialsUpdatedData;
9
9
  exports.getSpecialsDeletedData = getSpecialsDeletedData;
10
10
  exports.getSpecialWithPrice = getSpecialWithPrice;
11
+ exports.applyModifierSpecialRefRules = applyModifierSpecialRefRules;
11
12
  exports.convertSpecialsToWorkableSpecials = convertSpecialsToWorkableSpecials;
12
13
  exports.convertSpecialItemToOrderItem = convertSpecialItemToOrderItem;
13
14
  exports.deStructureSpecialOrderItem = deStructureSpecialOrderItem;
@@ -75,6 +76,39 @@ function getSpecialWithPrice(special, writeable) {
75
76
  : {}),
76
77
  };
77
78
  }
79
+ /**
80
+ * Applies a modifier-special group's reference rules to a constituent item's
81
+ * own modifiers:
82
+ * - excluded_refs: modifiers/options whose ref is listed are REMOVED.
83
+ * - exclusive_refs: modifiers/options whose ref is listed have their PRICE
84
+ * REMOVED (set to null → treated as free) but stay selectable.
85
+ *
86
+ * Returns null when nothing is left after exclusion.
87
+ */
88
+ function applyModifierSpecialRefRules(modifiers, exclusiveRefs, excludedRefs) {
89
+ if (!modifiers || modifiers.length === 0)
90
+ return null;
91
+ let result = modifiers.map((modifier) => {
92
+ const isModifierExclusive = exclusiveRefs.includes(modifier.ref);
93
+ const options = modifier.options.map((option) => {
94
+ if (isModifierExclusive || exclusiveRefs.includes(option.ref)) {
95
+ return { ...option, price: null };
96
+ }
97
+ return option;
98
+ });
99
+ return { ...modifier, options };
100
+ });
101
+ if (excludedRefs.length > 0) {
102
+ result = result
103
+ .filter((modifier) => !excludedRefs.includes(modifier.ref))
104
+ .map((modifier) => ({
105
+ ...modifier,
106
+ options: modifier.options.filter((option) => !excludedRefs.includes(option.ref)),
107
+ }));
108
+ }
109
+ const withOptions = result.filter((modifier) => modifier.options.length > 0);
110
+ return withOptions.length > 0 ? withOptions : null;
111
+ }
78
112
  function convertSpecialsToWorkableSpecials(specials, itemsMap, platform, tenant_id) {
79
113
  if (!platform || !tenant_id)
80
114
  return [];
@@ -125,6 +159,9 @@ function convertSpecialsToWorkableSpecials(specials, itemsMap, platform, tenant_
125
159
  if (item.upsell_price != null) {
126
160
  workableItem.price = (0, platform_num_1.mergePriceObjs)(workableItem.price, item.upsell_price);
127
161
  }
162
+ // Apply the group's reference rules to the item's own modifiers:
163
+ // excluded_refs removes modifiers/options, exclusive_refs removes their price.
164
+ workableItem.modifiers = applyModifierSpecialRefRules(workableItem.modifiers, modifierSpecial.exclusive_refs, modifierSpecial.excluded_refs);
128
165
  items.push(workableItem);
129
166
  }
130
167
  if (items.length === 0)
@@ -157,7 +194,7 @@ function convertSpecialsToWorkableSpecials(specials, itemsMap, platform, tenant_
157
194
  }
158
195
  return specialsMenu;
159
196
  }
160
- function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity, platform, existingOrderItem, }) {
197
+ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity, platform, existingOrderItem, orderItemId, }) {
161
198
  const modifiers = [];
162
199
  let menu_id;
163
200
  for (let i = 0; i < modifierSpecials.length; i++) {
@@ -170,7 +207,16 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
170
207
  const selectedItem = modifierSpecial.selectedItems[j];
171
208
  if (!selectedItem)
172
209
  continue;
173
- const option = {
210
+ // The selected menu id (each selection item carries it) — the special
211
+ // belongs to the selected menu; only its constituent items (modifiers)
212
+ // bypass the menu filter.
213
+ if (selectedItem.menu_id != null && menu_id == null) {
214
+ menu_id = selectedItem.menu_id;
215
+ }
216
+ // `_id` must be a BSON ObjectId (backend rejects plain strings). The
217
+ // selection item already carries a valid ObjectId — random on create and
218
+ // stable across edit round-trips. Determinism lives in `ref`.
219
+ options.push({
174
220
  _id: selectedItem._id,
175
221
  ref: selectedItem.item_id,
176
222
  name: selectedItem.name,
@@ -178,12 +224,25 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
178
224
  quantity: selectedItem.quantity,
179
225
  modifiers: selectedItem.modifiers,
180
226
  is_free: false,
181
- };
182
- options.push(option);
183
- if (selectedItem.menu_id != null && menu_id == null) {
184
- menu_id = selectedItem.menu_id;
185
- }
227
+ });
186
228
  }
229
+ // Mark the cheapest N selected items as free (num_free_items), same as the
230
+ // classic drawer flow (makeSpaceForFreeOptions semantics). Slot order kept.
231
+ const numFreeItems = originalModifierSpecial.num_free_items || 0;
232
+ if (numFreeItems > 0 && options.length > 0) {
233
+ const freeIndexes = new Set(options
234
+ .map((option, index) => ({ index, value: (option.price || 0) * (option.quantity || 1) }))
235
+ .sort((a, b) => a.value - b.value)
236
+ .slice(0, numFreeItems)
237
+ .map((entry) => entry.index));
238
+ options.forEach((option, index) => {
239
+ if (freeIndexes.has(index)) {
240
+ option.is_free = true;
241
+ }
242
+ });
243
+ }
244
+ // Modifier `_id` must be a BSON ObjectId — generated on create (same as the
245
+ // order engine). Edit matching uses the deterministic `ref`.
187
246
  const modifier = {
188
247
  _id: (0, bson_objectid_1.default)().toHexString(),
189
248
  ref: originalModifierSpecial._id,
@@ -200,8 +259,13 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
200
259
  _gst_amount: 0,
201
260
  _discount_amount: 0,
202
261
  ...existingOrderItem,
203
- _id: specialItem._id,
262
+ // Keep the original order-item id on edit (otherwise getCartItemsObj would
263
+ // treat it as removed + new); generate a fresh ObjectId on create, same as
264
+ // regular items.
265
+ _id: existingOrderItem?._id ?? orderItemId ?? (0, bson_objectid_1.default)().toHexString(),
204
266
  item_id: specialItem._id,
267
+ // The special belongs to the selected menu; only its constituent items
268
+ // (modifiers) bypass the menu filter.
205
269
  menu_id,
206
270
  item_type: constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL,
207
271
  /*
@@ -309,6 +373,9 @@ function convertSpecialToItem(special, itemsMap, platform, tenant_id) {
309
373
  }
310
374
  // ── Build modifiers from modifier_specials ─────────────────────
311
375
  const modifiers = [];
376
+ const allKeywords = [];
377
+ const allCaveats = [];
378
+ const allDietaryPreferences = [];
312
379
  for (let j = 0; j < special.modifier_specials.length; j++) {
313
380
  const modifierSpecial = special.modifier_specials[j];
314
381
  const omitItemLevelSchedule = modifierSpecial.omit_items_schedule;
@@ -323,6 +390,9 @@ function convertSpecialToItem(special, itemsMap, platform, tenant_id) {
323
390
  const workableItem = (0, platform_catalog_1.convertItemToWorkableItem)(catalogItem, platform, tenant_id, omitItemLevelSchedule, true);
324
391
  if (!workableItem)
325
392
  continue;
393
+ allKeywords.push(...(workableItem.keywords ?? []));
394
+ allCaveats.push(...(workableItem.caveats ?? []));
395
+ allDietaryPreferences.push(...(workableItem.dietary_preferences ?? []));
326
396
  // Determine option price:
327
397
  // special has its own price → options carry null price
328
398
  // (num_free_options in the drawer handles free-vs-paid)
@@ -332,22 +402,13 @@ function convertSpecialToItem(special, itemsMap, platform, tenant_id) {
332
402
  if (specialItem.upsell_price != null) {
333
403
  optionPrice = (0, platform_num_1.mergePriceObjs)(optionPrice, specialItem.upsell_price);
334
404
  }
335
- // Pre-filter the item's own modifiers using excluded_refs / exclusive_refs
336
- let itemModifiers = null;
337
- if (workableItem.modifiers != null && workableItem.modifiers.length > 0) {
338
- let filtered = workableItem.modifiers;
339
- if (modifierSpecial.exclusive_refs.length > 0) {
340
- // Only keep modifiers whose ref is in exclusive_refs
341
- filtered = filtered.filter((m) => modifierSpecial.exclusive_refs.includes(m.ref));
342
- }
343
- else if (modifierSpecial.excluded_refs.length > 0) {
344
- // Remove modifiers whose ref is in excluded_refs
345
- filtered = filtered.filter((m) => !modifierSpecial.excluded_refs.includes(m.ref));
346
- }
347
- itemModifiers = filtered.length > 0 ? filtered : null;
348
- }
405
+ // Pre-filter the item's own modifiers using excluded_refs / exclusive_refs:
406
+ // excluded_refs removes modifiers/options, exclusive_refs removes their price.
407
+ const itemModifiers = applyModifierSpecialRefRules(workableItem.modifiers, modifierSpecial.exclusive_refs, modifierSpecial.excluded_refs);
349
408
  options.push({
350
- _id: `${modifierSpecial._id}_${catalogItem._id}`,
409
+ // `_id` is a generated BSON ObjectId (unique); `ref` is the actual
410
+ // item `_id` (deterministic, used for reporting).
411
+ _id: (0, bson_objectid_1.default)().toHexString(),
351
412
  ref: catalogItem._id,
352
413
  name: catalogItem.name,
353
414
  active: catalogItem.active,
@@ -361,7 +422,10 @@ function convertSpecialToItem(special, itemsMap, platform, tenant_id) {
361
422
  if (options.length === 0)
362
423
  continue;
363
424
  modifiers.push({
364
- _id: modifierSpecial._id,
425
+ // `_id` is a generated BSON ObjectId — unique identifier of this
426
+ // modifier instance; `ref` is the source modifier-special `_id`
427
+ // (deterministic, used for matching/reporting).
428
+ _id: (0, bson_objectid_1.default)().toHexString(),
365
429
  ref: modifierSpecial._id,
366
430
  name: modifierSpecial.name,
367
431
  description: '',
@@ -390,10 +454,10 @@ function convertSpecialToItem(special, itemsMap, platform, tenant_id) {
390
454
  price,
391
455
  discount_amount,
392
456
  is_discount_percentage: special.is_discount_percentage,
393
- caveats: [],
394
- dietary_preferences: [],
457
+ caveats: Array.from(new Set(allCaveats)),
458
+ dietary_preferences: Array.from(new Set(allDietaryPreferences)),
395
459
  tax_rate: null,
396
- keywords: [],
460
+ keywords: Array.from(new Set(allKeywords)),
397
461
  image_url: special.image_url,
398
462
  ingredients: null,
399
463
  item_special_hours: null, // already resolved into price / discount_amount
@@ -1,4 +1,23 @@
1
1
  import { Schema } from 'mongoose';
2
+ export declare const HalfHalfSegmentSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
3
+ minimize: false;
4
+ }, {
5
+ _id: import("mongoose").Types.ObjectId;
6
+ name: string;
7
+ sort_id: number;
8
+ }, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
9
+ _id: import("mongoose").Types.ObjectId;
10
+ name: string;
11
+ sort_id: number;
12
+ }>> & import("mongoose").FlatRecord<{
13
+ _id: import("mongoose").Types.ObjectId;
14
+ name: string;
15
+ sort_id: number;
16
+ }> & Required<{
17
+ _id: import("mongoose").Types.ObjectId;
18
+ }> & {
19
+ __v: number;
20
+ }>;
2
21
  export declare const HalfHalfSetupSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
3
22
  timestamps: true;
4
23
  versionKey: false;
@@ -490,7 +509,19 @@ export declare const HalfHalfSetupSchema: Schema<any, import("mongoose").Model<a
490
509
  }>;
491
510
  excluded_refs: string[];
492
511
  exclusive_refs: string[];
493
- segments: number;
512
+ segments: import("mongoose").Types.DocumentArray<{
513
+ _id: import("mongoose").Types.ObjectId;
514
+ name: string;
515
+ sort_id: number;
516
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
517
+ _id: import("mongoose").Types.ObjectId;
518
+ name: string;
519
+ sort_id: number;
520
+ }> & {
521
+ _id: import("mongoose").Types.ObjectId;
522
+ name: string;
523
+ sort_id: number;
524
+ }>;
494
525
  upsell_price: {
495
526
  "POS_WALK-IN": number;
496
527
  "POS_TAKE-AWAY": number;
@@ -1022,7 +1053,19 @@ export declare const HalfHalfSetupSchema: Schema<any, import("mongoose").Model<a
1022
1053
  }>;
1023
1054
  excluded_refs: string[];
1024
1055
  exclusive_refs: string[];
1025
- segments: number;
1056
+ segments: import("mongoose").Types.DocumentArray<{
1057
+ _id: import("mongoose").Types.ObjectId;
1058
+ name: string;
1059
+ sort_id: number;
1060
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
1061
+ _id: import("mongoose").Types.ObjectId;
1062
+ name: string;
1063
+ sort_id: number;
1064
+ }> & {
1065
+ _id: import("mongoose").Types.ObjectId;
1066
+ name: string;
1067
+ sort_id: number;
1068
+ }>;
1026
1069
  upsell_price: {
1027
1070
  "POS_WALK-IN": number;
1028
1071
  "POS_TAKE-AWAY": number;
@@ -1554,7 +1597,19 @@ export declare const HalfHalfSetupSchema: Schema<any, import("mongoose").Model<a
1554
1597
  }>;
1555
1598
  excluded_refs: string[];
1556
1599
  exclusive_refs: string[];
1557
- segments: number;
1600
+ segments: import("mongoose").Types.DocumentArray<{
1601
+ _id: import("mongoose").Types.ObjectId;
1602
+ name: string;
1603
+ sort_id: number;
1604
+ }, import("mongoose").Types.Subdocument<import("mongoose").Types.ObjectId, any, {
1605
+ _id: import("mongoose").Types.ObjectId;
1606
+ name: string;
1607
+ sort_id: number;
1608
+ }> & {
1609
+ _id: import("mongoose").Types.ObjectId;
1610
+ name: string;
1611
+ sort_id: number;
1612
+ }>;
1558
1613
  upsell_price: {
1559
1614
  "POS_WALK-IN": number;
1560
1615
  "POS_TAKE-AWAY": number;
@@ -1,12 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HALFHALF_COLLECTION_INDEXES = exports.HALFHALF_COLLECTION_NAME = exports.HalfHalfSetupSchema = void 0;
3
+ exports.HALFHALF_COLLECTION_INDEXES = exports.HALFHALF_COLLECTION_NAME = exports.HalfHalfSetupSchema = exports.HalfHalfSegmentSchema = void 0;
4
4
  const mongoose_1 = require("mongoose");
5
5
  const categories_1 = require("./categories");
6
6
  const common_models_1 = require("./common-models");
7
7
  const items_1 = require("./items");
8
8
  const specials_1 = require("./specials");
9
9
  const tenants_1 = require("./tenants");
10
+ exports.HalfHalfSegmentSchema = new mongoose_1.Schema({
11
+ _id: {
12
+ type: mongoose_1.Schema.Types.ObjectId,
13
+ required: true,
14
+ immutable: true,
15
+ },
16
+ // `_id` is auto-generated as a BSON ObjectId by mongoose
17
+ name: {
18
+ type: String,
19
+ required: true,
20
+ },
21
+ sort_id: {
22
+ type: Number,
23
+ required: true,
24
+ },
25
+ }, {
26
+ minimize: false,
27
+ });
10
28
  exports.HalfHalfSetupSchema = new mongoose_1.Schema({
11
29
  _id: {
12
30
  type: mongoose_1.Schema.Types.ObjectId,
@@ -35,10 +53,12 @@ exports.HalfHalfSetupSchema = new mongoose_1.Schema({
35
53
  min: 1,
36
54
  },
37
55
  segments: {
38
- type: Number,
56
+ type: [exports.HalfHalfSegmentSchema],
39
57
  required: true,
40
- min: 2,
41
- max: 4,
58
+ validate: {
59
+ validator: (value) => Array.isArray(value) && (value.length === 2 || value.length === 4),
60
+ message: 'segments must have a length of 2 or 4',
61
+ },
42
62
  },
43
63
  image_url: {
44
64
  type: String,