@open-tender/utils 0.5.73 → 0.5.75
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/dist/cjs/__tests__/utils/cards.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/cards.test.js +28 -0
- package/dist/cjs/__tests__/utils/cart.test.d.ts +1 -0
- package/dist/cjs/__tests__/utils/cart.test.js +469 -0
- package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +1 -1
- package/dist/cjs/hooks/useGiftCardAssignForm.js +2 -2
- package/dist/cjs/utils/cards.d.ts +1 -0
- package/dist/cjs/utils/cards.js +6 -1
- package/dist/cjs/utils/cart.d.ts +24 -2
- package/dist/cjs/utils/cart.js +133 -8
- package/dist/esm/__tests__/utils/cards.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/cards.test.js +26 -0
- package/dist/esm/__tests__/utils/cart.test.d.ts +1 -0
- package/dist/esm/__tests__/utils/cart.test.js +467 -0
- package/dist/esm/hooks/useGiftCardAssignForm.d.ts +1 -1
- package/dist/esm/hooks/useGiftCardAssignForm.js +3 -3
- package/dist/esm/utils/cards.d.ts +1 -0
- package/dist/esm/utils/cards.js +4 -0
- package/dist/esm/utils/cart.d.ts +24 -2
- package/dist/esm/utils/cart.js +131 -7
- package/package.json +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useState, useEffect, useRef } from 'react';
|
|
2
|
-
import { makeFormErrors } from '../utils';
|
|
2
|
+
import { makeFormErrors, parseGiftCardNumber } from '../utils';
|
|
3
3
|
export const useGiftCardAssignForm = (loading, error, assign, callback) => {
|
|
4
4
|
const submitRef = useRef(null);
|
|
5
5
|
const inputRef = useRef(null);
|
|
@@ -29,8 +29,8 @@ export const useGiftCardAssignForm = (loading, error, assign, callback) => {
|
|
|
29
29
|
const handleSubmit = (evt) => {
|
|
30
30
|
var _a, _b, _c, _d;
|
|
31
31
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
32
|
-
const card_number =
|
|
33
|
-
if (
|
|
32
|
+
const card_number = parseGiftCardNumber(data.card_number);
|
|
33
|
+
if (card_number === null) {
|
|
34
34
|
setErrors({ card_number: 'Card numbers must be all digits' });
|
|
35
35
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
36
36
|
}
|
|
@@ -14,6 +14,7 @@ export declare const cardNumbersRegex: {
|
|
|
14
14
|
};
|
|
15
15
|
export declare const getCardType: (cardNumber: string) => CardType;
|
|
16
16
|
export declare const makeNumeric: (str: string) => string;
|
|
17
|
+
export declare const parseGiftCardNumber: (value: string) => string | null;
|
|
17
18
|
export declare const makeAlphaNumeric: (str: string) => string;
|
|
18
19
|
export declare const makeAcctNumber: (str: string, cardType: CardType) => string;
|
|
19
20
|
export declare const formatCardField: (field: keyof CreditCardData, value: string) => string;
|
package/dist/esm/utils/cards.js
CHANGED
|
@@ -43,6 +43,10 @@ export const makeNumeric = (str) => {
|
|
|
43
43
|
return '';
|
|
44
44
|
return strMatches.join('').replace(/\s/g, '');
|
|
45
45
|
};
|
|
46
|
+
export const parseGiftCardNumber = (value) => {
|
|
47
|
+
const cardNumber = value.trim();
|
|
48
|
+
return /^\d{1,24}$/.test(cardNumber) ? cardNumber : null;
|
|
49
|
+
};
|
|
46
50
|
export const makeAlphaNumeric = (str) => {
|
|
47
51
|
const strMatches = str.match(/[a-zA-Z0-9]+/g); // limit to alphanumeric input, returns an array
|
|
48
52
|
if (!strMatches)
|
package/dist/esm/utils/cart.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Cart, CartItem, CartItemGroups, CartLevels, CartItemOption, CartOwner, CheckoutConfig, CheckoutFormAddress, CheckoutFormCustomer, CheckoutPromoCodes, CheckoutTenders, DisplayItem, DisplayItems, Favorites, GroupOrderGuest, GroupOrderGuests, MenuCategories, MenuItem, MenuItems, Money, NutritionalInfo, NutritionalInfoCalcs, Order, OrderCreate, OrderCreateDiscounts, OrderCreateSurcharges, OrderData, OrderItem, OrderItemGroups, Orders, SimpleCart, SimpleCartGroups, SimpleCartItem, SoldOut, Images, CartErrors, BaseItem, FavoritesLookup, RequestedAt } from '@open-tender/types';
|
|
1
|
+
import { Cart, CartItem, CartItemGroups, CartLevels, CartItemOption, CartOwner, CheckoutConfig, CheckoutFormAddress, CheckoutFormCustomer, CheckoutPromoCodes, CheckoutTenders, DisplayItem, DisplayItems, Favorites, GroupOrderGuest, GroupOrderGuests, MenuCategories, MenuItem, MenuItems, Money, NutritionalInfo, NutritionalInfoCalcs, Order, OrderCreate, OrderCreateDiscounts, OrderCreateSurcharges, OrderData, OrderItem, OrderItemGroups, Orders, ReorderUnavailableLine, SimpleCart, SimpleCartGroups, SimpleCartItem, SoldOut, Images, CartErrors, BaseItem, FavoritesLookup, RequestedAt } from '@open-tender/types';
|
|
2
2
|
export declare const getItemOptions: <T extends BaseItem>(item: T) => BaseItem[];
|
|
3
3
|
export declare const makeModifierNames: <T extends BaseItem>(item: T) => string;
|
|
4
4
|
export declare const makeModifierNamesForGroups: (groups: CartItemGroups) => string;
|
|
@@ -14,7 +14,9 @@ export declare const checkSoldOut: (id: number, suspendUntil: number | null, sol
|
|
|
14
14
|
export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean, requestedAt?: RequestedAt | null) => CartItemOption | CartItem;
|
|
15
15
|
export declare const calcCartCounts: (cart: Cart) => Record<string, number>;
|
|
16
16
|
export declare const rehydrateOrderItem: (menuItem: MenuItem, simpleCartItem: SimpleCartItem, soldOut?: number[], requestedAt?: RequestedAt | null) => CartItem;
|
|
17
|
-
export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[]
|
|
17
|
+
export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[], options?: {
|
|
18
|
+
isReorder?: boolean;
|
|
19
|
+
}) => {
|
|
18
20
|
cart: {
|
|
19
21
|
index: number;
|
|
20
22
|
cartGuestId: number | null;
|
|
@@ -62,6 +64,7 @@ export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: Simp
|
|
|
62
64
|
excludeFromBuilder: boolean;
|
|
63
65
|
}[];
|
|
64
66
|
cartCounts: Record<string, number>;
|
|
67
|
+
unavailable: ReorderUnavailableLine[];
|
|
65
68
|
};
|
|
66
69
|
export declare const rehydrateCheckoutForm: (order: Order) => {
|
|
67
70
|
details: {
|
|
@@ -210,6 +213,25 @@ export declare const makeSimpleCart: <C extends {
|
|
|
210
213
|
made_for: string;
|
|
211
214
|
notes: string;
|
|
212
215
|
}[];
|
|
216
|
+
export declare const makeReorderCart: <C extends {
|
|
217
|
+
id: number;
|
|
218
|
+
name?: string | undefined;
|
|
219
|
+
pos_ext_id?: string | null | undefined;
|
|
220
|
+
quantity: number;
|
|
221
|
+
groups: T[];
|
|
222
|
+
madeFor?: string | null | undefined;
|
|
223
|
+
made_for?: string | null | undefined;
|
|
224
|
+
notes?: string | null | undefined;
|
|
225
|
+
}[], T extends {
|
|
226
|
+
id: number;
|
|
227
|
+
options: K[];
|
|
228
|
+
}, K extends {
|
|
229
|
+
id: number;
|
|
230
|
+
name?: string | undefined;
|
|
231
|
+
pos_ext_id?: string | null | undefined;
|
|
232
|
+
quantity: number;
|
|
233
|
+
groups: T[];
|
|
234
|
+
}>(cart: C) => SimpleCart;
|
|
213
235
|
export declare const validateCart: (cart: Cart, categories: MenuCategories, soldOut: SoldOut, requestedAt: RequestedAt) => {
|
|
214
236
|
newCart: Cart;
|
|
215
237
|
errors: CartErrors | null;
|
package/dist/esm/utils/cart.js
CHANGED
|
@@ -329,6 +329,7 @@ const makeSimpleGroupsLookup = (groups) => {
|
|
|
329
329
|
return groups.reduce((grpObj, g) => {
|
|
330
330
|
const options = g.options.reduce((optObj, o) => {
|
|
331
331
|
optObj[o.id] = {
|
|
332
|
+
name: o.name,
|
|
332
333
|
quantity: o.quantity,
|
|
333
334
|
pos_ext_id: o.pos_ext_id || null,
|
|
334
335
|
groupsLookup: makeSimpleGroupsLookup(o.groups)
|
|
@@ -366,18 +367,114 @@ export const rehydrateOrderItem = (menuItem, simpleCartItem, soldOut = [], reque
|
|
|
366
367
|
const pricedItem = calcPrices(orderItem);
|
|
367
368
|
return pricedItem;
|
|
368
369
|
};
|
|
369
|
-
|
|
370
|
+
// A saved option is the same as a current option when they have the same id
|
|
371
|
+
// and group, unless both carry a Checkmate id and the ids differ.
|
|
372
|
+
const isSameReorderOption = (simpleOption, option) => {
|
|
373
|
+
if (!simpleOption.pos_ext_id || !option.pos_ext_id)
|
|
374
|
+
return true;
|
|
375
|
+
return simpleOption.pos_ext_id === option.pos_ext_id;
|
|
376
|
+
};
|
|
377
|
+
// Reorder only. Saved options are matched to the options of the current menu
|
|
378
|
+
// item by group and id first. The remaining ones are matched by their
|
|
379
|
+
// Checkmate id alone, which is stable when an option, its group or its
|
|
380
|
+
// category is renamed or moved in Checkmate and gets a new id.
|
|
381
|
+
// Saved options that are not found are added to `unavailable`, and the
|
|
382
|
+
// caller drops the whole item.
|
|
383
|
+
const rehydrateReorderGroups = (groups, groupsLookup, unavailable) => {
|
|
384
|
+
if (!groupsLookup)
|
|
385
|
+
return groups;
|
|
386
|
+
const matches = new Map();
|
|
387
|
+
const used = new Set();
|
|
388
|
+
groups = groups || [];
|
|
389
|
+
groups.forEach(group => {
|
|
390
|
+
const simpleGroup = groupsLookup[group.id];
|
|
391
|
+
if (!simpleGroup)
|
|
392
|
+
return;
|
|
393
|
+
(group.options || []).forEach(option => {
|
|
394
|
+
const simpleOption = simpleGroup.options[option.id];
|
|
395
|
+
if (simpleOption && isSameReorderOption(simpleOption, option)) {
|
|
396
|
+
matches.set(option, simpleOption);
|
|
397
|
+
used.add(simpleOption);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
const simpleOptions = Object.values(groupsLookup).flatMap(simpleGroup => Object.values(simpleGroup.options));
|
|
402
|
+
groups.forEach(group => {
|
|
403
|
+
;
|
|
404
|
+
(group.options || []).forEach(option => {
|
|
405
|
+
if (matches.has(option) || !option.pos_ext_id)
|
|
406
|
+
return;
|
|
407
|
+
const simpleOption = simpleOptions.find(i => !used.has(i) && i.pos_ext_id === option.pos_ext_id);
|
|
408
|
+
if (simpleOption) {
|
|
409
|
+
matches.set(option, simpleOption);
|
|
410
|
+
used.add(simpleOption);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
matches.forEach((simpleOption, option) => {
|
|
415
|
+
option.quantity = simpleOption.quantity;
|
|
416
|
+
option.groups = rehydrateReorderGroups(option.groups, simpleOption.groupsLookup, unavailable);
|
|
417
|
+
});
|
|
418
|
+
simpleOptions
|
|
419
|
+
.filter(i => !used.has(i))
|
|
420
|
+
.forEach(i => unavailable.push({ name: i.name || '' }));
|
|
421
|
+
return groups;
|
|
422
|
+
};
|
|
423
|
+
const rehydrateReorderItem = (menuItem, simpleCartItem, soldOut) => {
|
|
424
|
+
const orderItem = makeOrderItem(menuItem, true, soldOut, simpleCartItem, undefined, null);
|
|
425
|
+
orderItem.quantity = simpleCartItem.quantity || 1;
|
|
426
|
+
const unavailable = [];
|
|
427
|
+
const groupsLookup = makeSimpleGroupsLookup(simpleCartItem.groups);
|
|
428
|
+
if (groupsLookup) {
|
|
429
|
+
orderItem.groups = rehydrateReorderGroups(orderItem.groups, groupsLookup, unavailable);
|
|
430
|
+
}
|
|
431
|
+
const pricedItem = calcPrices(orderItem);
|
|
432
|
+
return { item: pricedItem, unavailable };
|
|
433
|
+
};
|
|
434
|
+
// Reorder only. The Checkmate id of an item survives renames and category
|
|
435
|
+
// moves, which change the id of the item on the menu, so it is tried before
|
|
436
|
+
// the id.
|
|
437
|
+
const findReorderMenuItem = (menuItems, simpleCartItem) => {
|
|
438
|
+
const { id, pos_ext_id } = simpleCartItem;
|
|
439
|
+
const items = menuItems || [];
|
|
440
|
+
if (pos_ext_id) {
|
|
441
|
+
const matches = items.filter(i => i.pos_ext_id === pos_ext_id);
|
|
442
|
+
if (matches.length)
|
|
443
|
+
return matches.find(i => i.id === id) || matches[0];
|
|
444
|
+
}
|
|
445
|
+
return items.find(i => i.id === id);
|
|
446
|
+
};
|
|
447
|
+
// With `isReorder`, an item is dropped and reported in `unavailable` when it
|
|
448
|
+
// or any of its saved options is gone, or a required group is left unfilled,
|
|
449
|
+
// so the guest never gets something different from what they ordered.
|
|
450
|
+
// Without it, items that are not on the menu are skipped and everything else
|
|
451
|
+
// is kept, as it is for edited orders and group orders.
|
|
452
|
+
export const rehydrateCart = (menuItems, simpleCartItems, soldOut = [], options = {}) => {
|
|
370
453
|
const orderItems = [];
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
454
|
+
const unavailable = [];
|
|
455
|
+
const lines = options.isReorder ? simpleCartItems || [] : simpleCartItems;
|
|
456
|
+
lines.forEach(item => {
|
|
457
|
+
if (!options.isReorder) {
|
|
458
|
+
const menuItem = menuItems.find(i => i.id === item.id);
|
|
459
|
+
if (menuItem)
|
|
460
|
+
orderItems.push(rehydrateOrderItem(menuItem, item, soldOut));
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (!item)
|
|
464
|
+
return;
|
|
465
|
+
const menuItem = findReorderMenuItem(menuItems, item);
|
|
466
|
+
const rehydrated = menuItem && rehydrateReorderItem(menuItem, item, soldOut);
|
|
467
|
+
if (!rehydrated ||
|
|
468
|
+
rehydrated.unavailable.length ||
|
|
469
|
+
hasGroupsBelowMin(null, rehydrated.item.groups)) {
|
|
470
|
+
unavailable.push({ name: item.name || '' });
|
|
471
|
+
return;
|
|
376
472
|
}
|
|
473
|
+
orderItems.push(rehydrated.item);
|
|
377
474
|
});
|
|
378
475
|
const cart = orderItems.map((i, index) => (Object.assign(Object.assign({}, i), { index })));
|
|
379
476
|
const cartCounts = calcCartCounts(cart);
|
|
380
|
-
return { cart, cartCounts };
|
|
477
|
+
return { cart, cartCounts, unavailable };
|
|
381
478
|
};
|
|
382
479
|
// TODO: maybe refactor
|
|
383
480
|
const rehydrateDetails = (details) => {
|
|
@@ -657,6 +754,33 @@ export const makeSimpleCart = (cart) => {
|
|
|
657
754
|
});
|
|
658
755
|
return simpleCart;
|
|
659
756
|
};
|
|
757
|
+
const makeReorderGroups = (groups) => {
|
|
758
|
+
return (groups || []).map(g => {
|
|
759
|
+
const options = (g.options || [])
|
|
760
|
+
.filter(o => o.quantity !== 0)
|
|
761
|
+
.map(o => ({
|
|
762
|
+
id: o.id,
|
|
763
|
+
name: o.name,
|
|
764
|
+
pos_ext_id: o.pos_ext_id || null,
|
|
765
|
+
quantity: o.quantity,
|
|
766
|
+
groups: makeReorderGroups(o.groups || [])
|
|
767
|
+
}));
|
|
768
|
+
return { id: g.id, options: options };
|
|
769
|
+
});
|
|
770
|
+
};
|
|
771
|
+
// Same as makeSimpleCart, but keeps the names and Checkmate ids needed to find
|
|
772
|
+
// the items of a past order on the current menu and to name those that are gone.
|
|
773
|
+
export const makeReorderCart = (cart) => {
|
|
774
|
+
return (cart || []).map(i => ({
|
|
775
|
+
id: i.id,
|
|
776
|
+
name: i.name,
|
|
777
|
+
pos_ext_id: i.pos_ext_id || null,
|
|
778
|
+
quantity: i.quantity,
|
|
779
|
+
groups: makeReorderGroups(i.groups),
|
|
780
|
+
made_for: i.madeFor || i.made_for || '',
|
|
781
|
+
notes: i.notes || ''
|
|
782
|
+
}));
|
|
783
|
+
};
|
|
660
784
|
/* cart validation */
|
|
661
785
|
// TODO: refactor for nested modifiers
|
|
662
786
|
// convert menu items to order items for validation but only include
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-tender/utils",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.75",
|
|
4
4
|
"description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"private": false,
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@emotion/react": "^11.10.4",
|
|
39
|
-
"@open-tender/types": "^0.4.
|
|
39
|
+
"@open-tender/types": "^0.4.189",
|
|
40
40
|
"@reduxjs/toolkit": "^1.8.5",
|
|
41
41
|
"@types/google.maps": "^3.53.5",
|
|
42
42
|
"@types/jest": "^29.5.11",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"typescript": "^4.8.2"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
|
-
"@open-tender/types": "^0.4.
|
|
61
|
+
"@open-tender/types": "^0.4.189",
|
|
62
62
|
"@reduxjs/toolkit": "^1.8.5",
|
|
63
63
|
"react": "^18.2.0"
|
|
64
64
|
},
|