LCOV - code coverage report
Current view: top level - src/platform/billing/subscription/components - CancelSubscriptionDialogContent.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 75.3 % 81 61
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1            4 : <template>
       2              :   <CancellationStepLayout
       3              :     :title="
       4              :       didCancelSucceed
       5              :         ? $t('subscription.cancelFlow.cancelled.title')
       6              :         : $t('subscription.cancelFlow.confirm.title')
       7              :     "
       8              :     :subtitle="didCancelSucceed ? description : undefined"
       9              :     :close-disabled="isClosingBlocked"
      10              :     :on-close="onClose"
      11              :   >
      12              :     <template v-if="!didCancelSucceed">
      13              :       <p class="m-0 text-sm/5 text-muted-foreground">{{ description }}</p>
      14              :       <p class="m-0 text-sm/5">
      15              :         {{
      16              :           $t('subscription.cancelFlow.confirm.loseAccess', {
      17              :             date: formattedEndDate
      18              :           })
      19              :         }}
      20              :       </p>
      21              :       <ul class="m-0 grid list-none grid-cols-2 gap-2 p-0">
      22              :         <li
      23              :           v-for="feature in lostFeatures"
      24              :           :key="feature.key"
      25              :           class="flex flex-col gap-1.5 rounded-[10px] border border-border/50 bg-secondary/50 p-3"
      26              :         >
      27              :           <i :class="cn(feature.icon, 'text-foreground')" aria-hidden="true" />
      28              :           <span class="text-sm">{{ feature.title }}</span>
      29              :           <span class="text-xs text-muted-foreground">
      30              :             {{ feature.description }}
      31              :           </span>
      32              :         </li>
      33              :       </ul>
      34              :     </template>
      35              : 
      36              :     <template #actions>
      37              :       <Button
      38              :         v-if="didCancelSucceed"
      39              :         variant="secondary"
      40              :         size="lg"
      41              :         class="w-24"
      42              :         @click="onClose"
      43              :       >
      44              :         {{ $t('subscription.cancelFlow.done') }}
      45              :       </Button>
      46              :       <template v-else>
      47              :         <Button
      48              :           variant="textonly"
      49              :           size="lg"
      50              :           :disabled="isLoading"
      51              :           @click="onClose"
      52              :         >
      53              :           {{ $t('subscription.cancelFlow.keepPlan') }}
      54              :         </Button>
      55              :         <Button
      56              :           variant="destructive"
      57              :           size="lg"
      58              :           :loading="isLoading"
      59              :           @click="onConfirmCancel"
      60              :         >
      61              :           {{ $t('subscription.cancelFlow.confirm.cancelPlan') }}
      62              :         </Button>
      63              :       </template>
      64              :     </template>
      65              :   </CancellationStepLayout>
      66              : </template>
      67              : 
      68              : <script setup lang="ts">
      69              : import { useToast } from '@/components/ui/toast/toastStore'
      70              : import { computed, onMounted, onUnmounted, ref } from 'vue'
      71              : import { useI18n } from 'vue-i18n'
      72              : 
      73              : import { cn } from '@comfyorg/tailwind-utils'
      74              : 
      75              : import Button from '@comfyorg/design-system/button/Button'
      76              : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
      77              : import { useBillingRouting } from '@/platform/billing/composables/useBillingRouting'
      78              : import CancellationStepLayout from '@/platform/billing/subscription/components/CancellationStepLayout.vue'
      79              : import { useCancellationPlan } from '@/platform/billing/subscription/composables/useCancellationPlan'
      80              : import {
      81              :   createCancelFlowReporter,
      82              :   getSubscriptionCancellationMetadata
      83              : } from '@/platform/billing/subscription/utils/subscriptionCancellationTelemetry'
      84              : import { isCloud } from '@/platform/distribution/types'
      85              : import { useTelemetry } from '@comfyorg/telemetry'
      86              : import { categorizeBillingApiError } from '@/platform/telemetry/utils/billingFailureCategory'
      87              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
      88              : import { useBillingUI } from '@/platform/billing/composables/useBillingUI'
      89              : import { useDialogStore } from '@/stores/dialogStore'
      90              : import { parseIsoDateSafe } from '@/utils/dateTimeUtil'
      91              : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
      92              : 
      93              : const {
      94              :   cancelAt,
      95              :   flowAlreadyOpened = false,
      96              :   flowAlreadyConfirmed = false,
      97              :   isScopeCurrent = () => true
      98              : } = defineProps<{
      99              :   cancelAt?: string
     100              :   flowAlreadyOpened?: boolean
     101              :   flowAlreadyConfirmed?: boolean
     102              :   isScopeCurrent?: () => boolean
     103              : }>()
     104              : 
     105            4 : const { t, n } = useI18n()
     106            4 : const dialogStore = useDialogStore()
     107            4 : const toast = useToast()
     108            4 : const { cancelSubscription, fetchStatus, subscription, tier } =
     109              :   useBillingContext()
     110            4 : const { shouldUseWorkspaceBilling } = useBillingRouting()
     111            4 : const { canCancel } = useBillingCapabilities()
     112            4 : const { permissions } = useBillingUI()
     113            4 : const { planName, creditGrant } = useCancellationPlan()
     114            4 : const telemetry = useTelemetry()
     115              : 
     116            4 : const isLoading = ref(false)
     117            4 : const didCancelSucceed = ref(false)
     118            4 : const didScopeAbort = ref(false)
     119            4 : const cancelReport = createCancelFlowReporter(
     120              :   telemetry,
     121              :   () => ({
     122              :     duration: subscription.value?.duration,
     123              :     tier: tier.value
     124              :   }),
     125              :   { confirmed: flowAlreadyConfirmed }
     126              : )
     127              : 
     128            0 : function cancellationMetadata() {
     129            0 :   return getSubscriptionCancellationMetadata({
     130              :     cancelAt,
     131              :     duration: subscription.value?.duration,
     132              :     endDate: subscription.value?.endDate,
     133              :     tier: tier.value
     134              :   })
     135              : }
     136              : 
     137            4 : onMounted(() => {
     138            4 :   if (flowAlreadyOpened) return
     139            4 :   telemetry?.trackSubscriptionCancellation(
     140              :     'flow_opened',
     141              :     cancellationMetadata()
     142              :   )
     143            4 :   cancelReport.intent()
     144              : })
     145              : 
     146            2 : function reportAbandoned() {
     147            2 :   telemetry?.trackSubscriptionCancellation('abandoned', cancellationMetadata())
     148            2 :   cancelReport.abandoned()
     149              : }
     150              : 
     151            4 : onUnmounted(() => {
     152            2 :   if (didCancelSucceed.value || didScopeAbort.value || isLoading.value) return
     153            2 :   reportAbandoned()
     154              : })
     155              : 
     156            4 : const formattedEndDate = computed(() => {
     157            4 :   const date = parseIsoDateSafe(
     158              :     cancelAt ?? subscription.value?.endDate ?? subscription.value?.renewalDate
     159              :   )
     160            4 :   if (!date) return t('subscription.cancelDialog.endOfBillingPeriod')
     161            1 :   return date.toLocaleDateString('en-US', {
     162              :     month: 'long',
     163              :     day: 'numeric',
     164              :     year: 'numeric'
     165              :   })
     166              : })
     167              : 
     168            4 : const description = computed(() =>
     169              :   t('subscription.cancelFlow.confirm.description', {
     170              :     plan: planName.value,
     171              :     date: formattedEndDate.value
     172              :   })
     173              : )
     174              : 
     175            4 : function creditsTitle() {
     176            4 :   const grant = creditGrant.value
     177            4 :   if (!grant)
     178            4 :     return t('subscription.cancelFlow.confirm.features.credits.generic')
     179            0 :   const named = { credits: n(grant.credits) }
     180            4 :   return grant.cycle === 'yearly'
     181              :     ? t('subscription.cancelFlow.confirm.features.credits.yearly', named)
     182              :     : t('subscription.cancelFlow.confirm.features.credits.monthly', named)
     183              : }
     184              : 
     185            4 : const lostFeatures = computed(() => {
     186            4 :   return [
     187              :     {
     188              :       key: 'gpus',
     189              :       icon: 'icon-[lucide--cpu]',
     190              :       title: t('subscription.cancelFlow.confirm.features.gpus.title'),
     191              :       description: t(
     192              :         'subscription.cancelFlow.confirm.features.gpus.description'
     193              :       )
     194              :     },
     195              :     {
     196              :       key: 'models',
     197              :       icon: 'icon-[lucide--layers]',
     198              :       title: t('subscription.cancelFlow.confirm.features.models.title'),
     199              :       description: t(
     200              :         'subscription.cancelFlow.confirm.features.models.description'
     201              :       )
     202              :     },
     203              :     {
     204              :       key: 'customNodes',
     205              :       icon: 'icon-[lucide--blocks]',
     206              :       title: t('subscription.cancelFlow.confirm.features.customNodes.title'),
     207              :       description: t(
     208              :         'subscription.cancelFlow.confirm.features.customNodes.description'
     209              :       )
     210              :     },
     211              :     {
     212              :       key: 'credits',
     213              :       icon: 'icon-[lucide--coins]',
     214              :       title: creditsTitle(),
     215              :       description: t(
     216              :         'subscription.cancelFlow.confirm.features.credits.description'
     217              :       )
     218              :     }
     219              :   ]
     220              : })
     221              : 
     222            4 : const isClosingBlocked = computed(
     223              :   () => isLoading.value && !didCancelSucceed.value
     224              : )
     225              : 
     226            1 : function onClose() {
     227            1 :   if (isClosingBlocked.value) return
     228            1 :   dialogStore.closeDialog({ key: 'cancel-subscription' })
     229              : }
     230              : 
     231            0 : function abortForScopeChange() {
     232            0 :   didScopeAbort.value = true
     233            0 :   toast.warning(t('subscription.cancelDialog.workspaceChanged'))
     234            0 :   dialogStore.closeDialog({ key: 'cancel-subscription' })
     235              : }
     236              : 
     237            1 : function lacksWorkspaceCancelPermission() {
     238            1 :   return (
     239              :     shouldUseWorkspaceBilling.value &&
     240              :     !(isCloud
     241              :       ? canCancel.value
     242              :       : permissions.value.canManageSubscriptionLifecycle)
     243              :   )
     244              : }
     245              : 
     246            1 : function reportCancelFailure(error: unknown, confirmedBeforeCall: boolean) {
     247            1 :   if (!confirmedBeforeCall) {
     248            1 :     telemetry?.trackSubscriptionCancellation('failed', cancellationMetadata())
     249            1 :     cancelReport.confirmed({ operationFollows: false })
     250            1 :     cancelReport.failed(categorizeBillingApiError(error))
     251              :   }
     252            1 :   toast.error(t('subscription.cancelDialog.failed'), {
     253              :     description: getErrorMessage(error) ?? t('g.unknownError')
     254              :   })
     255            1 :   isLoading.value = false
     256              : }
     257              : 
     258            0 : function reportWorkspaceConfirmed() {
     259            0 :   telemetry?.trackSubscriptionCancellation('confirmed', cancellationMetadata())
     260            0 :   cancelReport.confirmed({ operationFollows: true })
     261              : }
     262              : 
     263            0 : async function finishWorkspaceCancel() {
     264            0 :   didCancelSucceed.value = true
     265            0 :   try {
     266            0 :     await fetchStatus()
     267              :   } catch {
     268              :     // Cancellation already succeeded; stale local subscription status should not report failure.
     269              :   }
     270            0 :   isLoading.value = false
     271              : }
     272              : 
     273            1 : function handleCancelError(error: unknown, confirmedBeforeCall: boolean) {
     274            1 :   if (!isScopeCurrent()) return abortForScopeChange()
     275            1 :   reportCancelFailure(error, confirmedBeforeCall)
     276              : }
     277              : 
     278            0 : async function finishCancel(confirmedBeforeCall: boolean) {
     279            0 :   if (!isScopeCurrent()) return abortForScopeChange()
     280            0 :   if (!confirmedBeforeCall) reportWorkspaceConfirmed()
     281            0 :   await finishWorkspaceCancel()
     282              : }
     283              : 
     284            1 : async function onConfirmCancel() {
     285            1 :   if (!isScopeCurrent()) return abortForScopeChange()
     286            1 :   if (lacksWorkspaceCancelPermission()) return
     287              : 
     288            1 :   const confirmedBeforeCall = shouldUseWorkspaceBilling.value
     289            1 :   if (confirmedBeforeCall) reportWorkspaceConfirmed()
     290            1 :   isLoading.value = true
     291            1 :   try {
     292            1 :     await cancelSubscription(isScopeCurrent)
     293              :   } catch (error) {
     294            1 :     return handleCancelError(error, confirmedBeforeCall)
     295              :   }
     296            0 :   await finishCancel(confirmedBeforeCall)
     297              : }
     298              : </script>
        

Generated by: LCOV version 2.0-1