@open-tender/utils 0.1.17 → 0.1.18

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.
@@ -27,7 +27,7 @@ const useOrderItem = (orderItem, favorite = null, allergenAlerts = [], displaySe
27
27
  const groupsBelowMin = (0, utils_1.hasGroupsBelowMin)(null, groups);
28
28
  const isIncomplete = totalPrice === 0 || quantity === 0 || groupsBelowMin;
29
29
  const nonSizeGroups = groups.filter(i => !i.isSize);
30
- const currentOptions = (0, utils_1.getItemOptions)({ groups: nonSizeGroups });
30
+ const currentOptions = (0, utils_1.getItemOptions)(Object.assign(Object.assign({}, orderItem), { groups: nonSizeGroups }));
31
31
  const hasSelections = currentOptions.length > 0;
32
32
  const showDesc = isMobile ? menuItemDescriptionMobile : menuItemDescription;
33
33
  const desc = showDesc === 'HIDE'
@@ -1,14 +1,14 @@
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 } from '@open-tender/types';
2
- export declare const getItemOptions: (item: Partial<CartItem>) => CartItemOption[];
3
- export declare const makeModifierNames: (item: CartItem) => string;
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 } from '@open-tender/types';
2
+ export declare const getItemOptions: <T extends BaseItem>(item: T) => BaseItem[];
3
+ export declare const makeModifierNames: <T extends BaseItem>(item: T) => string;
4
4
  export declare const hasGroupsBelowMin: (option: CartItemOption | null, groups: CartItemGroups) => boolean;
5
5
  export declare const makeModifierGroups: (groups: CartItemGroups) => [CartItemGroups, CartLevels];
6
- export declare const makeUpsellItems: (itemIds: number[], itemLookup: Record<string, MenuItem>) => (CartItem | CartItemOption)[];
6
+ export declare const makeUpsellItems: (itemIds: number[], itemLookup: Record<string, MenuItem>) => (CartItemOption | CartItem)[];
7
7
  export declare const calcNutrition: (item: CartItem | CartItemOption) => NutritionalInfoCalcs;
8
8
  export declare const calcNutritionalInfo: (item: CartItem | CartItemOption) => NutritionalInfo;
9
9
  export declare const calcPrices: (item: CartItem | CartItemOption) => CartItem | CartItemOption;
10
10
  export declare const checkSoldOut: (id: number, suspendUntil: number | null, soldOut?: number[]) => boolean;
11
- export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean) => CartItem | CartItemOption;
11
+ export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean) => CartItemOption | CartItem;
12
12
  export declare const calcCartCounts: (cart: Cart) => Record<string, number>;
13
13
  export declare const rehydrateOrderItem: (menuItem: MenuItem, simpleCartItem: SimpleCartItem, soldOut?: number[]) => CartItem;
14
14
  export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[]) => {
@@ -3,20 +3,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.checkAmountRemaining = exports.prepareOrder = exports.getDefaultTip = exports.validateCart = exports.makeSimpleCart = exports.decrementItem = exports.incrementItem = exports.removeItem = exports.addItem = exports.makeCartItemSignature = exports.makeItemSignature = exports.makeFavoritesLookup = exports.makeUniqueDisplayItems = exports.makeDisplayItems = exports.makeDisplayItem = exports.makeDisplayItemGroups = exports.makeItemImageUrl = exports.combineCarts = exports.makeGuestLookup = exports.rehydrateCheckoutForm = exports.rehydrateCart = exports.rehydrateOrderItem = exports.calcCartCounts = exports.makeOrderItem = exports.checkSoldOut = exports.calcPrices = exports.calcNutritionalInfo = exports.calcNutrition = exports.makeUpsellItems = exports.makeModifierGroups = exports.hasGroupsBelowMin = exports.makeModifierNames = exports.getItemOptions = void 0;
4
4
  const types_1 = require("@open-tender/types");
5
5
  const helpers_1 = require("./helpers");
6
- // TODO: refactor for nested mods
6
+ // TODO: refactor for nested mods (if needed)
7
7
  const getItemOptions = (item) => {
8
- if (!item.groups || !item.groups.length)
8
+ if (!item.groups.length)
9
9
  return [];
10
10
  const options = item.groups
11
- .map(group => { var _a; return (_a = group.options) === null || _a === void 0 ? void 0 : _a.filter(option => { var _a; return ((_a = option.quantity) !== null && _a !== void 0 ? _a : 0) > 0; }); })
12
- .reduce((arr, i) => [...(arr !== null && arr !== void 0 ? arr : []), ...(i !== null && i !== void 0 ? i : [])], []);
11
+ .map(group => group.options.filter(option => option.quantity > 0))
12
+ .reduce((arr, i) => [...arr, ...i], []);
13
13
  return options;
14
14
  };
15
15
  exports.getItemOptions = getItemOptions;
16
- // TODO: refactor for nested mods
17
16
  const makeModifierNames = (item) => {
18
- var _a;
19
- return (_a = (0, exports.getItemOptions)(item)) === null || _a === void 0 ? void 0 : _a.map(option => option.name).join(', ');
17
+ return item.groups
18
+ .map(group => group.options.filter(option => option.quantity > 0))
19
+ .reduce((arr, i) => [...arr, ...i], [])
20
+ .map(i => {
21
+ const optionNames = (0, exports.makeModifierNames)(i);
22
+ const name = optionNames ? `${i.name} (${optionNames})` : i.name;
23
+ return Object.assign(Object.assign({}, i), { name });
24
+ })
25
+ .map(option => option.name)
26
+ .join(', ');
20
27
  };
21
28
  exports.makeModifierNames = makeModifierNames;
22
29
  const hasGroupsBelowMin = (option, groups) => {
@@ -724,7 +731,11 @@ const validateCart = (cart, categories, soldOut) => {
724
731
  // above the max from the current menu
725
732
  if ((newItem.max && ((_c = oldItem.quantity) !== null && _c !== void 0 ? _c : 0) > newItem.max) ||
726
733
  (newItem.min && ((_d = oldItem.quantity) !== null && _d !== void 0 ? _d : 0) < newItem.min)) {
727
- invalidItems.push(oldItem);
734
+ const invalidItem = Object.assign(Object.assign({}, oldItem), { missingGroups,
735
+ invalidGroups,
736
+ missingOptions,
737
+ invalidOptions });
738
+ invalidItems.push(invalidItem);
728
739
  return null;
729
740
  }
730
741
  const nonNullGroups = updatedGroups === null || updatedGroups === void 0 ? void 0 : updatedGroups.filter(i => i !== null);
@@ -11,7 +11,7 @@ export declare const handleRespError: (err: RequestError) => {
11
11
  export declare const processError: (msg: string | null) => string;
12
12
  export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
13
13
  export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
14
- export declare const handleCheckoutErrors: (errors: RequestError, asMessages?: boolean) => {};
14
+ export declare const handleCheckoutErrors: (errors: RequestError, asMessages?: boolean) => Record<string, unknown>;
15
15
  export declare const errMessages: {
16
16
  unknown: string;
17
17
  forbidden: string;
@@ -24,7 +24,7 @@ export const useOrderItem = (orderItem, favorite = null, allergenAlerts = [], di
24
24
  const groupsBelowMin = hasGroupsBelowMin(null, groups);
25
25
  const isIncomplete = totalPrice === 0 || quantity === 0 || groupsBelowMin;
26
26
  const nonSizeGroups = groups.filter(i => !i.isSize);
27
- const currentOptions = getItemOptions({ groups: nonSizeGroups });
27
+ const currentOptions = getItemOptions(Object.assign(Object.assign({}, orderItem), { groups: nonSizeGroups }));
28
28
  const hasSelections = currentOptions.length > 0;
29
29
  const showDesc = isMobile ? menuItemDescriptionMobile : menuItemDescription;
30
30
  const desc = showDesc === 'HIDE'
@@ -1,14 +1,14 @@
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 } from '@open-tender/types';
2
- export declare const getItemOptions: (item: Partial<CartItem>) => CartItemOption[];
3
- export declare const makeModifierNames: (item: CartItem) => string;
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 } from '@open-tender/types';
2
+ export declare const getItemOptions: <T extends BaseItem>(item: T) => BaseItem[];
3
+ export declare const makeModifierNames: <T extends BaseItem>(item: T) => string;
4
4
  export declare const hasGroupsBelowMin: (option: CartItemOption | null, groups: CartItemGroups) => boolean;
5
5
  export declare const makeModifierGroups: (groups: CartItemGroups) => [CartItemGroups, CartLevels];
6
- export declare const makeUpsellItems: (itemIds: number[], itemLookup: Record<string, MenuItem>) => (CartItem | CartItemOption)[];
6
+ export declare const makeUpsellItems: (itemIds: number[], itemLookup: Record<string, MenuItem>) => (CartItemOption | CartItem)[];
7
7
  export declare const calcNutrition: (item: CartItem | CartItemOption) => NutritionalInfoCalcs;
8
8
  export declare const calcNutritionalInfo: (item: CartItem | CartItemOption) => NutritionalInfo;
9
9
  export declare const calcPrices: (item: CartItem | CartItemOption) => CartItem | CartItemOption;
10
10
  export declare const checkSoldOut: (id: number, suspendUntil: number | null, soldOut?: number[]) => boolean;
11
- export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean) => CartItem | CartItemOption;
11
+ export declare const makeOrderItem: (item: MenuItem, isEdit?: boolean, soldOut?: SoldOut, simpleItem?: SimpleCartItem, hasPoints?: boolean) => CartItemOption | CartItem;
12
12
  export declare const calcCartCounts: (cart: Cart) => Record<string, number>;
13
13
  export declare const rehydrateOrderItem: (menuItem: MenuItem, simpleCartItem: SimpleCartItem, soldOut?: number[]) => CartItem;
14
14
  export declare const rehydrateCart: (menuItems: MenuItems, simpleCartItems: SimpleCart, soldOut?: number[]) => {
@@ -1,18 +1,25 @@
1
1
  import { isCreditTender, isHouseAccountTender, isOrderCustomer } from '@open-tender/types';
2
2
  import { convertStringToArray } from './helpers';
3
- // TODO: refactor for nested mods
3
+ // TODO: refactor for nested mods (if needed)
4
4
  export const getItemOptions = (item) => {
5
- if (!item.groups || !item.groups.length)
5
+ if (!item.groups.length)
6
6
  return [];
7
7
  const options = item.groups
8
- .map(group => { var _a; return (_a = group.options) === null || _a === void 0 ? void 0 : _a.filter(option => { var _a; return ((_a = option.quantity) !== null && _a !== void 0 ? _a : 0) > 0; }); })
9
- .reduce((arr, i) => [...(arr !== null && arr !== void 0 ? arr : []), ...(i !== null && i !== void 0 ? i : [])], []);
8
+ .map(group => group.options.filter(option => option.quantity > 0))
9
+ .reduce((arr, i) => [...arr, ...i], []);
10
10
  return options;
11
11
  };
12
- // TODO: refactor for nested mods
13
12
  export const makeModifierNames = (item) => {
14
- var _a;
15
- return (_a = getItemOptions(item)) === null || _a === void 0 ? void 0 : _a.map(option => option.name).join(', ');
13
+ return item.groups
14
+ .map(group => group.options.filter(option => option.quantity > 0))
15
+ .reduce((arr, i) => [...arr, ...i], [])
16
+ .map(i => {
17
+ const optionNames = makeModifierNames(i);
18
+ const name = optionNames ? `${i.name} (${optionNames})` : i.name;
19
+ return Object.assign(Object.assign({}, i), { name });
20
+ })
21
+ .map(option => option.name)
22
+ .join(', ');
16
23
  };
17
24
  export const hasGroupsBelowMin = (option, groups) => {
18
25
  if (!groups || !groups.length)
@@ -692,7 +699,11 @@ export const validateCart = (cart, categories, soldOut) => {
692
699
  // above the max from the current menu
693
700
  if ((newItem.max && ((_c = oldItem.quantity) !== null && _c !== void 0 ? _c : 0) > newItem.max) ||
694
701
  (newItem.min && ((_d = oldItem.quantity) !== null && _d !== void 0 ? _d : 0) < newItem.min)) {
695
- invalidItems.push(oldItem);
702
+ const invalidItem = Object.assign(Object.assign({}, oldItem), { missingGroups,
703
+ invalidGroups,
704
+ missingOptions,
705
+ invalidOptions });
706
+ invalidItems.push(invalidItem);
696
707
  return null;
697
708
  }
698
709
  const nonNullGroups = updatedGroups === null || updatedGroups === void 0 ? void 0 : updatedGroups.filter(i => i !== null);
@@ -11,7 +11,7 @@ export declare const handleRespError: (err: RequestError) => {
11
11
  export declare const processError: (msg: string | null) => string;
12
12
  export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
13
13
  export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
14
- export declare const handleCheckoutErrors: (errors: RequestError, asMessages?: boolean) => {};
14
+ export declare const handleCheckoutErrors: (errors: RequestError, asMessages?: boolean) => Record<string, unknown>;
15
15
  export declare const errMessages: {
16
16
  unknown: string;
17
17
  forbidden: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
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",