alepha 0.19.2 → 0.19.4
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/assets/swagger-ui/swagger-ui-bundle.js +1 -1
- package/dist/api/audits/index.d.ts +8 -8
- package/dist/api/invitations/index.d.ts +790 -0
- package/dist/api/invitations/index.d.ts.map +1 -0
- package/dist/api/invitations/index.js +665 -0
- package/dist/api/invitations/index.js.map +1 -0
- package/dist/api/jobs/index.browser.js +8 -9
- package/dist/api/jobs/index.browser.js.map +1 -1
- package/dist/api/jobs/index.d.ts +90 -34
- package/dist/api/jobs/index.d.ts.map +1 -1
- package/dist/api/jobs/index.js +267 -44
- package/dist/api/jobs/index.js.map +1 -1
- package/dist/api/notifications/index.browser.js +0 -1
- package/dist/api/notifications/index.browser.js.map +1 -1
- package/dist/api/notifications/index.d.ts +3 -3
- package/dist/api/notifications/index.d.ts.map +1 -1
- package/dist/api/notifications/index.js +0 -1
- package/dist/api/notifications/index.js.map +1 -1
- package/dist/api/parameters/index.browser.js +112 -1
- package/dist/api/parameters/index.browser.js.map +1 -1
- package/dist/api/parameters/index.d.ts +90 -3
- package/dist/api/parameters/index.d.ts.map +1 -1
- package/dist/api/parameters/index.js +79 -12
- package/dist/api/parameters/index.js.map +1 -1
- package/dist/{billing → api/payments}/index.d.ts +67 -49
- package/dist/api/payments/index.d.ts.map +1 -0
- package/dist/{billing → api/payments}/index.js +108 -74
- package/dist/api/payments/index.js.map +1 -0
- package/dist/api/subscriptions/index.d.ts +1692 -0
- package/dist/api/subscriptions/index.d.ts.map +1 -0
- package/dist/api/subscriptions/index.js +1870 -0
- package/dist/api/subscriptions/index.js.map +1 -0
- package/dist/api/users/index.d.ts +27 -21
- package/dist/api/users/index.d.ts.map +1 -1
- package/dist/api/users/index.js +167 -34
- package/dist/api/users/index.js.map +1 -1
- package/dist/api/workflows/index.browser.js +246 -0
- package/dist/api/workflows/index.browser.js.map +1 -0
- package/dist/api/workflows/index.d.ts +1618 -0
- package/dist/api/workflows/index.d.ts.map +1 -0
- package/dist/api/workflows/index.js +1504 -0
- package/dist/api/workflows/index.js.map +1 -0
- package/dist/cli/config/index.d.ts +6 -28
- package/dist/cli/config/index.d.ts.map +1 -1
- package/dist/cli/config/index.js +5 -10
- package/dist/cli/config/index.js.map +1 -1
- package/dist/cli/core/index.d.ts +11669 -208
- package/dist/cli/core/index.d.ts.map +1 -1
- package/dist/cli/core/index.js +60 -69
- package/dist/cli/core/index.js.map +1 -1
- package/dist/cli/devtools/index.d.ts +5 -0
- package/dist/cli/devtools/index.d.ts.map +1 -1
- package/dist/cli/devtools/index.js +4 -0
- package/dist/cli/devtools/index.js.map +1 -1
- package/dist/cli/platform/index.d.ts +69 -64
- package/dist/cli/platform/index.d.ts.map +1 -1
- package/dist/cli/platform/index.js +6 -2
- package/dist/cli/platform/index.js.map +1 -1
- package/dist/cli/vendor/index.d.ts +38 -10
- package/dist/cli/vendor/index.d.ts.map +1 -1
- package/dist/cli/vendor/index.js +85 -26
- package/dist/cli/vendor/index.js.map +1 -1
- package/dist/core/index.browser.js +21 -2
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.d.ts +33 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +25 -2
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.native.js +25 -2
- package/dist/core/index.native.js.map +1 -1
- package/dist/core/index.workerd.js +25 -2
- package/dist/core/index.workerd.js.map +1 -1
- package/dist/email/smtp/index.js +24 -8
- package/dist/email/smtp/index.js.map +1 -1
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +1 -1
- package/dist/logger/index.js.map +1 -1
- package/dist/orm/core/index.browser.js +0 -18
- package/dist/orm/core/index.browser.js.map +1 -1
- package/dist/orm/core/index.bun.js +25 -73
- package/dist/orm/core/index.bun.js.map +1 -1
- package/dist/orm/core/index.d.ts +10 -32
- package/dist/orm/core/index.d.ts.map +1 -1
- package/dist/orm/core/index.js +25 -73
- package/dist/orm/core/index.js.map +1 -1
- package/dist/orm/postgres/index.bun.js +3 -3
- package/dist/orm/postgres/index.bun.js.map +1 -1
- package/dist/orm/postgres/index.d.ts +2 -1
- package/dist/orm/postgres/index.d.ts.map +1 -1
- package/dist/orm/postgres/index.js +3 -3
- package/dist/orm/postgres/index.js.map +1 -1
- package/dist/react/router/index.browser.js +25 -3
- package/dist/react/router/index.browser.js.map +1 -1
- package/dist/react/router/index.d.ts +16 -1
- package/dist/react/router/index.d.ts.map +1 -1
- package/dist/react/router/index.js +25 -3
- package/dist/react/router/index.js.map +1 -1
- package/dist/security/index.d.ts +28 -0
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security/index.js +28 -0
- package/dist/security/index.js.map +1 -1
- package/package.json +37 -20
- package/src/api/invitations/__tests__/InvitationService.spec.ts +439 -0
- package/src/api/invitations/controllers/AdminInvitationController.ts +86 -0
- package/src/api/invitations/controllers/InvitationController.ts +84 -0
- package/src/api/invitations/entities/invitations.ts +33 -0
- package/src/api/invitations/index.ts +65 -0
- package/src/api/invitations/jobs/InvitationJobs.ts +37 -0
- package/src/api/invitations/providers/InvitationProvider.ts +45 -0
- package/src/api/invitations/schemas/createInvitationSchema.ts +12 -0
- package/src/api/invitations/schemas/invitationConfigAtom.ts +20 -0
- package/src/api/invitations/schemas/invitationQuerySchema.ts +15 -0
- package/src/api/invitations/schemas/invitationResourceSchema.ts +6 -0
- package/src/api/invitations/schemas/invitationWithResourceInfoSchema.ts +22 -0
- package/src/api/invitations/schemas/myInvitationsQuerySchema.ts +10 -0
- package/src/api/invitations/services/InvitationService.ts +556 -0
- package/src/api/jobs/__tests__/$job.spec.ts +876 -0
- package/src/api/jobs/controllers/AdminJobController.ts +44 -0
- package/src/api/jobs/entities/jobExecutionEntity.ts +0 -2
- package/src/api/jobs/index.ts +0 -3
- package/src/api/jobs/primitives/$job.ts +22 -11
- package/src/api/jobs/providers/JobProvider.ts +239 -25
- package/src/api/jobs/schemas/jobConfigAtom.ts +4 -0
- package/src/api/jobs/schemas/jobCronInfoSchema.ts +1 -0
- package/src/api/jobs/schemas/jobExecutionQuerySchema.ts +0 -1
- package/src/api/jobs/schemas/jobQueueDepthSchema.ts +1 -0
- package/src/api/jobs/schemas/jobRegistrationSchema.ts +1 -6
- package/src/api/jobs/services/JobService.ts +51 -12
- package/src/api/notifications/schemas/notificationQuerySchema.ts +0 -1
- package/src/api/parameters/__tests__/$parameter.spec.ts +327 -0
- package/src/api/parameters/controllers/AdminParameterController.ts +29 -3
- package/src/api/parameters/index.browser.ts +12 -0
- package/src/api/parameters/primitives/$parameter.ts +20 -3
- package/src/api/parameters/services/ParameterProvider.ts +48 -7
- package/src/{billing → api/payments}/__tests__/PaymentMethodService.spec.ts +32 -6
- package/src/api/payments/__tests__/PaymentService.spec.ts +279 -0
- package/src/{billing/controllers/AdminBillingController.ts → api/payments/controllers/AdminPaymentController.ts} +26 -21
- package/src/{billing/controllers/BillingController.ts → api/payments/controllers/PaymentController.ts} +23 -11
- package/src/{billing → api/payments}/entities/paymentIntents.ts +1 -0
- package/src/{billing/errors/BillingError.ts → api/payments/errors/PaymentError.ts} +1 -1
- package/src/{billing → api/payments}/index.ts +31 -25
- package/src/{billing/providers/MemoryBillingProvider.ts → api/payments/providers/MemoryPaymentProvider.ts} +4 -4
- package/src/{billing/providers/BillingProvider.ts → api/payments/providers/PaymentProvider.ts} +9 -2
- package/src/{billing → api/payments}/services/PaymentMethodService.ts +5 -5
- package/src/{billing/services/BillingService.ts → api/payments/services/PaymentService.ts} +94 -18
- 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 +144 -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/api/users/__tests__/AdminUserController.spec.ts +80 -1
- package/src/api/users/__tests__/CredentialService.spec.ts +177 -0
- package/src/api/users/__tests__/EmailVerification.spec.ts +29 -18
- package/src/api/users/__tests__/PasswordReset.spec.ts +3 -0
- package/src/api/users/__tests__/RegistrationService.spec.ts +148 -1
- package/src/api/users/__tests__/SessionService.spec.ts +142 -1
- package/src/api/users/atoms/realmAuthSettingsAtom.ts +10 -1
- package/src/api/users/controllers/UserController.ts +3 -8
- package/src/api/users/notifications/UserNotifications.ts +23 -0
- package/src/api/users/schemas/loginSchema.ts +1 -1
- package/src/api/users/services/CredentialService.ts +51 -4
- package/src/api/users/services/RegistrationService.ts +38 -9
- package/src/api/users/services/SessionService.ts +62 -9
- package/src/api/users/services/UserService.ts +21 -12
- package/src/api/workflows/__tests__/$workflow.spec.ts +616 -0
- package/src/api/workflows/controllers/AdminWorkflowController.ts +191 -0
- package/src/api/workflows/entities/workflowExecutions.ts +74 -0
- package/src/api/workflows/entities/workflowStepExecutions.ts +74 -0
- package/src/api/workflows/entities/workflowStepLogs.ts +13 -0
- package/src/api/workflows/index.browser.ts +22 -0
- package/src/api/workflows/index.ts +124 -0
- package/src/api/workflows/jobs/WorkflowJobs.ts +77 -0
- package/src/api/workflows/primitives/$workflow.ts +202 -0
- package/src/api/workflows/providers/WorkflowProvider.ts +1284 -0
- package/src/api/workflows/schemas/workflowActivitySchema.ts +15 -0
- package/src/api/workflows/schemas/workflowConfigAtom.ts +51 -0
- package/src/api/workflows/schemas/workflowExecutionDetailSchema.ts +18 -0
- package/src/api/workflows/schemas/workflowExecutionQuerySchema.ts +26 -0
- package/src/api/workflows/schemas/workflowExecutionResourceSchema.ts +30 -0
- package/src/api/workflows/schemas/workflowRegistrationSchema.ts +26 -0
- package/src/api/workflows/schemas/workflowStatsSchema.ts +16 -0
- package/src/api/workflows/schemas/workflowStepExecutionResourceSchema.ts +15 -0
- package/src/api/workflows/services/WorkflowService.ts +382 -0
- package/src/cli/config/defineConfig.ts +17 -46
- package/src/cli/core/providers/ViteDevServerProvider.ts +45 -3
- package/src/cli/core/services/PackageManagerUtils.ts +3 -1
- package/src/cli/core/services/ProjectScaffolder.ts +5 -5
- package/src/cli/core/templates/agentMd.ts +14 -5
- package/src/cli/core/templates/webAppRouterTs.ts +5 -58
- package/src/cli/devtools/index.ts +21 -1
- package/src/cli/platform/index.ts +23 -2
- package/src/cli/vendor/__tests__/VendorService.spec.ts +283 -178
- package/src/cli/vendor/index.ts +20 -3
- package/src/cli/vendor/services/VendorService.ts +126 -27
- package/src/core/Alepha.ts +10 -0
- package/src/core/__tests__/TypeProvider.spec.ts +4 -2
- package/src/core/providers/SchemaValidator.ts +1 -1
- package/src/core/providers/TypeProvider.ts +46 -3
- package/src/logger/index.ts +6 -1
- package/src/orm/__tests__/enums.spec.ts +22 -29
- package/src/orm/__tests__/orm-showcase-tests.ts +430 -0
- package/src/orm/__tests__/orm-showcase.spec.ts +167 -0
- package/src/orm/core/providers/DatabaseTypeProvider.ts +0 -29
- package/src/orm/core/providers/DrizzleKitProvider.ts +56 -105
- package/src/orm/postgres/services/PostgresModelBuilder.ts +3 -6
- package/src/react/router/__tests__/$page.browser.spec.tsx +157 -0
- package/src/react/router/providers/ReactBrowserProvider.ts +39 -0
- package/src/react/router/providers/ReactBrowserRouterProvider.ts +22 -0
- package/src/security/__tests__/$secure-combinations.spec.ts +945 -0
- package/src/security/primitives/$secure.ts +28 -0
- package/tsconfig.base.json +0 -1
- package/dist/billing/index.d.ts.map +0 -1
- package/dist/billing/index.js.map +0 -1
- package/src/billing/__tests__/BillingService.spec.ts +0 -136
- /package/src/{billing → api/payments}/entities/paymentMethods.ts +0 -0
- /package/src/{billing → api/payments}/entities/refunds.ts +0 -0
- /package/src/{billing → api/payments}/schemas/intentSchemas.ts +0 -0
- /package/src/{billing → api/payments}/schemas/paymentMethodSchemas.ts +0 -0
- /package/src/{billing → api/payments}/schemas/refundSchemas.ts +0 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { AlephaOrmPostgres } from "alepha/orm/postgres";
|
|
4
|
+
import { BadRequestError } from "alepha/server";
|
|
5
|
+
import { describe, it } from "vitest";
|
|
6
|
+
import { AlephaApiSubscriptions } from "../index.ts";
|
|
7
|
+
import type { PlanDefinition } from "../schemas/planDefinitionSchema.ts";
|
|
8
|
+
import { SubscriptionConfig } from "../services/SubscriptionConfig.ts";
|
|
9
|
+
import { SubscriptionService } from "../services/SubscriptionService.ts";
|
|
10
|
+
|
|
11
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
class TestSubscriptionConfig extends SubscriptionConfig {
|
|
14
|
+
public async seedPlans(plans: PlanDefinition[]): Promise<void> {
|
|
15
|
+
await this.plans.set({ plans });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
const testPlans: PlanDefinition[] = [
|
|
22
|
+
{
|
|
23
|
+
id: "free",
|
|
24
|
+
name: "Free",
|
|
25
|
+
available: true,
|
|
26
|
+
pricing: [{ interval: "monthly", amount: 0, currency: "usd" }],
|
|
27
|
+
features: ["dashboard"],
|
|
28
|
+
limits: { seats: 1, "api-calls": 100 },
|
|
29
|
+
order: 0,
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "pro",
|
|
33
|
+
name: "Pro",
|
|
34
|
+
available: true,
|
|
35
|
+
pricing: [
|
|
36
|
+
{ interval: "monthly", amount: 2900, currency: "usd" },
|
|
37
|
+
{ interval: "yearly", amount: 29000, currency: "usd" },
|
|
38
|
+
],
|
|
39
|
+
trial: { days: 14, requirePaymentMethod: false },
|
|
40
|
+
features: ["dashboard", "analytics", "export"],
|
|
41
|
+
limits: { seats: 10, "api-calls": 10000 },
|
|
42
|
+
order: 1,
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
const setup = async () => {
|
|
49
|
+
const alepha = Alepha.create()
|
|
50
|
+
.with(AlephaOrmPostgres)
|
|
51
|
+
.with({ provide: SubscriptionConfig, use: TestSubscriptionConfig })
|
|
52
|
+
.with(AlephaApiSubscriptions);
|
|
53
|
+
|
|
54
|
+
const service = alepha.inject(SubscriptionService);
|
|
55
|
+
const config = alepha.inject(
|
|
56
|
+
TestSubscriptionConfig,
|
|
57
|
+
) as TestSubscriptionConfig;
|
|
58
|
+
await alepha.start();
|
|
59
|
+
|
|
60
|
+
await config.seedPlans(testPlans);
|
|
61
|
+
|
|
62
|
+
return { alepha, service, config };
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
describe("SubscriptionService", () => {
|
|
68
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
describe("subscribe", () => {
|
|
71
|
+
it("should create a trialing subscription with trial days", async ({
|
|
72
|
+
expect,
|
|
73
|
+
}) => {
|
|
74
|
+
const { service } = await setup();
|
|
75
|
+
const orgId = randomUUID();
|
|
76
|
+
|
|
77
|
+
const sub = await service.subscribe(orgId, "pro", "monthly");
|
|
78
|
+
|
|
79
|
+
expect(sub.status).toBe("trialing");
|
|
80
|
+
expect(sub.planId).toBe("pro");
|
|
81
|
+
expect(sub.interval).toBe("monthly");
|
|
82
|
+
expect(sub.organizationId).toBe(orgId);
|
|
83
|
+
expect(sub.trialStart).toBeDefined();
|
|
84
|
+
expect(sub.trialEnd).toBeDefined();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should create an active subscription when skipTrial", async ({
|
|
88
|
+
expect,
|
|
89
|
+
}) => {
|
|
90
|
+
const { service } = await setup();
|
|
91
|
+
const orgId = randomUUID();
|
|
92
|
+
|
|
93
|
+
const sub = await service.subscribe(orgId, "pro", "monthly", {
|
|
94
|
+
skipTrial: true,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
expect(sub.status).toBe("active");
|
|
98
|
+
expect(sub.planId).toBe("pro");
|
|
99
|
+
expect(sub.trialStart).toBeUndefined();
|
|
100
|
+
expect(sub.trialEnd).toBeUndefined();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should reject when plan not found", async ({ expect }) => {
|
|
104
|
+
const { service } = await setup();
|
|
105
|
+
const orgId = randomUUID();
|
|
106
|
+
|
|
107
|
+
await expect(
|
|
108
|
+
service.subscribe(orgId, "nonexistent", "monthly"),
|
|
109
|
+
).rejects.toThrow(BadRequestError);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should reject duplicate subscription for same org", async ({
|
|
113
|
+
expect,
|
|
114
|
+
}) => {
|
|
115
|
+
const { service } = await setup();
|
|
116
|
+
const orgId = randomUUID();
|
|
117
|
+
|
|
118
|
+
await service.subscribe(orgId, "pro", "monthly", {
|
|
119
|
+
skipTrial: true,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
await expect(service.subscribe(orgId, "free", "monthly")).rejects.toThrow(
|
|
123
|
+
BadRequestError,
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
129
|
+
|
|
130
|
+
describe("cancel", () => {
|
|
131
|
+
it("should cancel at period end", async ({ expect }) => {
|
|
132
|
+
const { service } = await setup();
|
|
133
|
+
const orgId = randomUUID();
|
|
134
|
+
|
|
135
|
+
const sub = await service.subscribe(orgId, "pro", "monthly", {
|
|
136
|
+
skipTrial: true,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
await service.cancel(sub.id, { immediate: false });
|
|
140
|
+
|
|
141
|
+
const updated = await service.getSubscription(sub.id);
|
|
142
|
+
expect(updated.status).toBe("cancelled");
|
|
143
|
+
expect(updated.cancelAtPeriodEnd).toBe(true);
|
|
144
|
+
expect(updated.cancelledAt).toBeDefined();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("should cancel immediately with expired status", async ({ expect }) => {
|
|
148
|
+
const { service } = await setup();
|
|
149
|
+
const orgId = randomUUID();
|
|
150
|
+
|
|
151
|
+
const sub = await service.subscribe(orgId, "pro", "monthly", {
|
|
152
|
+
skipTrial: true,
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
await service.cancel(sub.id, { immediate: true });
|
|
156
|
+
|
|
157
|
+
const updated = await service.getSubscription(sub.id);
|
|
158
|
+
expect(updated.status).toBe("expired");
|
|
159
|
+
expect(updated.cancelAtPeriodEnd).toBe(false);
|
|
160
|
+
expect(updated.cancelledAt).toBeDefined();
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
describe("resume", () => {
|
|
167
|
+
it("should resume a cancelled subscription back to active", async ({
|
|
168
|
+
expect,
|
|
169
|
+
}) => {
|
|
170
|
+
const { service } = await setup();
|
|
171
|
+
const orgId = randomUUID();
|
|
172
|
+
|
|
173
|
+
const sub = await service.subscribe(orgId, "pro", "monthly", {
|
|
174
|
+
skipTrial: true,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await service.cancel(sub.id, { immediate: false });
|
|
178
|
+
|
|
179
|
+
const cancelled = await service.getSubscription(sub.id);
|
|
180
|
+
expect(cancelled.status).toBe("cancelled");
|
|
181
|
+
|
|
182
|
+
await service.resume(sub.id);
|
|
183
|
+
|
|
184
|
+
const resumed = await service.getSubscription(sub.id);
|
|
185
|
+
expect(resumed.status).toBe("active");
|
|
186
|
+
expect(resumed.cancelAtPeriodEnd).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
describe("changePlan", () => {
|
|
193
|
+
it("should schedule plan change at period end", async ({ expect }) => {
|
|
194
|
+
const { service } = await setup();
|
|
195
|
+
const orgId = randomUUID();
|
|
196
|
+
|
|
197
|
+
const sub = await service.subscribe(orgId, "free", "monthly", {
|
|
198
|
+
skipTrial: true,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
await service.changePlan(sub.id, "pro", "monthly", {
|
|
202
|
+
immediate: false,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const updated = await service.getSubscription(sub.id);
|
|
206
|
+
expect(updated.pendingPlanId).toBe("pro");
|
|
207
|
+
expect(updated.planId).toBe("free");
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("should apply immediate plan change", async ({ expect }) => {
|
|
211
|
+
const { service } = await setup();
|
|
212
|
+
const orgId = randomUUID();
|
|
213
|
+
|
|
214
|
+
const sub = await service.subscribe(orgId, "free", "monthly", {
|
|
215
|
+
skipTrial: true,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
await service.changePlan(sub.id, "pro", "monthly", {
|
|
219
|
+
immediate: true,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const updated = await service.getSubscription(sub.id);
|
|
223
|
+
expect(updated.planId).toBe("pro");
|
|
224
|
+
expect(updated.pendingPlanId).toBeUndefined();
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// ---------------------------------------------------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
describe("entitlements", () => {
|
|
231
|
+
it("should return true for included feature", async ({ expect }) => {
|
|
232
|
+
const { service } = await setup();
|
|
233
|
+
const orgId = randomUUID();
|
|
234
|
+
|
|
235
|
+
await service.subscribe(orgId, "pro", "monthly", {
|
|
236
|
+
skipTrial: true,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const canAnalytics = await service.can(orgId, "analytics");
|
|
240
|
+
expect(canAnalytics).toBe(true);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("should return false for excluded feature", async ({ expect }) => {
|
|
244
|
+
const { service } = await setup();
|
|
245
|
+
const orgId = randomUUID();
|
|
246
|
+
|
|
247
|
+
await service.subscribe(orgId, "free", "monthly", {
|
|
248
|
+
skipTrial: true,
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const canAnalytics = await service.can(orgId, "analytics");
|
|
252
|
+
expect(canAnalytics).toBe(false);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("should return correct limit value", async ({ expect }) => {
|
|
256
|
+
const { service } = await setup();
|
|
257
|
+
const orgId = randomUUID();
|
|
258
|
+
|
|
259
|
+
await service.subscribe(orgId, "pro", "monthly", {
|
|
260
|
+
skipTrial: true,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const seats = await service.limit(orgId, "seats");
|
|
264
|
+
expect(seats).toBe(10);
|
|
265
|
+
|
|
266
|
+
const apiCalls = await service.limit(orgId, "api-calls");
|
|
267
|
+
expect(apiCalls).toBe(10000);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("should return 0 limit for no subscription", async ({ expect }) => {
|
|
271
|
+
const { service } = await setup();
|
|
272
|
+
const orgId = randomUUID();
|
|
273
|
+
|
|
274
|
+
const seats = await service.limit(orgId, "seats");
|
|
275
|
+
expect(seats).toBe(0);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
});
|
|
@@ -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
|
+
}
|