alepha 0.20.6 → 0.20.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +0 -1
- package/CLAUDE.md +0 -1
- package/assets/agents-template.md +0 -1
- package/dist/api/audits/index.browser.js +1 -0
- package/dist/api/audits/index.browser.js.map +1 -1
- package/dist/api/audits/index.d.ts +370 -355
- package/dist/api/audits/index.d.ts.map +1 -1
- package/dist/api/audits/index.js +1 -0
- package/dist/api/audits/index.js.map +1 -1
- package/dist/api/files/index.browser.js +1 -0
- package/dist/api/files/index.browser.js.map +1 -1
- package/dist/api/files/index.d.ts +179 -170
- package/dist/api/files/index.d.ts.map +1 -1
- package/dist/api/files/index.js +1 -0
- package/dist/api/files/index.js.map +1 -1
- package/dist/api/jobs/index.browser.js +7 -0
- package/dist/api/jobs/index.browser.js.map +1 -1
- package/dist/api/jobs/index.d.ts +259 -250
- package/dist/api/jobs/index.d.ts.map +1 -1
- package/dist/api/jobs/index.js +21 -3
- package/dist/api/jobs/index.js.map +1 -1
- package/dist/api/keys/index.d.ts +198 -192
- package/dist/api/keys/index.d.ts.map +1 -1
- package/dist/api/keys/index.js +1 -0
- package/dist/api/keys/index.js.map +1 -1
- package/dist/api/notifications/index.d.ts +246 -245
- package/dist/api/notifications/index.d.ts.map +1 -1
- package/dist/api/organizations/index.d.ts +100 -97
- package/dist/api/organizations/index.d.ts.map +1 -1
- package/dist/api/parameters/index.d.ts +323 -320
- package/dist/api/parameters/index.d.ts.map +1 -1
- package/dist/api/payments/index.d.ts +431 -376
- package/dist/api/payments/index.d.ts.map +1 -1
- package/dist/api/payments/index.js +202 -87
- package/dist/api/payments/index.js.map +1 -1
- package/dist/api/subscriptions/index.d.ts +1695 -0
- package/dist/api/subscriptions/index.d.ts.map +1 -0
- package/dist/api/subscriptions/index.js +1919 -0
- package/dist/api/subscriptions/index.js.map +1 -0
- package/dist/api/users/index.d.ts +857 -841
- package/dist/api/users/index.d.ts.map +1 -1
- package/dist/api/verifications/index.d.ts +128 -127
- package/dist/api/verifications/index.d.ts.map +1 -1
- package/dist/bucket/index.d.ts +3 -2
- package/dist/bucket/index.d.ts.map +1 -1
- package/dist/cache/core/index.d.ts +114 -4
- package/dist/cache/core/index.d.ts.map +1 -1
- package/dist/cache/core/index.js +181 -15
- package/dist/cache/core/index.js.map +1 -1
- package/dist/cache/core/index.workerd.js +181 -15
- package/dist/cache/core/index.workerd.js.map +1 -1
- package/dist/cache/database/index.d.ts +20 -19
- package/dist/cache/database/index.d.ts.map +1 -1
- package/dist/cache/redis/index.d.ts +3 -2
- package/dist/cache/redis/index.d.ts.map +1 -1
- package/dist/cli/core/index.d.ts +116 -132
- package/dist/cli/core/index.d.ts.map +1 -1
- package/dist/cli/core/index.js +75 -7
- package/dist/cli/core/index.js.map +1 -1
- package/dist/cli/devtools/index.d.ts +3 -2
- package/dist/cli/devtools/index.d.ts.map +1 -1
- package/dist/cli/platform/index.d.ts +346 -290
- package/dist/cli/platform/index.d.ts.map +1 -1
- package/dist/cli/platform/index.js +105 -6
- package/dist/cli/platform/index.js.map +1 -1
- package/dist/cli/vendor/index.d.ts +12 -11
- package/dist/cli/vendor/index.d.ts.map +1 -1
- package/dist/command/index.d.ts +5 -4
- package/dist/command/index.d.ts.map +1 -1
- package/dist/core/index.browser.js +1 -1
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +119 -118
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +1 -1
- package/dist/core/index.native.js.map +1 -1
- package/dist/core/index.workerd.js +1 -1
- package/dist/core/index.workerd.js.map +1 -1
- package/dist/crypto/index.d.ts +3 -2
- package/dist/crypto/index.d.ts.map +1 -1
- package/dist/email/core/index.d.ts +3 -2
- package/dist/email/core/index.d.ts.map +1 -1
- package/dist/email/smtp/index.d.ts +7 -6
- package/dist/email/smtp/index.d.ts.map +1 -1
- package/dist/lock/core/index.d.ts +5 -4
- package/dist/lock/core/index.d.ts.map +1 -1
- package/dist/logger/index.d.ts +10 -9
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/mcp/index.d.ts +9 -8
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/orm/core/index.browser.js +9 -3
- package/dist/orm/core/index.browser.js.map +1 -1
- package/dist/orm/core/index.bun.js +31 -10
- package/dist/orm/core/index.bun.js.map +1 -1
- package/dist/orm/core/index.d.ts +33 -14
- package/dist/orm/core/index.d.ts.map +1 -1
- package/dist/orm/core/index.js +31 -10
- package/dist/orm/core/index.js.map +1 -1
- package/dist/orm/postgres/index.d.ts +6 -5
- package/dist/orm/postgres/index.d.ts.map +1 -1
- package/dist/queue/core/index.d.ts +5 -4
- package/dist/queue/core/index.d.ts.map +1 -1
- package/dist/queue/redis/index.d.ts +3 -2
- package/dist/queue/redis/index.d.ts.map +1 -1
- package/dist/react/form/index.d.ts +5 -0
- package/dist/react/form/index.d.ts.map +1 -1
- package/dist/react/form/index.js +6 -4
- package/dist/react/form/index.js.map +1 -1
- package/dist/react/i18n/index.d.ts +2 -1
- package/dist/react/i18n/index.d.ts.map +1 -1
- package/dist/react/router/index.d.ts +206 -205
- package/dist/react/router/index.d.ts.map +1 -1
- package/dist/react/ui/index.d.ts +11 -11
- package/dist/react/ui/index.d.ts.map +1 -1
- package/dist/scheduler/index.d.ts +3 -2
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/security/index.browser.js +29 -1
- package/dist/security/index.browser.js.map +1 -1
- package/dist/security/index.d.ts +82 -35
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/index.js +56 -3
- package/dist/security/index.js.map +1 -1
- package/dist/server/auth/index.d.ts +163 -158
- package/dist/server/auth/index.d.ts.map +1 -1
- package/dist/server/auth/index.js +16 -4
- package/dist/server/auth/index.js.map +1 -1
- package/dist/server/core/index.d.ts +35 -34
- package/dist/server/core/index.d.ts.map +1 -1
- package/dist/server/cors/index.d.ts +7 -6
- package/dist/server/cors/index.d.ts.map +1 -1
- package/dist/server/health/index.d.ts +16 -15
- package/dist/server/health/index.d.ts.map +1 -1
- package/dist/server/links/index.d.ts +51 -50
- package/dist/server/links/index.d.ts.map +1 -1
- package/dist/server/rate-limit/index.d.ts +6 -5
- package/dist/server/rate-limit/index.d.ts.map +1 -1
- package/dist/server/swagger/index.d.ts +2 -1
- package/dist/server/swagger/index.d.ts.map +1 -1
- package/dist/topic/redis/index.d.ts +3 -2
- package/dist/topic/redis/index.d.ts.map +1 -1
- package/package.json +16 -32
- package/src/api/audits/entities/audits.ts +1 -0
- package/src/api/files/entities/files.ts +1 -0
- package/src/api/jobs/__tests__/$job.spec.ts +92 -40
- package/src/api/jobs/entities/jobExecutionEntity.ts +1 -0
- package/src/api/jobs/providers/JobProvider.ts +20 -5
- package/src/api/jobs/schemas/jobConfigAtom.ts +5 -0
- package/src/api/keys/entities/apiKeyEntity.ts +1 -0
- package/src/api/payments/controllers/MockCheckoutController.ts +146 -0
- package/src/api/payments/index.ts +3 -0
- package/src/api/payments/providers/MemoryPaymentProvider.ts +9 -4
- package/src/api/payments/providers/PaymentProvider.ts +25 -9
- package/src/api/payments/services/PaymentService.ts +3 -0
- package/src/api/subscriptions/__tests__/BillingService.spec.ts +218 -0
- package/src/api/subscriptions/__tests__/SubscriptionService.spec.ts +278 -0
- package/src/api/subscriptions/controllers/AdminSubscriptionController.ts +212 -0
- package/src/api/subscriptions/controllers/SubscriptionController.ts +189 -0
- package/src/api/subscriptions/entities/subscriptionEvents.ts +54 -0
- package/src/api/subscriptions/entities/subscriptions.ts +68 -0
- package/src/api/subscriptions/index.ts +133 -0
- package/src/api/subscriptions/jobs/SubscriptionJobs.ts +382 -0
- package/src/api/subscriptions/middleware/$requireLimit.ts +50 -0
- package/src/api/subscriptions/middleware/$requirePlan.ts +49 -0
- package/src/api/subscriptions/notifications/SubscriptionNotifications.ts +110 -0
- package/src/api/subscriptions/schemas/cancelSubscriptionSchema.ts +8 -0
- package/src/api/subscriptions/schemas/changePlanSchema.ts +9 -0
- package/src/api/subscriptions/schemas/createSubscriptionSchema.ts +11 -0
- package/src/api/subscriptions/schemas/entitlementsSchema.ts +21 -0
- package/src/api/subscriptions/schemas/mrrSchema.ts +13 -0
- package/src/api/subscriptions/schemas/planDefinitionSchema.ts +71 -0
- package/src/api/subscriptions/schemas/planResourceSchema.ts +25 -0
- package/src/api/subscriptions/schemas/subscriptionEventResourceSchema.ts +8 -0
- package/src/api/subscriptions/schemas/subscriptionQuerySchema.ts +19 -0
- package/src/api/subscriptions/schemas/subscriptionResourceSchema.ts +6 -0
- package/src/api/subscriptions/schemas/subscriptionSettingsSchema.ts +32 -0
- package/src/api/subscriptions/schemas/subscriptionStatsSchema.ts +23 -0
- package/src/api/subscriptions/services/BillingService.ts +437 -0
- package/src/api/subscriptions/services/SubscriptionConfig.ts +56 -0
- package/src/api/subscriptions/services/SubscriptionService.ts +867 -0
- package/src/api/subscriptions/services/UsageService.ts +118 -0
- package/src/cache/core/__tests__/$cache.memory.spec.ts +450 -0
- package/src/cache/core/__tests__/$cache.swr.spec.ts +394 -0
- package/src/cache/core/index.ts +16 -0
- package/src/cache/core/primitives/$cache.ts +347 -21
- package/src/cli/core/tasks/BuildCloudflareTask.ts +16 -0
- package/src/cli/core/templates/agentMd.ts +39 -4
- package/src/cli/core/templates/biomeJson.ts +25 -1
- package/src/cli/core/templates/saasAdminLayoutTsx.ts +2 -2
- package/src/cli/platform/__tests__/CloudflareAdapter.spec.ts +117 -0
- package/src/cli/platform/adapters/CloudflareAdapter.ts +104 -7
- package/src/cli/platform/atoms/platformOptions.ts +13 -0
- package/src/cli/platform/schemas/platform.ts +1 -0
- package/src/cli/platform/services/CloudflareApi.ts +61 -0
- package/src/cli/platform/services/PlatformOrchestrator.ts +9 -4
- package/src/core/__tests__/$module.spec.ts +2 -2
- package/src/core/primitives/$module.ts +4 -4
- package/src/mcp/providers/McpServerProvider.ts +1 -1
- package/src/orm/core/providers/DatabaseTypeProvider.ts +9 -3
- package/src/orm/core/providers/drivers/DatabaseProvider.ts +1 -1
- package/src/orm/core/schemas/insertSchema.ts +10 -2
- package/src/orm/core/services/Repository.ts +27 -7
- package/src/react/form/hooks/useFormState.ts +8 -1
- package/src/react/form/index.ts +10 -1
- package/src/react/form/services/FormModel.ts +9 -3
- package/src/security/atoms/currentTenantAtom.ts +34 -0
- package/src/security/index.browser.ts +1 -0
- package/src/security/index.ts +12 -1
- package/src/security/primitives/$issuer.ts +17 -1
- package/src/security/providers/SecurityProvider.ts +37 -0
- package/src/server/auth/__tests__/validateRedirectUri.spec.ts +78 -0
- package/src/server/auth/providers/ServerAuthProvider.ts +21 -5
- package/tsconfig.base.json +2 -1
- package/dist/react/websocket/index.d.ts +0 -117
- package/dist/react/websocket/index.d.ts.map +0 -1
- package/dist/react/websocket/index.js +0 -108
- package/dist/react/websocket/index.js.map +0 -1
- package/dist/websocket/index.browser.js +0 -848
- package/dist/websocket/index.browser.js.map +0 -1
- package/dist/websocket/index.d.ts +0 -876
- package/dist/websocket/index.d.ts.map +0 -1
- package/dist/websocket/index.js +0 -1185
- package/dist/websocket/index.js.map +0 -1
- package/src/react/websocket/hooks/useRoom.tsx +0 -251
- package/src/react/websocket/index.ts +0 -7
- package/src/websocket/__tests__/$channel.spec.ts +0 -30
- package/src/websocket/__tests__/$websocket-new.spec.ts +0 -195
- package/src/websocket/__tests__/RoomManager.spec.ts +0 -146
- package/src/websocket/__tests__/websocket-integration.spec.ts +0 -951
- package/src/websocket/errors/WebSocketError.ts +0 -34
- package/src/websocket/index.browser.ts +0 -25
- package/src/websocket/index.shared.ts +0 -8
- package/src/websocket/index.ts +0 -85
- package/src/websocket/interfaces/WebSocketInterfaces.ts +0 -252
- package/src/websocket/primitives/$channel.ts +0 -131
- package/src/websocket/primitives/$websocket.ts +0 -107
- package/src/websocket/providers/NodeWebSocketServerProvider.ts +0 -617
- package/src/websocket/providers/WebSocketServerProvider.ts +0 -56
- package/src/websocket/services/RoomManager.ts +0 -160
- package/src/websocket/services/WebSocketClient.ts +0 -642
- package/src/websocket/services/WebSocketTopicService.ts +0 -108
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { $inject, t } from "alepha";
|
|
2
|
+
import { $secure } from "alepha/security";
|
|
3
|
+
import { $action, okSchema } from "alepha/server";
|
|
4
|
+
import { cancelSubscriptionSchema } from "../schemas/cancelSubscriptionSchema.ts";
|
|
5
|
+
import { changePlanSchema } from "../schemas/changePlanSchema.ts";
|
|
6
|
+
import { mrrSchema } from "../schemas/mrrSchema.ts";
|
|
7
|
+
import { subscriptionQuerySchema } from "../schemas/subscriptionQuerySchema.ts";
|
|
8
|
+
import { subscriptionResourceSchema } from "../schemas/subscriptionResourceSchema.ts";
|
|
9
|
+
import { subscriptionStatsSchema } from "../schemas/subscriptionStatsSchema.ts";
|
|
10
|
+
import { SubscriptionConfig } from "../services/SubscriptionConfig.ts";
|
|
11
|
+
import { SubscriptionService } from "../services/SubscriptionService.ts";
|
|
12
|
+
|
|
13
|
+
export class AdminSubscriptionController {
|
|
14
|
+
protected readonly url = "/subscriptions";
|
|
15
|
+
protected readonly group = "admin:subscriptions";
|
|
16
|
+
protected readonly service = $inject(SubscriptionService);
|
|
17
|
+
protected readonly config = $inject(SubscriptionConfig);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Find subscriptions with pagination and filtering.
|
|
21
|
+
*/
|
|
22
|
+
public readonly findSubscriptions = $action({
|
|
23
|
+
path: this.url,
|
|
24
|
+
group: this.group,
|
|
25
|
+
use: [$secure({ permissions: ["admin:subscription:read"] })],
|
|
26
|
+
description: "Find subscriptions with pagination and filtering",
|
|
27
|
+
schema: {
|
|
28
|
+
query: subscriptionQuerySchema,
|
|
29
|
+
response: t.page(subscriptionResourceSchema),
|
|
30
|
+
},
|
|
31
|
+
handler: ({ query }) => this.service.findSubscriptions(query),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Get a subscription by ID.
|
|
36
|
+
*/
|
|
37
|
+
public readonly getSubscription = $action({
|
|
38
|
+
path: `${this.url}/:id`,
|
|
39
|
+
group: this.group,
|
|
40
|
+
use: [$secure({ permissions: ["admin:subscription:read"] })],
|
|
41
|
+
description: "Get a subscription by ID",
|
|
42
|
+
schema: {
|
|
43
|
+
params: t.object({ id: t.uuid() }),
|
|
44
|
+
response: subscriptionResourceSchema,
|
|
45
|
+
},
|
|
46
|
+
handler: ({ params }) => this.service.getSubscription(params.id),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get aggregated subscription statistics.
|
|
51
|
+
*/
|
|
52
|
+
public readonly getStats = $action({
|
|
53
|
+
path: `${this.url}/stats`,
|
|
54
|
+
group: this.group,
|
|
55
|
+
use: [$secure({ permissions: ["admin:subscription:read"] })],
|
|
56
|
+
description: "Get aggregated subscription statistics",
|
|
57
|
+
schema: {
|
|
58
|
+
response: subscriptionStatsSchema,
|
|
59
|
+
},
|
|
60
|
+
handler: () => this.service.getStats(),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get revenue data from recent subscription events.
|
|
65
|
+
*/
|
|
66
|
+
public readonly getRevenue = $action({
|
|
67
|
+
path: `${this.url}/revenue`,
|
|
68
|
+
group: this.group,
|
|
69
|
+
use: [$secure({ permissions: ["admin:subscription:read"] })],
|
|
70
|
+
description: "Get revenue data from recent subscription events",
|
|
71
|
+
schema: {
|
|
72
|
+
query: t.object({
|
|
73
|
+
days: t.optional(t.integer({ minimum: 1, maximum: 365 })),
|
|
74
|
+
}),
|
|
75
|
+
response: t.object({ total: t.integer(), count: t.integer() }),
|
|
76
|
+
},
|
|
77
|
+
handler: ({ query }) => this.service.getRevenue(query.days),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get Monthly Recurring Revenue breakdown.
|
|
82
|
+
*/
|
|
83
|
+
public readonly getMrr = $action({
|
|
84
|
+
path: `${this.url}/mrr`,
|
|
85
|
+
group: this.group,
|
|
86
|
+
use: [$secure({ permissions: ["admin:subscription:read"] })],
|
|
87
|
+
description: "Get Monthly Recurring Revenue breakdown",
|
|
88
|
+
schema: {
|
|
89
|
+
response: mrrSchema,
|
|
90
|
+
},
|
|
91
|
+
handler: async () => {
|
|
92
|
+
const activeSubs = await this.service.findSubscriptions({
|
|
93
|
+
status: "active",
|
|
94
|
+
size: 1000,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const plans = await this.config.getPlans();
|
|
98
|
+
const byPlan: Record<string, number> = {};
|
|
99
|
+
let total = 0;
|
|
100
|
+
|
|
101
|
+
for (const sub of activeSubs.content) {
|
|
102
|
+
const plan = plans.find((p) => p.id === sub.planId);
|
|
103
|
+
if (!plan) continue;
|
|
104
|
+
|
|
105
|
+
const pricing = plan.pricing.find((p) => p.interval === sub.interval);
|
|
106
|
+
if (!pricing) continue;
|
|
107
|
+
|
|
108
|
+
const monthlyAmount =
|
|
109
|
+
sub.interval === "yearly"
|
|
110
|
+
? Math.round(pricing.amount / 12)
|
|
111
|
+
: pricing.amount;
|
|
112
|
+
|
|
113
|
+
byPlan[sub.planId] = (byPlan[sub.planId] ?? 0) + monthlyAmount;
|
|
114
|
+
total += monthlyAmount;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
total,
|
|
119
|
+
byPlan,
|
|
120
|
+
growth: 0,
|
|
121
|
+
newMrr: 0,
|
|
122
|
+
expansionMrr: 0,
|
|
123
|
+
contractionMrr: 0,
|
|
124
|
+
churnMrr: 0,
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Force a plan change for a subscription (admin action).
|
|
131
|
+
*/
|
|
132
|
+
public readonly adminChangePlan = $action({
|
|
133
|
+
method: "POST",
|
|
134
|
+
path: `${this.url}/:id/change-plan`,
|
|
135
|
+
group: this.group,
|
|
136
|
+
use: [$secure({ permissions: ["admin:subscription:update"] })],
|
|
137
|
+
description: "Force a plan change for a subscription",
|
|
138
|
+
schema: {
|
|
139
|
+
params: t.object({ id: t.uuid() }),
|
|
140
|
+
body: changePlanSchema,
|
|
141
|
+
response: subscriptionResourceSchema,
|
|
142
|
+
},
|
|
143
|
+
handler: async ({ params, body }) => {
|
|
144
|
+
await this.service.changePlan(params.id, body.planId, body.interval, {
|
|
145
|
+
immediate: body.immediate,
|
|
146
|
+
});
|
|
147
|
+
return this.service.getSubscription(params.id);
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Force cancel a subscription (admin action).
|
|
153
|
+
*/
|
|
154
|
+
public readonly adminCancel = $action({
|
|
155
|
+
method: "POST",
|
|
156
|
+
path: `${this.url}/:id/cancel`,
|
|
157
|
+
group: this.group,
|
|
158
|
+
use: [$secure({ permissions: ["admin:subscription:update"] })],
|
|
159
|
+
description: "Force cancel a subscription",
|
|
160
|
+
schema: {
|
|
161
|
+
params: t.object({ id: t.uuid() }),
|
|
162
|
+
body: cancelSubscriptionSchema,
|
|
163
|
+
response: okSchema,
|
|
164
|
+
},
|
|
165
|
+
handler: async ({ params, body }) => {
|
|
166
|
+
await this.service.cancel(params.id, {
|
|
167
|
+
reason: body.reason,
|
|
168
|
+
immediate: body.immediate,
|
|
169
|
+
});
|
|
170
|
+
return { ok: true };
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Reactivate a suspended subscription (admin action).
|
|
176
|
+
*/
|
|
177
|
+
public readonly adminReactivate = $action({
|
|
178
|
+
method: "POST",
|
|
179
|
+
path: `${this.url}/:id/reactivate`,
|
|
180
|
+
group: this.group,
|
|
181
|
+
use: [$secure({ permissions: ["admin:subscription:update"] })],
|
|
182
|
+
description: "Reactivate a suspended subscription",
|
|
183
|
+
schema: {
|
|
184
|
+
params: t.object({ id: t.uuid() }),
|
|
185
|
+
response: okSchema,
|
|
186
|
+
},
|
|
187
|
+
handler: async ({ params }) => {
|
|
188
|
+
await this.service.reactivate(params.id);
|
|
189
|
+
return { ok: true };
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Extend the trial period for a trialing subscription (admin action).
|
|
195
|
+
*/
|
|
196
|
+
public readonly adminExtendTrial = $action({
|
|
197
|
+
method: "POST",
|
|
198
|
+
path: `${this.url}/:id/extend-trial`,
|
|
199
|
+
group: this.group,
|
|
200
|
+
use: [$secure({ permissions: ["admin:subscription:update"] })],
|
|
201
|
+
description: "Extend the trial period for a subscription",
|
|
202
|
+
schema: {
|
|
203
|
+
params: t.object({ id: t.uuid() }),
|
|
204
|
+
body: t.object({ days: t.integer({ minimum: 1, maximum: 365 }) }),
|
|
205
|
+
response: okSchema,
|
|
206
|
+
},
|
|
207
|
+
handler: async ({ params, body }) => {
|
|
208
|
+
await this.service.extendTrial(params.id, body.days);
|
|
209
|
+
return { ok: true };
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { $inject, t } from "alepha";
|
|
2
|
+
import { $secure } from "alepha/security";
|
|
3
|
+
import { $action, NotFoundError, okSchema } from "alepha/server";
|
|
4
|
+
import { cancelSubscriptionSchema } from "../schemas/cancelSubscriptionSchema.ts";
|
|
5
|
+
import { changePlanSchema } from "../schemas/changePlanSchema.ts";
|
|
6
|
+
import { createSubscriptionSchema } from "../schemas/createSubscriptionSchema.ts";
|
|
7
|
+
import { entitlementsSchema } from "../schemas/entitlementsSchema.ts";
|
|
8
|
+
import { planResourceSchema } from "../schemas/planResourceSchema.ts";
|
|
9
|
+
import { subscriptionEventResourceSchema } from "../schemas/subscriptionEventResourceSchema.ts";
|
|
10
|
+
import { subscriptionResourceSchema } from "../schemas/subscriptionResourceSchema.ts";
|
|
11
|
+
import { SubscriptionConfig } from "../services/SubscriptionConfig.ts";
|
|
12
|
+
import { SubscriptionService } from "../services/SubscriptionService.ts";
|
|
13
|
+
|
|
14
|
+
export class SubscriptionController {
|
|
15
|
+
protected readonly url = "/subscriptions";
|
|
16
|
+
protected readonly group = "subscriptions";
|
|
17
|
+
protected readonly service = $inject(SubscriptionService);
|
|
18
|
+
protected readonly config = $inject(SubscriptionConfig);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* List available subscription plans with pricing.
|
|
22
|
+
*/
|
|
23
|
+
public readonly getPlans = $action({
|
|
24
|
+
path: `${this.url}/plans`,
|
|
25
|
+
group: this.group,
|
|
26
|
+
description: "List available subscription plans",
|
|
27
|
+
schema: {
|
|
28
|
+
response: t.array(planResourceSchema),
|
|
29
|
+
},
|
|
30
|
+
handler: async () => {
|
|
31
|
+
const plans = await this.config.getPlans();
|
|
32
|
+
return plans
|
|
33
|
+
.filter((p) => p.available)
|
|
34
|
+
.map((p) => ({
|
|
35
|
+
id: p.id,
|
|
36
|
+
name: p.name,
|
|
37
|
+
description: p.description,
|
|
38
|
+
pricing: p.pricing,
|
|
39
|
+
features: p.features,
|
|
40
|
+
limits: p.limits,
|
|
41
|
+
trial: p.trial,
|
|
42
|
+
order: p.order,
|
|
43
|
+
}));
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get the current organization's subscription.
|
|
49
|
+
*/
|
|
50
|
+
public readonly getMySubscription = $action({
|
|
51
|
+
path: `${this.url}/mine`,
|
|
52
|
+
group: this.group,
|
|
53
|
+
use: [$secure()],
|
|
54
|
+
description: "Get the current organization subscription",
|
|
55
|
+
schema: {
|
|
56
|
+
response: subscriptionResourceSchema,
|
|
57
|
+
},
|
|
58
|
+
handler: async ({ user }) => {
|
|
59
|
+
const sub = await this.service.getByOrganization(user.organization!);
|
|
60
|
+
if (!sub)
|
|
61
|
+
throw new NotFoundError("No subscription found for your organization");
|
|
62
|
+
return sub;
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create a new subscription for the current organization.
|
|
68
|
+
*/
|
|
69
|
+
public readonly subscribe = $action({
|
|
70
|
+
method: "POST",
|
|
71
|
+
path: this.url,
|
|
72
|
+
group: this.group,
|
|
73
|
+
use: [$secure({ permissions: ["subscription:create"] })],
|
|
74
|
+
description: "Create a new subscription",
|
|
75
|
+
schema: {
|
|
76
|
+
body: createSubscriptionSchema,
|
|
77
|
+
response: subscriptionResourceSchema,
|
|
78
|
+
},
|
|
79
|
+
handler: ({ body, user }) =>
|
|
80
|
+
this.service.subscribe(user.organization!, body.planId, body.interval, {
|
|
81
|
+
skipTrial: body.skipTrial,
|
|
82
|
+
metadata: body.metadata,
|
|
83
|
+
}),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Change the plan for the current organization's subscription.
|
|
88
|
+
*/
|
|
89
|
+
public readonly changePlan = $action({
|
|
90
|
+
method: "POST",
|
|
91
|
+
path: `${this.url}/mine/change-plan`,
|
|
92
|
+
group: this.group,
|
|
93
|
+
use: [$secure({ permissions: ["subscription:update"] })],
|
|
94
|
+
description: "Upgrade or downgrade the subscription plan",
|
|
95
|
+
schema: {
|
|
96
|
+
body: changePlanSchema,
|
|
97
|
+
response: subscriptionResourceSchema,
|
|
98
|
+
},
|
|
99
|
+
handler: async ({ body, user }) => {
|
|
100
|
+
const sub = await this.service.getByOrganization(user.organization!);
|
|
101
|
+
if (!sub)
|
|
102
|
+
throw new NotFoundError("No subscription found for your organization");
|
|
103
|
+
await this.service.changePlan(sub.id, body.planId, body.interval, {
|
|
104
|
+
immediate: body.immediate,
|
|
105
|
+
});
|
|
106
|
+
return this.service.getSubscription(sub.id);
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Cancel the current organization's subscription.
|
|
112
|
+
*/
|
|
113
|
+
public readonly cancel = $action({
|
|
114
|
+
method: "POST",
|
|
115
|
+
path: `${this.url}/mine/cancel`,
|
|
116
|
+
group: this.group,
|
|
117
|
+
use: [$secure({ permissions: ["subscription:update"] })],
|
|
118
|
+
description: "Cancel the current subscription",
|
|
119
|
+
schema: {
|
|
120
|
+
body: cancelSubscriptionSchema,
|
|
121
|
+
response: okSchema,
|
|
122
|
+
},
|
|
123
|
+
handler: async ({ body, user }) => {
|
|
124
|
+
const sub = await this.service.getByOrganization(user.organization!);
|
|
125
|
+
if (!sub)
|
|
126
|
+
throw new NotFoundError("No subscription found for your organization");
|
|
127
|
+
await this.service.cancel(sub.id, {
|
|
128
|
+
reason: body.reason,
|
|
129
|
+
immediate: body.immediate,
|
|
130
|
+
});
|
|
131
|
+
return { ok: true };
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Resume a cancelled subscription before the period ends.
|
|
137
|
+
*/
|
|
138
|
+
public readonly resume = $action({
|
|
139
|
+
method: "POST",
|
|
140
|
+
path: `${this.url}/mine/resume`,
|
|
141
|
+
group: this.group,
|
|
142
|
+
use: [$secure({ permissions: ["subscription:update"] })],
|
|
143
|
+
description: "Resume a cancelled subscription",
|
|
144
|
+
schema: {
|
|
145
|
+
response: okSchema,
|
|
146
|
+
},
|
|
147
|
+
handler: async ({ user }) => {
|
|
148
|
+
const sub = await this.service.getByOrganization(user.organization!);
|
|
149
|
+
if (!sub)
|
|
150
|
+
throw new NotFoundError("No subscription found for your organization");
|
|
151
|
+
await this.service.resume(sub.id);
|
|
152
|
+
return { ok: true };
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Get the billing event history for the current organization's subscription.
|
|
158
|
+
*/
|
|
159
|
+
public readonly getSubscriptionHistory = $action({
|
|
160
|
+
path: `${this.url}/mine/history`,
|
|
161
|
+
group: this.group,
|
|
162
|
+
use: [$secure()],
|
|
163
|
+
description: "Get the subscription billing event history",
|
|
164
|
+
schema: {
|
|
165
|
+
response: t.array(subscriptionEventResourceSchema),
|
|
166
|
+
},
|
|
167
|
+
handler: async ({ user }) => {
|
|
168
|
+
const sub = await this.service.getByOrganization(user.organization!);
|
|
169
|
+
if (!sub)
|
|
170
|
+
throw new NotFoundError("No subscription found for your organization");
|
|
171
|
+
return this.service.getHistory(sub.id);
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Get the feature and usage limit entitlements for the current organization.
|
|
177
|
+
*/
|
|
178
|
+
public readonly getEntitlements = $action({
|
|
179
|
+
path: `${this.url}/mine/entitlements`,
|
|
180
|
+
group: this.group,
|
|
181
|
+
use: [$secure()],
|
|
182
|
+
description:
|
|
183
|
+
"Get the feature and limit entitlements for the current organization",
|
|
184
|
+
schema: {
|
|
185
|
+
response: entitlementsSchema,
|
|
186
|
+
},
|
|
187
|
+
handler: ({ user }) => this.service.getEntitlements(user.organization!),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Static, t } from "alepha";
|
|
2
|
+
import { $entity, db } from "alepha/orm";
|
|
3
|
+
import { subscriptions } from "./subscriptions.ts";
|
|
4
|
+
|
|
5
|
+
export const subscriptionEvents = $entity({
|
|
6
|
+
name: "subscription_events",
|
|
7
|
+
schema: t.object({
|
|
8
|
+
id: db.primaryKey(t.uuid()),
|
|
9
|
+
createdAt: db.createdAt(),
|
|
10
|
+
subscriptionId: db.ref(t.uuid(), () => subscriptions.cols.id, {
|
|
11
|
+
onDelete: "cascade",
|
|
12
|
+
}),
|
|
13
|
+
organizationId: db.organization(),
|
|
14
|
+
|
|
15
|
+
type: t.enum([
|
|
16
|
+
"created",
|
|
17
|
+
"trial_started",
|
|
18
|
+
"trial_ended",
|
|
19
|
+
"activated",
|
|
20
|
+
"renewed",
|
|
21
|
+
"payment_failed",
|
|
22
|
+
"payment_retried",
|
|
23
|
+
"past_due",
|
|
24
|
+
"suspended",
|
|
25
|
+
"reactivated",
|
|
26
|
+
"plan_changed",
|
|
27
|
+
"plan_change_scheduled",
|
|
28
|
+
"cancelled",
|
|
29
|
+
"expired",
|
|
30
|
+
"resumed",
|
|
31
|
+
]),
|
|
32
|
+
|
|
33
|
+
// Context
|
|
34
|
+
previousStatus: t.optional(t.string()),
|
|
35
|
+
newStatus: t.optional(t.string()),
|
|
36
|
+
previousPlanId: t.optional(t.string()),
|
|
37
|
+
newPlanId: t.optional(t.string()),
|
|
38
|
+
paymentIntentId: t.optional(t.uuid()),
|
|
39
|
+
amount: t.optional(t.integer()),
|
|
40
|
+
currency: t.optional(t.string()),
|
|
41
|
+
|
|
42
|
+
// Who / why
|
|
43
|
+
triggeredBy: t.optional(t.string()),
|
|
44
|
+
userId: t.optional(t.uuid()),
|
|
45
|
+
note: t.optional(t.string()),
|
|
46
|
+
}),
|
|
47
|
+
indexes: [
|
|
48
|
+
{ columns: ["subscriptionId", "createdAt"] },
|
|
49
|
+
{ columns: ["organizationId", "createdAt"] },
|
|
50
|
+
{ columns: ["type"] },
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export type SubscriptionEventEntity = Static<typeof subscriptionEvents.schema>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type Static, t } from "alepha";
|
|
2
|
+
import { $entity, db } from "alepha/orm";
|
|
3
|
+
|
|
4
|
+
export const subscriptions = $entity({
|
|
5
|
+
name: "subscriptions",
|
|
6
|
+
schema: t.object({
|
|
7
|
+
id: db.primaryKey(t.uuid()),
|
|
8
|
+
version: db.version(),
|
|
9
|
+
createdAt: db.createdAt(),
|
|
10
|
+
updatedAt: db.updatedAt(),
|
|
11
|
+
organizationId: db.organization(),
|
|
12
|
+
|
|
13
|
+
// Plan
|
|
14
|
+
planId: t.string(),
|
|
15
|
+
interval: t.enum(["monthly", "yearly"]),
|
|
16
|
+
|
|
17
|
+
// Status
|
|
18
|
+
status: t.enum([
|
|
19
|
+
"trialing",
|
|
20
|
+
"active",
|
|
21
|
+
"past_due",
|
|
22
|
+
"suspended",
|
|
23
|
+
"cancelled",
|
|
24
|
+
"expired",
|
|
25
|
+
]),
|
|
26
|
+
|
|
27
|
+
// Billing cycle
|
|
28
|
+
currentPeriodStart: t.datetime(),
|
|
29
|
+
currentPeriodEnd: t.datetime(),
|
|
30
|
+
|
|
31
|
+
// Trial
|
|
32
|
+
trialStart: t.optional(t.datetime()),
|
|
33
|
+
trialEnd: t.optional(t.datetime()),
|
|
34
|
+
|
|
35
|
+
// Cancellation
|
|
36
|
+
cancelledAt: t.optional(t.datetime()),
|
|
37
|
+
cancelReason: t.optional(t.string()),
|
|
38
|
+
cancelAtPeriodEnd: t.boolean({ default: false }),
|
|
39
|
+
|
|
40
|
+
// Payment tracking
|
|
41
|
+
lastPaymentIntentId: t.optional(t.uuid()),
|
|
42
|
+
lastPaymentAt: t.optional(t.datetime()),
|
|
43
|
+
nextBillingAt: t.optional(t.datetime()),
|
|
44
|
+
|
|
45
|
+
// Dunning state
|
|
46
|
+
dunningStartedAt: t.optional(t.datetime()),
|
|
47
|
+
dunningAttempt: t.integer({ default: 0 }),
|
|
48
|
+
dunningNextRetryAt: t.optional(t.datetime()),
|
|
49
|
+
|
|
50
|
+
// Plan change (pending)
|
|
51
|
+
pendingPlanId: t.optional(t.string()),
|
|
52
|
+
pendingInterval: t.optional(t.enum(["monthly", "yearly"])),
|
|
53
|
+
|
|
54
|
+
// Metadata
|
|
55
|
+
metadata: t.optional(t.record(t.text(), t.any())),
|
|
56
|
+
}),
|
|
57
|
+
indexes: [
|
|
58
|
+
{ columns: ["organizationId"], unique: true },
|
|
59
|
+
{ columns: ["status"] },
|
|
60
|
+
{ columns: ["planId", "status"] },
|
|
61
|
+
{ columns: ["nextBillingAt"] },
|
|
62
|
+
{ columns: ["trialEnd"] },
|
|
63
|
+
{ columns: ["dunningNextRetryAt"] },
|
|
64
|
+
{ columns: ["currentPeriodEnd"] },
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export type SubscriptionEntity = Static<typeof subscriptions.schema>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { $module } from "alepha";
|
|
2
|
+
import { AdminSubscriptionController } from "./controllers/AdminSubscriptionController.ts";
|
|
3
|
+
import { SubscriptionController } from "./controllers/SubscriptionController.ts";
|
|
4
|
+
import { SubscriptionJobs } from "./jobs/SubscriptionJobs.ts";
|
|
5
|
+
import { SubscriptionNotifications } from "./notifications/SubscriptionNotifications.ts";
|
|
6
|
+
import { BillingService } from "./services/BillingService.ts";
|
|
7
|
+
import { SubscriptionConfig } from "./services/SubscriptionConfig.ts";
|
|
8
|
+
import { SubscriptionService } from "./services/SubscriptionService.ts";
|
|
9
|
+
import { UsageService } from "./services/UsageService.ts";
|
|
10
|
+
|
|
11
|
+
// Controllers
|
|
12
|
+
export * from "./controllers/AdminSubscriptionController.ts";
|
|
13
|
+
export * from "./controllers/SubscriptionController.ts";
|
|
14
|
+
// Entities
|
|
15
|
+
export * from "./entities/subscriptionEvents.ts";
|
|
16
|
+
export * from "./entities/subscriptions.ts";
|
|
17
|
+
// Jobs
|
|
18
|
+
export * from "./jobs/SubscriptionJobs.ts";
|
|
19
|
+
// Middleware
|
|
20
|
+
export * from "./middleware/$requireLimit.ts";
|
|
21
|
+
export * from "./middleware/$requirePlan.ts";
|
|
22
|
+
// Notifications
|
|
23
|
+
export * from "./notifications/SubscriptionNotifications.ts";
|
|
24
|
+
// Schemas
|
|
25
|
+
export * from "./schemas/cancelSubscriptionSchema.ts";
|
|
26
|
+
export * from "./schemas/changePlanSchema.ts";
|
|
27
|
+
export * from "./schemas/createSubscriptionSchema.ts";
|
|
28
|
+
export * from "./schemas/entitlementsSchema.ts";
|
|
29
|
+
export * from "./schemas/mrrSchema.ts";
|
|
30
|
+
export * from "./schemas/planDefinitionSchema.ts";
|
|
31
|
+
export * from "./schemas/planResourceSchema.ts";
|
|
32
|
+
export * from "./schemas/subscriptionEventResourceSchema.ts";
|
|
33
|
+
export * from "./schemas/subscriptionQuerySchema.ts";
|
|
34
|
+
export * from "./schemas/subscriptionResourceSchema.ts";
|
|
35
|
+
export * from "./schemas/subscriptionSettingsSchema.ts";
|
|
36
|
+
export * from "./schemas/subscriptionStatsSchema.ts";
|
|
37
|
+
// Services
|
|
38
|
+
export * from "./services/BillingService.ts";
|
|
39
|
+
export * from "./services/SubscriptionConfig.ts";
|
|
40
|
+
export * from "./services/SubscriptionService.ts";
|
|
41
|
+
export * from "./services/UsageService.ts";
|
|
42
|
+
|
|
43
|
+
declare module "alepha" {
|
|
44
|
+
interface Hooks {
|
|
45
|
+
"subscription:created": {
|
|
46
|
+
subscriptionId: string;
|
|
47
|
+
organizationId: string;
|
|
48
|
+
planId: string;
|
|
49
|
+
trial: boolean;
|
|
50
|
+
};
|
|
51
|
+
"subscription:activated": {
|
|
52
|
+
subscriptionId: string;
|
|
53
|
+
organizationId: string;
|
|
54
|
+
planId: string;
|
|
55
|
+
};
|
|
56
|
+
"subscription:renewed": {
|
|
57
|
+
subscriptionId: string;
|
|
58
|
+
organizationId: string;
|
|
59
|
+
planId: string;
|
|
60
|
+
amount: number;
|
|
61
|
+
currency: string;
|
|
62
|
+
};
|
|
63
|
+
"subscription:cancelled": {
|
|
64
|
+
subscriptionId: string;
|
|
65
|
+
organizationId: string;
|
|
66
|
+
planId: string;
|
|
67
|
+
reason?: string;
|
|
68
|
+
immediate: boolean;
|
|
69
|
+
};
|
|
70
|
+
"subscription:expired": {
|
|
71
|
+
subscriptionId: string;
|
|
72
|
+
organizationId: string;
|
|
73
|
+
planId: string;
|
|
74
|
+
};
|
|
75
|
+
"subscription:resumed": {
|
|
76
|
+
subscriptionId: string;
|
|
77
|
+
organizationId: string;
|
|
78
|
+
planId: string;
|
|
79
|
+
};
|
|
80
|
+
"subscription:plan_changed": {
|
|
81
|
+
subscriptionId: string;
|
|
82
|
+
organizationId: string;
|
|
83
|
+
oldPlanId: string;
|
|
84
|
+
newPlanId: string;
|
|
85
|
+
immediate: boolean;
|
|
86
|
+
};
|
|
87
|
+
"subscription:payment_failed": {
|
|
88
|
+
subscriptionId: string;
|
|
89
|
+
organizationId: string;
|
|
90
|
+
planId: string;
|
|
91
|
+
attempt: number;
|
|
92
|
+
};
|
|
93
|
+
"subscription:suspended": {
|
|
94
|
+
subscriptionId: string;
|
|
95
|
+
organizationId: string;
|
|
96
|
+
planId: string;
|
|
97
|
+
};
|
|
98
|
+
"subscription:reactivated": {
|
|
99
|
+
subscriptionId: string;
|
|
100
|
+
organizationId: string;
|
|
101
|
+
planId: string;
|
|
102
|
+
};
|
|
103
|
+
"subscription:trial_ending": {
|
|
104
|
+
subscriptionId: string;
|
|
105
|
+
organizationId: string;
|
|
106
|
+
planId: string;
|
|
107
|
+
endsAt: string;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Subscription management module — plan-based access control, billing integration,
|
|
114
|
+
* usage limits, and lifecycle events (trial, renewal, cancellation, suspension).
|
|
115
|
+
*
|
|
116
|
+
* Depends on `AlephaPayments` for payment processing — register it in your app
|
|
117
|
+
* alongside this module. Use `SubscriptionConfig` to declare your plans and limits.
|
|
118
|
+
*
|
|
119
|
+
* @module alepha.api.subscriptions
|
|
120
|
+
*/
|
|
121
|
+
export const AlephaApiSubscriptions = $module({
|
|
122
|
+
name: "alepha.api.subscriptions",
|
|
123
|
+
services: [
|
|
124
|
+
SubscriptionConfig,
|
|
125
|
+
SubscriptionService,
|
|
126
|
+
BillingService,
|
|
127
|
+
UsageService,
|
|
128
|
+
SubscriptionJobs,
|
|
129
|
+
SubscriptionNotifications,
|
|
130
|
+
SubscriptionController,
|
|
131
|
+
AdminSubscriptionController,
|
|
132
|
+
],
|
|
133
|
+
});
|