@managesome/eposmatic-toolkit 0.3.2 → 0.3.3
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/constants/ORDER.d.ts +11 -0
- package/lib/constants/ORDER.js +13 -1
- package/lib/helpers/store/cart-actions.js +56 -0
- package/lib/helpers/store/common-order-actions.d.ts +1 -0
- package/lib/helpers/store/common-order-actions.js +13 -1
- package/lib/tests/offer-apply-helpers.test.js +103 -2
- package/lib/tests/void-items-with-reason.test.d.ts +1 -0
- package/lib/tests/void-items-with-reason.test.js +82 -0
- package/package.json +1 -1
package/lib/constants/ORDER.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { ORDER_STATUS_ENUM, ORDER_TYPE_ENUM } from './ENUMS';
|
|
2
|
+
/**
|
|
3
|
+
* System-generated void reason stamped on items the offer synchronization
|
|
4
|
+
* removes automatically — for example when an applied offer/reward is no
|
|
5
|
+
* longer qualified/unlocked after the cashier edited the order.
|
|
6
|
+
*
|
|
7
|
+
* The "Auto removed by system" marker identifies the void as automatic. Plain
|
|
8
|
+
* system text only: it must never carry staff attribution ("Permitted By ..." /
|
|
9
|
+
* "Staff uid: ...") — manual override attribution only applies to explicit
|
|
10
|
+
* cashier voids.
|
|
11
|
+
*/
|
|
12
|
+
export declare const OFFER_NO_LONGER_QUALIFIED_VOID_REASON = "Auto removed by system - Offer no longer qualified";
|
|
2
13
|
export interface IOrderStatusColorCode {
|
|
3
14
|
color: string;
|
|
4
15
|
contrast_color: string;
|
package/lib/constants/ORDER.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_ORDER_STATUS_BY_ORDER_TYPE = exports.DEFAULT_ORDER_STATUS_COLOR_CODE_MAP = void 0;
|
|
3
|
+
exports.DEFAULT_ORDER_STATUS_BY_ORDER_TYPE = exports.DEFAULT_ORDER_STATUS_COLOR_CODE_MAP = exports.OFFER_NO_LONGER_QUALIFIED_VOID_REASON = void 0;
|
|
4
4
|
const ENUMS_1 = require("./ENUMS");
|
|
5
|
+
// =========================>>>><<<<=========================
|
|
6
|
+
/**
|
|
7
|
+
* System-generated void reason stamped on items the offer synchronization
|
|
8
|
+
* removes automatically — for example when an applied offer/reward is no
|
|
9
|
+
* longer qualified/unlocked after the cashier edited the order.
|
|
10
|
+
*
|
|
11
|
+
* The "Auto removed by system" marker identifies the void as automatic. Plain
|
|
12
|
+
* system text only: it must never carry staff attribution ("Permitted By ..." /
|
|
13
|
+
* "Staff uid: ...") — manual override attribution only applies to explicit
|
|
14
|
+
* cashier voids.
|
|
15
|
+
*/
|
|
16
|
+
exports.OFFER_NO_LONGER_QUALIFIED_VOID_REASON = 'Auto removed by system - Offer no longer qualified';
|
|
5
17
|
exports.DEFAULT_ORDER_STATUS_COLOR_CODE_MAP = {
|
|
6
18
|
[ENUMS_1.ORDER_STATUS_ENUM.UNCONFIRMED]: {
|
|
7
19
|
color: '#727472',
|
|
@@ -1269,6 +1269,11 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
|
1269
1269
|
}
|
|
1270
1270
|
}
|
|
1271
1271
|
}
|
|
1272
|
+
// Items dropped below because their offer/reward is no longer qualified.
|
|
1273
|
+
// The last-synced iteration keeps them with a system-generated void reason
|
|
1274
|
+
// so the removal is traceable and never carries staff name/uid (manual
|
|
1275
|
+
// "Permitted By" attribution only applies to explicit cashier voids).
|
|
1276
|
+
const removedItemsReasonMap = {};
|
|
1272
1277
|
const newCategories = [];
|
|
1273
1278
|
for (let i = 0; i < categories.length; i++) {
|
|
1274
1279
|
const category = categories[i];
|
|
@@ -1285,6 +1290,7 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
|
1285
1290
|
if (normalizedLoyaltyDiscountId != null) {
|
|
1286
1291
|
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
1287
1292
|
}
|
|
1293
|
+
removedItemsReasonMap[item._id] = constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1288
1294
|
continue;
|
|
1289
1295
|
}
|
|
1290
1296
|
if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
@@ -1292,6 +1298,7 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
|
1292
1298
|
if (normalizedLoyaltyDiscountId != null) {
|
|
1293
1299
|
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
1294
1300
|
}
|
|
1301
|
+
removedItemsReasonMap[item._id] = constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1295
1302
|
continue;
|
|
1296
1303
|
}
|
|
1297
1304
|
unlockedOffersMap[loyaltyDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
@@ -1305,6 +1312,7 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
|
1305
1312
|
if (normalizedVoucherDiscountId != null) {
|
|
1306
1313
|
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
1307
1314
|
}
|
|
1315
|
+
removedItemsReasonMap[item._id] = constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1308
1316
|
continue;
|
|
1309
1317
|
}
|
|
1310
1318
|
if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
@@ -1312,6 +1320,7 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
|
1312
1320
|
if (normalizedVoucherDiscountId != null) {
|
|
1313
1321
|
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
1314
1322
|
}
|
|
1323
|
+
removedItemsReasonMap[item._id] = constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1315
1324
|
continue;
|
|
1316
1325
|
}
|
|
1317
1326
|
unlockedOffersMap[voucherDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
@@ -1338,13 +1347,60 @@ function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
|
1338
1347
|
if (!hasChanges) {
|
|
1339
1348
|
return null;
|
|
1340
1349
|
}
|
|
1350
|
+
const syncedIterations = getIterationsWithAutoRemovedVoidReasons({
|
|
1351
|
+
iterations: data.iterations,
|
|
1352
|
+
removedItemsReasonMap,
|
|
1353
|
+
});
|
|
1341
1354
|
return {
|
|
1342
1355
|
...data,
|
|
1356
|
+
...(syncedIterations != null && syncedIterations !== data.iterations ? { iterations: syncedIterations } : {}),
|
|
1343
1357
|
categories: newCategories,
|
|
1344
1358
|
order_discount_details,
|
|
1345
1359
|
redeemed_offers,
|
|
1346
1360
|
};
|
|
1347
1361
|
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Stamp the system-generated void reason on the last-synced iteration items
|
|
1364
|
+
* for items the offer synchronization removed from the live cart.
|
|
1365
|
+
*
|
|
1366
|
+
* Removed items' reasons live on the first iteration (mirrors
|
|
1367
|
+
* `getRemoveItemFromCartData`) so the cart, receipts and the backend
|
|
1368
|
+
* void-items pipeline (`getVoidItemsWithReason`) can surface why the item was
|
|
1369
|
+
* removed. Items that never existed in the last-synced iteration (added during
|
|
1370
|
+
* the current edit) and items that already carry an explicit cashier reason
|
|
1371
|
+
* are left untouched.
|
|
1372
|
+
*/
|
|
1373
|
+
function getIterationsWithAutoRemovedVoidReasons({ iterations, removedItemsReasonMap, }) {
|
|
1374
|
+
if (Object.keys(removedItemsReasonMap).length === 0 || iterations == null || iterations.length === 0) {
|
|
1375
|
+
return iterations;
|
|
1376
|
+
}
|
|
1377
|
+
const [firstIteration, ...restIterations] = iterations;
|
|
1378
|
+
let hasChanges = false;
|
|
1379
|
+
const firstIterationCategories = firstIteration.categories.map((category) => {
|
|
1380
|
+
let categoryHasChanges = false;
|
|
1381
|
+
const items = category.items.map((prevItem) => {
|
|
1382
|
+
const autoVoidReason = removedItemsReasonMap[prevItem._id];
|
|
1383
|
+
if (autoVoidReason == null) {
|
|
1384
|
+
return prevItem;
|
|
1385
|
+
}
|
|
1386
|
+
// Keep an explicit cashier-provided reason when one already exists.
|
|
1387
|
+
if (prevItem.void_reason != null && prevItem.void_reason.length > 0) {
|
|
1388
|
+
return prevItem;
|
|
1389
|
+
}
|
|
1390
|
+
categoryHasChanges = true;
|
|
1391
|
+
hasChanges = true;
|
|
1392
|
+
return {
|
|
1393
|
+
...prevItem,
|
|
1394
|
+
void_reason: autoVoidReason,
|
|
1395
|
+
};
|
|
1396
|
+
});
|
|
1397
|
+
return categoryHasChanges ? { ...category, items } : category;
|
|
1398
|
+
});
|
|
1399
|
+
if (!hasChanges) {
|
|
1400
|
+
return iterations;
|
|
1401
|
+
}
|
|
1402
|
+
return [{ ...firstIteration, categories: firstIterationCategories }, ...restIterations];
|
|
1403
|
+
}
|
|
1348
1404
|
function hasItemLevelLoyaltyDiscountForId(categories, discountId) {
|
|
1349
1405
|
for (let categoryIndex = 0; categoryIndex < categories.length; categoryIndex++) {
|
|
1350
1406
|
const items = categories[categoryIndex].items;
|
|
@@ -32,6 +32,7 @@ export declare function getVoidItemsWithReason({ currentCategories, iterationsFr
|
|
|
32
32
|
newIterationCategories: IOrderCategory[];
|
|
33
33
|
voidOrderItems: IOrderItem[];
|
|
34
34
|
hasNewItems: boolean;
|
|
35
|
+
hasMissingVoidReason: boolean;
|
|
35
36
|
};
|
|
36
37
|
export declare function getSplitOrdersFromOrder(order: IOrder, splitBill: ISplitBill): IOrder[];
|
|
37
38
|
export {};
|
|
@@ -440,10 +440,17 @@ function getVoidItemsWithReason({ currentCategories, iterationsFromFrontend, pre
|
|
|
440
440
|
const hasNewItems = newItems.length > 0;
|
|
441
441
|
const voidOrderItems = [];
|
|
442
442
|
const newVoidOrderItemReasonsMap = {};
|
|
443
|
+
// A removed item with no non-empty `void_reason` in the latest frontend
|
|
444
|
+
// iteration falls back to the '-' placeholder on voidOrderItems below.
|
|
445
|
+
// Surface it so callers can reject reasonless voids before persisting.
|
|
446
|
+
let hasMissingVoidReason = false;
|
|
443
447
|
for (let i = 0; i < removedItems.length; i++) {
|
|
444
448
|
const { orderItem } = removedItems[i];
|
|
445
449
|
const voidReason = voidOrderItemReasonsMap[orderItem._id];
|
|
446
450
|
const isReady = voidOrderItemIsReadyMap[orderItem._id];
|
|
451
|
+
if (voidReason == null) {
|
|
452
|
+
hasMissingVoidReason = true;
|
|
453
|
+
}
|
|
447
454
|
voidOrderItems.push({
|
|
448
455
|
...orderItem,
|
|
449
456
|
...(isReady != null ? { is_ready: isReady } : {}),
|
|
@@ -481,7 +488,12 @@ function getVoidItemsWithReason({ currentCategories, iterationsFromFrontend, pre
|
|
|
481
488
|
items: newCategoryItems,
|
|
482
489
|
});
|
|
483
490
|
}
|
|
484
|
-
return {
|
|
491
|
+
return {
|
|
492
|
+
newIterationCategories,
|
|
493
|
+
voidOrderItems,
|
|
494
|
+
hasNewItems,
|
|
495
|
+
hasMissingVoidReason,
|
|
496
|
+
};
|
|
485
497
|
}
|
|
486
498
|
// ======================>Split Bill<======================
|
|
487
499
|
function syncDineinCoursesOnSplit(dinein_details, swapedItemIdsMap, totalChecks, isLastCheck) {
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const constants_1 = require("../constants");
|
|
3
4
|
const cart_actions_1 = require("../helpers/store/cart-actions");
|
|
4
5
|
const offer_apply_helpers_1 = require("../helpers/utils/offer-apply-helpers");
|
|
5
6
|
const OFFER_ID = 'offer-reward-threshold';
|
|
6
7
|
const POST_ORDER_REWARD_TYPES = ['POINTS', 'CASHBACK', 'VOUCHER', 'GIFT_CARDS'];
|
|
7
|
-
function makeCategories({ itemDiscountDetails = null, quantity = 1, subtotal = 699, } = {}) {
|
|
8
|
+
function makeCategories({ itemDiscountDetails = null, quantity = 1, subtotal = 699, itemId = 'cart-item-1', voidReason = undefined, } = {}) {
|
|
8
9
|
return [
|
|
9
10
|
{
|
|
10
11
|
category_id: 'category-pizza',
|
|
11
12
|
items: [
|
|
12
13
|
{
|
|
13
|
-
_id:
|
|
14
|
+
_id: itemId,
|
|
14
15
|
item_id: 'item-classic-euro',
|
|
15
16
|
name: 'Classic Euro',
|
|
16
17
|
quantity,
|
|
17
18
|
subtotal,
|
|
18
19
|
modifiers: [],
|
|
19
20
|
item_discount_details: itemDiscountDetails,
|
|
21
|
+
...(voidReason != null ? { void_reason: voidReason } : {}),
|
|
20
22
|
},
|
|
21
23
|
],
|
|
22
24
|
},
|
|
@@ -108,3 +110,102 @@ describe('offer redeemed_offers sync', () => {
|
|
|
108
110
|
expect(result.redeemed_offers?.[OFFER_ID]).toBe(1);
|
|
109
111
|
});
|
|
110
112
|
});
|
|
113
|
+
describe('offer sync auto-removed items void reason', () => {
|
|
114
|
+
const getFlatItems = (categories) => (categories ?? []).flatMap((category) => category.items ?? []);
|
|
115
|
+
const makeIteration = (categories) => [{ categories }];
|
|
116
|
+
it('stamps the auto void reason on the last iteration when a de-qualified item reward is removed', () => {
|
|
117
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
118
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
119
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
120
|
+
iterations: makeIteration(makeCategories({ itemDiscountDetails })),
|
|
121
|
+
}), [], {}, {}, [makeOffer('ITEMS')]);
|
|
122
|
+
expect(getFlatItems(result.categories)).toHaveLength(0);
|
|
123
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBe(constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON);
|
|
124
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toContain('Auto removed by system');
|
|
125
|
+
});
|
|
126
|
+
it('stamps the auto void reason when the item reward evidence is gone', () => {
|
|
127
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
128
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
129
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
130
|
+
redeemed_offers: null,
|
|
131
|
+
iterations: makeIteration(makeCategories({ itemDiscountDetails })),
|
|
132
|
+
}), [], {}, { [OFFER_ID]: 1 }, [makeOffer('POINTS')]);
|
|
133
|
+
expect(getFlatItems(result.categories)).toHaveLength(0);
|
|
134
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBe(constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON);
|
|
135
|
+
});
|
|
136
|
+
it('does not stamp a void reason while the offer item remains qualified', () => {
|
|
137
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
138
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
139
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
140
|
+
iterations: makeIteration(makeCategories({ itemDiscountDetails })),
|
|
141
|
+
}), [], {}, { [OFFER_ID]: 1 }, [makeOffer('ITEMS')]);
|
|
142
|
+
expect(getFlatItems(result.categories)).toHaveLength(1);
|
|
143
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBeUndefined();
|
|
144
|
+
});
|
|
145
|
+
it('does not stamp reasons for removed items that are not part of the last iteration', () => {
|
|
146
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
147
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
148
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
149
|
+
iterations: makeIteration(makeCategories({ itemId: 'cart-item-2' })),
|
|
150
|
+
}), [], {}, {}, [makeOffer('ITEMS')]);
|
|
151
|
+
expect(getFlatItems(result.categories)).toHaveLength(0);
|
|
152
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBeUndefined();
|
|
153
|
+
});
|
|
154
|
+
it('preserves an explicit cashier reason already present on the last iteration item', () => {
|
|
155
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
156
|
+
const explicitReason = 'Quality Issue - Food quality not up to standard';
|
|
157
|
+
const result = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
158
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
159
|
+
iterations: makeIteration(makeCategories({ itemDiscountDetails, voidReason: explicitReason })),
|
|
160
|
+
}), [], {}, {}, [makeOffer('ITEMS')]);
|
|
161
|
+
expect(getFlatItems(result.categories)).toHaveLength(0);
|
|
162
|
+
expect(getFlatItems(result.iterations?.[0]?.categories)[0]?.void_reason).toBe(explicitReason);
|
|
163
|
+
});
|
|
164
|
+
it('clears the stamped reason when the same item is added back (redo/replace) after re-qualifying', () => {
|
|
165
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
166
|
+
// Offer de-qualifies: sync removes the item and stamps the auto reason.
|
|
167
|
+
const dropped = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
168
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
169
|
+
iterations: makeIteration(makeCategories({ itemDiscountDetails })),
|
|
170
|
+
}), [], {}, {}, [makeOffer('ITEMS')]);
|
|
171
|
+
const removedItem = getFlatItems(dropped.iterations?.[0]?.categories)[0];
|
|
172
|
+
expect(removedItem?.void_reason).toBe(constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON);
|
|
173
|
+
// Cart redo/replace re-adds the exact removed line (same _id, item object
|
|
174
|
+
// still carries the auto reason).
|
|
175
|
+
const restored = (0, cart_actions_1.getAddItemToCartData)(dropped, {
|
|
176
|
+
category_id: 'category-pizza',
|
|
177
|
+
name: 'Pizza',
|
|
178
|
+
item: removedItem,
|
|
179
|
+
chosen_course_id: null,
|
|
180
|
+
replace_id: removedItem._id,
|
|
181
|
+
}, [], {});
|
|
182
|
+
const restoredItems = getFlatItems(restored.categories);
|
|
183
|
+
expect(restoredItems).toHaveLength(1);
|
|
184
|
+
expect(restoredItems[0]._id).toBe('cart-item-1');
|
|
185
|
+
expect(restoredItems[0].void_reason).toBeUndefined();
|
|
186
|
+
expect(getFlatItems(restored.iterations?.[0]?.categories)[0]?.void_reason).toBeUndefined();
|
|
187
|
+
});
|
|
188
|
+
it('keeps the removed line void reason when the reward is re-added as a new line', () => {
|
|
189
|
+
const itemDiscountDetails = { loyalty_discount_id: OFFER_ID };
|
|
190
|
+
const dropped = (0, offer_apply_helpers_1.getSyncOffersWithCartData)(makeOrder({
|
|
191
|
+
categories: makeCategories({ itemDiscountDetails }),
|
|
192
|
+
iterations: makeIteration(makeCategories({ itemDiscountDetails })),
|
|
193
|
+
}), [], {}, {}, [makeOffer('ITEMS')]);
|
|
194
|
+
const restored = (0, cart_actions_1.getAddItemToCartData)(dropped, {
|
|
195
|
+
category_id: 'category-pizza',
|
|
196
|
+
name: 'Pizza',
|
|
197
|
+
item: {
|
|
198
|
+
...getFlatItems(dropped.iterations?.[0]?.categories)[0],
|
|
199
|
+
_id: 'cart-item-3',
|
|
200
|
+
item_discount_details: itemDiscountDetails,
|
|
201
|
+
},
|
|
202
|
+
chosen_course_id: null,
|
|
203
|
+
}, [], {});
|
|
204
|
+
const restoredItems = getFlatItems(restored.categories);
|
|
205
|
+
expect(restoredItems).toHaveLength(1);
|
|
206
|
+
expect(restoredItems[0]._id).toBe('cart-item-3');
|
|
207
|
+
expect(restoredItems[0].void_reason).toBeUndefined();
|
|
208
|
+
// The original line stays removed and keeps its auto reason for the void record.
|
|
209
|
+
expect(getFlatItems(restored.iterations?.[0]?.categories)[0]?.void_reason).toBe(constants_1.OFFER_NO_LONGER_QUALIFIED_VOID_REASON);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_order_actions_1 = require("../helpers/store/common-order-actions");
|
|
4
|
+
const ENUMS_1 = require("../constants/ENUMS");
|
|
5
|
+
function makeItem(params) {
|
|
6
|
+
return {
|
|
7
|
+
_id: params._id,
|
|
8
|
+
item_id: `item-${params._id}`,
|
|
9
|
+
name: `Item ${params._id}`,
|
|
10
|
+
price: 100,
|
|
11
|
+
quantity: 1,
|
|
12
|
+
subtotal: 100,
|
|
13
|
+
items_price: 100,
|
|
14
|
+
modifiers: [],
|
|
15
|
+
...(params.void_reason != null ? { void_reason: params.void_reason } : {}),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function makeCategory(items) {
|
|
19
|
+
return { category_id: 'c1', name: 'Category', items };
|
|
20
|
+
}
|
|
21
|
+
function getVoidResult(params) {
|
|
22
|
+
return (0, common_order_actions_1.getVoidItemsWithReason)({
|
|
23
|
+
currentCategories: [makeCategory(params.currentItems)],
|
|
24
|
+
previousCategories: [makeCategory(params.previousItems)],
|
|
25
|
+
iterationsFromFrontend: [{ categories: [makeCategory(params.latestIterationItems)] }],
|
|
26
|
+
platform: ENUMS_1.PLATFORMS_ENUM.POS_TAKE_AWAY,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
describe('getVoidItemsWithReason - hasMissingVoidReason', () => {
|
|
30
|
+
it('is false and carries the reason when the latest iteration has one', () => {
|
|
31
|
+
const removedItem = makeItem({ _id: 'removed-1' });
|
|
32
|
+
const keptItem = makeItem({ _id: 'kept-1' });
|
|
33
|
+
const result = getVoidResult({
|
|
34
|
+
currentItems: [keptItem],
|
|
35
|
+
previousItems: [removedItem, keptItem],
|
|
36
|
+
latestIterationItems: [{ ...removedItem, void_reason: 'Wrong order' }, keptItem],
|
|
37
|
+
});
|
|
38
|
+
expect(result.voidOrderItems).toHaveLength(1);
|
|
39
|
+
expect(result.voidOrderItems[0]._id).toBe('removed-1');
|
|
40
|
+
expect(result.voidOrderItems[0].void_reason).toBe('Wrong order');
|
|
41
|
+
expect(result.hasMissingVoidReason).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
it('is true and applies the "-" placeholder when no reason was sent', () => {
|
|
44
|
+
const removedItem = makeItem({ _id: 'removed-1' });
|
|
45
|
+
const keptItem = makeItem({ _id: 'kept-1' });
|
|
46
|
+
const result = getVoidResult({
|
|
47
|
+
currentItems: [keptItem],
|
|
48
|
+
previousItems: [removedItem, keptItem],
|
|
49
|
+
latestIterationItems: [removedItem, keptItem],
|
|
50
|
+
});
|
|
51
|
+
expect(result.voidOrderItems).toHaveLength(1);
|
|
52
|
+
expect(result.voidOrderItems[0].void_reason).toBe('-');
|
|
53
|
+
expect(result.hasMissingVoidReason).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
it('is true when only one of multiple removed items has a reason', () => {
|
|
56
|
+
const removedWithReason = makeItem({ _id: 'removed-1' });
|
|
57
|
+
const removedWithoutReason = makeItem({ _id: 'removed-2' });
|
|
58
|
+
const keptItem = makeItem({ _id: 'kept-1' });
|
|
59
|
+
const result = getVoidResult({
|
|
60
|
+
currentItems: [keptItem],
|
|
61
|
+
previousItems: [removedWithReason, removedWithoutReason, keptItem],
|
|
62
|
+
latestIterationItems: [
|
|
63
|
+
{ ...removedWithReason, void_reason: 'Customer changed mind' },
|
|
64
|
+
removedWithoutReason,
|
|
65
|
+
keptItem,
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
expect(result.voidOrderItems).toHaveLength(2);
|
|
69
|
+
expect(result.hasMissingVoidReason).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
it('is false when nothing was removed', () => {
|
|
72
|
+
const keptItem = makeItem({ _id: 'kept-1' });
|
|
73
|
+
const result = getVoidResult({
|
|
74
|
+
currentItems: [keptItem],
|
|
75
|
+
previousItems: [keptItem],
|
|
76
|
+
latestIterationItems: [keptItem],
|
|
77
|
+
});
|
|
78
|
+
expect(result.voidOrderItems).toHaveLength(0);
|
|
79
|
+
expect(result.hasNewItems).toBe(false);
|
|
80
|
+
expect(result.hasMissingVoidReason).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
});
|