@managesome/eposmatic-toolkit 0.2.659 → 0.2.675
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/helpers/types/order-engine.d.ts +3 -0
- package/lib/helpers/utils/halfhalf-setup-helpers.d.ts +8 -5
- package/lib/helpers/utils/halfhalf-setup-helpers.js +65 -27
- package/lib/helpers/utils/order-engine.js +7 -1
- package/lib/helpers/utils/receipt-engine.js +63 -0
- package/lib/helpers/utils/specials-helpers.d.ts +13 -2
- package/lib/helpers/utils/specials-helpers.js +91 -27
- package/lib/schemas/models/halfhalf_setup.d.ts +58 -3
- package/lib/schemas/models/halfhalf_setup.js +24 -4
- package/lib/schemas/models/orders.d.ts +324 -0
- package/lib/schemas/models/orders.js +12 -0
- package/lib/schemas/models/specials.js +2 -0
- package/lib/schemas/types/halfhalf_setup.d.ts +6 -1
- package/lib/schemas/types/orders.d.ts +3 -0
- package/package.json +1 -1
|
@@ -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
|
-
*
|
|
46
|
-
*
|
|
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
|
-
*
|
|
54
|
-
*
|
|
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,
|
|
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
|
|
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
|
|
207
|
-
|
|
208
|
-
|
|
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
|
|
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
|
-
*
|
|
260
|
-
*
|
|
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
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
319
|
-
*
|
|
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
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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,
|
|
@@ -459,8 +459,59 @@ function getReceiptBody({ orderItems, currency_symbol, show_modifiers, display_m
|
|
|
459
459
|
return items;
|
|
460
460
|
}
|
|
461
461
|
// =========================><=========================
|
|
462
|
+
/**
|
|
463
|
+
* For a SPECIAL / HALF_HALF order item (a deal or half-half pizza), the
|
|
464
|
+
* top-level modifiers hold the real constituent items as options: each
|
|
465
|
+
* `option.ref` is an item `_id` that may live in any category.
|
|
466
|
+
*
|
|
467
|
+
* In partial printing these composite products must not be printed blindly:
|
|
468
|
+
* - an option whose item is not selected in ANY included category is dropped;
|
|
469
|
+
* - a modifier left with no options is dropped;
|
|
470
|
+
* - if every modifier is dropped the whole item is dropped as well.
|
|
471
|
+
*
|
|
472
|
+
* Returns a shallow-cloned item with the trimmed modifiers, or null when
|
|
473
|
+
* nothing survives the filter.
|
|
474
|
+
*/
|
|
475
|
+
function getPartiallyIncludedCompositeItem(item, includedItemIds) {
|
|
476
|
+
const trimmedModifiers = [];
|
|
477
|
+
const itemModifiers = item.modifiers ?? [];
|
|
478
|
+
for (let i = 0; i < itemModifiers.length; i++) {
|
|
479
|
+
const modifier = itemModifiers[i];
|
|
480
|
+
// option.ref acts as a real item _id; keep the option only when that item
|
|
481
|
+
// is part of the partial menu in any of its categories.
|
|
482
|
+
const keptOptions = modifier.options.filter((option) => includedItemIds.has(option.ref));
|
|
483
|
+
if (keptOptions.length === 0)
|
|
484
|
+
continue;
|
|
485
|
+
trimmedModifiers.push({
|
|
486
|
+
...modifier,
|
|
487
|
+
options: keptOptions,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
if (trimmedModifiers.length === 0)
|
|
491
|
+
return null;
|
|
492
|
+
return {
|
|
493
|
+
...item,
|
|
494
|
+
modifiers: trimmedModifiers,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
462
497
|
const getPartialItems = (categories = [], included_categories_map, filterEmpty) => {
|
|
463
498
|
const partialCategories = [];
|
|
499
|
+
// Union of every included item id across all categories. Built lazily and
|
|
500
|
+
// only when a composite item (deal / half-half) actually needs it, so plain
|
|
501
|
+
// orders don't pay for it.
|
|
502
|
+
let allIncludedItemIds = null;
|
|
503
|
+
const getAllIncludedItemIds = () => {
|
|
504
|
+
if (allIncludedItemIds == null) {
|
|
505
|
+
allIncludedItemIds = new Set();
|
|
506
|
+
for (const categoryId in included_categories_map) {
|
|
507
|
+
const includedItems = included_categories_map[categoryId].includedItemsMap;
|
|
508
|
+
for (const itemId in includedItems) {
|
|
509
|
+
allIncludedItemIds.add(itemId);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return allIncludedItemIds;
|
|
514
|
+
};
|
|
464
515
|
for (let i = 0; i < categories.length; i++) {
|
|
465
516
|
const category = categories[i];
|
|
466
517
|
const includedCategory = included_categories_map[category.category_id];
|
|
@@ -470,8 +521,20 @@ const getPartialItems = (categories = [], included_categories_map, filterEmpty)
|
|
|
470
521
|
for (let j = 0; j < category.items.length; j++) {
|
|
471
522
|
const item = category.items[j];
|
|
472
523
|
const includedItem = includedCategory.includedItemsMap[item.item_id];
|
|
524
|
+
// A composite item (deal / half-half) is still gated by its own item_id
|
|
525
|
+
// exactly like any other item; only once included do we trim its parts.
|
|
473
526
|
if (!includedItem)
|
|
474
527
|
continue;
|
|
528
|
+
if ([constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL, constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF].includes(item.item_type)) {
|
|
529
|
+
const partialItem = getPartiallyIncludedCompositeItem(item, getAllIncludedItemIds());
|
|
530
|
+
if (partialItem == null)
|
|
531
|
+
continue;
|
|
532
|
+
partialItems.push({
|
|
533
|
+
sortId: includedItem.sortId,
|
|
534
|
+
item: partialItem,
|
|
535
|
+
});
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
475
538
|
partialItems.push({
|
|
476
539
|
sortId: includedItem.sortId,
|
|
477
540
|
item,
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
337
|
-
|
|
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
|
|
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
|
|
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
|