@managesome/eposmatic-toolkit 0.3.7 → 0.3.8

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.
@@ -840,11 +840,16 @@ function convertCatalogItemModifierToOrderEngineItemModifier({ modifiers, orderI
840
840
  // option (e.g. "Half/Half Pizza -> Pizza Size -> Large 13\""). Applied
841
841
  // recursively: nested required groups under an option get the same
842
842
  // treatment; they only reveal once their parent option is selected.
843
+ // SOLD-OUT lone options are NOT selectable: they stay visible in the
844
+ // drawer (struck through, selection blocked), but are never defaulted —
845
+ // the required group then reports its usual "select an option" error
846
+ // instead of silently charging a sold-out option.
843
847
  const isRequiredModifier = (modifier.min_num_options != null && modifier.min_num_options > 0) ||
844
848
  (modifier.min_aggregate_options_quantity != null && modifier.min_aggregate_options_quantity > 0);
845
849
  if (isRequiredModifier && orderEngineSingleModifier.options.length === 1) {
846
850
  const singleOption = orderEngineSingleModifier.options[0];
847
- if (!singleOption.default) {
851
+ const isSingleSoldout = singleOption.sold_out.includes(tenantId) || soldoutModifiers?.includes(singleOption.ref);
852
+ if (!singleOption.default && !isSingleSoldout) {
848
853
  orderEngineSingleModifier.options[0] = {
849
854
  ...singleOption,
850
855
  default: true,
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const constants_1 = require("../constants");
4
+ const order_engine_1 = require("../helpers/utils/order-engine");
5
+ // ============================================================================
6
+ // SOLD-OUT DEFAULT GUARD
7
+ //
8
+ // Sold-out options (tenant id in `option.sold_out`, or the option ref in
9
+ // `soldoutModifiers`) are deliberately KEPT in the engine tree — the drawer
10
+ // shows them struck through and blocks selection (pointer-events hardening).
11
+ // They must NEVER be selected automatically, including through the
12
+ // lone-option shortcut of a required group: that shortcut only applies to
13
+ // SELECTABLE options. A sold-out lone option leaves the group unselected so
14
+ // the regular required-group error surfaces instead of silently charging a
15
+ // sold-out option.
16
+ // ============================================================================
17
+ const TENANT_ID = 'tenant-1';
18
+ const PLATFORM = constants_1.PLATFORMS_ENUM.POS_WALK_IN;
19
+ function makeOption(overrides = {}) {
20
+ return {
21
+ _id: 'opt-a',
22
+ ref: 'opt-a',
23
+ name: 'A',
24
+ active: true,
25
+ sold_out: [],
26
+ sort_id: 1,
27
+ excluded_platforms: [],
28
+ price: { BASE_PRICE: 300 },
29
+ modifiers: null,
30
+ ...overrides,
31
+ };
32
+ }
33
+ function makeRequiredModifier(option) {
34
+ return {
35
+ _id: 'mod-1',
36
+ ref: 'mod-1',
37
+ name: 'Sauce',
38
+ description: '',
39
+ active: true,
40
+ sold_out: [],
41
+ sort_id: 1,
42
+ min_num_options: 1,
43
+ max_num_options: 1,
44
+ options: [option],
45
+ };
46
+ }
47
+ function makeItem(modifier) {
48
+ return {
49
+ _id: 'item-1',
50
+ brand_id: 'brand-1',
51
+ tenants: [TENANT_ID],
52
+ category_id: 'cat-1',
53
+ included_platforms: [PLATFORM],
54
+ name: 'Item',
55
+ hide_name: false,
56
+ description: '',
57
+ active: true,
58
+ sold_out: [],
59
+ sort_id: 1,
60
+ price: { BASE_PRICE: 500 },
61
+ discount_amount: null,
62
+ is_discount_percentage: false,
63
+ tax_rate: null,
64
+ keywords: [],
65
+ caveats: [],
66
+ dietary_preferences: [],
67
+ image_url: '',
68
+ ingredients: null,
69
+ item_special_hours: null,
70
+ modifiers: [modifier],
71
+ is_featured: false,
72
+ featured_sort_id: 0,
73
+ display_price: null,
74
+ show_starting_from: false,
75
+ createdAt: new Date('2024-01-01'),
76
+ updatedAt: new Date('2024-01-02'),
77
+ };
78
+ }
79
+ function convert(item, soldoutModifiers = []) {
80
+ return (0, order_engine_1.convertCatalogItemToOrderEngineItem)({
81
+ item,
82
+ orderItem: undefined,
83
+ platform: PLATFORM,
84
+ tenantId: TENANT_ID,
85
+ orderItemType: constants_1.ORDER_ITEM_TYPE_ENUM.ITEM,
86
+ defaultMenuId: null,
87
+ soldoutModifiers,
88
+ }).workingData;
89
+ }
90
+ describe('sold-out guard: lone required option is never auto-selected', () => {
91
+ it('keeps the sold-out option visible but leaves it unselected (required error)', () => {
92
+ const item = makeItem(makeRequiredModifier(makeOption({ sold_out: [TENANT_ID] })));
93
+ const engine = convert(item);
94
+ const options = engine.modifiers[0].options;
95
+ // Kept for the drawer (never filtered out), but not defaulted.
96
+ expect(options).toHaveLength(1);
97
+ expect(options[0].default).toBe(false);
98
+ // The required group reports its usual error instead of silently
99
+ // charging the sold-out option.
100
+ expect(engine.error_id).toBe('mod-1');
101
+ expect(engine.error_message).toContain('Sauce');
102
+ });
103
+ it('does not default a lone option flagged through soldoutModifiers', () => {
104
+ const item = makeItem(makeRequiredModifier(makeOption()));
105
+ const engine = convert(item, ['opt-a']);
106
+ expect(engine.modifiers[0].options[0].default).toBe(false);
107
+ expect(engine.error_id).toBe('mod-1');
108
+ });
109
+ it('still auto-selects the lone option when it is NOT sold out (control)', () => {
110
+ const item = makeItem(makeRequiredModifier(makeOption()));
111
+ const engine = convert(item);
112
+ expect(engine.modifiers[0].options[0]).toEqual(expect.objectContaining({ default: true, quantity: 1 }));
113
+ expect(engine.error_id).toBeUndefined();
114
+ });
115
+ it('never defaults a sold-out option even when the catalog marks it default', () => {
116
+ const soldoutDefault = makeOption({ default: true, sold_out: [TENANT_ID] });
117
+ const sibling = makeOption({ _id: 'opt-b', ref: 'opt-b', name: 'B' });
118
+ const modifier = makeRequiredModifier(soldoutDefault);
119
+ modifier.options.push(sibling);
120
+ const engine = convert(makeItem(modifier));
121
+ const options = engine.modifiers[0].options;
122
+ expect(options.find((option) => option._id === 'opt-a').default).toBe(false);
123
+ expect(options.find((option) => option._id === 'opt-b').default).toBe(false);
124
+ expect(engine.error_id).toBe('mod-1');
125
+ });
126
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@managesome/eposmatic-toolkit",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "A Toolkit Library for Eposmatic",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",