@langwatch/server 3.1.0-beta.3 → 3.1.0-beta.5
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/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
- package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
- package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
- package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
- package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
- package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
- package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
- package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
- package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
- package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
- package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
- package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
- package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
- package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
- package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
- package/langwatch/ee/admin/impersonation.service.ts +170 -0
- package/langwatch/ee/admin/isAdmin.ts +9 -0
- package/langwatch/ee/admin/routes/admin.ts +531 -0
- package/langwatch/ee/admin/safeSelects.ts +64 -0
- package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
- package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
- package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
- package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
- package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
- package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
- package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
- package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
- package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
- package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
- package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
- package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
- package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
- package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
- package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
- package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
- package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
- package/langwatch/ee/billing/currencyRouter.ts +18 -0
- package/langwatch/ee/billing/errors.ts +129 -0
- package/langwatch/ee/billing/index.ts +67 -0
- package/langwatch/ee/billing/middleware.ts +17 -0
- package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
- package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
- package/langwatch/ee/billing/notifications/types.ts +9 -0
- package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
- package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
- package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
- package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
- package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
- package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
- package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
- package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
- package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
- package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
- package/langwatch/ee/billing/nurturing/types.ts +125 -0
- package/langwatch/ee/billing/planLimits.ts +190 -0
- package/langwatch/ee/billing/planProvider.ts +119 -0
- package/langwatch/ee/billing/planTypes.ts +53 -0
- package/langwatch/ee/billing/pricing.ts +57 -0
- package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
- package/langwatch/ee/billing/services/customerService.ts +90 -0
- package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
- package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
- package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
- package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
- package/langwatch/ee/billing/services/subscription.service.ts +549 -0
- package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
- package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
- package/langwatch/ee/billing/services/webhookService.ts +858 -0
- package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
- package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
- package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
- package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
- package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
- package/langwatch/ee/billing/types.ts +102 -0
- package/langwatch/ee/billing/utils/currency.ts +99 -0
- package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
- package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
- package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
- package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
- package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
- package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
- package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
- package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
- package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
- package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
- package/langwatch/ee/licensing/client.ts +29 -0
- package/langwatch/ee/licensing/constants.ts +195 -0
- package/langwatch/ee/licensing/defaults.ts +55 -0
- package/langwatch/ee/licensing/errors.ts +11 -0
- package/langwatch/ee/licensing/index.ts +52 -0
- package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
- package/langwatch/ee/licensing/licenseHandler.ts +308 -0
- package/langwatch/ee/licensing/planInfo.ts +46 -0
- package/langwatch/ee/licensing/planMapping.ts +37 -0
- package/langwatch/ee/licensing/planTemplates.ts +109 -0
- package/langwatch/ee/licensing/server.ts +43 -0
- package/langwatch/ee/licensing/signing.ts +43 -0
- package/langwatch/ee/licensing/types.ts +156 -0
- package/langwatch/ee/licensing/validation.ts +107 -0
- package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
- package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
- package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
- package/langwatch/package.json +1 -1
- package/langwatch_nlp/uv.lock +179 -5
- package/package.json +1 -1
- package/packages/server/dist/cli.cjs +192 -142
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const mockSendSlackSubscriptionEvent = vi.fn().mockResolvedValue(undefined);
|
|
4
|
+
|
|
5
|
+
vi.mock("../../../src/server/app-layer/app", () => ({
|
|
6
|
+
getApp: () => ({
|
|
7
|
+
notifications: {
|
|
8
|
+
sendSlackSubscriptionEvent: mockSendSlackSubscriptionEvent,
|
|
9
|
+
},
|
|
10
|
+
}),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
import type { PrismaClient } from "@prisma/client";
|
|
14
|
+
import type Stripe from "stripe";
|
|
15
|
+
import { PlanTypes, SubscriptionStatus } from "../planTypes";
|
|
16
|
+
import { EESubscriptionService, RECENT_INVOICES_LIMIT } from "../services/subscription.service";
|
|
17
|
+
import { InvalidPlanError, OrganizationNotFoundError, SeatBillingUnavailableError } from "../errors";
|
|
18
|
+
import type { SeatEventSubscriptionFns } from "../services/seatEventSubscription";
|
|
19
|
+
import type { SubscriptionRepository } from "../../../src/server/app-layer/subscription/subscription.repository";
|
|
20
|
+
import type { OrganizationRepository } from "../../../src/server/app-layer/organizations/repositories/organization.repository";
|
|
21
|
+
|
|
22
|
+
const createMockStripe = () => ({
|
|
23
|
+
subscriptions: {
|
|
24
|
+
retrieve: vi.fn(),
|
|
25
|
+
update: vi.fn(),
|
|
26
|
+
cancel: vi.fn(),
|
|
27
|
+
},
|
|
28
|
+
checkout: {
|
|
29
|
+
sessions: {
|
|
30
|
+
create: vi.fn(),
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
billingPortal: {
|
|
34
|
+
sessions: {
|
|
35
|
+
create: vi.fn(),
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
invoices: {
|
|
39
|
+
list: vi.fn(),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const createMockRepository = (): {
|
|
44
|
+
[K in keyof SubscriptionRepository]: ReturnType<typeof vi.fn>;
|
|
45
|
+
} => ({
|
|
46
|
+
findLastNonCancelled: vi.fn(),
|
|
47
|
+
createPending: vi.fn(),
|
|
48
|
+
updateStatus: vi.fn(),
|
|
49
|
+
updatePlan: vi.fn(),
|
|
50
|
+
findByStripeId: vi.fn(),
|
|
51
|
+
linkStripeId: vi.fn(),
|
|
52
|
+
activate: vi.fn(),
|
|
53
|
+
recordPaymentFailure: vi.fn(),
|
|
54
|
+
cancel: vi.fn(),
|
|
55
|
+
cancelTrialSubscriptions: vi.fn(),
|
|
56
|
+
migrateToSeatEvent: vi.fn(),
|
|
57
|
+
updateQuantities: vi.fn(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const createMockDb = () => ({
|
|
61
|
+
team: {
|
|
62
|
+
findFirst: vi.fn(),
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const createMockItemCalculator = () => ({
|
|
67
|
+
getItemsToUpdate: vi.fn().mockReturnValue([]),
|
|
68
|
+
createItemsToAdd: vi.fn().mockReturnValue([]),
|
|
69
|
+
prices: { LAUNCH: "price_launch", FREE: undefined } as any,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const createMockOrganizationRepository = (): {
|
|
73
|
+
[K in keyof OrganizationRepository]: ReturnType<typeof vi.fn>;
|
|
74
|
+
} => ({
|
|
75
|
+
getOrganizationIdByTeamId: vi.fn(),
|
|
76
|
+
getProjectIds: vi.fn(),
|
|
77
|
+
getFeature: vi.fn(),
|
|
78
|
+
findWithAdmins: vi.fn(),
|
|
79
|
+
updateSentPlanLimitAlert: vi.fn(),
|
|
80
|
+
findProjectsWithName: vi.fn(),
|
|
81
|
+
clearTrialLicense: vi.fn(),
|
|
82
|
+
updateCurrency: vi.fn(),
|
|
83
|
+
getPricingModel: vi.fn(),
|
|
84
|
+
getStripeCustomerId: vi.fn(),
|
|
85
|
+
findByStripeCustomerId: vi.fn(),
|
|
86
|
+
findNameById: vi.fn(),
|
|
87
|
+
getOrganizationForBilling: vi.fn(),
|
|
88
|
+
createAndAssign: vi.fn(),
|
|
89
|
+
getAllForUser: vi.fn(),
|
|
90
|
+
getOrganizationWithMembers: vi.fn(),
|
|
91
|
+
getMemberById: vi.fn(),
|
|
92
|
+
getAllMembers: vi.fn(),
|
|
93
|
+
update: vi.fn(),
|
|
94
|
+
deleteMember: vi.fn(),
|
|
95
|
+
updateMemberRole: vi.fn(),
|
|
96
|
+
updateTeamMemberRole: vi.fn(),
|
|
97
|
+
getAuditLogs: vi.fn(),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const createMockSeatEventFns = (): {
|
|
101
|
+
[K in keyof SeatEventSubscriptionFns]: ReturnType<typeof vi.fn>;
|
|
102
|
+
} => ({
|
|
103
|
+
createSeatEventCheckout: vi.fn(),
|
|
104
|
+
updateSeatEventItems: vi.fn(),
|
|
105
|
+
previewProration: vi.fn(),
|
|
106
|
+
seatEventBillingPortalUrl: vi.fn(),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const createServiceWithSeatEventFns = ({
|
|
110
|
+
db,
|
|
111
|
+
repository,
|
|
112
|
+
stripe: stripeInstance,
|
|
113
|
+
itemCalculator: calc,
|
|
114
|
+
organizationRepository: orgRepo,
|
|
115
|
+
seatEventFns,
|
|
116
|
+
}: {
|
|
117
|
+
db: ReturnType<typeof createMockDb>;
|
|
118
|
+
repository: ReturnType<typeof createMockRepository>;
|
|
119
|
+
stripe: ReturnType<typeof createMockStripe>;
|
|
120
|
+
itemCalculator: ReturnType<typeof createMockItemCalculator>;
|
|
121
|
+
organizationRepository: ReturnType<typeof createMockOrganizationRepository>;
|
|
122
|
+
seatEventFns: ReturnType<typeof createMockSeatEventFns>;
|
|
123
|
+
}) =>
|
|
124
|
+
new EESubscriptionService({
|
|
125
|
+
prisma: db as unknown as PrismaClient,
|
|
126
|
+
repository: repository as unknown as SubscriptionRepository,
|
|
127
|
+
stripe: stripeInstance as unknown as Stripe,
|
|
128
|
+
itemCalculator: calc,
|
|
129
|
+
organizationRepository: orgRepo as unknown as OrganizationRepository,
|
|
130
|
+
seatEventFns: seatEventFns as unknown as SeatEventSubscriptionFns,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe("EESubscriptionService", () => {
|
|
134
|
+
let stripe: ReturnType<typeof createMockStripe>;
|
|
135
|
+
let db: ReturnType<typeof createMockDb>;
|
|
136
|
+
let repository: ReturnType<typeof createMockRepository>;
|
|
137
|
+
let itemCalculator: ReturnType<typeof createMockItemCalculator>;
|
|
138
|
+
let organizationRepository: ReturnType<typeof createMockOrganizationRepository>;
|
|
139
|
+
let service: EESubscriptionService;
|
|
140
|
+
|
|
141
|
+
beforeEach(() => {
|
|
142
|
+
vi.clearAllMocks();
|
|
143
|
+
stripe = createMockStripe();
|
|
144
|
+
db = createMockDb();
|
|
145
|
+
repository = createMockRepository();
|
|
146
|
+
itemCalculator = createMockItemCalculator();
|
|
147
|
+
organizationRepository = createMockOrganizationRepository();
|
|
148
|
+
service = new EESubscriptionService({
|
|
149
|
+
prisma: db as unknown as PrismaClient,
|
|
150
|
+
repository: repository as unknown as SubscriptionRepository,
|
|
151
|
+
stripe: stripe as unknown as Stripe,
|
|
152
|
+
itemCalculator,
|
|
153
|
+
organizationRepository: organizationRepository as unknown as OrganizationRepository,
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("updateSubscriptionItems()", () => {
|
|
158
|
+
describe("when active subscription exists", () => {
|
|
159
|
+
it("updates subscription items via Stripe", async () => {
|
|
160
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
161
|
+
id: "sub_db_1",
|
|
162
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
163
|
+
status: SubscriptionStatus.ACTIVE,
|
|
164
|
+
plan: PlanTypes.LAUNCH,
|
|
165
|
+
});
|
|
166
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
167
|
+
items: { data: [{ id: "si_1", price: { id: "price_launch" } }] },
|
|
168
|
+
});
|
|
169
|
+
itemCalculator.getItemsToUpdate.mockReturnValue([
|
|
170
|
+
{ id: "si_1", quantity: 1 },
|
|
171
|
+
]);
|
|
172
|
+
stripe.subscriptions.update.mockResolvedValue({});
|
|
173
|
+
|
|
174
|
+
const result = await service.updateSubscriptionItems({
|
|
175
|
+
organizationId: "org_123",
|
|
176
|
+
plan: PlanTypes.LAUNCH,
|
|
177
|
+
upgradeMembers: true,
|
|
178
|
+
upgradeTraces: true,
|
|
179
|
+
totalMembers: 5,
|
|
180
|
+
totalTraces: 30_000,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
expect(result).toEqual({ success: true });
|
|
184
|
+
expect(stripe.subscriptions.update).toHaveBeenCalledWith(
|
|
185
|
+
"sub_stripe_1",
|
|
186
|
+
{ items: [{ id: "si_1", quantity: 1 }] },
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("when upgradeTraces is false", () => {
|
|
192
|
+
it("passes zero traces to the item calculator", async () => {
|
|
193
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
194
|
+
id: "sub_db_1",
|
|
195
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
196
|
+
status: SubscriptionStatus.ACTIVE,
|
|
197
|
+
plan: PlanTypes.LAUNCH,
|
|
198
|
+
});
|
|
199
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
200
|
+
items: { data: [{ id: "si_1", price: { id: "price_launch" } }] },
|
|
201
|
+
});
|
|
202
|
+
itemCalculator.getItemsToUpdate.mockReturnValue([]);
|
|
203
|
+
stripe.subscriptions.update.mockResolvedValue({});
|
|
204
|
+
|
|
205
|
+
await service.updateSubscriptionItems({
|
|
206
|
+
organizationId: "org_123",
|
|
207
|
+
plan: PlanTypes.LAUNCH,
|
|
208
|
+
upgradeMembers: true,
|
|
209
|
+
upgradeTraces: false,
|
|
210
|
+
totalMembers: 5,
|
|
211
|
+
totalTraces: 30_000,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
expect(itemCalculator.getItemsToUpdate).toHaveBeenCalledWith(
|
|
215
|
+
expect.objectContaining({
|
|
216
|
+
tracesToAdd: 0,
|
|
217
|
+
membersToAdd: 5,
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe("when upgradeMembers is false", () => {
|
|
224
|
+
it("passes zero members to the item calculator", async () => {
|
|
225
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
226
|
+
id: "sub_db_1",
|
|
227
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
228
|
+
status: SubscriptionStatus.ACTIVE,
|
|
229
|
+
plan: PlanTypes.LAUNCH,
|
|
230
|
+
});
|
|
231
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
232
|
+
items: { data: [{ id: "si_1", price: { id: "price_launch" } }] },
|
|
233
|
+
});
|
|
234
|
+
itemCalculator.getItemsToUpdate.mockReturnValue([]);
|
|
235
|
+
stripe.subscriptions.update.mockResolvedValue({});
|
|
236
|
+
|
|
237
|
+
await service.updateSubscriptionItems({
|
|
238
|
+
organizationId: "org_123",
|
|
239
|
+
plan: PlanTypes.LAUNCH,
|
|
240
|
+
upgradeMembers: false,
|
|
241
|
+
upgradeTraces: true,
|
|
242
|
+
totalMembers: 5,
|
|
243
|
+
totalTraces: 30_000,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
expect(itemCalculator.getItemsToUpdate).toHaveBeenCalledWith(
|
|
247
|
+
expect.objectContaining({
|
|
248
|
+
tracesToAdd: 30_000,
|
|
249
|
+
membersToAdd: 0,
|
|
250
|
+
}),
|
|
251
|
+
);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe("when no active subscription exists", () => {
|
|
256
|
+
it("returns success false", async () => {
|
|
257
|
+
repository.findLastNonCancelled.mockResolvedValue(null);
|
|
258
|
+
|
|
259
|
+
const result = await service.updateSubscriptionItems({
|
|
260
|
+
organizationId: "org_123",
|
|
261
|
+
plan: PlanTypes.LAUNCH,
|
|
262
|
+
upgradeMembers: true,
|
|
263
|
+
upgradeTraces: true,
|
|
264
|
+
totalMembers: 5,
|
|
265
|
+
totalTraces: 30_000,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
expect(result).toEqual({ success: false });
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe("createOrUpdateSubscription()", () => {
|
|
274
|
+
describe("when cancelling to FREE with existing subscription", () => {
|
|
275
|
+
it("cancels Stripe subscription and updates status via repository", async () => {
|
|
276
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
277
|
+
id: "sub_db_1",
|
|
278
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
279
|
+
status: SubscriptionStatus.ACTIVE,
|
|
280
|
+
});
|
|
281
|
+
stripe.subscriptions.cancel.mockResolvedValue({
|
|
282
|
+
status: "canceled",
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
const result = await service.createOrUpdateSubscription({
|
|
286
|
+
organizationId: "org_123",
|
|
287
|
+
baseUrl: "https://app.test",
|
|
288
|
+
plan: PlanTypes.FREE,
|
|
289
|
+
customerId: "cus_123",
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
expect(result.url).toBe("https://app.test/settings/subscription");
|
|
293
|
+
expect(repository.updateStatus).toHaveBeenCalledWith({
|
|
294
|
+
id: "sub_db_1",
|
|
295
|
+
status: SubscriptionStatus.CANCELLED,
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe("when upgrading existing subscription", () => {
|
|
301
|
+
it("updates Stripe subscription items and plan via repository", async () => {
|
|
302
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
303
|
+
id: "sub_db_1",
|
|
304
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
305
|
+
status: SubscriptionStatus.ACTIVE,
|
|
306
|
+
});
|
|
307
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
308
|
+
items: { data: [] },
|
|
309
|
+
});
|
|
310
|
+
stripe.subscriptions.update.mockResolvedValue({
|
|
311
|
+
status: "active",
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const result = await service.createOrUpdateSubscription({
|
|
315
|
+
organizationId: "org_123",
|
|
316
|
+
baseUrl: "https://app.test",
|
|
317
|
+
plan: PlanTypes.ACCELERATE,
|
|
318
|
+
customerId: "cus_123",
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
expect(result.url).toBe(
|
|
322
|
+
"https://app.test/settings/subscription?success",
|
|
323
|
+
);
|
|
324
|
+
expect(repository.updatePlan).toHaveBeenCalledWith({
|
|
325
|
+
id: "sub_db_1",
|
|
326
|
+
plan: PlanTypes.ACCELERATE,
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
describe("when creating new subscription", () => {
|
|
332
|
+
it("creates checkout session for new plan", async () => {
|
|
333
|
+
repository.findLastNonCancelled.mockResolvedValue(null);
|
|
334
|
+
repository.createPending.mockResolvedValue({ id: "sub_new" });
|
|
335
|
+
stripe.checkout.sessions.create.mockResolvedValue({
|
|
336
|
+
url: "https://checkout.stripe.com/session",
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
const result = await service.createOrUpdateSubscription({
|
|
340
|
+
organizationId: "org_123",
|
|
341
|
+
baseUrl: "https://app.test",
|
|
342
|
+
plan: PlanTypes.LAUNCH,
|
|
343
|
+
customerId: "cus_123",
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
expect(result.url).toBe("https://checkout.stripe.com/session");
|
|
347
|
+
expect(repository.createPending).toHaveBeenCalledWith({
|
|
348
|
+
organizationId: "org_123",
|
|
349
|
+
plan: PlanTypes.LAUNCH,
|
|
350
|
+
});
|
|
351
|
+
expect(stripe.checkout.sessions.create).toHaveBeenCalledWith(
|
|
352
|
+
expect.objectContaining({
|
|
353
|
+
mode: "subscription",
|
|
354
|
+
customer: "cus_123",
|
|
355
|
+
client_reference_id: "subscription_setup_sub_new",
|
|
356
|
+
}),
|
|
357
|
+
);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
describe("when plan is invalid", () => {
|
|
362
|
+
it("throws InvalidPlanError without creating a pending subscription", async () => {
|
|
363
|
+
repository.findLastNonCancelled.mockResolvedValue(null);
|
|
364
|
+
|
|
365
|
+
await expect(
|
|
366
|
+
service.createOrUpdateSubscription({
|
|
367
|
+
organizationId: "org_123",
|
|
368
|
+
baseUrl: "https://app.test",
|
|
369
|
+
plan: "INVALID_PLAN" as any,
|
|
370
|
+
customerId: "cus_123",
|
|
371
|
+
}),
|
|
372
|
+
).rejects.toThrow(InvalidPlanError);
|
|
373
|
+
|
|
374
|
+
expect(repository.createPending).not.toHaveBeenCalled();
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
describe("when selecting FREE with no existing subscription", () => {
|
|
379
|
+
it("returns subscription settings URL", async () => {
|
|
380
|
+
repository.findLastNonCancelled.mockResolvedValue(null);
|
|
381
|
+
|
|
382
|
+
const result = await service.createOrUpdateSubscription({
|
|
383
|
+
organizationId: "org_123",
|
|
384
|
+
baseUrl: "https://app.test",
|
|
385
|
+
plan: PlanTypes.FREE,
|
|
386
|
+
customerId: "cus_123",
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
expect(result.url).toBe("https://app.test/settings/subscription");
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
describe("when createPending succeeds but stripe checkout throws", () => {
|
|
394
|
+
it("propagates the error", async () => {
|
|
395
|
+
repository.findLastNonCancelled.mockResolvedValue(null);
|
|
396
|
+
repository.createPending.mockResolvedValue({ id: "sub_new" });
|
|
397
|
+
stripe.checkout.sessions.create.mockRejectedValue(
|
|
398
|
+
new Error("Stripe checkout failed"),
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
await expect(
|
|
402
|
+
service.createOrUpdateSubscription({
|
|
403
|
+
organizationId: "org_123",
|
|
404
|
+
baseUrl: "https://app.test",
|
|
405
|
+
plan: PlanTypes.LAUNCH,
|
|
406
|
+
customerId: "cus_123",
|
|
407
|
+
}),
|
|
408
|
+
).rejects.toThrow("Stripe checkout failed");
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
describe("when upgrading and stripe subscriptions.update throws", () => {
|
|
413
|
+
it("does not call repository.updatePlan", async () => {
|
|
414
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
415
|
+
id: "sub_db_1",
|
|
416
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
417
|
+
status: SubscriptionStatus.ACTIVE,
|
|
418
|
+
});
|
|
419
|
+
stripe.subscriptions.retrieve.mockResolvedValue({
|
|
420
|
+
items: { data: [] },
|
|
421
|
+
});
|
|
422
|
+
stripe.subscriptions.update.mockRejectedValue(
|
|
423
|
+
new Error("Stripe update failed"),
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
await expect(
|
|
427
|
+
service.createOrUpdateSubscription({
|
|
428
|
+
organizationId: "org_123",
|
|
429
|
+
baseUrl: "https://app.test",
|
|
430
|
+
plan: PlanTypes.ACCELERATE,
|
|
431
|
+
customerId: "cus_123",
|
|
432
|
+
}),
|
|
433
|
+
).rejects.toThrow("Stripe update failed");
|
|
434
|
+
|
|
435
|
+
expect(repository.updatePlan).not.toHaveBeenCalled();
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
describe("when cancelling and stripe subscriptions.cancel throws", () => {
|
|
440
|
+
it("does not call repository.updateStatus", async () => {
|
|
441
|
+
repository.findLastNonCancelled.mockResolvedValue({
|
|
442
|
+
id: "sub_db_1",
|
|
443
|
+
stripeSubscriptionId: "sub_stripe_1",
|
|
444
|
+
status: SubscriptionStatus.ACTIVE,
|
|
445
|
+
});
|
|
446
|
+
stripe.subscriptions.cancel.mockRejectedValue(
|
|
447
|
+
new Error("Stripe cancel failed"),
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
await expect(
|
|
451
|
+
service.createOrUpdateSubscription({
|
|
452
|
+
organizationId: "org_123",
|
|
453
|
+
baseUrl: "https://app.test",
|
|
454
|
+
plan: PlanTypes.FREE,
|
|
455
|
+
customerId: "cus_123",
|
|
456
|
+
}),
|
|
457
|
+
).rejects.toThrow("Stripe cancel failed");
|
|
458
|
+
|
|
459
|
+
expect(repository.updateStatus).not.toHaveBeenCalled();
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
describe("createBillingPortalSession()", () => {
|
|
465
|
+
describe("when called with valid customer and base URL", () => {
|
|
466
|
+
it("creates portal session with return URL", async () => {
|
|
467
|
+
stripe.billingPortal.sessions.create.mockResolvedValue({
|
|
468
|
+
url: "https://billing.stripe.com/session",
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
const result = await service.createBillingPortalSession({
|
|
472
|
+
customerId: "cus_123",
|
|
473
|
+
baseUrl: "https://app.test",
|
|
474
|
+
organizationId: "org_123",
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
expect(result.url).toBe("https://billing.stripe.com/session");
|
|
478
|
+
expect(stripe.billingPortal.sessions.create).toHaveBeenCalledWith({
|
|
479
|
+
customer: "cus_123",
|
|
480
|
+
return_url: "https://app.test/settings/subscription",
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
describe("getLastNonCancelledSubscription()", () => {
|
|
487
|
+
describe("when querying for an organization", () => {
|
|
488
|
+
it("delegates to repository and returns the result", async () => {
|
|
489
|
+
const mockSub = { id: "sub_1", status: "ACTIVE" };
|
|
490
|
+
repository.findLastNonCancelled.mockResolvedValue(mockSub);
|
|
491
|
+
|
|
492
|
+
const result =
|
|
493
|
+
await service.getLastNonCancelledSubscription("org_123");
|
|
494
|
+
|
|
495
|
+
expect(result).toEqual(mockSub);
|
|
496
|
+
expect(repository.findLastNonCancelled).toHaveBeenCalledWith("org_123");
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
describe("notifyProspective()", () => {
|
|
502
|
+
describe("when organization exists", () => {
|
|
503
|
+
it("dispatches prospective notification", async () => {
|
|
504
|
+
organizationRepository.findNameById.mockResolvedValue({
|
|
505
|
+
id: "org_123",
|
|
506
|
+
name: "Acme",
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
const result = await service.notifyProspective({
|
|
510
|
+
organizationId: "org_123",
|
|
511
|
+
plan: PlanTypes.LAUNCH,
|
|
512
|
+
customerName: "John",
|
|
513
|
+
customerEmail: "john@example.com",
|
|
514
|
+
actorEmail: "actor@example.com",
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
expect(result).toEqual({ success: true });
|
|
518
|
+
expect(mockSendSlackSubscriptionEvent).toHaveBeenCalledWith({
|
|
519
|
+
type: "prospective",
|
|
520
|
+
organizationId: "org_123",
|
|
521
|
+
organizationName: "Acme",
|
|
522
|
+
plan: PlanTypes.LAUNCH,
|
|
523
|
+
customerName: "John",
|
|
524
|
+
customerEmail: "john@example.com",
|
|
525
|
+
actorEmail: "actor@example.com",
|
|
526
|
+
note: undefined,
|
|
527
|
+
});
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
describe("when organization not found", () => {
|
|
532
|
+
it("throws OrganizationNotFoundError", async () => {
|
|
533
|
+
organizationRepository.findNameById.mockResolvedValue(null);
|
|
534
|
+
|
|
535
|
+
await expect(
|
|
536
|
+
service.notifyProspective({
|
|
537
|
+
organizationId: "org_missing",
|
|
538
|
+
plan: PlanTypes.LAUNCH,
|
|
539
|
+
actorEmail: "actor@example.com",
|
|
540
|
+
}),
|
|
541
|
+
).rejects.toThrow(OrganizationNotFoundError);
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
describe("createSubscriptionWithInvites()", () => {
|
|
547
|
+
describe("when seatEventFns is not configured", () => {
|
|
548
|
+
it("throws SeatBillingUnavailableError", async () => {
|
|
549
|
+
await expect(
|
|
550
|
+
service.createSubscriptionWithInvites({
|
|
551
|
+
organizationId: "org_123",
|
|
552
|
+
baseUrl: "https://app.test",
|
|
553
|
+
membersToAdd: 3,
|
|
554
|
+
customerId: "cus_123",
|
|
555
|
+
invites: [{ email: "alice@example.com", role: "MEMBER" as any }],
|
|
556
|
+
}),
|
|
557
|
+
).rejects.toThrow(SeatBillingUnavailableError);
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
describe("when seatEventFns is configured", () => {
|
|
562
|
+
it("creates seat event checkout with mapped invites", async () => {
|
|
563
|
+
const seatEventFns = createMockSeatEventFns();
|
|
564
|
+
const svcWithSeats = createServiceWithSeatEventFns({
|
|
565
|
+
db,
|
|
566
|
+
repository,
|
|
567
|
+
stripe,
|
|
568
|
+
itemCalculator,
|
|
569
|
+
organizationRepository,
|
|
570
|
+
seatEventFns,
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
db.team.findFirst.mockResolvedValue({ id: "team_1" });
|
|
574
|
+
organizationRepository.getPricingModel.mockResolvedValue("SEAT_EVENT");
|
|
575
|
+
seatEventFns.createSeatEventCheckout.mockResolvedValue({
|
|
576
|
+
url: "https://checkout.stripe.com/seat-session",
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
const result = await svcWithSeats.createSubscriptionWithInvites({
|
|
580
|
+
organizationId: "org_123",
|
|
581
|
+
baseUrl: "https://app.test",
|
|
582
|
+
membersToAdd: 2,
|
|
583
|
+
customerId: "cus_123",
|
|
584
|
+
invites: [
|
|
585
|
+
{ email: "alice@example.com", role: "MEMBER" as any },
|
|
586
|
+
{ email: "bob@example.com", role: "ADMIN" as any },
|
|
587
|
+
],
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
expect(result.url).toBe("https://checkout.stripe.com/seat-session");
|
|
591
|
+
expect(seatEventFns.createSeatEventCheckout).toHaveBeenCalledWith(
|
|
592
|
+
expect.objectContaining({
|
|
593
|
+
organizationId: "org_123",
|
|
594
|
+
customerId: "cus_123",
|
|
595
|
+
membersToAdd: 2,
|
|
596
|
+
invites: [
|
|
597
|
+
{ email: "alice@example.com", role: "MEMBER", teamIds: "team_1" },
|
|
598
|
+
{ email: "bob@example.com", role: "ADMIN", teamIds: "team_1" },
|
|
599
|
+
],
|
|
600
|
+
}),
|
|
601
|
+
);
|
|
602
|
+
});
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
describe("previewProration()", () => {
|
|
607
|
+
describe("when seatEventFns is not configured", () => {
|
|
608
|
+
it("throws SeatBillingUnavailableError", async () => {
|
|
609
|
+
await expect(
|
|
610
|
+
service.previewProration({
|
|
611
|
+
organizationId: "org_123",
|
|
612
|
+
newTotalSeats: 5,
|
|
613
|
+
}),
|
|
614
|
+
).rejects.toThrow(SeatBillingUnavailableError);
|
|
615
|
+
});
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
describe("when seatEventFns is configured", () => {
|
|
619
|
+
it("delegates to seatEventFns.previewProration", async () => {
|
|
620
|
+
const seatEventFns = createMockSeatEventFns();
|
|
621
|
+
const svcWithSeats = createServiceWithSeatEventFns({
|
|
622
|
+
db,
|
|
623
|
+
repository,
|
|
624
|
+
stripe,
|
|
625
|
+
itemCalculator,
|
|
626
|
+
organizationRepository,
|
|
627
|
+
seatEventFns,
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
const mockResult = {
|
|
631
|
+
formattedAmountDue: "$10.00",
|
|
632
|
+
formattedRecurringTotal: "$50.00",
|
|
633
|
+
billingInterval: "month",
|
|
634
|
+
};
|
|
635
|
+
seatEventFns.previewProration.mockResolvedValue(mockResult);
|
|
636
|
+
|
|
637
|
+
const result = await svcWithSeats.previewProration({
|
|
638
|
+
organizationId: "org_123",
|
|
639
|
+
newTotalSeats: 5,
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
expect(result).toEqual(mockResult);
|
|
643
|
+
expect(seatEventFns.previewProration).toHaveBeenCalledWith({
|
|
644
|
+
organizationId: "org_123",
|
|
645
|
+
newTotalSeats: 5,
|
|
646
|
+
});
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
describe("listInvoices()", () => {
|
|
652
|
+
describe("when organization has no stripeCustomerId", () => {
|
|
653
|
+
it("returns an empty array", async () => {
|
|
654
|
+
organizationRepository.getStripeCustomerId.mockResolvedValue(null);
|
|
655
|
+
|
|
656
|
+
const result = await service.listInvoices({
|
|
657
|
+
organizationId: "org_no_stripe",
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
expect(result).toEqual([]);
|
|
661
|
+
expect(stripe.invoices.list).not.toHaveBeenCalled();
|
|
662
|
+
});
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
describe("when organization is not found", () => {
|
|
666
|
+
it("returns an empty array", async () => {
|
|
667
|
+
organizationRepository.getStripeCustomerId.mockResolvedValue(null);
|
|
668
|
+
|
|
669
|
+
const result = await service.listInvoices({
|
|
670
|
+
organizationId: "org_missing",
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
expect(result).toEqual([]);
|
|
674
|
+
});
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
describe("when organization has a stripeCustomerId", () => {
|
|
678
|
+
it("returns mapped invoices excluding drafts", async () => {
|
|
679
|
+
organizationRepository.getStripeCustomerId.mockResolvedValue("cus_123");
|
|
680
|
+
|
|
681
|
+
stripe.invoices.list.mockResolvedValue({
|
|
682
|
+
data: [
|
|
683
|
+
{
|
|
684
|
+
id: "inv_1",
|
|
685
|
+
number: "INV-001",
|
|
686
|
+
created: 1700000000,
|
|
687
|
+
amount_due: 5000,
|
|
688
|
+
currency: "usd",
|
|
689
|
+
status: "paid",
|
|
690
|
+
invoice_pdf: "https://pdf.example.com/inv_1",
|
|
691
|
+
hosted_invoice_url: "https://hosted.example.com/inv_1",
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
id: "inv_draft",
|
|
695
|
+
number: null,
|
|
696
|
+
created: 1700001000,
|
|
697
|
+
amount_due: 3000,
|
|
698
|
+
currency: "eur",
|
|
699
|
+
status: "draft",
|
|
700
|
+
invoice_pdf: null,
|
|
701
|
+
hosted_invoice_url: null,
|
|
702
|
+
},
|
|
703
|
+
],
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
const result = await service.listInvoices({
|
|
707
|
+
organizationId: "org_with_stripe",
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
expect(stripe.invoices.list).toHaveBeenCalledWith({
|
|
711
|
+
customer: "cus_123",
|
|
712
|
+
limit: RECENT_INVOICES_LIMIT,
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
expect(result).toEqual([
|
|
716
|
+
{
|
|
717
|
+
id: "inv_1",
|
|
718
|
+
number: "INV-001",
|
|
719
|
+
date: 1700000000,
|
|
720
|
+
amountDue: 5000,
|
|
721
|
+
currency: "usd",
|
|
722
|
+
status: "paid",
|
|
723
|
+
pdfUrl: "https://pdf.example.com/inv_1",
|
|
724
|
+
hostedUrl: "https://hosted.example.com/inv_1",
|
|
725
|
+
},
|
|
726
|
+
]);
|
|
727
|
+
});
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
});
|