Line data Source code
1 : import type { BillingBalance } from '@comfyorg/account-core/billing'
2 :
3 : import type { BillingBalanceResponse } from '@comfyorg/billing-core'
4 :
5 : import { asSafeNumber } from './safeInt64'
6 :
7 : interface TopupBound {
8 : cents?: number
9 : credits?: number
10 : }
11 :
12 0 : function projectTopupBound(
13 : rawCents: bigint | undefined,
14 : rawCredits: bigint | undefined
15 : ): TopupBound {
16 0 : if (rawCents === undefined) return {}
17 0 : const cents = asSafeNumber(rawCents)
18 0 : if (cents === undefined) return {}
19 0 : if (rawCredits === undefined) return { cents }
20 0 : const credits = asSafeNumber(rawCredits)
21 0 : return credits === undefined ? {} : { cents, credits }
22 : }
23 :
24 : /** The SDK's decoded balance in the host's shape, dropping a top-up bound a number cannot hold exactly. */
25 0 : export function projectBillingBalance({
26 : max_topup_cents,
27 : max_topup_credits,
28 : min_topup_cents,
29 : min_topup_credits,
30 : ...balance
31 : }: BillingBalance): BillingBalanceResponse {
32 0 : const max = projectTopupBound(max_topup_cents, max_topup_credits)
33 0 : const min = projectTopupBound(min_topup_cents, min_topup_credits)
34 0 : return {
35 : ...balance,
36 : ...(max.cents !== undefined && { max_topup_cents: max.cents }),
37 : ...(max.credits !== undefined && { max_topup_credits: max.credits }),
38 : ...(min.cents !== undefined && { min_topup_cents: min.cents }),
39 : ...(min.credits !== undefined && { min_topup_credits: min.credits })
40 : }
41 : }
|