LCOV - code coverage report
Current view: top level - packages/account-core/src/core/billing - paymentMethods.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 14.3 % 7 1
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * The saved payment methods read: the cards a workspace can charge, cached per
       3              :  * scope.
       4              :  *
       5              :  * Card details never reach this core beyond what the server already publishes —
       6              :  * brand, last four digits, and which one is the default — so nothing here has
       7              :  * to be redacted before a host renders it.
       8              :  */
       9              : import { zListSavedPaymentMethodsResponse } from '@comfyorg/ingest-types/zod'
      10              : import type { zSavedPaymentMethod } from '@comfyorg/ingest-types/zod'
      11              : import type { z } from 'zod'
      12              : 
      13              : import type { BillingResult, BillingTransport } from './billingContracts'
      14              : import type { BillingScope, BillingScopeSource } from './billingScope'
      15              : import { createScopedReader } from './scopedReader'
      16              : 
      17         2356 : export const PAYMENT_METHODS_ROUTE = '/billing/payment-methods'
      18              : 
      19              : export type SavedPaymentMethod = z.infer<typeof zSavedPaymentMethod>
      20              : 
      21              : export type PaymentMethodsScope = BillingScope
      22              : 
      23              : export interface PaymentMethodsSnapshot {
      24              :   readonly scope: PaymentMethodsScope
      25              :   readonly methods: readonly SavedPaymentMethod[]
      26              :   /** ms since epoch at which this read settled. */
      27              :   readonly readAt: number
      28              : }
      29              : 
      30              : export interface PaymentMethodsReadOptions {
      31              :   readonly signal?: AbortSignal
      32              :   /** Bounds the request, when this caller is the one that issues it. */
      33              :   readonly timeoutMs?: number
      34              : }
      35              : 
      36              : export interface PaymentMethodsReader {
      37              :   /**
      38              :    * Always requests. Concurrent callers for one scope share a single request.
      39              :    */
      40              :   read: (
      41              :     options?: PaymentMethodsReadOptions
      42              :   ) => Promise<BillingResult<PaymentMethodsSnapshot>>
      43              :   /** The last list published for the current scope. */
      44              :   getSnapshot: () => PaymentMethodsSnapshot | undefined
      45              :   /**
      46              :    * Drops the published list without a scope change. A payment-portal round
      47              :    * trip adds or removes a card outside this reader, so the host that sent the
      48              :    * user there is the only thing that knows the list it holds is now wrong.
      49              :    */
      50              :   invalidate: () => void
      51              :   dispose: () => void
      52              : }
      53              : 
      54              : export interface PaymentMethodsReaderOptions {
      55              :   readonly transport: BillingTransport
      56              :   /** Where the core learns which user, workspace, and role it runs as. */
      57              :   readonly scopeSource: BillingScopeSource
      58              :   readonly now?: () => number
      59              : }
      60              : 
      61            0 : export function createPaymentMethodsReader(
      62              :   options: PaymentMethodsReaderOptions
      63              : ): PaymentMethodsReader {
      64            0 :   const { transport, scopeSource, now = Date.now } = options
      65              : 
      66            0 :   const reader = createScopedReader<
      67              :     readonly SavedPaymentMethod[],
      68              :     PaymentMethodsSnapshot,
      69              :     PaymentMethodsReadOptions
      70              :   >({
      71              :     transport,
      72              :     scopeSource,
      73              :     route: PAYMENT_METHODS_ROUTE,
      74              :     parse: (body) => zListSavedPaymentMethodsResponse.safeParse(body),
      75              :     timeoutMs: (readOptions) => readOptions?.timeoutMs,
      76              :     project: ({ data }, scope) => ({
      77              :       status: 'ok',
      78              :       value: { scope, methods: data, readAt: now() }
      79              :     }),
      80              :     // A list resolved before the change the host is reporting may be served to
      81              :     // the callers already waiting on it, but must not be published.
      82              :     publish: (value, fenced) => (fenced ? undefined : value)
      83              :   })
      84              : 
      85            0 :   return {
      86              :     read: reader.read,
      87              :     getSnapshot: reader.getSnapshot,
      88              :     invalidate: () => {
      89            0 :       reader.setSnapshot(undefined)
      90            0 :       reader.fenceInFlight({ detach: true })
      91              :     },
      92              :     dispose: reader.dispose
      93              :   }
      94              : }
        

Generated by: LCOV version 2.0-1