-
Notifications
You must be signed in to change notification settings - Fork 490
Guard downgrades via billing portal #7813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+135
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/platform/cloud/subscription/utils/subscriptionTierRank.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { getPlanRank, isPlanDowngrade } from './subscriptionTierRank' | ||
|
|
||
| describe('subscriptionTierRank', () => { | ||
| it('returns consistent order for ranked plans', () => { | ||
| const yearlyPro = getPlanRank({ tierKey: 'pro', billingCycle: 'yearly' }) | ||
| const monthlyStandard = getPlanRank({ | ||
| tierKey: 'standard', | ||
| billingCycle: 'monthly' | ||
| }) | ||
|
|
||
| expect(yearlyPro).toBeLessThan(monthlyStandard) | ||
| }) | ||
|
|
||
| it('identifies downgrades correctly', () => { | ||
| const result = isPlanDowngrade({ | ||
| current: { tierKey: 'pro', billingCycle: 'yearly' }, | ||
| target: { tierKey: 'creator', billingCycle: 'monthly' } | ||
| }) | ||
|
|
||
| expect(result).toBe(true) | ||
| }) | ||
|
|
||
| it('treats lateral or upgrade moves as non-downgrades', () => { | ||
| expect( | ||
| isPlanDowngrade({ | ||
| current: { tierKey: 'standard', billingCycle: 'monthly' }, | ||
| target: { tierKey: 'creator', billingCycle: 'monthly' } | ||
| }) | ||
| ).toBe(false) | ||
|
|
||
| expect( | ||
| isPlanDowngrade({ | ||
| current: { tierKey: 'creator', billingCycle: 'monthly' }, | ||
| target: { tierKey: 'creator', billingCycle: 'monthly' } | ||
| }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('treats unknown plans (e.g., founder) as non-downgrade cases', () => { | ||
| const result = isPlanDowngrade({ | ||
| current: { tierKey: 'founder', billingCycle: 'monthly' }, | ||
| target: { tierKey: 'standard', billingCycle: 'monthly' } | ||
| }) | ||
|
|
||
| expect(result).toBe(false) | ||
| }) | ||
| }) |
58 changes: 58 additions & 0 deletions
58
src/platform/cloud/subscription/utils/subscriptionTierRank.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import type { TierKey } from '@/platform/cloud/subscription/constants/tierPricing' | ||
|
|
||
| export type BillingCycle = 'monthly' | 'yearly' | ||
|
|
||
| type RankedTierKey = Exclude<TierKey, 'founder'> | ||
| type RankedPlanKey = `${BillingCycle}-${RankedTierKey}` | ||
|
|
||
| interface PlanDescriptor { | ||
| tierKey: TierKey | ||
| billingCycle: BillingCycle | ||
| } | ||
|
|
||
| const PLAN_ORDER: RankedPlanKey[] = [ | ||
| 'yearly-pro', | ||
| 'yearly-creator', | ||
| 'yearly-standard', | ||
| 'monthly-pro', | ||
| 'monthly-creator', | ||
| 'monthly-standard' | ||
| ] | ||
|
|
||
| const PLAN_RANK = PLAN_ORDER.reduce<Map<RankedPlanKey, number>>( | ||
| (acc, plan, index) => acc.set(plan, index), | ||
| new Map() | ||
| ) | ||
|
|
||
| const toRankedPlanKey = ( | ||
| tierKey: TierKey, | ||
| billingCycle: BillingCycle | ||
| ): RankedPlanKey | null => { | ||
| if (tierKey === 'founder') return null | ||
| return `${billingCycle}-${tierKey}` as RankedPlanKey | ||
| } | ||
|
|
||
| export const getPlanRank = ({ | ||
| tierKey, | ||
| billingCycle | ||
| }: PlanDescriptor): number => { | ||
| const planKey = toRankedPlanKey(tierKey, billingCycle) | ||
| if (!planKey) return Number.POSITIVE_INFINITY | ||
|
|
||
| return PLAN_RANK.get(planKey) ?? Number.POSITIVE_INFINITY | ||
| } | ||
|
|
||
| interface DowngradeCheckParams { | ||
| current: PlanDescriptor | ||
| target: PlanDescriptor | ||
| } | ||
|
|
||
| export const isPlanDowngrade = ({ | ||
| current, | ||
| target | ||
| }: DowngradeCheckParams): boolean => { | ||
| const currentRank = getPlanRank(current) | ||
| const targetRank = getPlanRank(target) | ||
|
|
||
| return targetRank > currentRank | ||
| } | ||
|
Comment on lines
+50
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Use function declaration for consistency. Same suggestion as above for 🔎 Proposed refactor to function declaration-export const isPlanDowngrade = ({
- current,
- target
-}: DowngradeCheckParams): boolean => {
+export function isPlanDowngrade({
+ current,
+ target
+}: DowngradeCheckParams): boolean {
const currentRank = getPlanRank(current)
const targetRank = getPlanRank(target)
return targetRank > currentRank
}🤖 Prompt for AI Agents |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Use function declarations for exported pure functions.
Per coding guidelines, prefer function declarations over function expressions for pure functions. This improves hoisting clarity and aligns with repository conventions.
🔎 Proposed refactor to function declaration
📝 Committable suggestion