@open-tender/utils 0.0.1

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.
Files changed (67) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/index.d.ts +1 -0
  4. package/dist/cjs/index.js +4 -0
  5. package/dist/cjs/utils/cards.d.ts +20 -0
  6. package/dist/cjs/utils/cards.js +153 -0
  7. package/dist/cjs/utils/cart.d.ts +422 -0
  8. package/dist/cjs/utils/cart.js +848 -0
  9. package/dist/cjs/utils/checkout.d.ts +23 -0
  10. package/dist/cjs/utils/checkout.js +108 -0
  11. package/dist/cjs/utils/constants.d.ts +52 -0
  12. package/dist/cjs/utils/constants.js +58 -0
  13. package/dist/cjs/utils/customer.d.ts +3 -0
  14. package/dist/cjs/utils/customer.js +23 -0
  15. package/dist/cjs/utils/datetimes.d.ts +174 -0
  16. package/dist/cjs/utils/datetimes.js +704 -0
  17. package/dist/cjs/utils/errors.d.ts +48 -0
  18. package/dist/cjs/utils/errors.js +143 -0
  19. package/dist/cjs/utils/helpers.d.ts +30 -0
  20. package/dist/cjs/utils/helpers.js +141 -0
  21. package/dist/cjs/utils/index.d.ts +14 -0
  22. package/dist/cjs/utils/index.js +17 -0
  23. package/dist/cjs/utils/keypad.d.ts +9 -0
  24. package/dist/cjs/utils/keypad.js +105 -0
  25. package/dist/cjs/utils/maps.d.ts +264 -0
  26. package/dist/cjs/utils/maps.js +287 -0
  27. package/dist/cjs/utils/orders.d.ts +40 -0
  28. package/dist/cjs/utils/orders.js +191 -0
  29. package/dist/cjs/utils/refunds.d.ts +35 -0
  30. package/dist/cjs/utils/refunds.js +98 -0
  31. package/dist/cjs/utils/rewards.d.ts +18 -0
  32. package/dist/cjs/utils/rewards.js +69 -0
  33. package/dist/cjs/utils/version.d.ts +4 -0
  34. package/dist/cjs/utils/version.js +38 -0
  35. package/dist/esm/index.d.ts +1 -0
  36. package/dist/esm/index.js +1 -0
  37. package/dist/esm/utils/cards.d.ts +20 -0
  38. package/dist/esm/utils/cards.js +144 -0
  39. package/dist/esm/utils/cart.d.ts +422 -0
  40. package/dist/esm/utils/cart.js +805 -0
  41. package/dist/esm/utils/checkout.d.ts +23 -0
  42. package/dist/esm/utils/checkout.js +100 -0
  43. package/dist/esm/utils/constants.d.ts +52 -0
  44. package/dist/esm/utils/constants.js +55 -0
  45. package/dist/esm/utils/customer.d.ts +3 -0
  46. package/dist/esm/utils/customer.js +18 -0
  47. package/dist/esm/utils/datetimes.d.ts +174 -0
  48. package/dist/esm/utils/datetimes.js +631 -0
  49. package/dist/esm/utils/errors.d.ts +48 -0
  50. package/dist/esm/utils/errors.js +135 -0
  51. package/dist/esm/utils/helpers.d.ts +30 -0
  52. package/dist/esm/utils/helpers.js +117 -0
  53. package/dist/esm/utils/index.d.ts +14 -0
  54. package/dist/esm/utils/index.js +14 -0
  55. package/dist/esm/utils/keypad.d.ts +9 -0
  56. package/dist/esm/utils/keypad.js +97 -0
  57. package/dist/esm/utils/maps.d.ts +264 -0
  58. package/dist/esm/utils/maps.js +269 -0
  59. package/dist/esm/utils/orders.d.ts +40 -0
  60. package/dist/esm/utils/orders.js +171 -0
  61. package/dist/esm/utils/refunds.d.ts +35 -0
  62. package/dist/esm/utils/refunds.js +90 -0
  63. package/dist/esm/utils/rewards.d.ts +18 -0
  64. package/dist/esm/utils/rewards.js +63 -0
  65. package/dist/esm/utils/version.d.ts +4 -0
  66. package/dist/esm/utils/version.js +34 -0
  67. package/package.json +65 -0
@@ -0,0 +1,269 @@
1
+ import { MAX_DISTANCE } from './constants';
2
+ import { isString } from './helpers';
3
+ export var GooglePlaceBusinessStatus;
4
+ (function (GooglePlaceBusinessStatus) {
5
+ GooglePlaceBusinessStatus["OPERATIONAL"] = "OPERATIONAL";
6
+ GooglePlaceBusinessStatus["CLOSED_TEMPORARILY"] = "CLOSED_TEMPORARILY";
7
+ GooglePlaceBusinessStatus["CLOSED_PERMANENTLY"] = "CLOSED_PERMANENTLY";
8
+ })(GooglePlaceBusinessStatus || (GooglePlaceBusinessStatus = {}));
9
+ // seee example Google Maps Place at bottom of file
10
+ const makeComponents = (components) => {
11
+ return components.reduce((obj, i) => {
12
+ return Object.assign(Object.assign({}, obj), { [i.types[0]]: { long_name: i.long_name, short_name: i.short_name } });
13
+ }, {});
14
+ };
15
+ export const makeAddress = (place) => {
16
+ const { address_components, formatted_address, geometry } = place;
17
+ const components = makeComponents(address_components);
18
+ const { street_number, route, locality: city, sublocality_level_1: subcity, administrative_area_level_1: state, postal_code: postalCode } = components;
19
+ const streetNumber = street_number ? street_number.short_name : '';
20
+ const street = route ? route.long_name : '';
21
+ const addressCity = city || subcity;
22
+ return {
23
+ street: `${streetNumber} ${street}`.trim(),
24
+ city: addressCity ? addressCity.long_name : '',
25
+ state: state ? state.short_name : '',
26
+ postal_code: postalCode ? postalCode.short_name : '',
27
+ lat: geometry.location.lat || null,
28
+ lng: geometry.location.lng || null,
29
+ formatted_address
30
+ };
31
+ };
32
+ const RADIUS_MILES = 3959; // radius of the earth in miles
33
+ const RADIUS_KM = 6371; // radius of the earth in kilometers
34
+ // https://stackoverflow.com/questions/18883601/function-to-calculate-distance-between-two-coordinates
35
+ export const getDistance = (pointA, pointB, inMiles = true) => {
36
+ const { lat: lat1, lng: lng1 } = pointA;
37
+ const { lat: lat2, lng: lng2 } = pointB;
38
+ const R = inMiles ? RADIUS_MILES : RADIUS_KM;
39
+ const dLat = deg2rad(lat2 - lat1); // see deg2rad below
40
+ const dLng = deg2rad(lng2 - lng1);
41
+ const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
42
+ Math.cos(deg2rad(lat1)) *
43
+ Math.cos(deg2rad(lat2)) *
44
+ Math.sin(dLng / 2) *
45
+ Math.sin(dLng / 2);
46
+ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
47
+ const d = R * c;
48
+ return d;
49
+ };
50
+ const deg2rad = (deg) => {
51
+ return deg * (Math.PI / 180);
52
+ };
53
+ // ray-casting algorithm based on
54
+ // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
55
+ const pointInPolygon = (point, vs) => {
56
+ const x = point[0], y = point[1];
57
+ let inside = false;
58
+ for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
59
+ const xi = vs[i][0], yi = vs[i][1];
60
+ const xj = vs[j][0], yj = vs[j][1];
61
+ const intersect = yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
62
+ if (intersect)
63
+ inside = !inside;
64
+ }
65
+ return inside;
66
+ };
67
+ export const inZone = (address, polygon) => {
68
+ const point = [address.lat, address.lng];
69
+ return pointInPolygon(point, polygon);
70
+ };
71
+ export const addDistance = (revenueCenters, address) => {
72
+ if (!address)
73
+ return revenueCenters;
74
+ const withDistance = revenueCenters.map(i => {
75
+ const latLng = i.address.lat
76
+ ? { lat: i.address.lat, lng: i.address.lng }
77
+ : null;
78
+ i.distance = latLng ? getDistance(address, latLng) : 1000;
79
+ const { coordinates, priority } = i.delivery_zone;
80
+ i.inZone = coordinates ? inZone(address, coordinates) : false;
81
+ i.priority = priority;
82
+ return i;
83
+ });
84
+ return withDistance;
85
+ };
86
+ export const sortRevenueCenters = (revenueCenters, isDelivery = false) => {
87
+ if (!isDelivery) {
88
+ return [...revenueCenters].sort((a, b) => { var _a, _b; return ((_a = a.distance) !== null && _a !== void 0 ? _a : 0) - ((_b = b.distance) !== null && _b !== void 0 ? _b : 0); });
89
+ }
90
+ const inZoneWithPriority = revenueCenters
91
+ .filter(i => i.inZone && i.priority)
92
+ .sort((a, b) => { var _a, _b; return ((_a = a.priority) !== null && _a !== void 0 ? _a : 0) - ((_b = b.priority) !== null && _b !== void 0 ? _b : 0); });
93
+ const inZoneWithoutPriority = revenueCenters
94
+ .filter(i => i.inZone && !i.priority)
95
+ .sort((a, b) => { var _a, _b; return ((_a = a.distance) !== null && _a !== void 0 ? _a : 0) - ((_b = b.distance) !== null && _b !== void 0 ? _b : 0); });
96
+ const outOfZone = revenueCenters
97
+ .filter(i => !i.inZone)
98
+ .sort((a, b) => { var _a, _b; return ((_a = a.distance) !== null && _a !== void 0 ? _a : 0) - ((_b = b.distance) !== null && _b !== void 0 ? _b : 0); });
99
+ return [...inZoneWithPriority, ...inZoneWithoutPriority, ...outOfZone];
100
+ };
101
+ export const calcMinDistance = (revenueCenters, maxDistance = MAX_DISTANCE) => {
102
+ const withDistance = revenueCenters
103
+ .filter(i => i.distance !== null && i.distance !== undefined)
104
+ .map(i => { var _a; return (_a = i.distance) !== null && _a !== void 0 ? _a : 0; });
105
+ return withDistance ? Math.min(...withDistance) : maxDistance;
106
+ };
107
+ export const checkServiceType = (revenueCenter, serviceType) => {
108
+ const { settings, service_types } = revenueCenter;
109
+ const serviceTypes = settings ? settings.service_types : service_types;
110
+ if (!serviceTypes)
111
+ return false;
112
+ return serviceTypes.includes(serviceType);
113
+ };
114
+ export const makePickupRevenueCenters = (revenueCenters, maxDistance = MAX_DISTANCE) => {
115
+ const hasPickup = revenueCenters
116
+ .filter(i => checkServiceType(i, 'PICKUP'))
117
+ .filter(i => !i.distance || i.distance < maxDistance);
118
+ return sortRevenueCenters(hasPickup);
119
+ };
120
+ export const makeWalkinRevenueCenters = (revenueCenters, maxDistance = MAX_DISTANCE) => {
121
+ const hasWalkin = revenueCenters
122
+ .filter(i => checkServiceType(i, 'WALKIN'))
123
+ .filter(i => !i.distance || i.distance < maxDistance);
124
+ return sortRevenueCenters(hasWalkin);
125
+ };
126
+ export const makeDeliveryRevenueCenters = (revenueCenters) => {
127
+ const hasDelivery = revenueCenters.filter(i => checkServiceType(i, 'DELIVERY'));
128
+ const sorted = sortRevenueCenters(hasDelivery, true);
129
+ return sorted.filter(i => i.inZone);
130
+ };
131
+ export const makePickupMesssaging = (address, geoLatLng, count, minDistance, maxDistance = MAX_DISTANCE, messages = LOCATIONS_MESSAGES) => {
132
+ if (address) {
133
+ if (minDistance >= maxDistance) {
134
+ return messages.PICKUP.addressFar;
135
+ }
136
+ else {
137
+ return {
138
+ title: `${count} ${messages.PICKUP.address.title}`,
139
+ msg: messages.PICKUP.address.msg
140
+ };
141
+ }
142
+ }
143
+ else if (geoLatLng) {
144
+ if (minDistance >= maxDistance) {
145
+ return messages.PICKUP.geoFar;
146
+ }
147
+ else {
148
+ return {
149
+ title: `${count} ${messages.PICKUP.geo.title}`,
150
+ msg: messages.PICKUP.geo.msg
151
+ };
152
+ }
153
+ }
154
+ else {
155
+ return messages.PICKUP.default;
156
+ }
157
+ };
158
+ export const makeWalkinMessaging = (address, geoLatLng, count, minDistance, maxDistance = MAX_DISTANCE, messages = LOCATIONS_MESSAGES) => {
159
+ const { title, msg } = makePickupMesssaging(address, geoLatLng, count, minDistance, maxDistance, messages);
160
+ return {
161
+ title: title.replace('pickup', 'dine-in'),
162
+ msg: msg.replace('pickup', 'dine-in')
163
+ };
164
+ };
165
+ export const makeDeliveryMesssaging = (address, count, messages = LOCATIONS_MESSAGES) => {
166
+ if (!address) {
167
+ return messages.DELIVERY.default;
168
+ }
169
+ else if (!address.street) {
170
+ return messages.DELIVERY.noStreet;
171
+ }
172
+ else {
173
+ if (count) {
174
+ const locationMsg = count > 1 ? 'locations deliver' : 'location delivers';
175
+ return {
176
+ title: messages.DELIVERY.hasDelivery.title,
177
+ msg: `${count} ${locationMsg} to your address.`,
178
+ error: null
179
+ };
180
+ }
181
+ else {
182
+ return messages.DELIVERY.noDelivery;
183
+ }
184
+ }
185
+ };
186
+ export const makeDisplayedRevenueCenters = (revenueCenters, serviceType, address, geoLatLng, maxDistance) => {
187
+ if (serviceType === 'WALKIN') {
188
+ const displayed = makeWalkinRevenueCenters(revenueCenters, maxDistance);
189
+ const minDistance = calcMinDistance(displayed);
190
+ const count = displayed.length;
191
+ const { title, msg } = makeWalkinMessaging(address, geoLatLng, count, minDistance, maxDistance);
192
+ const error = null;
193
+ return { title, msg, error, displayed };
194
+ }
195
+ else if (serviceType === 'PICKUP') {
196
+ const displayed = makePickupRevenueCenters(revenueCenters, maxDistance);
197
+ const minDistance = calcMinDistance(displayed);
198
+ const count = displayed.length;
199
+ const { title, msg } = makePickupMesssaging(address, geoLatLng, count, minDistance, maxDistance);
200
+ const error = null;
201
+ return { title, msg, error, displayed };
202
+ }
203
+ else {
204
+ const displayed = makeDeliveryRevenueCenters(revenueCenters);
205
+ const count = displayed.length;
206
+ const { title, msg, error } = makeDeliveryMesssaging(address, count);
207
+ return { title, msg, error, displayed };
208
+ }
209
+ };
210
+ export const LOCATIONS_MESSAGES = {
211
+ PICKUP: {
212
+ default: {
213
+ title: 'Please choose a location',
214
+ msg: 'Or enter a zip code to find the location nearest you.'
215
+ },
216
+ address: {
217
+ title: 'locations near you',
218
+ msg: 'Please choose a location below.'
219
+ },
220
+ addressFar: {
221
+ title: "Looks like we don't have any locations that offer pickup in your area",
222
+ msg: 'Sorry about that. Please enter a different address or head back and choose a different order type.'
223
+ },
224
+ geo: {
225
+ title: 'locations in your area',
226
+ msg: 'Please enter an address or zip code for a more accurate result.'
227
+ },
228
+ geoFar: {
229
+ title: "Looks like we don't have any locations that offer pickup in your area",
230
+ msg: 'Please enter an address or zip code if you live in a different area.'
231
+ }
232
+ },
233
+ DELIVERY: {
234
+ default: {
235
+ title: "Let's find the nearest location",
236
+ msg: 'Please enter your address.',
237
+ error: null
238
+ },
239
+ noStreet: {
240
+ title: 'Please enter a street address',
241
+ msg: '',
242
+ error: 'A full address with street number is required for delivery orders.'
243
+ },
244
+ hasDelivery: {
245
+ title: 'Delivery is available!',
246
+ msg: 'Please choose a location below.',
247
+ error: null
248
+ },
249
+ noDelivery: {
250
+ title: "Delivery isn't available in your area at this time",
251
+ msg: "We're really sorry about that. Please enter a different address or head back and start a pickup order.",
252
+ error: null
253
+ }
254
+ }
255
+ };
256
+ export const renameLocation = (str, names) => {
257
+ if (!isString(str))
258
+ return str;
259
+ const [singular, plural] = names;
260
+ return str
261
+ .replace('1 locations', '1 location')
262
+ .replace('locations', plural)
263
+ .replace('location', singular)
264
+ .replace(' a a', ' an a')
265
+ .replace(' a e', ' an e')
266
+ .replace(' a i', ' an i')
267
+ .replace(' a o', ' an o')
268
+ .replace(' a u', ' an u');
269
+ };
@@ -0,0 +1,40 @@
1
+ import { Cart, ItemTypes, OrderChannel, OrderCounts, Orders, PrepStatus, Store, TenderType, Ticket, TimezonePython, Timezone } from '@open-tender/types';
2
+ export declare const makeChannelName: (channel: OrderChannel) => string | null | undefined;
3
+ export declare const makeTenderTypeName: (tenderType: TenderType) => string;
4
+ export declare const notDone: (prep_status: PrepStatus) => boolean;
5
+ export declare const isDone: (prep_status: PrepStatus) => boolean;
6
+ export declare const notCompleted: (prep_status: PrepStatus) => boolean;
7
+ export declare const isCompleted: (prep_status: PrepStatus) => boolean;
8
+ export declare const makeOrdersCounts: (itemTypes?: ItemTypes, orders?: Orders) => {
9
+ current: {
10
+ Orders: number;
11
+ };
12
+ future: {
13
+ Orders: number;
14
+ };
15
+ qa: {
16
+ Orders: number;
17
+ };
18
+ };
19
+ export declare const makeOrderBuckets: (orders: Orders, tz: TimezonePython | Timezone, prepStates?: Array<PrepStatus>) => {
20
+ orders: import("@open-tender/types").Order[];
21
+ start: number | Date;
22
+ end: number | Date;
23
+ }[];
24
+ export declare const makeOrderBucketsCounts: (itemTypes?: ItemTypes, orders?: Orders) => {
25
+ Orders: number;
26
+ };
27
+ export declare const makeTicketCounts: (tickets: Array<Ticket>) => any;
28
+ export declare const makeCartLookup: (cart: Cart) => {};
29
+ export declare const makeItemTypeSettings: (itemType: Ticket) => {
30
+ is_default: boolean;
31
+ is_grouped: boolean;
32
+ is_hidden_assembly: boolean;
33
+ is_hidden_qa: boolean;
34
+ print_on_completed: boolean;
35
+ };
36
+ export declare const makeItemTypesMap: (itemTypes: Array<Ticket>) => {};
37
+ export declare const makeTicketGroups: (tickets: Array<Ticket>, cart: Cart, itemTypes: Array<Ticket>, isAssembly: boolean) => unknown[];
38
+ export declare const makeDisplayCounts: (counts: OrderCounts) => any[] | "";
39
+ export declare const displayCounts: (counts: OrderCounts) => string;
40
+ export declare const makeColor: (settings: Store, minutes: number) => string;
@@ -0,0 +1,171 @@
1
+ import { prepStatus, tenderTypeNamesMap } from './constants';
2
+ import { capitalize } from './helpers';
3
+ import { timezoneMap, isoToDate, currentLocalDate, makeIntervals } from './datetimes';
4
+ export const makeChannelName = (channel) => {
5
+ switch (channel.type) {
6
+ case 'ONLINE':
7
+ return 'Web';
8
+ case 'APP':
9
+ return 'App';
10
+ case 'PORTAL':
11
+ return channel.ext_name;
12
+ default:
13
+ return channel.type;
14
+ }
15
+ };
16
+ export const makeTenderTypeName = (tenderType) => {
17
+ const name = tenderTypeNamesMap[tenderType];
18
+ return name || capitalize(tenderType.replace('_', ' '));
19
+ };
20
+ export const notDone = (prep_status) => {
21
+ const notDoneStates = [prepStatus.TODO, prepStatus.IN_PROGRESS];
22
+ return notDoneStates.includes(prep_status);
23
+ };
24
+ export const isDone = (prep_status) => {
25
+ const doneStates = [
26
+ prepStatus.DONE,
27
+ prepStatus.COMPLETED,
28
+ prepStatus.FULFILLED
29
+ ];
30
+ return doneStates.includes(prep_status);
31
+ };
32
+ export const notCompleted = (prep_status) => {
33
+ const notCompletedStates = [
34
+ prepStatus.TODO,
35
+ prepStatus.IN_PROGRESS,
36
+ prepStatus.DONE
37
+ ];
38
+ return notCompletedStates.includes(prep_status);
39
+ };
40
+ export const isCompleted = (prep_status) => {
41
+ const completeStates = [prepStatus.COMPLETED, prepStatus.FULFILLED];
42
+ return completeStates.includes(prep_status);
43
+ };
44
+ const updateCounts = (counts, ticketCounts) => {
45
+ // console.log('BEFORE', counts, ticketCounts)
46
+ const updated = Object.entries(counts).reduce((obj, [key, count]) => {
47
+ return Object.assign(Object.assign({}, obj), { [key]: count + (ticketCounts[key] || 0) });
48
+ }, {});
49
+ updated.Orders = counts.Orders + 1;
50
+ // console.log('AFTER', updated)
51
+ return updated;
52
+ };
53
+ export const makeOrdersCounts = (itemTypes = [], orders = []) => {
54
+ const itemTypeCounts = itemTypes
55
+ .filter(i => i.is_default)
56
+ .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: 0 })), {});
57
+ let current = Object.assign({ Orders: 0 }, itemTypeCounts);
58
+ let qa = Object.assign({ Orders: 0 }, itemTypeCounts);
59
+ let future = Object.assign({ Orders: 0 }, itemTypeCounts);
60
+ const openOrders = orders.filter(i => notCompleted(i.prep_status));
61
+ openOrders.forEach(order => {
62
+ var _a;
63
+ const tz = timezoneMap[order.timezone];
64
+ const fireDate = isoToDate(order.fire_at, tz);
65
+ const currentDate = currentLocalDate(tz);
66
+ const isDone = order.prep_status === prepStatus.DONE;
67
+ const isCurrent = fireDate < currentDate;
68
+ const ticketCounts = (_a = order.tickets) === null || _a === void 0 ? void 0 : _a.reduce((obj, ticket) => {
69
+ const count = obj[ticket.item_type_name] || 0;
70
+ return Object.assign(Object.assign({}, obj), { [ticket.item_type_name]: count + ticket.item_nos.length });
71
+ }, {});
72
+ if (isDone) {
73
+ qa = updateCounts(qa, ticketCounts);
74
+ }
75
+ else if (isCurrent) {
76
+ current = updateCounts(current, ticketCounts);
77
+ }
78
+ else {
79
+ future = updateCounts(future, ticketCounts);
80
+ }
81
+ });
82
+ return { current, future, qa };
83
+ };
84
+ export const makeOrderBuckets = (orders, tz, prepStates = []) => {
85
+ const intervals = makeIntervals(tz);
86
+ const withOrders = intervals.map(i => {
87
+ const inBucket = orders.filter(order => {
88
+ const incomplete = prepStates.includes(order.prep_status);
89
+ const readyAt = isoToDate(order.ready_at, tz);
90
+ return incomplete && i.start < readyAt && readyAt <= i.end;
91
+ });
92
+ return Object.assign(Object.assign({}, i), { orders: inBucket });
93
+ });
94
+ return withOrders;
95
+ };
96
+ export const makeOrderBucketsCounts = (itemTypes = [], orders = []) => {
97
+ const itemTypeCounts = itemTypes
98
+ .filter(i => i.is_default)
99
+ .reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: 0 })), {});
100
+ let counts = Object.assign({ Orders: 0 }, itemTypeCounts);
101
+ orders.forEach(order => {
102
+ var _a;
103
+ const ticketCounts = (_a = order.tickets) === null || _a === void 0 ? void 0 : _a.reduce((obj, ticket) => {
104
+ const count = obj[ticket.item_type_name] || 0;
105
+ return Object.assign(Object.assign({}, obj), { [ticket.item_type_name]: count + ticket.item_nos.length });
106
+ }, {});
107
+ counts = updateCounts(counts, ticketCounts);
108
+ });
109
+ return counts;
110
+ };
111
+ export const makeTicketCounts = (tickets) => {
112
+ return tickets.reduce((obj, i) => {
113
+ const count = obj[i.item_type_id] || 0;
114
+ return Object.assign(Object.assign({}, obj), { [i.item_type_id]: count + i.item_nos.length });
115
+ }, {});
116
+ };
117
+ export const makeCartLookup = (cart) => {
118
+ return cart.reduce((obj, i) => (i.item_no ? Object.assign(Object.assign({}, obj), { [i.item_no]: i }) : Object.assign({}, obj)), {});
119
+ };
120
+ export const makeItemTypeSettings = (itemType) => {
121
+ const { is_default, is_grouped, is_hidden_assembly, is_hidden_qa, print_on_completed } = itemType || {};
122
+ return {
123
+ is_default,
124
+ is_grouped,
125
+ is_hidden_assembly,
126
+ is_hidden_qa,
127
+ print_on_completed
128
+ };
129
+ };
130
+ export const makeItemTypesMap = (itemTypes) => {
131
+ return itemTypes
132
+ ? itemTypes.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.item_type_id]: i })), {})
133
+ : {};
134
+ };
135
+ export const makeTicketGroups = (tickets, cart, itemTypes, isAssembly) => {
136
+ const lookup = makeCartLookup(cart);
137
+ const grouped = tickets.reduce((obj, i) => {
138
+ const items = i.item_nos.map(n => lookup[n]);
139
+ const settings = makeItemTypeSettings(itemTypes[i.item_type_id]);
140
+ const hideTicket = isAssembly && settings.is_hidden_assembly;
141
+ const ticket = Object.assign(Object.assign(Object.assign({}, i), { items }), settings);
142
+ const group = obj[i.item_type_id] || [];
143
+ const newGroup = hideTicket ? group : [...group, ticket];
144
+ return Object.assign(Object.assign({}, obj), { [i.item_type_id]: newGroup });
145
+ }, {});
146
+ return Object.values(grouped);
147
+ };
148
+ export const makeDisplayCounts = (counts) => {
149
+ if (!counts)
150
+ return '';
151
+ return Object.entries(counts).map(([, value]) => value);
152
+ };
153
+ export const displayCounts = (counts) => {
154
+ if (!counts)
155
+ return '';
156
+ const countStr = Object.entries(counts)
157
+ // .map(([key, value]) => `${value}-${key.charAt(0)}`)
158
+ .map(([, value]) => `${value}`)
159
+ .join('/');
160
+ return ` (${countStr})`;
161
+ };
162
+ export const makeColor = (settings, minutes) => {
163
+ const { warning_minutes, alert_minutes } = settings || {};
164
+ if (!warning_minutes && !alert_minutes)
165
+ return '';
166
+ return minutes < warning_minutes
167
+ ? 'warning'
168
+ : minutes < alert_minutes
169
+ ? 'alert'
170
+ : 'text';
171
+ };
@@ -0,0 +1,35 @@
1
+ import { Order, OrderCart } from '@open-tender/types';
2
+ export declare const makeRefundCartLookup: (cart: OrderCart) => {
3
+ [key: number]: {
4
+ quantity?: number | undefined;
5
+ price_total?: `${number}.${number}` | undefined;
6
+ price?: string | undefined;
7
+ };
8
+ };
9
+ export declare const makeAmountsLookup: (items: Array<any>, key?: string) => any;
10
+ export declare const makeRefundLookup: (order: Order) => {
11
+ cart: {
12
+ [key: number]: {
13
+ quantity?: number | undefined;
14
+ price_total?: `${number}.${number}` | undefined;
15
+ price?: string | undefined;
16
+ };
17
+ };
18
+ gift_cards: any;
19
+ surcharges: any;
20
+ discounts: any;
21
+ taxes: any;
22
+ tenders: any;
23
+ subtotal: `${number}.${number}`;
24
+ gift_card: `${number}.${number}`;
25
+ surcharge: `${number}.${number}`;
26
+ discount: `-${number}.${number}`;
27
+ tax: `${number}.${number}`;
28
+ tip: `${number}.${number}`;
29
+ shipping: `${number}.${number}`;
30
+ total: `${number}.${number}`;
31
+ };
32
+ export declare const adjustAmounts: (order: Record<string, any>, lookup: {
33
+ [key: string]: any;
34
+ }, list: string, key?: string) => Record<string, any>;
35
+ export declare const makeNetOrder: (order: Order, refunds: Array<Order>) => any;
@@ -0,0 +1,90 @@
1
+ export const makeRefundCartLookup = (cart) => {
2
+ const lookup = {};
3
+ cart.forEach(item => {
4
+ var _a;
5
+ const { line_no, quantity, price_total } = item;
6
+ lookup[line_no !== null && line_no !== void 0 ? line_no : 0] = { quantity, price_total };
7
+ (_a = item === null || item === void 0 ? void 0 : item.groups) === null || _a === void 0 ? void 0 : _a.forEach(group => group.options.forEach(option => {
8
+ const { line_no, quantity, price } = option;
9
+ lookup[line_no !== null && line_no !== void 0 ? line_no : 0] = { quantity, price };
10
+ }));
11
+ });
12
+ return lookup;
13
+ };
14
+ export const makeAmountsLookup = (items, key = 'id') => {
15
+ return items.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i[key]]: i.amount })), {});
16
+ };
17
+ export const makeRefundLookup = (order) => {
18
+ const { cart, gift_cards, surcharges, discounts, taxes, totals, tenders } = order;
19
+ const { subtotal, gift_card, surcharge, discount, tax, tip, shipping, total } = totals;
20
+ return {
21
+ cart: makeRefundCartLookup(cart),
22
+ gift_cards: makeAmountsLookup(gift_cards, 'code'),
23
+ surcharges: makeAmountsLookup(surcharges),
24
+ discounts: makeAmountsLookup(discounts),
25
+ taxes: makeAmountsLookup(taxes),
26
+ tenders: makeAmountsLookup(tenders, 'tender_index'),
27
+ subtotal,
28
+ gift_card,
29
+ surcharge,
30
+ discount,
31
+ tax,
32
+ tip,
33
+ shipping,
34
+ total
35
+ };
36
+ };
37
+ const adjustAmount = (amount, refundAmount) => {
38
+ return (parseFloat(amount) + parseFloat(refundAmount)).toFixed(2);
39
+ };
40
+ export const adjustAmounts = (order, lookup, list, key = 'id') => {
41
+ if (!order[list])
42
+ return order;
43
+ order[list].forEach((i) => {
44
+ const amount = lookup[list][i[key]];
45
+ if (amount)
46
+ i.amount = adjustAmount(i.amount, amount);
47
+ });
48
+ return order;
49
+ };
50
+ const amounts = [
51
+ 'subtotal',
52
+ 'gift_card',
53
+ 'surcharge',
54
+ 'discount',
55
+ 'tax',
56
+ 'tip',
57
+ 'shipping',
58
+ 'total'
59
+ ];
60
+ export const makeNetOrder = (order, refunds) => {
61
+ if (!refunds || !refunds.length)
62
+ return order;
63
+ let copy = JSON.parse(JSON.stringify(order));
64
+ refunds.forEach(refund => {
65
+ const lookup = makeRefundLookup(refund);
66
+ copy.cart.forEach((i) => {
67
+ var _a, _b, _c, _d;
68
+ const item = lookup.cart[(_a = i.line_no) !== null && _a !== void 0 ? _a : 0];
69
+ if (item) {
70
+ i.quantity -= (_b = item.quantity) !== null && _b !== void 0 ? _b : 0;
71
+ i.price_total = adjustAmount(i.price_total, (_c = item.price_total) !== null && _c !== void 0 ? _c : '0.0');
72
+ }
73
+ if (i.quantity === 0) {
74
+ (_d = i.groups) === null || _d === void 0 ? void 0 : _d.forEach(group => group.options.forEach(option => {
75
+ option.quantity = 0;
76
+ option.price_total = '0.00';
77
+ }));
78
+ }
79
+ });
80
+ copy = adjustAmounts(copy, lookup, 'gift_cards', 'code');
81
+ copy = adjustAmounts(copy, lookup, 'surcharges');
82
+ copy = adjustAmounts(copy, lookup, 'discounts');
83
+ copy = adjustAmounts(copy, lookup, 'taxes');
84
+ copy = adjustAmounts(copy, lookup, 'tenders', 'tender_index');
85
+ amounts.forEach(i => {
86
+ copy.totals[i] = adjustAmount(copy.totals[i], lookup[i]);
87
+ });
88
+ });
89
+ return copy;
90
+ };
@@ -0,0 +1,18 @@
1
+ import { Deals, LoyaltyPoints, LoyaltyRedemption, LoyaltySpend, LoyaltyStatus, LoyaltyTier, LoyaltyType, OrderType, ServiceType } from '@open-tender/types';
2
+ export declare const makeValidDeals: (deals: Deals, orderType: OrderType, serviceType: ServiceType, revenueCenterId: number) => Deals;
3
+ export declare const makeStatus: (tiers: Array<LoyaltyTier>, status: LoyaltyStatus, points: LoyaltyPoints) => {
4
+ progress: number;
5
+ progressMsg: string;
6
+ tiers: {
7
+ points: LoyaltyPoints;
8
+ percentage: number;
9
+ color: string;
10
+ value: string;
11
+ description: string;
12
+ hex_code: string;
13
+ name: string;
14
+ threshold: string;
15
+ discount: import("@open-tender/types").LoyaltyDiscount;
16
+ }[];
17
+ } | null;
18
+ export declare const makeProgress: (loyalty_type: LoyaltyType, spend: LoyaltySpend, redemption: LoyaltyRedemption) => number | null;