Skip to content
6 changes: 6 additions & 0 deletions apps/billing-web/e2e/fixtures/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export function capabilitiesWith(
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: true,
Expand Down
17 changes: 17 additions & 0 deletions apps/billing-web/src/checkout/endingScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ const REFUSAL_COPY: Readonly<Record<CapabilityDenialReason, RefusalCopy>> = {
subscription_status_unrecognized: 'unknown',
subscription_change_in_progress: 'change_unnamed',
not_a_member: 'unknown',
not_original_owner: 'unknown',
creator_only_provisioning: 'unknown',
payment_past_due: 'unknown',
free_plan: 'unknown',
no_subscription: 'unknown',
subscription_ended: 'unknown',
billing_operation_in_progress: 'unknown',
billing_operation_parked: 'unknown',
cancellation_scheduled: 'unknown',
cancellation_not_scheduled: 'unknown',
workspace_sso_attached: 'unknown',
personal_workspace: 'unknown',
plan_not_ended: 'unknown',
membership_managed_by_directory: 'unknown',
personal_workspace_creator: 'unknown',
workspace_creator: 'unknown',
only_owner: 'unknown',
unspecified: 'unknown'
}

Expand Down
6 changes: 6 additions & 0 deletions apps/billing-web/src/test/fakeBillingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ export function createFakeBillingClient(
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: false,
Expand Down
6 changes: 6 additions & 0 deletions apps/website/e2e/fixtures/buyCredits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const CAPABILITIES: BillingCapabilitiesResponse = {
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: true,
Expand Down
6 changes: 6 additions & 0 deletions apps/website/src/config/workshop-billing-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const CLOUD_BODIES: Readonly<Record<string, unknown>> = {
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: false,
Expand Down
6 changes: 6 additions & 0 deletions browser_tests/fixtures/data/billingCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export function createBillingCapabilities(
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/account-core/src/core/billing/capabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const CAPABILITIES = {
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from './billingContracts.js'
import { sessionBillingScopeSource } from './billingScope.js'
import { createCapabilitiesReader } from './capabilities.js'
import type { BillingCapabilities } from './capabilities.js'
import {
authenticated,
credential,
Expand Down Expand Up @@ -49,12 +50,18 @@ const TAKEOVERS = [
{ takeover: 'another owner with the same role', next: SECOND_OWNER_SESSION }
]

const NO_RIGHTS = {
const NO_RIGHTS: BillingCapabilities = {
can_cancel: false,
can_change_seats: false,
can_delete_workspace: false,
can_downgrade_to_personal: false,
can_invite_members: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_members: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: false,
Expand Down
60 changes: 48 additions & 12 deletions packages/account-core/src/core/billing/capabilityDenials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,62 @@ export type CapabilityDenialReason =
| 'subscription_change_in_progress'
/** The server has no guidance for the row — distinct from a deliberate refusal. */
| 'subscription_status_unrecognized'
| 'not_original_owner'
| 'creator_only_provisioning'
| 'payment_past_due'
| 'free_plan'
| 'no_subscription'
| 'subscription_ended'
| 'billing_operation_in_progress'
| 'billing_operation_parked'
| 'cancellation_scheduled'
| 'cancellation_not_scheduled'
| 'workspace_sso_attached'
| 'personal_workspace'
| 'plan_not_ended'
| 'membership_managed_by_directory'
| 'personal_workspace_creator'
| 'workspace_creator'
| 'only_owner'
/**
* A reason outside the set above. Kept as a reason rather than dropped so a
* refusal the server explained still reads as explained; the copy it maps to
* is the generic one.
*/
| 'unspecified'

const KNOWN_REASONS: ReadonlySet<string> = new Set<CapabilityDenialReason>([
'not_a_member',
'not_workspace_owner',
'tier_not_self_serve',
'subscription_not_started',
'subscription_change_in_progress',
'subscription_status_unrecognized'
])
const KNOWN_REASONS: Readonly<
Record<Exclude<CapabilityDenialReason, 'unspecified'>, true>
> = {
not_a_member: true,
not_workspace_owner: true,
tier_not_self_serve: true,
subscription_not_started: true,
subscription_change_in_progress: true,
subscription_status_unrecognized: true,
not_original_owner: true,
creator_only_provisioning: true,
payment_past_due: true,
free_plan: true,
no_subscription: true,
subscription_ended: true,
billing_operation_in_progress: true,
billing_operation_parked: true,
cancellation_scheduled: true,
cancellation_not_scheduled: true,
workspace_sso_attached: true,
personal_workspace: true,
plan_not_ended: true,
membership_managed_by_directory: true,
personal_workspace_creator: true,
workspace_creator: true,
only_owner: true
}

/**
* Only `can_subscribe_self_serve` carries a reason today. The record is open
* because the server adds keys as policy grows, and a client that rejected an
* unknown key would fail the whole read over a field it does not use.
* The record is open because the server adds keys as policy grows, and a
* client that rejected an unknown key would fail the whole read over a field
* it does not use.
*/
export type CapabilityDenials = Readonly<
Partial<Record<string, CapabilityDenialReason>>
Expand All @@ -70,7 +106,7 @@ export function decodeCapabilityDenials(raw: unknown): CapabilityDenials {

const denials: Record<string, CapabilityDenialReason> = {}
for (const [capability, reason] of Object.entries(parsed.data)) {
denials[capability] = KNOWN_REASONS.has(reason)
denials[capability] = Object.hasOwn(KNOWN_REASONS, reason)
? (reason as CapabilityDenialReason)
: 'unspecified'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import type {
import { describe, expect, expectTypeOf, it } from 'vitest'
import type { z } from 'zod'

import type { CapabilityDenialReason } from '../capabilityDenials.js'

type CapabilitiesResponseBody = z.input<typeof zBillingCapabilitiesResponse>
type CapabilitiesResponse = z.infer<typeof zBillingCapabilitiesResponse>
type Capabilities = z.infer<typeof zBillingCapabilities>
type CapabilityScope = z.infer<typeof zBillingCapabilityScope>
type RolloutDefaults = z.infer<typeof zBillingCapabilityRolloutDefaults>
type DeniedReasons = NonNullable<CapabilitiesResponse['denied_reasons']>
type ServerDenialReason = NonNullable<DeniedReasons[keyof DeniedReasons]>

function capabilities(): Capabilities {
return {
Expand All @@ -20,6 +24,12 @@ function capabilities(): Capabilities {
can_downgrade_to_personal: false,
can_invite_members: true,
can_manage_members: true,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: true,
Expand All @@ -44,13 +54,12 @@ function capabilitiesBody(
}
}

// Compile-time pins: a regen that moves these fails the package typecheck.

// The five fields the reader composes its own schema from, plus two it leaves out.
expectTypeOf<keyof CapabilitiesResponse>().toEqualTypeOf<
| 'capabilities'
| 'denied_reasons'
| 'expires_at'
| 'plan_kind'
| 'recovery_route'
| 'resolved_for'
| 'revision'
| 'rollout_defaults_applied'
Expand All @@ -59,14 +68,23 @@ expectTypeOf<keyof CapabilitiesResponse>().toEqualTypeOf<
expectTypeOf<Capabilities>().toEqualTypeOf<{
can_cancel: boolean
can_change_seats: boolean
can_delete_workspace: boolean
can_downgrade_to_personal: boolean
can_invite_members: boolean
can_leave_workspace: boolean
can_manage_billing: boolean
can_manage_members: boolean
can_manage_partner_node_access: boolean
can_reactivate: boolean
can_revert_scheduled_change: boolean
can_subscribe_self_serve: boolean
can_top_up: boolean
can_view_pending_invites: boolean
can_view_team_usage: boolean
}>()
expectTypeOf<ServerDenialReason>().toEqualTypeOf<
Exclude<CapabilityDenialReason, 'unspecified'>
>()
expectTypeOf<CapabilityScope>().toEqualTypeOf<{
user_id: string
workspace_id: string
Expand All @@ -76,7 +94,6 @@ expectTypeOf<RolloutDefaults>().toEqualTypeOf<{
can_subscribe_self_serve: boolean
can_top_up: boolean
}>()
// The expiry the reader paces freshness from; the revision it reads as a number.
expectTypeOf<CapabilitiesResponse['expires_at']>().toEqualTypeOf<string>()
expectTypeOf<CapabilitiesResponse['revision']>().toEqualTypeOf<bigint>()

Expand Down
6 changes: 6 additions & 0 deletions packages/account-core/src/core/billing/scopedReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ function capabilitiesBody(overrides: Record<string, unknown> = {}) {
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: false,
Expand Down
6 changes: 6 additions & 0 deletions packages/account-core/src/core/billing/topup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ const CAPABILITIES = {
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ export const CAPABILITIES = {
can_downgrade_to_personal: false,
can_invite_members: false,
can_manage_members: false,
can_delete_workspace: false,
can_leave_workspace: false,
can_manage_billing: false,
can_manage_partner_node_access: false,
can_view_pending_invites: false,
can_view_team_usage: false,
can_reactivate: false,
can_revert_scheduled_change: false,
can_subscribe_self_serve: true,
Expand Down
Loading
Loading