@managesome/eposmatic-toolkit 0.3.6 → 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,
@@ -15,6 +15,7 @@ export declare const TenantHrShiftSchema: Schema<any, import("mongoose").Model<a
15
15
  start_time: number;
16
16
  end_time: number;
17
17
  is_active: boolean;
18
+ grace_period_minutes: number;
18
19
  required_positions: import("mongoose").Types.DocumentArray<{
19
20
  count: number;
20
21
  position: string;
@@ -37,6 +38,7 @@ export declare const TenantHrShiftSchema: Schema<any, import("mongoose").Model<a
37
38
  start_time: number;
38
39
  end_time: number;
39
40
  is_active: boolean;
41
+ grace_period_minutes: number;
40
42
  required_positions: import("mongoose").Types.DocumentArray<{
41
43
  count: number;
42
44
  position: string;
@@ -59,6 +61,7 @@ export declare const TenantHrShiftSchema: Schema<any, import("mongoose").Model<a
59
61
  start_time: number;
60
62
  end_time: number;
61
63
  is_active: boolean;
64
+ grace_period_minutes: number;
62
65
  required_positions: import("mongoose").Types.DocumentArray<{
63
66
  count: number;
64
67
  position: string;
@@ -36,6 +36,10 @@ exports.TenantHrShiftSchema = new mongoose_1.Schema({
36
36
  type: Number,
37
37
  required: true,
38
38
  },
39
+ grace_period_minutes: {
40
+ type: Number,
41
+ default: null,
42
+ },
39
43
  required_positions: {
40
44
  type: [hrRequiredPositionSchema],
41
45
  default: [],
@@ -8,6 +8,7 @@ export interface ITenantHrShift {
8
8
  name: string;
9
9
  start_time: number;
10
10
  end_time: number;
11
+ grace_period_minutes?: number | null;
11
12
  required_positions: ITenantHrRequiredPosition[];
12
13
  is_active: boolean;
13
14
  createdAt: Date;
@@ -174,6 +174,95 @@ describe('edit-order baseline keeps the applied item reward through the full sta
174
174
  expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBe(constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON);
175
175
  });
176
176
  });
177
+ describe('edit-order baseline keeps an applied order-level discount through the full stats → sync chain', () => {
178
+ const makeOrderLevelDiscountOffer = () => makeOffer('DISCOUNT', {
179
+ included_platforms: [constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY],
180
+ pathway_to_unlock_type_orders: {
181
+ scope: 'OVERALL',
182
+ min_num_of_orders: 0,
183
+ max_num_of_orders: null,
184
+ total_amount: 1000,
185
+ max_amount: null,
186
+ orders_between_unlocks: null,
187
+ max_claims: null,
188
+ max_uses_per_customer: 1,
189
+ },
190
+ reward_discount: {
191
+ discount_value: 10,
192
+ is_discount_percentage: true,
193
+ max_discount_amount: null,
194
+ max_discount_percentage: null,
195
+ excluded_refs: [],
196
+ },
197
+ });
198
+ const makeEditOrderStats = ({ isEdit, editedOrderRedeemedOffers, }) => (0, offers_helpers_1.getOffersWithStats)({
199
+ offers: [makeOrderLevelDiscountOffer()],
200
+ customer: { total_orders: 1, loyalty_points: 0 },
201
+ vouchers: [],
202
+ giftCards: [],
203
+ customerOfferRedemptionCounts: {
204
+ USED: { [OFFER_ID]: 1 },
205
+ ASSIGNED: {},
206
+ },
207
+ redeemed_offers: { [OFFER_ID]: 1 },
208
+ platform: constants_1.PLATFORMS_ENUM.WEB_TAKE_AWAY,
209
+ currency_symbol: constants_1.CURRENCY_SYMBOL_ENUM.$,
210
+ categories: makeCategories({ subtotal: 2000 }),
211
+ currentItemsPrice: 2000,
212
+ totalDiscountApplied: 0,
213
+ order_discount_details: null,
214
+ isEdit,
215
+ editedOrderRedeemedOffers,
216
+ });
217
+ const makeDiscountOrder = () => makeOrder({
218
+ order_discount_details: {
219
+ loyalty_discount_id: OFFER_ID,
220
+ loyalty_discount_name: 'DISCOUNT threshold reward',
221
+ loyalty_discount: 10,
222
+ is_loyalty_discount_percentage: true,
223
+ total_discount: 10,
224
+ },
225
+ iterations: [{ categories: makeCategories({ subtotal: 2000 }) }],
226
+ });
227
+ it('keeps the applied discount when editing the order that consumed it', () => {
228
+ const stats = makeEditOrderStats({
229
+ isEdit: true,
230
+ editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
231
+ });
232
+ expect(stats.unlockedOffersApplicableTimes[OFFER_ID]).toBe(1);
233
+ const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeDiscountOrder(), [], {}, stats.unlockedOffersApplicableTimes, [
234
+ makeOrderLevelDiscountOffer(),
235
+ ]);
236
+ expect(result.order_discount_details?.loyalty_discount_id).toBe(OFFER_ID);
237
+ });
238
+ it('clears the discount when the limit is genuinely exhausted (regression guard)', () => {
239
+ const stats = makeEditOrderStats({ isEdit: false, editedOrderRedeemedOffers: null });
240
+ expect(stats.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
241
+ const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeDiscountOrder(), [], {}, stats.unlockedOffersApplicableTimes, [
242
+ makeOrderLevelDiscountOffer(),
243
+ ]);
244
+ expect(result.order_discount_details?.loyalty_discount_id).toBeUndefined();
245
+ });
246
+ });
247
+ describe('wallet cashback discount survives the sync while its evidence is present', () => {
248
+ const makeWalletCashbackOrder = (walletRedemptions) => makeOrder({
249
+ order_discount_details: {
250
+ loyalty_discount_id: constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID,
251
+ loyalty_discount: 5,
252
+ is_loyalty_discount_percentage: false,
253
+ total_discount: 5,
254
+ },
255
+ wallet_redemptions: walletRedemptions,
256
+ });
257
+ it('keeps the wallet cashback loyalty discount when cashback evidence exists', () => {
258
+ const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeWalletCashbackOrder({ cashback_amount: 5, gift_cards: [] }), [], {}, {}, []);
259
+ expect(result.order_discount_details?.loyalty_discount_id).toBe(constants_1.WALLET_CASHBACK_LOYALTY_DISCOUNT_ID);
260
+ });
261
+ it('clears it when the wallet evidence is gone (legacy/empty state)', () => {
262
+ const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeWalletCashbackOrder(null), [], {}, {}, []);
263
+ expect(result.order_discount_details?.loyalty_discount_id).toBeUndefined();
264
+ });
265
+ });
177
266
  describe('offer sync auto-removed items void reason', () => {
178
267
  const getFlatItems = (categories) => (categories ?? []).flatMap((category) => category.items ?? []);
179
268
  const makeIteration = (categories) => [{ categories }];
@@ -147,6 +147,25 @@ describe('getOffersWithStats — edited-order redemption baseline', () => {
147
147
  expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
148
148
  expect(stats.message).toContain('personal limit');
149
149
  });
150
+ it('ignores a provided baseline snapshot when the cart is not an edit (new order)', () => {
151
+ const result = runStats({
152
+ counts: makeUsedCounts(1),
153
+ editedOrderRedeemedOffers: { [OFFER_ID]: 1 },
154
+ });
155
+ const { stats } = result.offersWithStats[0];
156
+ // The baseline only applies to edit sessions — isEdit must be set, so a
157
+ // stray snapshot can never change new-order behavior.
158
+ expect(stats.applicable_quantity).toBe(0);
159
+ expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
160
+ expect(stats.message).toContain('personal limit');
161
+ });
162
+ it('stays fail-closed for new orders while counts are unknown (pre-change behavior)', () => {
163
+ const result = runStats({ counts: null });
164
+ const { stats } = result.offersWithStats[0];
165
+ expect(stats.applicable_quantity).toBe(0);
166
+ expect(result.unlockedOffersApplicableTimes[OFFER_ID]).toBe(0);
167
+ expect(stats.message).toContain('personal limit');
168
+ });
150
169
  it('preserves the claim while counts are unknown if the edited order carried it', () => {
151
170
  const result = runStats({
152
171
  counts: null,
@@ -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.6",
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",