@managesome/eposmatic-toolkit 0.2.766 → 0.2.768
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/store/cart-actions.d.ts +23 -1
- package/lib/helpers/store/cart-actions.js +31 -20
- package/lib/helpers/utils/specials-helpers.d.ts +6 -2
- package/lib/helpers/utils/specials-helpers.js +93 -43
- package/lib/tests/specials-quantity-merge.test.d.ts +1 -0
- package/lib/tests/specials-quantity-merge.test.js +198 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DISCOUNT_APPLICABILITY_ENUM, GIFT_CARD_REWARD_TYPE_ENUM, ORDER_STATUS_ENUM, PLATFORMS_ENUM, SORT_TYPE_ENUM } from '../../constants';
|
|
2
|
-
import { ICourse, IDiscountDetails, IGeneralItem, IItem, IModifier, IOffer, IOrder, IOrderCategory, IOrderCourse, IOrderDeliveryDetails, IOrderItem, IOrderIteration, IPrice } from '../../schemas';
|
|
2
|
+
import { ICourse, IDiscountDetails, IGeneralItem, IItem, IModifier, IOffer, IOrder, IOrderCategory, IOrderCourse, IOrderDeliveryDetails, IOrderItem, IOrderIteration, IOrderModifier, IPrice } from '../../schemas';
|
|
3
3
|
import { ICartListnerSettings } from '../types';
|
|
4
4
|
import { ICartItemsObj } from '../types/cart-actions';
|
|
5
5
|
interface IGetItemQuantityChangeResultsParams {
|
|
@@ -74,6 +74,28 @@ export interface ISetAddItemToCartActionPayload {
|
|
|
74
74
|
rewardClaim?: IRewardClaim;
|
|
75
75
|
}
|
|
76
76
|
export declare const getOrderItemComaparableData: (item: IOrderItem) => Partial<IOrderItem>;
|
|
77
|
+
/**
|
|
78
|
+
* Merge-comparable projection of an order item: everything that makes two
|
|
79
|
+
* items "the same" for quantity merging (item, modifiers, per-unit terms),
|
|
80
|
+
* with the quantity-dependent amounts normalized to a single unit.
|
|
81
|
+
*
|
|
82
|
+
* Two items whose projections are deeply equal merge into ONE with a summed
|
|
83
|
+
* quantity — the cart-line merge (`getAddItemToCartData`) and the specials
|
|
84
|
+
* slot merge (`convertSpecialItemToOrderItem`, e.g. the same pizza picked in
|
|
85
|
+
* several slots of a deal) both key on this projection.
|
|
86
|
+
*/
|
|
87
|
+
export declare function getComparableOrderItem(item: IOrderItem): {
|
|
88
|
+
item_id: string;
|
|
89
|
+
menu_id: string | null | undefined;
|
|
90
|
+
item_type: import("../../constants").ORDER_ITEM_TYPE_ENUM;
|
|
91
|
+
name: string;
|
|
92
|
+
price: number;
|
|
93
|
+
item_discount_details: IDiscountDetails | null;
|
|
94
|
+
tax_rate: number;
|
|
95
|
+
special_instructions: string;
|
|
96
|
+
is_ready: boolean;
|
|
97
|
+
modifiers: IOrderModifier[] | null;
|
|
98
|
+
};
|
|
77
99
|
export declare function getAddItemToCartData(data: Partial<IOrder>, payload: ISetAddItemToCartActionPayload, courses: ICourse[], listnerSettings: ICartListnerSettings, appliedCustomDiscounts?: IAppliedCustomDiscount[]): Partial<IOrder>;
|
|
78
100
|
interface IGetSplitCartItemToTwo {
|
|
79
101
|
data: Partial<IOrder>;
|
|
@@ -15,6 +15,7 @@ exports.getSurchargeAmount = getSurchargeAmount;
|
|
|
15
15
|
exports.calculateGstForItem = calculateGstForItem;
|
|
16
16
|
exports.getCashierAppliedDiscountAmount = getCashierAppliedDiscountAmount;
|
|
17
17
|
exports.calculateCartOrderInfoAndSyncData = calculateCartOrderInfoAndSyncData;
|
|
18
|
+
exports.getComparableOrderItem = getComparableOrderItem;
|
|
18
19
|
exports.getAddItemToCartData = getAddItemToCartData;
|
|
19
20
|
exports.getSplitCartItemToTwo = getSplitCartItemToTwo;
|
|
20
21
|
exports.getAddItemsToCartData = getAddItemsToCartData;
|
|
@@ -1947,25 +1948,35 @@ const getOrderItemComaparableData = (item) => ({
|
|
|
1947
1948
|
quantity: item.quantity,
|
|
1948
1949
|
});
|
|
1949
1950
|
exports.getOrderItemComaparableData = getOrderItemComaparableData;
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1951
|
+
/**
|
|
1952
|
+
* Merge-comparable projection of an order item: everything that makes two
|
|
1953
|
+
* items "the same" for quantity merging (item, modifiers, per-unit terms),
|
|
1954
|
+
* with the quantity-dependent amounts normalized to a single unit.
|
|
1955
|
+
*
|
|
1956
|
+
* Two items whose projections are deeply equal merge into ONE with a summed
|
|
1957
|
+
* quantity — the cart-line merge (`getAddItemToCartData`) and the specials
|
|
1958
|
+
* slot merge (`convertSpecialItemToOrderItem`, e.g. the same pizza picked in
|
|
1959
|
+
* several slots of a deal) both key on this projection.
|
|
1960
|
+
*/
|
|
1961
|
+
function getComparableOrderItem(item) {
|
|
1962
|
+
return {
|
|
1963
|
+
item_id: item.item_id,
|
|
1964
|
+
menu_id: item.menu_id,
|
|
1965
|
+
item_type: item.item_type,
|
|
1966
|
+
name: item.name,
|
|
1967
|
+
price: item.price,
|
|
1968
|
+
item_discount_details: getItemQuantityChangeResults({
|
|
1969
|
+
prevQuantity: item.quantity,
|
|
1970
|
+
newQuantity: 1,
|
|
1971
|
+
prevItemDiscountDetails: item.item_discount_details,
|
|
1972
|
+
}),
|
|
1973
|
+
tax_rate: item.tax_rate,
|
|
1974
|
+
special_instructions: item.special_instructions,
|
|
1975
|
+
is_ready: item.is_ready ?? false,
|
|
1976
|
+
modifiers: item.modifiers,
|
|
1968
1977
|
};
|
|
1978
|
+
}
|
|
1979
|
+
function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
1969
1980
|
const { category_id, name, item, chosen_course_id, replace_id, rewardClaim } = payload;
|
|
1970
1981
|
const normalizedRewardClaim = getNormalizedRewardClaim(rewardClaim);
|
|
1971
1982
|
const validatedRewardItemAddContext = getValidatedRewardItemAddContext({
|
|
@@ -1974,7 +1985,7 @@ function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCu
|
|
|
1974
1985
|
});
|
|
1975
1986
|
const itemWithValidatedRewardClaim = validatedRewardItemAddContext.item;
|
|
1976
1987
|
const validatedRewardClaim = validatedRewardItemAddContext.rewardClaim;
|
|
1977
|
-
const comparableItem =
|
|
1988
|
+
const comparableItem = getComparableOrderItem(itemWithValidatedRewardClaim);
|
|
1978
1989
|
let mainItemId = itemWithValidatedRewardClaim._id;
|
|
1979
1990
|
const orderData = { ...data };
|
|
1980
1991
|
// =====================><=====================
|
|
@@ -2008,7 +2019,7 @@ function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCu
|
|
|
2008
2019
|
// We make sure that the item does not have loyalty_discount_id and voucher_discount_id
|
|
2009
2020
|
itm.item_discount_details?.loyalty_discount_id == null &&
|
|
2010
2021
|
itm.item_discount_details?.voucher_discount_id == null &&
|
|
2011
|
-
(0, lodash_1.isEqual)(
|
|
2022
|
+
(0, lodash_1.isEqual)(getComparableOrderItem(itm), comparableItem)) {
|
|
2012
2023
|
mainItemId = replace_id ?? itm._id;
|
|
2013
2024
|
const newQuantity = itemWithValidatedRewardClaim.quantity + itm.quantity;
|
|
2014
2025
|
allItems[j] = {
|
|
@@ -58,8 +58,12 @@ export declare function convertSpecialItemToOrderItem({ specialItem, modifierSpe
|
|
|
58
58
|
* used by the SpecialsScreen selection flow (edit without classic preview).
|
|
59
59
|
*
|
|
60
60
|
* Each modifier on the order item maps back to its `IModifierSpecial` group
|
|
61
|
-
* (matched by ref or _id)
|
|
62
|
-
*
|
|
61
|
+
* (matched by ref or _id). Options MERGE identical slot selections into one
|
|
62
|
+
* option with a summed `quantity` (see `convertSpecialItemToOrderItem`), so
|
|
63
|
+
* the reverse fill EXPANDS a quantity into that many slots (one unit per
|
|
64
|
+
* slot, up to modifier_special.quantity, at least one slot reserved per
|
|
65
|
+
* remaining option) — the modern view is refilled with the same number of
|
|
66
|
+
* filled slots the cashier originally picked.
|
|
63
67
|
*
|
|
64
68
|
* Returns null if the order item doesn't belong to the provided special.
|
|
65
69
|
*/
|
|
@@ -14,6 +14,7 @@ exports.convertSpecialItemToOrderItem = convertSpecialItemToOrderItem;
|
|
|
14
14
|
exports.deStructureSpecialOrderItem = deStructureSpecialOrderItem;
|
|
15
15
|
exports.convertSpecialToItem = convertSpecialToItem;
|
|
16
16
|
const bson_objectid_1 = __importDefault(require("bson-objectid"));
|
|
17
|
+
const lodash_1 = require("lodash");
|
|
17
18
|
const constants_1 = require("../../constants");
|
|
18
19
|
const store_1 = require("../store");
|
|
19
20
|
const halfhalf_setup_helpers_1 = require("./halfhalf-setup-helpers");
|
|
@@ -257,7 +258,10 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
|
|
|
257
258
|
const originalModifierSpecial = specialItem.modifier_specials.find((ms) => ms._id === modifierSpecial._id);
|
|
258
259
|
if (!originalModifierSpecial)
|
|
259
260
|
continue;
|
|
260
|
-
|
|
261
|
+
// One entry per filled slot; `comparable` is the SAME "is this the same
|
|
262
|
+
// selection?" projection the cart-line merge uses (`getAddItemToCartData`
|
|
263
|
+
// -> `getComparableOrderItem`) — computed once per slot item.
|
|
264
|
+
const entries = [];
|
|
261
265
|
for (let j = 0; j < modifierSpecial.selectedItems.length; j++) {
|
|
262
266
|
const selectedItem = modifierSpecial.selectedItems[j];
|
|
263
267
|
if (!selectedItem)
|
|
@@ -270,35 +274,58 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
|
|
|
270
274
|
}
|
|
271
275
|
// `_id` is the real catalog item `_id` (like an IItem option), so raw
|
|
272
276
|
// modifiers comparison works for merge and edits stay deterministic.
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
277
|
+
entries.push({
|
|
278
|
+
option: {
|
|
279
|
+
_id: selectedItem.item_id,
|
|
280
|
+
ref: selectedItem.item_id,
|
|
281
|
+
name: selectedItem.name,
|
|
282
|
+
price: selectedItem.price,
|
|
283
|
+
quantity: selectedItem.quantity,
|
|
284
|
+
modifiers: selectedItem.modifiers,
|
|
285
|
+
is_free: false,
|
|
286
|
+
// The option IS an item or a half-half — carry its kind.
|
|
287
|
+
ref_type: selectedItem.item_type === constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF
|
|
288
|
+
? constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF
|
|
289
|
+
: constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.ITEM,
|
|
290
|
+
},
|
|
291
|
+
comparable: (0, store_1.getComparableOrderItem)(selectedItem),
|
|
285
292
|
});
|
|
286
293
|
}
|
|
287
294
|
// Mark the cheapest N selected items as free (num_free_items), same as the
|
|
288
295
|
// classic drawer flow (makeSpaceForFreeOptions semantics). Slot order kept.
|
|
289
296
|
const numFreeItems = originalModifierSpecial.num_free_items || 0;
|
|
290
|
-
if (numFreeItems > 0 &&
|
|
291
|
-
const freeIndexes = new Set(
|
|
292
|
-
.map((
|
|
297
|
+
if (numFreeItems > 0 && entries.length > 0) {
|
|
298
|
+
const freeIndexes = new Set(entries
|
|
299
|
+
.map((entry, index) => ({
|
|
300
|
+
index,
|
|
301
|
+
value: (entry.option.price || 0) * (entry.option.quantity || 1),
|
|
302
|
+
}))
|
|
293
303
|
.sort((a, b) => a.value - b.value)
|
|
294
304
|
.slice(0, numFreeItems)
|
|
295
305
|
.map((entry) => entry.index));
|
|
296
|
-
|
|
306
|
+
entries.forEach((entry, index) => {
|
|
297
307
|
if (freeIndexes.has(index)) {
|
|
298
|
-
option.is_free = true;
|
|
308
|
+
entry.option.is_free = true;
|
|
299
309
|
}
|
|
300
310
|
});
|
|
301
311
|
}
|
|
312
|
+
// MERGE identical selections: choosing the same item with the same
|
|
313
|
+
// modifiers in several slots means N units of that option, not N options —
|
|
314
|
+
// the stored line carries ONE option whose `quantity` is the unit sum
|
|
315
|
+
// (mirrors the cart merging two identical lines into one with a summed
|
|
316
|
+
// quantity). Free marking ran per slot BEFORE this and `is_free` is part
|
|
317
|
+
// of the merge key, so free units keep the exact count they had per slot
|
|
318
|
+
// (a mixed group stays as a free option + a paid option).
|
|
319
|
+
const mergedEntries = [];
|
|
320
|
+
for (const entry of entries) {
|
|
321
|
+
const existing = mergedEntries.find((candidate) => candidate.option.is_free === entry.option.is_free && (0, lodash_1.isEqual)(candidate.comparable, entry.comparable));
|
|
322
|
+
if (existing != null) {
|
|
323
|
+
existing.option.quantity = (existing.option.quantity || 1) + (entry.option.quantity || 1);
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
mergedEntries.push(entry);
|
|
327
|
+
}
|
|
328
|
+
const options = mergedEntries.map((entry) => entry.option);
|
|
302
329
|
// Group id = the modifier-special `_id` (same as ref — deterministic).
|
|
303
330
|
groups.push({
|
|
304
331
|
ref: originalModifierSpecial._id,
|
|
@@ -363,8 +390,12 @@ function convertSpecialItemToOrderItem({ specialItem, modifierSpecials, quantity
|
|
|
363
390
|
* used by the SpecialsScreen selection flow (edit without classic preview).
|
|
364
391
|
*
|
|
365
392
|
* Each modifier on the order item maps back to its `IModifierSpecial` group
|
|
366
|
-
* (matched by ref or _id)
|
|
367
|
-
*
|
|
393
|
+
* (matched by ref or _id). Options MERGE identical slot selections into one
|
|
394
|
+
* option with a summed `quantity` (see `convertSpecialItemToOrderItem`), so
|
|
395
|
+
* the reverse fill EXPANDS a quantity into that many slots (one unit per
|
|
396
|
+
* slot, up to modifier_special.quantity, at least one slot reserved per
|
|
397
|
+
* remaining option) — the modern view is refilled with the same number of
|
|
398
|
+
* filled slots the cashier originally picked.
|
|
368
399
|
*
|
|
369
400
|
* Returns null if the order item doesn't belong to the provided special.
|
|
370
401
|
*/
|
|
@@ -390,30 +421,49 @@ function deStructureSpecialOrderItem(orderItem, specialItem) {
|
|
|
390
421
|
consumedModifierIds.add(orderModifier._id);
|
|
391
422
|
const selectedItems = new Array(modifierSpecial.quantity).fill(null);
|
|
392
423
|
if (orderModifier != null) {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
424
|
+
const buildSlotItem = (option, quantity) => ({
|
|
425
|
+
_id: option._id,
|
|
426
|
+
item_id: option.ref,
|
|
427
|
+
menu_id: orderItem.menu_id ?? null,
|
|
428
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.SPECIAL,
|
|
429
|
+
name: option.name,
|
|
430
|
+
price: option.price,
|
|
431
|
+
subtotal: option.price,
|
|
432
|
+
tax_rate: 0,
|
|
433
|
+
quantity,
|
|
434
|
+
special_instructions: '',
|
|
435
|
+
item_discount_details: null,
|
|
436
|
+
modifiers: option.modifiers ?? null,
|
|
437
|
+
// These values will be auto calculated
|
|
438
|
+
_discount_amount: 0,
|
|
439
|
+
_gross_amount: 0,
|
|
440
|
+
_gst_amount: 0,
|
|
441
|
+
_net_amount: 0,
|
|
442
|
+
});
|
|
443
|
+
// Reverse fill (see the docblock): a merged option with quantity N is
|
|
444
|
+
// refilled as N slot selections (one unit per slot) where the slot
|
|
445
|
+
// budget allows. Every remaining option keeps at least one slot
|
|
446
|
+
// reserved so a merged quantity can never starve the others; when the
|
|
447
|
+
// slots run out the leftover units stay on a single slot (a
|
|
448
|
+
// drawer-stepped quantity keeps its legacy one-slot shape).
|
|
449
|
+
let freeSlots = selectedItems.filter((item) => item === null).length;
|
|
450
|
+
const storedOptions = orderModifier.options;
|
|
451
|
+
for (let j = 0; j < storedOptions.length; j++) {
|
|
452
|
+
if (freeSlots <= 0)
|
|
397
453
|
break;
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
// These values will be auto calculated
|
|
412
|
-
_discount_amount: 0,
|
|
413
|
-
_gross_amount: 0,
|
|
414
|
-
_gst_amount: 0,
|
|
415
|
-
_net_amount: 0,
|
|
416
|
-
};
|
|
454
|
+
const option = storedOptions[j];
|
|
455
|
+
const units = option.quantity > 0 ? option.quantity : 1;
|
|
456
|
+
const remainingOptions = storedOptions.length - j - 1;
|
|
457
|
+
const slotsForOption = Math.min(units, Math.max(1, freeSlots - remainingOptions));
|
|
458
|
+
const unitsPerSlot = Math.floor(units / slotsForOption);
|
|
459
|
+
const slotsWithExtraUnit = units % slotsForOption;
|
|
460
|
+
for (let k = 0; k < slotsForOption; k++) {
|
|
461
|
+
const slotIndex = selectedItems.findIndex((item) => item === null);
|
|
462
|
+
if (slotIndex === -1)
|
|
463
|
+
break;
|
|
464
|
+
selectedItems[slotIndex] = buildSlotItem(option, unitsPerSlot + (k < slotsWithExtraUnit ? 1 : 0));
|
|
465
|
+
freeSlots -= 1;
|
|
466
|
+
}
|
|
417
467
|
}
|
|
418
468
|
}
|
|
419
469
|
modifierSpecials.push({ _id: modifierSpecial._id, selectedItems });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
4
|
+
const store_1 = require("../helpers/store");
|
|
5
|
+
const specials_helpers_1 = require("../helpers/utils/specials-helpers");
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Specials: identical slot selections merge into ONE option with a summed
|
|
8
|
+
// quantity — picking the same pizza (same modifiers, same instructions) in
|
|
9
|
+
// two slots must produce `quantity: 2` on a single option, exactly like the
|
|
10
|
+
// cart merges two identical lines (getComparableItem + deep equality).
|
|
11
|
+
// The reverse (deStructure) must refill the modern view with the same number
|
|
12
|
+
// of slots the cashier picked.
|
|
13
|
+
// ============================================================================
|
|
14
|
+
const PLATFORM = constants_1.PLATFORMS_ENUM.POS_WALK_IN;
|
|
15
|
+
function makeModifierSpecial(overrides = {}) {
|
|
16
|
+
return {
|
|
17
|
+
_id: 'mod-pizza',
|
|
18
|
+
name: 'Pizza',
|
|
19
|
+
quantity: 3,
|
|
20
|
+
num_free_items: 0,
|
|
21
|
+
items: [],
|
|
22
|
+
exclusive_refs: [],
|
|
23
|
+
excluded_refs: [],
|
|
24
|
+
...overrides,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function makeWorkableSpecial(overrides = {}) {
|
|
28
|
+
return {
|
|
29
|
+
_id: 'special-1',
|
|
30
|
+
name: 'Student Deal',
|
|
31
|
+
description: 'Deal',
|
|
32
|
+
sold_out: [],
|
|
33
|
+
image_url: 'https://example.com/special.jpg',
|
|
34
|
+
category_id: 'combo-cat',
|
|
35
|
+
price: { BASE_PRICE: 1000 },
|
|
36
|
+
discount_amount: null,
|
|
37
|
+
is_discount_percentage: false,
|
|
38
|
+
display_price: null,
|
|
39
|
+
show_starting_from: false,
|
|
40
|
+
modifier_specials: [makeModifierSpecial()],
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function makeOrderModifier(id, optionName = 'Small') {
|
|
45
|
+
return {
|
|
46
|
+
_id: id,
|
|
47
|
+
ref: id,
|
|
48
|
+
name: 'Size',
|
|
49
|
+
options: [
|
|
50
|
+
{
|
|
51
|
+
_id: `${id}-opt`,
|
|
52
|
+
ref: `${id}-opt`,
|
|
53
|
+
name: optionName,
|
|
54
|
+
price: 0,
|
|
55
|
+
quantity: 1,
|
|
56
|
+
is_free: false,
|
|
57
|
+
modifiers: [],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function makeSlotItem(overrides = {}) {
|
|
63
|
+
return {
|
|
64
|
+
_id: 'slot-1',
|
|
65
|
+
item_id: 'pizza-a',
|
|
66
|
+
menu_id: 'menu-1',
|
|
67
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.ITEM,
|
|
68
|
+
name: 'Pizza A',
|
|
69
|
+
price: 500,
|
|
70
|
+
subtotal: 500,
|
|
71
|
+
quantity: 1,
|
|
72
|
+
special_instructions: '',
|
|
73
|
+
item_discount_details: null,
|
|
74
|
+
tax_rate: 0,
|
|
75
|
+
modifiers: null,
|
|
76
|
+
_discount_amount: 0,
|
|
77
|
+
_gross_amount: 0,
|
|
78
|
+
_gst_amount: 0,
|
|
79
|
+
_net_amount: 0,
|
|
80
|
+
...overrides,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function convert(selectedItems, specialItem = makeWorkableSpecial()) {
|
|
84
|
+
return (0, specials_helpers_1.convertSpecialItemToOrderItem)({
|
|
85
|
+
specialItem,
|
|
86
|
+
modifierSpecials: [{ _id: 'mod-pizza', selectedItems }],
|
|
87
|
+
quantity: 1,
|
|
88
|
+
platform: PLATFORM,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
describe('convertSpecialItemToOrderItem: identical selections merge into one option', () => {
|
|
92
|
+
it('sets the summed quantity on a single option instead of N options', () => {
|
|
93
|
+
const converted = convert([makeSlotItem(), makeSlotItem()]);
|
|
94
|
+
const group = converted.modifiers[0];
|
|
95
|
+
expect(group.options).toHaveLength(1);
|
|
96
|
+
expect(group.options[0]._id).toBe('pizza-a');
|
|
97
|
+
expect(group.options[0].quantity).toBe(2);
|
|
98
|
+
});
|
|
99
|
+
it('keeps the totals identical to the per-slot math', () => {
|
|
100
|
+
// 1000 (special price) + 2 × 500 (the two pizzas) — the merge must not
|
|
101
|
+
// change what the cashier pays.
|
|
102
|
+
const merged = convert([makeSlotItem(), makeSlotItem()]);
|
|
103
|
+
expect(merged.subtotal).toBe(2000);
|
|
104
|
+
const mixed = convert([makeSlotItem(), makeSlotItem({ quantity: 2 })]);
|
|
105
|
+
expect(mixed.modifiers[0].options[0].quantity).toBe(3);
|
|
106
|
+
expect(mixed.subtotal).toBe(2500); // 1000 + 3 × 500
|
|
107
|
+
});
|
|
108
|
+
it('keeps selections with different modifiers apart', () => {
|
|
109
|
+
const converted = convert([makeSlotItem(), makeSlotItem({ modifiers: [makeOrderModifier('size-large')] })]);
|
|
110
|
+
expect(converted.modifiers[0].options).toHaveLength(2);
|
|
111
|
+
});
|
|
112
|
+
it('keeps selections with different instructions apart', () => {
|
|
113
|
+
const converted = convert([makeSlotItem(), makeSlotItem({ special_instructions: 'no onions' })]);
|
|
114
|
+
expect(converted.modifiers[0].options).toHaveLength(2);
|
|
115
|
+
});
|
|
116
|
+
it('merges identical half-half selections the same way', () => {
|
|
117
|
+
const halfHalfSlot = {
|
|
118
|
+
item_id: 'halfhalf-1',
|
|
119
|
+
item_type: constants_1.ORDER_ITEM_TYPE_ENUM.HALF_HALF,
|
|
120
|
+
name: 'Half/Half Pizza Large',
|
|
121
|
+
price: 300,
|
|
122
|
+
modifiers: [makeOrderModifier('seg-1', 'First Half')],
|
|
123
|
+
};
|
|
124
|
+
const converted = convert([makeSlotItem(halfHalfSlot), makeSlotItem(halfHalfSlot)]);
|
|
125
|
+
const group = converted.modifiers[0];
|
|
126
|
+
expect(group.options).toHaveLength(1);
|
|
127
|
+
expect(group.options[0].quantity).toBe(2);
|
|
128
|
+
expect(group.options[0].ref_type).toBe(constants_1.MODIFIER_OPTION_REF_TYPE_ENUM.HALF_HALF);
|
|
129
|
+
});
|
|
130
|
+
it('keeps the per-slot free semantics for mixed free/paid identical units', () => {
|
|
131
|
+
// num_free_items = 1 with 2 identical: the cheapest slot (the first) is
|
|
132
|
+
// free — one FREE unit + one PAID unit, never "2 free".
|
|
133
|
+
const special = makeWorkableSpecial({
|
|
134
|
+
modifier_specials: [makeModifierSpecial({ num_free_items: 1 })],
|
|
135
|
+
});
|
|
136
|
+
const converted = convert([makeSlotItem(), makeSlotItem()], special);
|
|
137
|
+
const options = converted.modifiers[0].options;
|
|
138
|
+
expect(options).toHaveLength(2);
|
|
139
|
+
const freeOption = options.find((option) => option.is_free);
|
|
140
|
+
const paidOption = options.find((option) => !option.is_free);
|
|
141
|
+
expect(freeOption?.quantity).toBe(1);
|
|
142
|
+
expect(paidOption?.quantity).toBe(1);
|
|
143
|
+
});
|
|
144
|
+
it('merges the free units together when all of them are free', () => {
|
|
145
|
+
const special = makeWorkableSpecial({
|
|
146
|
+
modifier_specials: [makeModifierSpecial({ num_free_items: 2 })],
|
|
147
|
+
});
|
|
148
|
+
const converted = convert([makeSlotItem(), makeSlotItem(), makeSlotItem()], special);
|
|
149
|
+
const options = converted.modifiers[0].options;
|
|
150
|
+
expect(options).toHaveLength(2);
|
|
151
|
+
expect(options.find((option) => option.is_free)?.quantity).toBe(2);
|
|
152
|
+
expect(options.find((option) => !option.is_free)?.quantity).toBe(1);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
describe('deStructureSpecialOrderItem: reverse fill the modern view', () => {
|
|
156
|
+
it('expands a merged quantity into the same number of slots', () => {
|
|
157
|
+
const converted = convert([makeSlotItem(), makeSlotItem()]);
|
|
158
|
+
const reversed = (0, specials_helpers_1.deStructureSpecialOrderItem)(converted, makeWorkableSpecial());
|
|
159
|
+
const slots = reversed.modifierSpecials[0].selectedItems;
|
|
160
|
+
expect(slots).toHaveLength(3);
|
|
161
|
+
expect(slots.map((item) => item?.item_id ?? null)).toEqual(['pizza-a', 'pizza-a', null]);
|
|
162
|
+
expect(slots.map((item) => item?.quantity ?? null)).toEqual([1, 1, null]);
|
|
163
|
+
});
|
|
164
|
+
it('reserves a slot for every remaining option when expanding', () => {
|
|
165
|
+
const converted = convert([makeSlotItem(), makeSlotItem(), makeSlotItem({ item_id: 'pizza-b', name: 'Pizza B' })]);
|
|
166
|
+
const reversed = (0, specials_helpers_1.deStructureSpecialOrderItem)(converted, makeWorkableSpecial());
|
|
167
|
+
const slots = reversed.modifierSpecials[0].selectedItems;
|
|
168
|
+
expect(slots.map((item) => item?.item_id ?? null)).toEqual(['pizza-a', 'pizza-a', 'pizza-b']);
|
|
169
|
+
expect(slots.map((item) => item?.quantity ?? null)).toEqual([1, 1, 1]);
|
|
170
|
+
});
|
|
171
|
+
it('keeps a legacy one-slot quantity shape when the group has no room', () => {
|
|
172
|
+
const converted = convert([makeSlotItem(), makeSlotItem()]);
|
|
173
|
+
const singleSlotSpecial = makeWorkableSpecial({
|
|
174
|
+
modifier_specials: [makeModifierSpecial({ quantity: 1 })],
|
|
175
|
+
});
|
|
176
|
+
const reversed = (0, specials_helpers_1.deStructureSpecialOrderItem)(converted, singleSlotSpecial);
|
|
177
|
+
const slots = reversed.modifierSpecials[0].selectedItems;
|
|
178
|
+
expect(slots).toHaveLength(1);
|
|
179
|
+
expect(slots[0]?.quantity).toBe(2);
|
|
180
|
+
});
|
|
181
|
+
it('round-trips: slots -> merged order item -> slots -> the SAME order item', () => {
|
|
182
|
+
const slots = [makeSlotItem(), makeSlotItem(), makeSlotItem({ item_id: 'pizza-b', name: 'Pizza B' })];
|
|
183
|
+
const first = convert(slots);
|
|
184
|
+
const reversed = (0, specials_helpers_1.deStructureSpecialOrderItem)(first, makeWorkableSpecial());
|
|
185
|
+
const second = convert(reversed.modifierSpecials[0].selectedItems);
|
|
186
|
+
expect(second.modifiers[0].options).toEqual(first.modifiers[0].options);
|
|
187
|
+
expect(second.subtotal).toBe(first.subtotal);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
describe('getComparableOrderItem', () => {
|
|
191
|
+
it('ignores quantity (and _id) so summed units stay comparable', () => {
|
|
192
|
+
expect((0, store_1.getComparableOrderItem)(makeSlotItem())).toEqual((0, store_1.getComparableOrderItem)(makeSlotItem({ quantity: 5 })));
|
|
193
|
+
});
|
|
194
|
+
it('separates items with different instructions or modifiers', () => {
|
|
195
|
+
expect((0, store_1.getComparableOrderItem)(makeSlotItem())).not.toEqual((0, store_1.getComparableOrderItem)(makeSlotItem({ special_instructions: 'no onions' })));
|
|
196
|
+
expect((0, store_1.getComparableOrderItem)(makeSlotItem())).not.toEqual((0, store_1.getComparableOrderItem)(makeSlotItem({ modifiers: [makeOrderModifier('size-large')] })));
|
|
197
|
+
});
|
|
198
|
+
});
|