|
| 1 | +import type { |
| 2 | + BillingCredits, |
| 3 | + BillingCreditsJSON, |
| 4 | + BillingDiscounts, |
| 5 | + BillingDiscountsJSON, |
| 6 | + BillingMoneyAmount, |
| 7 | + BillingMoneyAmountJSON, |
| 8 | + BillingPeriodTotals, |
| 9 | + BillingPeriodTotalsJSON, |
| 10 | + BillingPerUnitTotal, |
| 11 | + BillingPerUnitTotalJSON, |
| 12 | + BillingSubscriptionItemSeats, |
| 13 | + BillingSubscriptionItemSeatsJSON, |
| 14 | + BillingTotals, |
| 15 | + BillingTotalsJSON, |
| 16 | +} from '@clerk/shared/types'; |
| 17 | + |
| 18 | +export const billingMoneyAmountFromJSON = (amount: BillingMoneyAmountJSON): BillingMoneyAmount => ({ |
| 19 | + amount: amount.amount, |
| 20 | + amountFormatted: amount.amount_formatted, |
| 21 | + currency: amount.currency, |
| 22 | + currencySymbol: amount.currency_symbol, |
| 23 | +}); |
| 24 | + |
| 25 | +export const billingPerUnitTotalsFromJSON = (perUnitTotals: BillingPerUnitTotalJSON[]): BillingPerUnitTotal[] => |
| 26 | + perUnitTotals.map(perUnitTotal => ({ |
| 27 | + name: perUnitTotal.name, |
| 28 | + blockSize: perUnitTotal.block_size, |
| 29 | + tiers: perUnitTotal.tiers.map(tier => ({ |
| 30 | + quantity: tier.quantity, |
| 31 | + feePerBlock: billingMoneyAmountFromJSON(tier.fee_per_block), |
| 32 | + total: billingMoneyAmountFromJSON(tier.total), |
| 33 | + })), |
| 34 | + })); |
| 35 | + |
| 36 | +const billingCreditsFromJSON = (credits: BillingCreditsJSON): BillingCredits => ({ |
| 37 | + proration: credits.proration |
| 38 | + ? { |
| 39 | + amount: billingMoneyAmountFromJSON(credits.proration.amount), |
| 40 | + cycleDaysRemaining: credits.proration.cycle_days_remaining, |
| 41 | + cycleDaysTotal: credits.proration.cycle_days_total, |
| 42 | + cycleRemainingPercent: credits.proration.cycle_remaining_percent, |
| 43 | + } |
| 44 | + : null, |
| 45 | + payer: credits.payer |
| 46 | + ? { |
| 47 | + remainingBalance: billingMoneyAmountFromJSON(credits.payer.remaining_balance), |
| 48 | + appliedAmount: billingMoneyAmountFromJSON(credits.payer.applied_amount), |
| 49 | + } |
| 50 | + : null, |
| 51 | + total: billingMoneyAmountFromJSON(credits.total), |
| 52 | +}); |
| 53 | + |
| 54 | +const billingDiscountsFromJSON = (discounts: BillingDiscountsJSON): BillingDiscounts => ({ |
| 55 | + proration: discounts.proration |
| 56 | + ? { |
| 57 | + amount: billingMoneyAmountFromJSON(discounts.proration.amount), |
| 58 | + cycleDaysPassed: discounts.proration.cycle_days_passed, |
| 59 | + cycleDaysTotal: discounts.proration.cycle_days_total, |
| 60 | + cyclePassedPercent: discounts.proration.cycle_passed_percent, |
| 61 | + } |
| 62 | + : null, |
| 63 | + total: billingMoneyAmountFromJSON(discounts.total), |
| 64 | +}); |
| 65 | + |
| 66 | +const billingPeriodTotalsFromJSON = (totals: BillingPeriodTotalsJSON): BillingPeriodTotals => ({ |
| 67 | + subtotal: billingMoneyAmountFromJSON(totals.subtotal), |
| 68 | + baseFee: billingMoneyAmountFromJSON(totals.base_fee), |
| 69 | + taxTotal: billingMoneyAmountFromJSON(totals.tax_total), |
| 70 | + grandTotal: billingMoneyAmountFromJSON(totals.grand_total), |
| 71 | + perUnitTotals: totals.per_unit_totals ? billingPerUnitTotalsFromJSON(totals.per_unit_totals) : undefined, |
| 72 | +}); |
| 73 | + |
| 74 | +export const billingTotalsFromJSON = (totals: BillingTotalsJSON): BillingTotals => ({ |
| 75 | + subtotal: billingMoneyAmountFromJSON(totals.subtotal), |
| 76 | + baseFee: totals.base_fee ? billingMoneyAmountFromJSON(totals.base_fee) : null, |
| 77 | + taxTotal: billingMoneyAmountFromJSON(totals.tax_total), |
| 78 | + grandTotal: billingMoneyAmountFromJSON(totals.grand_total), |
| 79 | + totalDueAfterFreeTrial: totals.total_due_after_free_trial |
| 80 | + ? billingMoneyAmountFromJSON(totals.total_due_after_free_trial) |
| 81 | + : totals.total_due_after_free_trial, |
| 82 | + credit: totals.credit ? billingMoneyAmountFromJSON(totals.credit) : totals.credit, |
| 83 | + credits: totals.credits ? billingCreditsFromJSON(totals.credits) : null, |
| 84 | + discounts: totals.discounts ? billingDiscountsFromJSON(totals.discounts) : null, |
| 85 | + pastDue: totals.past_due ? billingMoneyAmountFromJSON(totals.past_due) : totals.past_due, |
| 86 | + totalDueNow: totals.total_due_now ? billingMoneyAmountFromJSON(totals.total_due_now) : undefined, |
| 87 | + perUnitTotals: totals.per_unit_totals ? billingPerUnitTotalsFromJSON(totals.per_unit_totals) : undefined, |
| 88 | + totalsDuePerPeriod: totals.totals_due_per_period |
| 89 | + ? billingPeriodTotalsFromJSON(totals.totals_due_per_period) |
| 90 | + : undefined, |
| 91 | + totalDuePerPeriod: totals.total_due_per_period ? billingMoneyAmountFromJSON(totals.total_due_per_period) : undefined, |
| 92 | +}); |
| 93 | + |
| 94 | +export const billingSubscriptionItemSeatsFromJSON = ( |
| 95 | + seats: BillingSubscriptionItemSeatsJSON, |
| 96 | +): BillingSubscriptionItemSeats => ({ |
| 97 | + quantity: seats.quantity, |
| 98 | + tiers: seats.tiers |
| 99 | + ? seats.tiers.map(tier => ({ |
| 100 | + quantity: tier.quantity, |
| 101 | + feePerBlock: billingMoneyAmountFromJSON(tier.fee_per_block), |
| 102 | + total: billingMoneyAmountFromJSON(tier.total), |
| 103 | + })) |
| 104 | + : undefined, |
| 105 | +}); |
0 commit comments