-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
542 lines (470 loc) · 16.7 KB
/
utils.ts
File metadata and controls
542 lines (470 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import { formatAmount, formatAmountFromString, formatPriceUnit } from '../money/formatters';
import { PricingModel } from '../prices/constants';
import { isVariablePriceItem } from '../prices/is-variable-price';
import { isCompositePrice } from '../prices/utils';
import { isTruthy } from '../shared/is-truthy';
import type {
Currency,
BillingPeriod,
CompositePrice,
CompositePriceItem,
Price,
PriceItem,
RecurrenceAmount,
Tax,
TierDetails,
I18n,
TFunction,
Coupon,
} from '../shared/types';
import { getDisplayTierByQuantity, getTierDescription } from '../tiers/utils';
import { normalizeTimeFrequency } from '../time-frequency/normalizers';
import type { TimeFrequency } from '../time-frequency/types';
import { RECURRENCE_ORDERING } from './constants';
import type { GetTieredUnitAmountOptions, PriceDisplayType, PriceItemWithParent } from './types';
export const EMPTY_VALUE_PLACEHOLDER = '---';
const TEMPORARY_TAX_MAPPER = {
standard: '19%',
reduced: '7%',
} as const;
type TaxRateName = keyof typeof TEMPORARY_TAX_MAPPER;
export const safeFormatAmount = ({
amount,
currency,
locale,
enableSubunitDisplay,
}: {
amount: number | string;
currency?: Currency;
locale?: string;
enableSubunitDisplay?: boolean;
}) => {
try {
if (!amount) {
return formatAmount({
amount: 0,
currency: currency,
locale,
enableSubunitDisplay,
});
}
if (typeof amount === 'string') {
return formatAmountFromString({
decimalAmount: amount,
currency: currency,
locale,
useRealPrecision: true,
enableSubunitDisplay,
});
}
return formatAmount({
amount: typeof amount === 'number' ? amount : 0,
currency: currency,
locale,
enableSubunitDisplay,
});
} catch (e) {
/**
* @todo Should cast to string,
* as most places where `safeFormatAmount` is used expect a string
*/
return amount;
}
};
const safeFormatAmountWithPrefix = ({
prefix,
...options
}: {
amount: number | string;
currency?: Currency;
locale?: string;
enableSubunitDisplay?: boolean;
} & { prefix?: string }) => {
const formattedValue = safeFormatAmount(options);
return prefix ? `${prefix} ${formattedValue}` : formattedValue;
};
export const computeRecurrenceAmounts = <
Recurrence extends Pick<RecurrenceAmount, 'amount_total' | 'amount_subtotal' | 'amount_tax'>,
>(
recurrence: Recurrence,
{ currency, locale }: { currency: Currency; locale: string },
) => {
const formatAmount = (amount: number) => safeFormatAmount({ amount, currency, locale });
return {
amount_total: formatAmount(recurrence.amount_total || 0),
...(!isNaN(Number(recurrence.amount_total)) && {
amount_total_decimal: String(recurrence.amount_total / 100),
}),
amount_subtotal: formatAmount(recurrence.amount_subtotal || 0),
...(!isNaN(Number(recurrence.amount_subtotal)) && {
amount_subtotal_decimal: String(recurrence.amount_subtotal / 100),
}),
amount_tax: formatAmount(recurrence.amount_tax || 0),
...(typeof recurrence.amount_tax !== 'undefined' &&
!isNaN(Number(recurrence.amount_tax)) && {
amount_tax_decimal: String(recurrence.amount_tax / 100),
}),
};
};
export const getRecurrenceInterval = (
recurrence: Pick<RecurrenceAmount, 'billing_period' | 'type'>,
): BillingPeriod | 'one-time' =>
recurrence.type === 'recurring' ? recurrence.billing_period! : (recurrence.type as 'one-time');
// valid line items must have _price snapshot and not have _position (not flattened)
export const withValidLineItem = (item: any) => !!item?.['_price'] && !item?.['_position'];
/**
* @todo Use String.padEnd instead
*/
export const fillPostSpaces = (value: string, fillLength: number) => {
if (value) {
const fillSpaces = fillLength - value.length;
for (let i = 0; i < fillSpaces; i++) {
value = value + ' ';
}
}
return value;
};
export const unitAmountApproved = (item: PriceItemWithParent): boolean => {
const itemDisplayType = getPriceDisplayType(item._price);
const parentDisplayType = getPriceDisplayType(item.parent_item?._price);
const hasNonHiddenPrice = !isHiddenPriceType(itemDisplayType);
const hasNonHiddenParentPrice = !isHiddenPriceType(parentDisplayType);
const isHiddenPriceApproved = item.on_request_approved || item.parent_item?.on_request_approved;
if (isCompositePrice(item)) {
const hasNonHiddenComponent = !findHiddenComponent(item);
return Boolean((hasNonHiddenComponent && hasNonHiddenPrice) || isHiddenPriceApproved);
} else {
return Boolean((hasNonHiddenPrice && hasNonHiddenParentPrice) || isHiddenPriceApproved);
}
};
export const getUnitAmount = (
item: PriceItemWithParent,
i18n: I18n,
{
isUnitAmountApproved,
useUnitAmountNet,
isDiscountCoupon,
isCashbackCoupon,
isItemContainingDiscountCoupon,
}: GetTieredUnitAmountOptions & {
isDiscountCoupon: boolean;
isCashbackCoupon: boolean;
isItemContainingDiscountCoupon: boolean;
},
) => {
if (item._price?.is_composite_price) {
return undefined;
}
if (
item._price?.pricing_model === PricingModel.tieredGraduated ||
item._price?.pricing_model === PricingModel.tieredVolume ||
item._price?.pricing_model === PricingModel.tieredFlatFee
) {
return getTieredUnitAmount(item, i18n, { isUnitAmountApproved, useUnitAmountNet });
}
if (item._price?.pricing_model === PricingModel.externalGetAG) {
return getGetAgUnitAmount(item as PriceItem, i18n, useUnitAmountNet);
}
let amount;
if (isDiscountCoupon) {
amount = useUnitAmountNet ? -item.unit_discount_amount_net! : -item.unit_discount_amount!;
} else if (isCashbackCoupon) {
return undefined;
} else if (isItemContainingDiscountCoupon) {
amount = useUnitAmountNet ? item.before_discount_unit_amount_net : item.before_discount_unit_amount;
} else {
amount = useUnitAmountNet ? item.unit_amount_net : item.unit_amount_decimal || item._price?.unit_amount_decimal;
}
return safeFormatAmount({
amount: amount || 0,
currency: item.currency as any,
locale: i18n.language,
});
};
const getTieredUnitAmount = (
item: PriceItemWithParent,
i18n: I18n,
{ isUnitAmountApproved, useUnitAmountNet }: GetTieredUnitAmountOptions,
) => {
if (!item._price?.tiers?.length) {
return EMPTY_VALUE_PLACEHOLDER;
}
const language = i18n.language;
const parentItem = item.parent_item;
const itemPriceMapping = (item.price_mappings || parentItem?.price_mappings)?.find(
(mapping) => mapping.price_id === item.price_id,
);
const { value: numberInput, frequency_unit: frequencyUnit } = itemPriceMapping || {};
const tax = useUnitAmountNet
? {
isInclusive: item.is_tax_inclusive ?? item._price?.is_tax_inclusive ?? true,
rate: item.taxes?.[0]?.tax?.rate || item.taxes?.[0]?.rateValue || 0,
}
: undefined;
/**
* @todo Instead of using normalizeTimeFrequency, use normalizeValueToFrequencyUnit
*/
const normalizedInput =
item._price?.pricing_model !== PricingModel.tieredGraduated &&
numberInput &&
normalizeTimeFrequency(
numberInput,
(frequencyUnit as TimeFrequency) || (item._price.billing_period as TimeFrequency),
item._price.billing_period as TimeFrequency,
);
// always return the first tier when using cumulative tiers since this is just used when displaying the masked "Starts At" price
const displayableTier = getDisplayTierByQuantity(
item._price.tiers,
item._price.pricing_model === PricingModel.tieredGraduated ? 1 : normalizedInput || item.quantity!,
item._price.pricing_model as PricingModel,
);
const descriptionUnit = '';
const descriptionCurrency = item.currency as Currency;
const descriptionTranslationFactory = (key: string) => i18n.t(`table_order.${key}`);
switch (item._price.pricing_model) {
case PricingModel.tieredGraduated: {
if (item._price.price_display_in_journeys !== 'show_as_starting_price') {
return '';
}
return getTierDescription(
PricingModel.tieredGraduated,
displayableTier,
descriptionUnit,
language,
descriptionCurrency,
descriptionTranslationFactory,
{ showStartsAt: false },
tax,
)?.split('/')[0]; // remove the unit part as it is not needed
}
case PricingModel.tieredVolume: {
return getTierDescription(
PricingModel.tieredVolume,
displayableTier,
descriptionUnit,
language,
descriptionCurrency,
descriptionTranslationFactory,
{ showStartsAt: false, showOnRequest: !isUnitAmountApproved },
tax,
)?.split('/')[0]; // remove the unit part as it is not needed
}
case PricingModel.tieredFlatFee: {
return getTierDescription(
PricingModel.tieredFlatFee,
displayableTier,
descriptionUnit,
language,
descriptionCurrency,
descriptionTranslationFactory,
{ showStartsAt: false },
tax,
)?.split('/')[0]; // remove the unit part as it is not needed
}
default:
return EMPTY_VALUE_PLACEHOLDER;
}
};
const getGetAgUnitAmount = (item: PriceItem, i18n: I18n, useUnitAmountNet: boolean) => {
const amount = useUnitAmountNet
? item.unit_amount_net || 0
: ((item.is_tax_inclusive ?? item._price?.is_tax_inclusive) ? item.unit_amount_gross : item.unit_amount_net) || 0;
if (!item._price?.is_composite_price) {
return safeFormatAmount({
amount,
currency: item.currency as any,
locale: i18n.language,
});
}
};
export const getSafeAmount = (value: unknown) =>
typeof value !== 'object' && typeof value !== 'undefined' ? value : undefined;
export const processRecurrences = (
item: any,
{ currency }: { currency?: Currency },
{ language: locale, t }: I18n,
prefix?: string,
) =>
RECURRENCE_ORDERING
/* Get recurrences in the right order */
.map((interval) =>
item.total_details?.breakdown?.recurrences?.find(
(item: PriceItem) => item.type == interval || (item.type === 'recurring' && item.billing_period === interval),
),
)
/* Filter out any which weren't matched */
.filter(isTruthy)
.map(({ amount_total = 0, amount_subtotal = 0, amount_tax = 0, billing_period, ...recurrence }) => ({
...recurrence,
amount_total: safeFormatAmountWithPrefix({ amount: amount_total, currency, locale, prefix }),
amount_subtotal: safeFormatAmountWithPrefix({ amount: amount_subtotal, currency, locale, prefix }),
amount_tax: safeFormatAmountWithPrefix({ amount: amount_tax, currency, locale, prefix }),
billing_period: billing_period ? t(`table_order.recurrences.billing_period.${billing_period}`) : '',
}));
export const getHiddenAmountString = (t: TFunction, displayInJourneys?: PriceDisplayType, value?: string | number) => {
const valueStr = (value === 0 ? '0' : value) ? ` ${value}` : '';
return !displayInJourneys
? EMPTY_VALUE_PLACEHOLDER
: displayInJourneys === 'show_as_starting_price'
? `${t(displayInJourneys, EMPTY_VALUE_PLACEHOLDER)}${valueStr}`
: t(displayInJourneys, EMPTY_VALUE_PLACEHOLDER);
};
export const getPriceDisplayInJourneys = (
priceItem: PriceItem | CompositePriceItem | (PriceItem & { parent_item: CompositePriceItem }),
): PriceDisplayType | undefined => {
const itemDisplayType = getPriceDisplayType(priceItem._price);
if (isHiddenPriceType(itemDisplayType)) {
return itemDisplayType;
}
if (isCompositePrice(priceItem)) {
return findHiddenComponentDisplayInJourney(priceItem);
} else {
const parentDisplayType = getPriceDisplayType((priceItem as PriceItemWithParent).parent_item?._price);
return isHiddenPriceType(parentDisplayType) ? parentDisplayType : itemDisplayType;
}
};
export const processTaxRecurrences = (
/**
* @todo Type narrowly
*/
item: any,
i18n: I18n,
) => {
const taxes: Tax[] = [];
for (let index = 0; index < item.total_details.breakdown.taxes.length; index++) {
taxes.push({
tax: getTaxRate(item.total_details.breakdown, i18n, index),
amount: safeFormatAmount({
amount: item.total_details.breakdown.taxes[index].amount || 0,
currency: item.currency,
locale: i18n.language,
}),
} as never);
}
return taxes;
};
export const getTaxRate = (
source: any,
i18n: I18n,
index = 0,
emptyTaxPlaceholder = i18n.t('table_order.no_tax', '(no tax)'),
) => {
const tax = source.taxes?.[index]?.tax;
if (typeof tax === 'object' && tax !== null) {
const rate = tax.rate;
const description = tax.description;
if (rate === null) {
return description || emptyTaxPlaceholder;
}
const mappedRate = TEMPORARY_TAX_MAPPER[rate as TaxRateName];
if (mappedRate) {
return mappedRate;
}
return rate !== undefined ? `${rate}%` : emptyTaxPlaceholder;
}
return emptyTaxPlaceholder;
};
export const getFormattedTieredDetails = (
tiersDetails: Array<TierDetails & { _position: number }> | undefined,
item: PriceItem,
isUnitAmountApproved: boolean,
locale: string,
) => {
if (
!Array.isArray(tiersDetails) ||
item._price?.pricing_model !== PricingModel.tieredGraduated ||
!isUnitAmountApproved
) {
return;
}
const currency = item._price.currency;
return (
tiersDetails.map((tierDetails) => {
const unit = formatPriceUnit(item._price?.unit, true);
return {
_position: tierDetails._position,
quantity: unit ? `${tierDetails.quantity} ${unit}` : tierDetails.quantity,
unit_amount: safeFormatAmount({
amount: tierDetails.unit_amount_decimal || 0,
currency: currency,
locale,
}),
unit_amount_net: safeFormatAmount({
amount: tierDetails.unit_amount_net || 0,
currency: currency,
locale,
}),
amount_total: safeFormatAmount({
amount: tierDetails.amount_total || 0,
currency: currency,
locale,
}),
amount_subtotal: safeFormatAmount({
amount: tierDetails.amount_subtotal || 0,
currency: currency,
locale,
}),
amount_tax: safeFormatAmount({
amount: tierDetails.amount_tax || 0,
currency: currency,
locale,
}),
};
}) ?? []
);
};
export const getQuantity = (item: PriceItem, parentItem?: PriceItem) => {
if (!parentItem && isVariablePriceItem(item)) {
const itemPriceMapping = item.price_mappings?.find((mapping) => mapping.price_id === item.price_id);
const quantity =
typeof itemPriceMapping?.value === 'number'
? `${itemPriceMapping?.value} ${formatPriceUnit(item?._price?.unit, true)}`
: EMPTY_VALUE_PLACEHOLDER;
return item.quantity == 1 ? quantity : `${item.quantity} x ${quantity}`;
}
if (parentItem && !isVariablePriceItem(item)) {
return parentItem.quantity == 1 ? `${item.quantity}` : `${parentItem.quantity} x ${item.quantity}`;
}
if (parentItem && isVariablePriceItem(item)) {
const itemPriceMapping = parentItem.price_mappings?.find((mapping) => mapping.price_id === item.price_id);
const quantity =
typeof itemPriceMapping?.value === 'number'
? `${itemPriceMapping?.value} ${formatPriceUnit(item._price?.unit, true)}`
: EMPTY_VALUE_PLACEHOLDER;
return parentItem.quantity == 1 ? quantity : `${parentItem.quantity} x ${quantity}`;
}
return `${item.quantity}`;
};
export const getDisplayUnit = (item: PriceItem) => {
if (
item._price?.pricing_model === PricingModel.tieredFlatFee ||
item._price?.price_display_in_journeys === 'show_as_on_request'
) {
return;
}
const unit = formatPriceUnit(item._price?.unit, true);
return unit ? `/${unit}` : '';
};
const getPriceDisplayType = (price?: Price | CompositePrice): PriceDisplayType | undefined =>
price?.price_display_in_journeys;
const isHiddenPriceType = (displayType?: PriceDisplayType): boolean =>
displayType === 'show_as_on_request' || displayType === 'show_as_starting_price';
const findHiddenComponent = (item: CompositePriceItem): PriceItem | undefined =>
item.item_components?.find((component) => isHiddenPriceType(getPriceDisplayType(component._price)));
const findHiddenComponentDisplayInJourney = (item: CompositePriceItem): PriceDisplayType | undefined => {
const hiddenComponent = findHiddenComponent(item);
return hiddenComponent ? getPriceDisplayType(hiddenComponent._price) : undefined;
};
export const getCouponItems = (item: PriceItem): (PriceItem & { coupon: Coupon })[] => {
const clonedItem = clone(item);
const couponItems = ((clonedItem._coupons as Array<Coupon> | undefined) ?? [])?.map<PriceItem & { coupon: Coupon }>(
(coupon) => ({ ...clonedItem, coupon }),
);
return couponItems;
};
export const clone = <T>(item: T): T => {
if (!item) {
return item;
}
return JSON.parse(JSON.stringify(item));
};