@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.
Files changed (119) hide show
  1. package/langwatch/ee/admin/ImpersonationBanner.tsx +61 -0
  2. package/langwatch/ee/admin/ImpersonationSwitchBackMenuItem.tsx +30 -0
  3. package/langwatch/ee/admin/__tests__/ImpersonationBanner.integration.test.tsx +82 -0
  4. package/langwatch/ee/admin/__tests__/impersonation.service.unit.test.ts +231 -0
  5. package/langwatch/ee/admin/backoffice/BackofficeTable.tsx +200 -0
  6. package/langwatch/ee/admin/backoffice/__tests__/adminClient.unit.test.ts +147 -0
  7. package/langwatch/ee/admin/backoffice/__tests__/dateInputToISO.unit.test.ts +42 -0
  8. package/langwatch/ee/admin/backoffice/__tests__/userVisibility.unit.test.ts +174 -0
  9. package/langwatch/ee/admin/backoffice/adminClient.ts +114 -0
  10. package/langwatch/ee/admin/backoffice/resources/OrganizationFeaturesView.tsx +373 -0
  11. package/langwatch/ee/admin/backoffice/resources/OrganizationsView.tsx +677 -0
  12. package/langwatch/ee/admin/backoffice/resources/ProjectsView.tsx +647 -0
  13. package/langwatch/ee/admin/backoffice/resources/SubscriptionsView.tsx +628 -0
  14. package/langwatch/ee/admin/backoffice/resources/UsersView.tsx +556 -0
  15. package/langwatch/ee/admin/backoffice/useAdminResource.ts +104 -0
  16. package/langwatch/ee/admin/backoffice/userVisibility.ts +105 -0
  17. package/langwatch/ee/admin/impersonation.service.ts +170 -0
  18. package/langwatch/ee/admin/isAdmin.ts +9 -0
  19. package/langwatch/ee/admin/routes/admin.ts +531 -0
  20. package/langwatch/ee/admin/safeSelects.ts +64 -0
  21. package/langwatch/ee/billing/__tests__/customerService.unit.test.ts +183 -0
  22. package/langwatch/ee/billing/__tests__/growthSeatEvent.unit.test.ts +213 -0
  23. package/langwatch/ee/billing/__tests__/licensePurchaseHandler.unit.test.ts +250 -0
  24. package/langwatch/ee/billing/__tests__/notification.service.unit.test.ts +768 -0
  25. package/langwatch/ee/billing/__tests__/planLimits.unit.test.ts +19 -0
  26. package/langwatch/ee/billing/__tests__/planProvider.unit.test.ts +440 -0
  27. package/langwatch/ee/billing/__tests__/planTypes.unit.test.ts +32 -0
  28. package/langwatch/ee/billing/__tests__/pricing.unit.test.ts +54 -0
  29. package/langwatch/ee/billing/__tests__/seatEventSubscription.unit.test.ts +717 -0
  30. package/langwatch/ee/billing/__tests__/stripe.integration.test.ts +344 -0
  31. package/langwatch/ee/billing/__tests__/stripePrices.types.unit.test.ts +27 -0
  32. package/langwatch/ee/billing/__tests__/stripePricesLoader.unit.test.ts +72 -0
  33. package/langwatch/ee/billing/__tests__/subscription.service.unit.test.ts +730 -0
  34. package/langwatch/ee/billing/__tests__/subscriptionItemCalculator.unit.test.ts +176 -0
  35. package/langwatch/ee/billing/__tests__/usage-limit.service.unit.test.ts +706 -0
  36. package/langwatch/ee/billing/__tests__/usageReportingService.integration.test.ts +421 -0
  37. package/langwatch/ee/billing/__tests__/usageReportingService.unit.test.ts +515 -0
  38. package/langwatch/ee/billing/__tests__/webhookService.unit.test.ts +958 -0
  39. package/langwatch/ee/billing/currencyRouter.ts +18 -0
  40. package/langwatch/ee/billing/errors.ts +129 -0
  41. package/langwatch/ee/billing/index.ts +67 -0
  42. package/langwatch/ee/billing/middleware.ts +17 -0
  43. package/langwatch/ee/billing/notifications/notification.service.ts +651 -0
  44. package/langwatch/ee/billing/notifications/repositories/notification.repository.ts +51 -0
  45. package/langwatch/ee/billing/notifications/types.ts +9 -0
  46. package/langwatch/ee/billing/notifications/usage-limit.service.ts +622 -0
  47. package/langwatch/ee/billing/nurturing/hooks/activityTracking.ts +90 -0
  48. package/langwatch/ee/billing/nurturing/hooks/activityTracking.unit.test.ts +237 -0
  49. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.ts +120 -0
  50. package/langwatch/ee/billing/nurturing/hooks/featureAdoption.unit.test.ts +324 -0
  51. package/langwatch/ee/billing/nurturing/hooks/index.ts +17 -0
  52. package/langwatch/ee/billing/nurturing/hooks/productInterest.ts +68 -0
  53. package/langwatch/ee/billing/nurturing/hooks/productInterest.unit.test.ts +138 -0
  54. package/langwatch/ee/billing/nurturing/hooks/promptCreation.integration.test.ts +288 -0
  55. package/langwatch/ee/billing/nurturing/hooks/promptCreation.ts +131 -0
  56. package/langwatch/ee/billing/nurturing/hooks/promptCreation.unit.test.ts +134 -0
  57. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.ts +101 -0
  58. package/langwatch/ee/billing/nurturing/hooks/signupIdentification.unit.test.ts +317 -0
  59. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.ts +49 -0
  60. package/langwatch/ee/billing/nurturing/hooks/subscriptionSync.unit.test.ts +170 -0
  61. package/langwatch/ee/billing/nurturing/hooks/userSync.ts +143 -0
  62. package/langwatch/ee/billing/nurturing/hooks/userSync.unit.test.ts +307 -0
  63. package/langwatch/ee/billing/nurturing/nurturing.service.ts +179 -0
  64. package/langwatch/ee/billing/nurturing/nurturing.service.unit.test.ts +266 -0
  65. package/langwatch/ee/billing/nurturing/nurturing.service.wiring.unit.test.ts +109 -0
  66. package/langwatch/ee/billing/nurturing/types.ts +125 -0
  67. package/langwatch/ee/billing/planLimits.ts +190 -0
  68. package/langwatch/ee/billing/planProvider.ts +119 -0
  69. package/langwatch/ee/billing/planTypes.ts +53 -0
  70. package/langwatch/ee/billing/pricing.ts +57 -0
  71. package/langwatch/ee/billing/services/billableEventsQuery.ts +248 -0
  72. package/langwatch/ee/billing/services/customerService.ts +90 -0
  73. package/langwatch/ee/billing/services/licensePurchaseHandler.ts +73 -0
  74. package/langwatch/ee/billing/services/seatEventSubscription.ts +351 -0
  75. package/langwatch/ee/billing/services/seatSyncService.ts +34 -0
  76. package/langwatch/ee/billing/services/subscription.repository.ts +203 -0
  77. package/langwatch/ee/billing/services/subscription.service.ts +549 -0
  78. package/langwatch/ee/billing/services/subscriptionItemCalculator.ts +249 -0
  79. package/langwatch/ee/billing/services/usageReportingService.ts +301 -0
  80. package/langwatch/ee/billing/services/webhookService.ts +858 -0
  81. package/langwatch/ee/billing/stripe/stripeCatalog.json +1781 -0
  82. package/langwatch/ee/billing/stripe/stripeClient.ts +13 -0
  83. package/langwatch/ee/billing/stripe/stripePriceCatalog.ts +70 -0
  84. package/langwatch/ee/billing/stripe/stripePrices.types.ts +118 -0
  85. package/langwatch/ee/billing/subscriptionRouter.ts +202 -0
  86. package/langwatch/ee/billing/types.ts +102 -0
  87. package/langwatch/ee/billing/utils/currency.ts +99 -0
  88. package/langwatch/ee/billing/utils/growthSeatEvent.ts +137 -0
  89. package/langwatch/ee/licensing/__tests__/constants.unit.test.ts +56 -0
  90. package/langwatch/ee/licensing/__tests__/defaults.unit.test.ts +192 -0
  91. package/langwatch/ee/licensing/__tests__/fixtures/testKeys.ts +86 -0
  92. package/langwatch/ee/licensing/__tests__/fixtures/testLicenses.ts +92 -0
  93. package/langwatch/ee/licensing/__tests__/licenseGenerationService.unit.test.ts +232 -0
  94. package/langwatch/ee/licensing/__tests__/licenseHandler.integration.test.ts +419 -0
  95. package/langwatch/ee/licensing/__tests__/planMapping.unit.test.ts +195 -0
  96. package/langwatch/ee/licensing/__tests__/planTemplates.unit.test.ts +193 -0
  97. package/langwatch/ee/licensing/__tests__/signing.unit.test.ts +164 -0
  98. package/langwatch/ee/licensing/__tests__/validation.unit.test.ts +281 -0
  99. package/langwatch/ee/licensing/client.ts +29 -0
  100. package/langwatch/ee/licensing/constants.ts +195 -0
  101. package/langwatch/ee/licensing/defaults.ts +55 -0
  102. package/langwatch/ee/licensing/errors.ts +11 -0
  103. package/langwatch/ee/licensing/index.ts +52 -0
  104. package/langwatch/ee/licensing/licenseGenerationService.ts +87 -0
  105. package/langwatch/ee/licensing/licenseHandler.ts +308 -0
  106. package/langwatch/ee/licensing/planInfo.ts +46 -0
  107. package/langwatch/ee/licensing/planMapping.ts +37 -0
  108. package/langwatch/ee/licensing/planTemplates.ts +109 -0
  109. package/langwatch/ee/licensing/server.ts +43 -0
  110. package/langwatch/ee/licensing/signing.ts +43 -0
  111. package/langwatch/ee/licensing/types.ts +156 -0
  112. package/langwatch/ee/licensing/validation.ts +107 -0
  113. package/langwatch/ee/managed-providers/ManagedModelProviderAlert.tsx +29 -0
  114. package/langwatch/ee/managed-providers/managedBedrockConfig.ts +226 -0
  115. package/langwatch/ee/saas/ExtraFooterComponents.tsx +169 -0
  116. package/langwatch/package.json +1 -1
  117. package/langwatch_nlp/uv.lock +179 -5
  118. package/package.json +1 -1
  119. package/packages/server/dist/cli.cjs +192 -142
@@ -0,0 +1,203 @@
1
+ import {
2
+ type PrismaClient,
3
+ type Subscription,
4
+ SubscriptionStatus as PrismaSubscriptionStatus,
5
+ PlanTypes as PrismaPlanTypes,
6
+ } from "@prisma/client";
7
+ import type {
8
+ CancelledSubscription,
9
+ SubscriptionRepository,
10
+ SubscriptionWithOrg,
11
+ } from "../../../src/server/app-layer/subscription/subscription.repository";
12
+ import { NUMERIC_OVERRIDE_FIELDS } from "../planProvider";
13
+ import { PlanTypes, SubscriptionStatus } from "../planTypes";
14
+
15
+ /**
16
+ * Prisma-backed implementation of SubscriptionRepository.
17
+ * Handles only subscription-table CRUD -- no organization or team queries.
18
+ */
19
+ export class PrismaSubscriptionRepository implements SubscriptionRepository {
20
+ constructor(private readonly prisma: PrismaClient) {}
21
+
22
+ async findLastNonCancelled(
23
+ organizationId: string,
24
+ ): Promise<Subscription | null> {
25
+ return await this.prisma.subscription.findFirst({
26
+ where: {
27
+ organizationId,
28
+ status: {
29
+ not: SubscriptionStatus.CANCELLED as PrismaSubscriptionStatus,
30
+ },
31
+ },
32
+ orderBy: { createdAt: "desc" },
33
+ });
34
+ }
35
+
36
+ async createPending(input: {
37
+ organizationId: string;
38
+ plan: string;
39
+ }): Promise<Subscription> {
40
+ return await this.prisma.subscription.create({
41
+ data: {
42
+ organizationId: input.organizationId,
43
+ status: SubscriptionStatus.PENDING as PrismaSubscriptionStatus,
44
+ plan: input.plan as PrismaPlanTypes,
45
+ },
46
+ });
47
+ }
48
+
49
+ async updateStatus(input: {
50
+ id: string;
51
+ status: string;
52
+ }): Promise<Subscription> {
53
+ return await this.prisma.subscription.update({
54
+ where: { id: input.id },
55
+ data: { status: input.status as PrismaSubscriptionStatus },
56
+ });
57
+ }
58
+
59
+ async updatePlan(input: {
60
+ id: string;
61
+ plan: string;
62
+ }): Promise<Subscription> {
63
+ return await this.prisma.subscription.update({
64
+ where: { id: input.id },
65
+ data: { plan: input.plan as PrismaPlanTypes },
66
+ });
67
+ }
68
+
69
+ // --- Webhook handler methods ---
70
+
71
+ async findByStripeId(
72
+ stripeSubscriptionId: string,
73
+ ): Promise<Subscription | null> {
74
+ return await this.prisma.subscription.findUnique({
75
+ where: { stripeSubscriptionId },
76
+ });
77
+ }
78
+
79
+ async linkStripeId(input: {
80
+ id: string;
81
+ stripeSubscriptionId: string;
82
+ }): Promise<{ count: number }> {
83
+ return await this.prisma.subscription.updateMany({
84
+ where: { id: input.id },
85
+ data: { stripeSubscriptionId: input.stripeSubscriptionId },
86
+ });
87
+ }
88
+
89
+ async activate(input: {
90
+ id: string;
91
+ previousStatus: string;
92
+ }): Promise<SubscriptionWithOrg> {
93
+ return await this.prisma.subscription.update({
94
+ where: { id: input.id },
95
+ data: {
96
+ status: SubscriptionStatus.ACTIVE as PrismaSubscriptionStatus,
97
+ ...(input.previousStatus !== SubscriptionStatus.ACTIVE && {
98
+ startDate: new Date(),
99
+ }),
100
+ lastPaymentFailedDate: null,
101
+ },
102
+ include: { organization: true },
103
+ });
104
+ }
105
+
106
+ async recordPaymentFailure(input: {
107
+ id: string;
108
+ currentStatus: string;
109
+ }): Promise<void> {
110
+ await this.prisma.subscription.update({
111
+ where: { id: input.id },
112
+ data: {
113
+ status:
114
+ input.currentStatus === SubscriptionStatus.ACTIVE
115
+ ? (SubscriptionStatus.ACTIVE as PrismaSubscriptionStatus)
116
+ : (SubscriptionStatus.FAILED as PrismaSubscriptionStatus),
117
+ lastPaymentFailedDate: new Date(),
118
+ },
119
+ });
120
+ }
121
+
122
+ async cancel(input: { id: string }): Promise<void> {
123
+ await this.prisma.subscription.update({
124
+ where: { id: input.id },
125
+ data: {
126
+ status: SubscriptionStatus.CANCELLED as PrismaSubscriptionStatus,
127
+ endDate: new Date(),
128
+ ...Object.fromEntries(
129
+ NUMERIC_OVERRIDE_FIELDS.map((f) => [f, null]),
130
+ ),
131
+ },
132
+ });
133
+ }
134
+
135
+ async cancelTrialSubscriptions(_organizationId: string): Promise<void> {
136
+ // No-op: the `isTrial` column does not exist in the schema yet.
137
+ // Once the migration lands, restore the updateMany query filtering on isTrial.
138
+ }
139
+
140
+ async migrateToSeatEvent(input: {
141
+ organizationId: string;
142
+ excludeSubscriptionId: string;
143
+ }): Promise<CancelledSubscription[]> {
144
+ const TIERED_PLAN_TYPES: PlanTypes[] = [
145
+ PlanTypes.LAUNCH,
146
+ PlanTypes.ACCELERATE,
147
+ PlanTypes.LAUNCH_ANNUAL,
148
+ PlanTypes.ACCELERATE_ANNUAL,
149
+ PlanTypes.PRO,
150
+ PlanTypes.GROWTH,
151
+ ];
152
+
153
+ return await this.prisma.$transaction(async (tx) => {
154
+ await tx.organization.update({
155
+ where: { id: input.organizationId },
156
+ data: { pricingModel: "SEAT_EVENT" },
157
+ });
158
+
159
+ const oldSubs = await tx.subscription.findMany({
160
+ where: {
161
+ organizationId: input.organizationId,
162
+ id: { not: input.excludeSubscriptionId },
163
+ status: {
164
+ not: SubscriptionStatus.CANCELLED as PrismaSubscriptionStatus,
165
+ },
166
+ stripeSubscriptionId: { not: null },
167
+ plan: { in: TIERED_PLAN_TYPES as PrismaPlanTypes[] },
168
+ },
169
+ });
170
+
171
+ for (const oldSub of oldSubs) {
172
+ await tx.subscription.update({
173
+ where: { id: oldSub.id },
174
+ data: {
175
+ status: SubscriptionStatus.CANCELLED as PrismaSubscriptionStatus,
176
+ endDate: new Date(),
177
+ },
178
+ });
179
+ }
180
+
181
+ return oldSubs.map((s) => ({
182
+ stripeSubscriptionId: s.stripeSubscriptionId,
183
+ }));
184
+ });
185
+ }
186
+
187
+ async updateQuantities(input: {
188
+ id: string;
189
+ maxMembers: number | null;
190
+ maxMessagesPerMonth: number | null;
191
+ }): Promise<SubscriptionWithOrg> {
192
+ return await this.prisma.subscription.update({
193
+ where: { id: input.id },
194
+ data: {
195
+ status: SubscriptionStatus.ACTIVE as PrismaSubscriptionStatus,
196
+ lastPaymentFailedDate: null,
197
+ maxMembers: input.maxMembers,
198
+ maxMessagesPerMonth: input.maxMessagesPerMonth,
199
+ },
200
+ include: { organization: true },
201
+ });
202
+ }
203
+ }