Skip to content

Commit 75a228f

Browse files
committed
Initial backend impl
1 parent 415374c commit 75a228f

File tree

12 files changed

+1203
-6
lines changed

12 files changed

+1203
-6
lines changed

common/src/constants/analytics-events.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export enum AnalyticsEvent {
3030
ADVISORY_LOCK_CONTENTION = 'backend.advisory_lock_contention',
3131
TRANSACTION_RETRY_THRESHOLD_EXCEEDED = 'backend.transaction_retry_threshold_exceeded',
3232

33+
// Backend - Subscription
34+
SUBSCRIPTION_CREATED = 'backend.subscription_created',
35+
SUBSCRIPTION_CANCELED = 'backend.subscription_canceled',
36+
SUBSCRIPTION_PAYMENT_FAILED = 'backend.subscription_payment_failed',
37+
SUBSCRIPTION_BLOCK_CREATED = 'backend.subscription_block_created',
38+
SUBSCRIPTION_BLOCK_LIMIT_HIT = 'backend.subscription_block_limit_hit',
39+
SUBSCRIPTION_WEEKLY_LIMIT_HIT = 'backend.subscription_weekly_limit_hit',
40+
SUBSCRIPTION_CREDITS_MIGRATED = 'backend.subscription_credits_migrated',
41+
3342
// Web
3443
SIGNUP = 'web.signup',
3544

common/src/constants/grant-priorities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GrantType } from '@codebuff/common/types/grant'
22

33
export const GRANT_PRIORITIES: Record<GrantType, number> = {
4+
subscription: 10,
45
free: 20,
56
referral: 30,
67
ad: 40,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const PLAN_NAMES = ['pro'] as const
2+
export type PlanName = (typeof PLAN_NAMES)[number]
3+
4+
export interface PlanConfig {
5+
name: PlanName
6+
displayName: string
7+
monthlyPrice: number
8+
creditsPerBlock: number
9+
blockDurationHours: number
10+
weeklyCreditsLimit: number
11+
}
12+
13+
export const PLANS = {
14+
pro: {
15+
name: 'pro',
16+
displayName: 'Pro',
17+
monthlyPrice: 200,
18+
creditsPerBlock: 1250,
19+
blockDurationHours: 5,
20+
weeklyCreditsLimit: 15000,
21+
},
22+
} as const satisfies Record<PlanName, PlanConfig>
23+
24+
export function isPlanName(name: string): name is PlanName {
25+
return (PLAN_NAMES as readonly string[]).includes(name)
26+
}

common/src/types/grant.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type GrantType =
22
| 'free'
33
| 'referral'
4+
| 'subscription'
45
| 'purchase'
56
| 'admin'
67
| 'organization'
@@ -9,6 +10,7 @@ export type GrantType =
910
export const GrantTypeValues = [
1011
'free',
1112
'referral',
13+
'subscription',
1214
'purchase',
1315
'admin',
1416
'organization',

packages/billing/src/__tests__/usage-service.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const mockBalance = {
1919
totalRemaining: 1000,
2020
totalDebt: 0,
2121
netBalance: 1000,
22-
breakdown: { free: 500, paid: 500, referral: 0, purchase: 0, admin: 0, organization: 0, ad: 0 },
23-
principals: { free: 500, paid: 500, referral: 0, purchase: 0, admin: 0, organization: 0, ad: 0 },
22+
breakdown: { free: 500, referral: 0, subscription: 0, purchase: 500, admin: 0, organization: 0, ad: 0 },
23+
principals: { free: 500, referral: 0, subscription: 0, purchase: 500, admin: 0, organization: 0, ad: 0 },
2424
}
2525

2626
describe('usage-service', () => {

packages/billing/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ export * from './usage-service'
1919
// Credit delegation
2020
export * from './credit-delegation'
2121

22+
// Subscription
23+
export * from './subscription'
24+
25+
// Subscription webhooks
26+
export * from './subscription-webhooks'
27+
2228
// Utilities
2329
export * from './utils'

0 commit comments

Comments
 (0)