LCOV - code coverage report
Current view: top level - src/platform/billing/composables - usePaymentRecoveryDialog.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 36.8 % 38 14
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computed, shallowRef } from 'vue'
       2              : 
       3              : import type { BillingPortalOutcome } from '@/platform/billing/composables/types'
       4              : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
       5              : import { useErrorHandling } from '@/composables/useErrorHandling'
       6              : import { reportError } from '@comfyorg/telemetry/reportError'
       7              : import SubscriptionPausedDialog from '@/platform/billing/components/SubscriptionPausedDialog.vue'
       8              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
       9              : import { useDialogService } from '@/services/dialogService'
      10              : import { useDialogStore } from '@/stores/dialogStore'
      11              : 
      12         2356 : const DIALOG_KEY = 'subscription-paused'
      13              : 
      14         2356 : const paymentPortalRequest = shallowRef<Promise<BillingPortalOutcome> | null>(
      15              :   null
      16              : )
      17              : 
      18              : /** Whether the server refused top-up because a subscription payment is past due. */
      19            4 : export function isTopUpDeniedPastDue(denial: string | undefined): boolean {
      20            4 :   return denial === 'payment_past_due'
      21              : }
      22              : 
      23              : /** Opens the Update payment dialog, offering the portal only with `can_manage_billing`. */
      24           23 : export function usePaymentRecoveryDialog() {
      25           23 :   const { manageSubscription } = useBillingContext()
      26           23 :   const {
      27              :     canManageBilling,
      28              :     paymentRecoveryAvailable,
      29              :     runWorkflowsDenial,
      30              :     refresh: refreshCapabilities
      31              :   } = useBillingCapabilities()
      32           23 :   const dialogService = useDialogService()
      33           23 :   const dialogStore = useDialogStore()
      34           23 :   const { toastErrorHandler } = useErrorHandling()
      35              : 
      36           23 :   const isUpdatingPayment = computed(() => paymentPortalRequest.value !== null)
      37           23 :   const runDeniedPaymentPastDue = computed(
      38              :     () => runWorkflowsDenial.value === 'payment_past_due'
      39              :   )
      40           23 :   const paymentRecoveryLock = computed<'owner' | 'member' | null>(() =>
      41              :     paymentRecoveryAvailable.value
      42              :       ? 'owner'
      43              :       : runDeniedPaymentPastDue.value
      44              :         ? 'member'
      45              :         : null
      46              :   )
      47              : 
      48            0 :   function closePaymentRecoveryDialog() {
      49            0 :     dialogStore.closeDialog({ key: DIALOG_KEY })
      50              :   }
      51              : 
      52            0 :   function showUpdatingPayment(isUpdatingPayment: boolean) {
      53            0 :     dialogStore.updateDialog({
      54              :       key: DIALOG_KEY,
      55              :       contentProps: { isUpdatingPayment }
      56              :     })
      57              :   }
      58              : 
      59            0 :   async function requestPaymentPortal(): Promise<BillingPortalOutcome> {
      60            0 :     try {
      61            0 :       return await manageSubscription()
      62              :     } catch (error) {
      63            0 :       reportError(error, {
      64              :         surface: 'billing',
      65              :         errorType: 'payment_recovery_portal_failure'
      66              :       })
      67            0 :       toastErrorHandler(error)
      68            0 :       return { ok: false, reason: 'failed' }
      69              :     }
      70              :   }
      71              : 
      72            0 :   async function openPaymentPortal(): Promise<BillingPortalOutcome> {
      73            0 :     showUpdatingPayment(true)
      74            0 :     const outcome = await requestPaymentPortal()
      75            0 :     showUpdatingPayment(false)
      76            0 :     if (outcome.ok) closePaymentRecoveryDialog()
      77            0 :     return outcome
      78              :   }
      79              : 
      80              :   /** Opens the billing portal once at a time; the dialog closes only when the portal opened. */
      81            0 :   function updatePayment(): Promise<BillingPortalOutcome> {
      82            0 :     paymentPortalRequest.value ??= openPaymentPortal().finally(() => {
      83            0 :       paymentPortalRequest.value = null
      84              :     })
      85            0 :     return paymentPortalRequest.value
      86              :   }
      87              : 
      88            0 :   async function updatePaymentFromDialog(onPortalOpened?: () => void) {
      89              :     const outcome = await updatePayment()
      90            0 :     if (outcome.ok) onPortalOpened?.()
      91              :   }
      92              : 
      93              :   /** `onPortalOpened` runs each time the billing portal opens from this dialog. */
      94            0 :   function showPaymentRecoveryDialog(onPortalOpened?: () => void) {
      95            0 :     return dialogService.showLayoutDialog({
      96              :       key: DIALOG_KEY,
      97              :       component: SubscriptionPausedDialog,
      98              :       props: {
      99              :         canManage: canManageBilling.value,
     100              :         status: 'payment_failed',
     101              :         isUpdatingPayment: isUpdatingPayment.value,
     102              :         onClose: closePaymentRecoveryDialog,
     103              :         onUpdatePayment: () => updatePaymentFromDialog(onPortalOpened)
     104              :       },
     105              :       dialogComponentProps: {
     106              :         headless: true
     107              :       }
     108              :     })
     109              :   }
     110              : 
     111           23 :   return {
     112              :     showPaymentRecoveryDialog,
     113              :     updatePayment,
     114              :     isUpdatingPayment,
     115              :     paymentRecoveryLock,
     116              :     refreshCapabilities
     117              :   }
     118              : }
        

Generated by: LCOV version 2.0-1