Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
379 changes: 96 additions & 283 deletions src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.test.ts

Large diffs are not rendered by default.

107 changes: 12 additions & 95 deletions src/components/actionbar/ComfyRunButton/CloudRunButtonWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,36 @@
!showsSubscribeToRunPrompt || paymentRecoveryLock || isSalesManagedPlan
"
:payment-recovery-lock="paymentRecoveryLock"
@payment-recovery-click="showPaymentRecoveryDialog"
@payment-recovery-click="showPaymentRecoveryDialog(refreshOnReturn)"
/>
<SubscribeToRunButton v-else />
</template>
<script setup lang="ts">
import { useEventListener } from '@vueuse/core'
import { computed, onUnmounted, ref } from 'vue'
import { computed } from 'vue'
import ComfyQueueButton from '@/components/actionbar/ComfyRunButton/ComfyQueueButton.vue'
import { useBillingContext } from '@/composables/billing/useBillingContext'
import { useErrorHandling } from '@/composables/useErrorHandling'
import { useFeatureFlags } from '@/composables/useFeatureFlags'
import SubscribeToRunButton from '@/platform/cloud/subscription/components/SubscribeToRun.vue'
import { isSalesManagedTier } from '@/platform/cloud/subscription/constants/tierPricing'
import SubscriptionPausedDialog from '@/platform/workspace/components/SubscriptionPausedDialog.vue'
import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'
import { useDialogService } from '@/services/dialogService'
import { useDialogStore } from '@/stores/dialogStore'
import { usePaymentRecoveryDialog } from '@/platform/workspace/composables/usePaymentRecoveryDialog'
const DIALOG_KEY = 'subscription-paused'
const {
showsSubscribeToRunPrompt,
billingStatus,
subscription,
manageSubscription,
fetchStatus,
fetchBalance
} = useBillingContext()
const { showsSubscribeToRunPrompt, subscription, fetchStatus, fetchBalance } =
useBillingContext()
const { paymentRecoveryLock, refreshCapabilities, showPaymentRecoveryDialog } =
usePaymentRecoveryDialog()
// A sales-managed plan has no self-serve checkout to upsell into; the run
// button stays a run button and lapsed access routes through sales.
const isSalesManagedPlan = computed(() =>
isSalesManagedTier(subscription.value?.tier)
)
const { flags } = useFeatureFlags()
const { permissions } = useWorkspaceUI()
const dialogService = useDialogService()
const dialogStore = useDialogStore()
const { toastErrorHandler } = useErrorHandling()
const isUpdatingPayment = ref(false)
let paymentPortalRequest: Promise<void> | null = null
let billingRefreshRequest: Promise<void> | null = null
let isUnmounted = false
let refreshBillingOnFocus = false
onUnmounted(() => {
isUnmounted = true
})
const paymentRecoveryLock = computed<'owner' | 'member' | null>(() =>
billingStatus.value === 'payment_failed' ||
(flags.v1PaymentRecovery && billingStatus.value === 'paused')
? permissions.value.canManageSubscription
? 'owner'
: 'member'
: null
)
const paymentRecoveryStatus = computed<'paused' | 'payment_failed'>(() =>
billingStatus.value === 'payment_failed' ? 'payment_failed' : 'paused'
)
function refreshOnReturn() {
refreshBillingOnFocus = true
}
function refreshStaleBillingState() {
if (
Expand All @@ -78,7 +47,8 @@ function refreshStaleBillingState() {
refreshBillingOnFocus = false
billingRefreshRequest = Promise.allSettled([
fetchStatus(),
fetchBalance()
fetchBalance(),
refreshCapabilities()
]).then(() => undefined)
void billingRefreshRequest.finally(() => {
billingRefreshRequest = null
Expand All @@ -89,57 +59,4 @@ useEventListener(window, 'focus', refreshStaleBillingState)
useEventListener(document, 'visibilitychange', () => {
if (document.visibilityState === 'visible') refreshStaleBillingState()
})
function closePaymentRecoveryDialog() {
dialogStore.closeDialog({ key: DIALOG_KEY })
}
function updatePayment(): Promise<void> {
if (paymentPortalRequest) return paymentPortalRequest
paymentPortalRequest = (async () => {
isUpdatingPayment.value = true
dialogStore.updateDialog({
key: DIALOG_KEY,
contentProps: { isUpdatingPayment: true }
})
try {
const outcome = await manageSubscription()
if (outcome.ok && !isUnmounted) {
refreshBillingOnFocus = true
closePaymentRecoveryDialog()
}
} catch (error) {
if (!isUnmounted) toastErrorHandler(error)
} finally {
isUpdatingPayment.value = false
paymentPortalRequest = null
if (!isUnmounted) {
dialogStore.updateDialog({
key: DIALOG_KEY,
contentProps: { isUpdatingPayment: false }
})
}
}
})()
return paymentPortalRequest
}
function showPaymentRecoveryDialog() {
dialogService.showLayoutDialog({
key: DIALOG_KEY,
component: SubscriptionPausedDialog,
props: {
canManage: paymentRecoveryLock.value === 'owner',
status: paymentRecoveryStatus.value,
isUpdatingPayment: isUpdatingPayment.value,
onClose: closePaymentRecoveryDialog,
onUpdatePayment: updatePayment
},
dialogComponentProps: {
headless: true
}
})
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('showTopUpCreditsDialog', () => {
vi.mocked(usePaymentRecoveryDialog).mockReturnValue({
showPaymentRecoveryDialog: mockShowPaymentRecoveryDialog,
updatePayment: vi.fn(),
isUpdatingPayment: computed(() => false)
isUpdatingPayment: computed(() => false),
paymentRecoveryLock: computed(() => null),
refreshCapabilities: vi.fn(async () => undefined)
})

mockIsCloud.value = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { onTestFinished, vi } from 'vitest'
import { computed } from 'vue'
import { computed, toValue } from 'vue'
import type { MaybeRefOrGetter } from 'vue'

import { receiveServerFact } from '@/platform/serverFacts/serverFact'
import type { useBillingCapabilities as realUseBillingCapabilities } from '../useBillingCapabilities'
Expand Down Expand Up @@ -42,6 +43,12 @@ const defaults: ReturnType<typeof realUseBillingCapabilities> = {
receiveServerFact('can_manage_auto_top_up', false)
),
autoTopUpSetup: computed(() => null),
paymentRecoveryAvailable: computed(() =>
receiveServerFact('payment_recovery_available', false)
),
runDeniedPaymentPastDue: computed(() =>
receiveServerFact('denied_reasons.can_run_workflows', false)
),
isReady: computed(() => true),
hasResolvedCapabilities: computed(() => true),
snapshotAuthoritative: computed(() => true),
Expand All @@ -55,6 +62,25 @@ export function canManageBillingFact(granted: boolean) {
return computed(() => receiveServerFact('can_manage_billing', granted))
}

export function paymentRecoveryAvailableFact(
available: MaybeRefOrGetter<boolean>
) {
return computed(() =>
receiveServerFact('payment_recovery_available', toValue(available))
)
}

export function runDeniedPaymentPastDueFact(
deniedPastDue: MaybeRefOrGetter<boolean>
) {
return computed(() =>
receiveServerFact(
'denied_reasons.can_run_workflows',
toValue(deniedPastDue)
)
)
}

export const useBillingCapabilities = vi.fn(() => {
onTestFinished(() => {
Object.assign(capabilities, defaults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,25 @@ describe('useBillingCapabilities', () => {
expect(billingCapabilities.canManageBilling.value).toBe(true)
})

it.for([
{ served: true, expected: true },
{ served: false, expected: false },
{ served: undefined, expected: false }
])(
'renders payment_recovery_available $served as $expected',
async ({ served, expected }) => {
const response = capabilitiesResponse(true, 'workspace-1', true)
response.payment_recovery_available = served
vi.mocked(workspaceApi.getBillingCapabilities).mockResolvedValueOnce(
response
)

await billingCapabilities.initialize()

expect(billingCapabilities.paymentRecoveryAvailable.value).toBe(expected)
}
)

it('offers workspace editing only when the server does', async () => {
vi.mocked(workspaceApi.getBillingCapabilities).mockResolvedValueOnce(
capabilitiesResponse(
Expand Down Expand Up @@ -1493,6 +1512,27 @@ describe('useBillingCapabilities on the SDK rail', () => {
expect(readCapabilities).toHaveBeenCalledTimes(2)
expect(billingCapabilities.snapshotAuthoritative.value).toBe(true)
})

it.for([
{ denial: 'payment_past_due', expected: true },
{ denial: 'no_funds', expected: false },
{ denial: undefined, expected: false }
])(
'reads a can_run_workflows denial of $denial as payment past due: $expected',
async ({ denial, expected }) => {
readCapabilities.mockResolvedValueOnce({
status: 'ok',
value: {
...capabilitiesResponse(true),
denied_reasons: { can_run_workflows: denial }
}
})

await billingCapabilities.initialize()

expect(billingCapabilities.runDeniedPaymentPastDue.value).toBe(expected)
}
)
})

describe('useBillingCapabilities capability gates', () => {
Expand Down
16 changes: 16 additions & 0 deletions src/platform/workspace/composables/useBillingCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
} from '@vueuse/core'
import { computed, onScopeDispose, shallowRef, watch } from 'vue'

import { pendingServerFact } from '@/platform/serverFacts/pendingServerFact'

Check warning on line 8 in src/platform/workspace/composables/useBillingCapabilities.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'billing/application' to zone 'server-facts/domain' is not allowed. src/platform/workspace/composables/useBillingCapabilities.ts -> src/platform/serverFacts/pendingServerFact.ts Route the import through an allowed zone or restructure the dependency.
import { receiveServerFact } from '@/platform/serverFacts/serverFact'
import { useTelemetry } from '@/platform/telemetry'
import { reportError } from '@/platform/telemetry/reportError'
Expand Down Expand Up @@ -196,6 +197,19 @@
const autoTopUpSetup = computed(
() => resolvedResponse.value?.auto_top_up_setup ?? null
)
const paymentRecoveryAvailable = computed(() =>
receiveServerFact(
'payment_recovery_available',
resolvedResponse.value?.payment_recovery_available ?? false
)
)
const runDeniedPaymentPastDue = computed(() =>
pendingServerFact<'denied_reasons.can_run_workflows', boolean>(
'BE-21535',
resolvedResponse.value?.denied_reasons?.can_run_workflows ===
'payment_past_due'
)
)
const isReady = computed(() => {
const state = readState.value
if (state.status === 'idle' || state.status === 'pending') return false
Expand Down Expand Up @@ -539,6 +553,8 @@
canDowngradeToPersonal,
canManageAutoTopUp,
autoTopUpSetup,
paymentRecoveryAvailable,
runDeniedPaymentPastDue,
isReady,
hasResolvedCapabilities,
snapshotAuthoritative,
Expand Down
Loading
Loading