@langwatch/server 3.1.0-beta.4 → 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/package.json +1 -1
- package/packages/server/dist/cli.cjs +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { firePromptCreatedNurturing } from "./promptCreation";
|
|
3
|
+
|
|
4
|
+
vi.mock("../../../../src/utils/logger/server", () => ({
|
|
5
|
+
createLogger: () => ({
|
|
6
|
+
info: vi.fn(),
|
|
7
|
+
warn: vi.fn(),
|
|
8
|
+
error: vi.fn(),
|
|
9
|
+
debug: vi.fn(),
|
|
10
|
+
}),
|
|
11
|
+
}));
|
|
12
|
+
vi.mock("../../../../src/utils/posthogErrorCapture", () => ({
|
|
13
|
+
captureException: vi.fn(),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const mockNurturing = {
|
|
17
|
+
identifyUser: vi.fn().mockResolvedValue(undefined),
|
|
18
|
+
trackEvent: vi.fn().mockResolvedValue(undefined),
|
|
19
|
+
groupUser: vi.fn().mockResolvedValue(undefined),
|
|
20
|
+
batch: vi.fn().mockResolvedValue(undefined),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
let currentNurturing: typeof mockNurturing | undefined = mockNurturing;
|
|
24
|
+
|
|
25
|
+
vi.mock("../../../../src/server/app-layer/app", () => ({
|
|
26
|
+
getApp: () => ({
|
|
27
|
+
get nurturing() {
|
|
28
|
+
return currentNurturing;
|
|
29
|
+
},
|
|
30
|
+
}),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
describe("firePromptCreatedNurturing()", () => {
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
vi.clearAllMocks();
|
|
36
|
+
currentNurturing = mockNurturing;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("given an organization with no prompts", () => {
|
|
40
|
+
describe("when a user creates their first prompt", () => {
|
|
41
|
+
it("identifies user with has_prompts true and prompt_count 1", async () => {
|
|
42
|
+
firePromptCreatedNurturing({
|
|
43
|
+
userId: "user-1",
|
|
44
|
+
projectId: "proj-1",
|
|
45
|
+
orgPromptCount: 1,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
49
|
+
userId: "user-1",
|
|
50
|
+
traits: { has_prompts: true, prompt_count: 1 },
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("tracks first_prompt_created event with project_id", async () => {
|
|
55
|
+
firePromptCreatedNurturing({
|
|
56
|
+
userId: "user-1",
|
|
57
|
+
projectId: "proj-1",
|
|
58
|
+
orgPromptCount: 1,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
|
|
62
|
+
userId: "user-1",
|
|
63
|
+
event: "first_prompt_created",
|
|
64
|
+
properties: { project_id: "proj-1" },
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("given an organization that already has prompts", () => {
|
|
71
|
+
describe("when a user creates another prompt", () => {
|
|
72
|
+
it("identifies user with has_prompts true and updated prompt_count", async () => {
|
|
73
|
+
firePromptCreatedNurturing({
|
|
74
|
+
userId: "user-1",
|
|
75
|
+
projectId: "proj-1",
|
|
76
|
+
orgPromptCount: 5,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
80
|
+
userId: "user-1",
|
|
81
|
+
traits: { has_prompts: true, prompt_count: 5 },
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("does not send first_prompt_created event", async () => {
|
|
86
|
+
firePromptCreatedNurturing({
|
|
87
|
+
userId: "user-1",
|
|
88
|
+
projectId: "proj-1",
|
|
89
|
+
orgPromptCount: 5,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("when Customer.io API is unavailable", () => {
|
|
98
|
+
it("does not throw (fire-and-forget)", async () => {
|
|
99
|
+
const { captureException } = await import(
|
|
100
|
+
"../../../../src/utils/posthogErrorCapture"
|
|
101
|
+
);
|
|
102
|
+
mockNurturing.identifyUser.mockRejectedValueOnce(
|
|
103
|
+
new Error("CIO unavailable"),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
expect(() =>
|
|
107
|
+
firePromptCreatedNurturing({
|
|
108
|
+
userId: "user-1",
|
|
109
|
+
projectId: "proj-1",
|
|
110
|
+
orgPromptCount: 1,
|
|
111
|
+
}),
|
|
112
|
+
).not.toThrow();
|
|
113
|
+
|
|
114
|
+
await vi.waitFor(() => {
|
|
115
|
+
expect(captureException).toHaveBeenCalled();
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("when nurturing is undefined (no Customer.io key)", () => {
|
|
121
|
+
it("silently skips without calling any nurturing methods", () => {
|
|
122
|
+
currentNurturing = undefined;
|
|
123
|
+
|
|
124
|
+
firePromptCreatedNurturing({
|
|
125
|
+
userId: "user-1",
|
|
126
|
+
projectId: "proj-1",
|
|
127
|
+
orgPromptCount: 1,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
|
|
131
|
+
expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { getApp } from "../../../../src/server/app-layer/app";
|
|
4
|
+
import type { signUpDataSchema } from "../../../../src/server/schemas/sign-up-data.schema";
|
|
5
|
+
import { captureException } from "../../../../src/utils/posthogErrorCapture";
|
|
6
|
+
import type { CioPersonTraits } from "../types";
|
|
7
|
+
|
|
8
|
+
type SignUpData = z.infer<typeof signUpDataSchema>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns a new object with null, undefined, and empty-string values
|
|
12
|
+
* removed. Lets call sites list traits as data (`lead_source: foo?.bar`)
|
|
13
|
+
* instead of boilerplate conditional spreads. The return type narrows
|
|
14
|
+
* values to `NonNullable<T[K]>` so the result is assignable to trait
|
|
15
|
+
* containers that don't accept null.
|
|
16
|
+
*/
|
|
17
|
+
function pickDefined<T extends Record<string, unknown>>(
|
|
18
|
+
obj: T,
|
|
19
|
+
): { [K in keyof T]?: NonNullable<T[K]> } {
|
|
20
|
+
const result: Record<string, unknown> = {};
|
|
21
|
+
for (const key in obj) {
|
|
22
|
+
const value = obj[key];
|
|
23
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
24
|
+
result[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result as { [K in keyof T]?: NonNullable<T[K]> };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Identifies a new user in Customer.io during onboarding.
|
|
32
|
+
*
|
|
33
|
+
* Fires three calls — identifyUser, groupUser, trackEvent —
|
|
34
|
+
* all fire-and-forget so that Customer.io failures never block onboarding.
|
|
35
|
+
*/
|
|
36
|
+
export function fireSignupNurturingCalls({
|
|
37
|
+
userId,
|
|
38
|
+
email,
|
|
39
|
+
name,
|
|
40
|
+
organizationId,
|
|
41
|
+
organizationName,
|
|
42
|
+
signUpData,
|
|
43
|
+
}: {
|
|
44
|
+
userId: string;
|
|
45
|
+
email: string | null | undefined;
|
|
46
|
+
name: string | null | undefined;
|
|
47
|
+
organizationId: string;
|
|
48
|
+
organizationName: string;
|
|
49
|
+
signUpData?: SignUpData | null;
|
|
50
|
+
}): void {
|
|
51
|
+
const nurturing = getApp().nurturing;
|
|
52
|
+
if (!nurturing) return;
|
|
53
|
+
|
|
54
|
+
const traits: Partial<CioPersonTraits> = {
|
|
55
|
+
...pickDefined({
|
|
56
|
+
email,
|
|
57
|
+
name,
|
|
58
|
+
role: signUpData?.yourRole,
|
|
59
|
+
company_size: signUpData?.companySize,
|
|
60
|
+
signup_usage: signUpData?.usage,
|
|
61
|
+
signup_solution: signUpData?.solution,
|
|
62
|
+
signup_feature_usage: signUpData?.featureUsage,
|
|
63
|
+
utm_campaign: signUpData?.utmCampaign,
|
|
64
|
+
how_heard: signUpData?.howDidYouHearAboutUs,
|
|
65
|
+
lead_source: signUpData?.leadSource,
|
|
66
|
+
utm_source: signUpData?.utmSource,
|
|
67
|
+
utm_medium: signUpData?.utmMedium,
|
|
68
|
+
utm_term: signUpData?.utmTerm,
|
|
69
|
+
utm_content: signUpData?.utmContent,
|
|
70
|
+
referrer: signUpData?.referrer,
|
|
71
|
+
}),
|
|
72
|
+
has_traces: false,
|
|
73
|
+
has_evaluations: false,
|
|
74
|
+
has_prompts: false,
|
|
75
|
+
has_simulations: false,
|
|
76
|
+
has_subscription: false,
|
|
77
|
+
createdAt: new Date().toISOString(),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
void nurturing.identifyUser({ userId, traits }).catch(captureException);
|
|
81
|
+
|
|
82
|
+
void nurturing
|
|
83
|
+
.groupUser({
|
|
84
|
+
userId,
|
|
85
|
+
groupId: organizationId,
|
|
86
|
+
traits: {
|
|
87
|
+
name: organizationName,
|
|
88
|
+
...pickDefined({ company_size: signUpData?.companySize }),
|
|
89
|
+
plan: "free",
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
.catch(captureException);
|
|
93
|
+
|
|
94
|
+
void nurturing
|
|
95
|
+
.trackEvent({
|
|
96
|
+
userId,
|
|
97
|
+
event: "signed_up",
|
|
98
|
+
properties: pickDefined(signUpData ?? {}),
|
|
99
|
+
})
|
|
100
|
+
.catch(captureException);
|
|
101
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { fireSignupNurturingCalls } from "./signupIdentification";
|
|
3
|
+
|
|
4
|
+
// Suppress logger output
|
|
5
|
+
vi.mock("../../../../src/utils/logger/server", () => ({
|
|
6
|
+
createLogger: () => ({
|
|
7
|
+
info: vi.fn(),
|
|
8
|
+
warn: vi.fn(),
|
|
9
|
+
error: vi.fn(),
|
|
10
|
+
debug: vi.fn(),
|
|
11
|
+
}),
|
|
12
|
+
}));
|
|
13
|
+
vi.mock("../../../../src/utils/posthogErrorCapture", () => ({
|
|
14
|
+
captureException: vi.fn(),
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
const mockNurturing = {
|
|
18
|
+
identifyUser: vi.fn().mockResolvedValue(undefined),
|
|
19
|
+
trackEvent: vi.fn().mockResolvedValue(undefined),
|
|
20
|
+
groupUser: vi.fn().mockResolvedValue(undefined),
|
|
21
|
+
batch: vi.fn().mockResolvedValue(undefined),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
let currentNurturing: typeof mockNurturing | undefined = mockNurturing;
|
|
25
|
+
|
|
26
|
+
vi.mock("../../../../src/server/app-layer/app", () => ({
|
|
27
|
+
getApp: () => ({
|
|
28
|
+
get nurturing() {
|
|
29
|
+
return currentNurturing;
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
describe("Signup identification hook", () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
vi.clearAllMocks();
|
|
37
|
+
currentNurturing = mockNurturing;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("when the onboarding flow completes", () => {
|
|
41
|
+
const baseArgs = {
|
|
42
|
+
userId: "user-123",
|
|
43
|
+
email: "jane@example.com",
|
|
44
|
+
name: "Jane Doe",
|
|
45
|
+
organizationId: "org-456",
|
|
46
|
+
organizationName: "Acme Corp",
|
|
47
|
+
signUpData: {
|
|
48
|
+
yourRole: "engineer",
|
|
49
|
+
companySize: "11-50",
|
|
50
|
+
usage: "monitoring",
|
|
51
|
+
solution: "llm-ops",
|
|
52
|
+
featureUsage: "evaluations",
|
|
53
|
+
utmCampaign: "launch-week",
|
|
54
|
+
howDidYouHearAboutUs: "twitter",
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
it("identifies user in Customer.io with email, name, role, and company_size", () => {
|
|
59
|
+
fireSignupNurturingCalls(baseArgs);
|
|
60
|
+
|
|
61
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
62
|
+
userId: "user-123",
|
|
63
|
+
traits: expect.objectContaining({
|
|
64
|
+
email: "jane@example.com",
|
|
65
|
+
name: "Jane Doe",
|
|
66
|
+
role: "engineer",
|
|
67
|
+
company_size: "11-50",
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("includes has_traces false and has_evaluations false in traits", () => {
|
|
73
|
+
fireSignupNurturingCalls(baseArgs);
|
|
74
|
+
|
|
75
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
76
|
+
userId: "user-123",
|
|
77
|
+
traits: expect.objectContaining({
|
|
78
|
+
has_traces: false,
|
|
79
|
+
has_evaluations: false,
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("includes has_prompts false and has_simulations false in traits", () => {
|
|
85
|
+
fireSignupNurturingCalls(baseArgs);
|
|
86
|
+
|
|
87
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
88
|
+
userId: "user-123",
|
|
89
|
+
traits: expect.objectContaining({
|
|
90
|
+
has_prompts: false,
|
|
91
|
+
has_simulations: false,
|
|
92
|
+
}),
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("includes signup_usage, signup_solution, and signup_feature_usage in traits", () => {
|
|
97
|
+
fireSignupNurturingCalls(baseArgs);
|
|
98
|
+
|
|
99
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
100
|
+
userId: "user-123",
|
|
101
|
+
traits: expect.objectContaining({
|
|
102
|
+
signup_usage: "monitoring",
|
|
103
|
+
signup_solution: "llm-ops",
|
|
104
|
+
signup_feature_usage: "evaluations",
|
|
105
|
+
}),
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("includes utm_campaign and how_heard when present", () => {
|
|
110
|
+
fireSignupNurturingCalls(baseArgs);
|
|
111
|
+
|
|
112
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
113
|
+
userId: "user-123",
|
|
114
|
+
traits: expect.objectContaining({
|
|
115
|
+
utm_campaign: "launch-week",
|
|
116
|
+
how_heard: "twitter",
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("includes createdAt as an ISO 8601 timestamp", () => {
|
|
122
|
+
fireSignupNurturingCalls(baseArgs);
|
|
123
|
+
|
|
124
|
+
const args = mockNurturing.identifyUser.mock.calls[0]![0];
|
|
125
|
+
expect(args.traits.createdAt).toBeDefined();
|
|
126
|
+
expect(new Date(args.traits.createdAt).toISOString()).toBe(args.traits.createdAt);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("associates user with organization via group call", () => {
|
|
130
|
+
fireSignupNurturingCalls(baseArgs);
|
|
131
|
+
|
|
132
|
+
expect(mockNurturing.groupUser).toHaveBeenCalledWith({
|
|
133
|
+
userId: "user-123",
|
|
134
|
+
groupId: "org-456",
|
|
135
|
+
traits: expect.objectContaining({
|
|
136
|
+
name: "Acme Corp",
|
|
137
|
+
company_size: "11-50",
|
|
138
|
+
plan: "free",
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("tracks signed_up event with signup metadata", () => {
|
|
144
|
+
fireSignupNurturingCalls(baseArgs);
|
|
145
|
+
|
|
146
|
+
expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
|
|
147
|
+
userId: "user-123",
|
|
148
|
+
event: "signed_up",
|
|
149
|
+
properties: expect.objectContaining({
|
|
150
|
+
yourRole: "engineer",
|
|
151
|
+
companySize: "11-50",
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("when signup data has no optional marketing fields", () => {
|
|
158
|
+
it("omits utm_campaign and how_heard from traits", () => {
|
|
159
|
+
fireSignupNurturingCalls({
|
|
160
|
+
userId: "user-123",
|
|
161
|
+
email: "jane@example.com",
|
|
162
|
+
name: "Jane Doe",
|
|
163
|
+
organizationId: "org-456",
|
|
164
|
+
organizationName: "Acme Corp",
|
|
165
|
+
signUpData: {
|
|
166
|
+
yourRole: "engineer",
|
|
167
|
+
companySize: "11-50",
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const args = mockNurturing.identifyUser.mock.calls[0]![0];
|
|
172
|
+
expect(args.traits.utm_campaign).toBeUndefined();
|
|
173
|
+
expect(args.traits.how_heard).toBeUndefined();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("omits lead_source, utm_source, utm_medium, utm_term, utm_content, and referrer from traits", () => {
|
|
177
|
+
fireSignupNurturingCalls({
|
|
178
|
+
userId: "user-123",
|
|
179
|
+
email: "jane@example.com",
|
|
180
|
+
name: "Jane Doe",
|
|
181
|
+
organizationId: "org-456",
|
|
182
|
+
organizationName: "Acme Corp",
|
|
183
|
+
signUpData: {
|
|
184
|
+
yourRole: "engineer",
|
|
185
|
+
companySize: "11-50",
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const args = mockNurturing.identifyUser.mock.calls[0]![0];
|
|
190
|
+
expect(args.traits.lead_source).toBeUndefined();
|
|
191
|
+
expect(args.traits.utm_source).toBeUndefined();
|
|
192
|
+
expect(args.traits.utm_medium).toBeUndefined();
|
|
193
|
+
expect(args.traits.utm_term).toBeUndefined();
|
|
194
|
+
expect(args.traits.utm_content).toBeUndefined();
|
|
195
|
+
expect(args.traits.referrer).toBeUndefined();
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("when signup data includes first-touch attribution", () => {
|
|
200
|
+
const attributionArgs = {
|
|
201
|
+
userId: "user-123",
|
|
202
|
+
email: "jane@example.com",
|
|
203
|
+
name: "Jane Doe",
|
|
204
|
+
organizationId: "org-456",
|
|
205
|
+
organizationName: "Acme Corp",
|
|
206
|
+
signUpData: {
|
|
207
|
+
yourRole: "engineer",
|
|
208
|
+
leadSource: "website",
|
|
209
|
+
utmSource: "newsletter",
|
|
210
|
+
utmMedium: "email",
|
|
211
|
+
utmCampaign: "apr2026",
|
|
212
|
+
utmTerm: "agents",
|
|
213
|
+
utmContent: "cta",
|
|
214
|
+
referrer: "https://www.langwatch.ai/",
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
it("maps leadSource to lead_source identify trait", () => {
|
|
219
|
+
fireSignupNurturingCalls(attributionArgs);
|
|
220
|
+
|
|
221
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
222
|
+
userId: "user-123",
|
|
223
|
+
traits: expect.objectContaining({
|
|
224
|
+
lead_source: "website",
|
|
225
|
+
}),
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("maps the utm tuple to snake_case identify traits", () => {
|
|
230
|
+
fireSignupNurturingCalls(attributionArgs);
|
|
231
|
+
|
|
232
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
233
|
+
userId: "user-123",
|
|
234
|
+
traits: expect.objectContaining({
|
|
235
|
+
utm_source: "newsletter",
|
|
236
|
+
utm_medium: "email",
|
|
237
|
+
utm_campaign: "apr2026",
|
|
238
|
+
utm_term: "agents",
|
|
239
|
+
utm_content: "cta",
|
|
240
|
+
}),
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("includes referrer in identify traits", () => {
|
|
245
|
+
fireSignupNurturingCalls(attributionArgs);
|
|
246
|
+
|
|
247
|
+
expect(mockNurturing.identifyUser).toHaveBeenCalledWith({
|
|
248
|
+
userId: "user-123",
|
|
249
|
+
traits: expect.objectContaining({
|
|
250
|
+
referrer: "https://www.langwatch.ai/",
|
|
251
|
+
}),
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("forwards attribution fields as signed_up event properties", () => {
|
|
256
|
+
fireSignupNurturingCalls(attributionArgs);
|
|
257
|
+
|
|
258
|
+
expect(mockNurturing.trackEvent).toHaveBeenCalledWith({
|
|
259
|
+
userId: "user-123",
|
|
260
|
+
event: "signed_up",
|
|
261
|
+
properties: expect.objectContaining({
|
|
262
|
+
leadSource: "website",
|
|
263
|
+
utmSource: "newsletter",
|
|
264
|
+
utmMedium: "email",
|
|
265
|
+
utmCampaign: "apr2026",
|
|
266
|
+
utmTerm: "agents",
|
|
267
|
+
utmContent: "cta",
|
|
268
|
+
referrer: "https://www.langwatch.ai/",
|
|
269
|
+
}),
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
describe("when Customer.io API is unavailable", () => {
|
|
275
|
+
it("does not throw (fire-and-forget)", async () => {
|
|
276
|
+
const { captureException } = await import(
|
|
277
|
+
"../../../../src/utils/posthogErrorCapture"
|
|
278
|
+
);
|
|
279
|
+
mockNurturing.identifyUser.mockRejectedValueOnce(
|
|
280
|
+
new Error("CIO unavailable"),
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
expect(() =>
|
|
284
|
+
fireSignupNurturingCalls({
|
|
285
|
+
userId: "user-123",
|
|
286
|
+
email: "jane@example.com",
|
|
287
|
+
name: "Jane Doe",
|
|
288
|
+
organizationId: "org-456",
|
|
289
|
+
organizationName: "Acme Corp",
|
|
290
|
+
}),
|
|
291
|
+
).not.toThrow();
|
|
292
|
+
|
|
293
|
+
// Wait for the rejected promise to be caught
|
|
294
|
+
await vi.waitFor(() => {
|
|
295
|
+
expect(captureException).toHaveBeenCalled();
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
describe("when nurturing is undefined (no Customer.io key)", () => {
|
|
301
|
+
it("silently skips without calling any nurturing methods", () => {
|
|
302
|
+
currentNurturing = undefined;
|
|
303
|
+
|
|
304
|
+
fireSignupNurturingCalls({
|
|
305
|
+
userId: "user-123",
|
|
306
|
+
email: "jane@example.com",
|
|
307
|
+
name: "Jane Doe",
|
|
308
|
+
organizationId: "org-456",
|
|
309
|
+
organizationName: "Acme Corp",
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
expect(mockNurturing.identifyUser).not.toHaveBeenCalled();
|
|
313
|
+
expect(mockNurturing.groupUser).not.toHaveBeenCalled();
|
|
314
|
+
expect(mockNurturing.trackEvent).not.toHaveBeenCalled();
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getApp } from "../../../../src/server/app-layer/app";
|
|
2
|
+
import { prisma } from "../../../../src/server/db";
|
|
3
|
+
import { captureException } from "../../../../src/utils/posthogErrorCapture";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Syncs has_subscription trait to Customer.io for all members of an organization.
|
|
7
|
+
*
|
|
8
|
+
* Called from the Stripe webhook service when a subscription is activated or cancelled.
|
|
9
|
+
* Fire-and-forget: never throws, never blocks the webhook handler.
|
|
10
|
+
*/
|
|
11
|
+
export function fireSubscriptionSyncNurturing({
|
|
12
|
+
organizationId,
|
|
13
|
+
hasSubscription,
|
|
14
|
+
}: {
|
|
15
|
+
organizationId: string;
|
|
16
|
+
hasSubscription: boolean;
|
|
17
|
+
}): void {
|
|
18
|
+
const nurturing = getApp().nurturing;
|
|
19
|
+
if (!nurturing) return;
|
|
20
|
+
|
|
21
|
+
void syncSubscriptionTrait({ organizationId, hasSubscription }).catch(
|
|
22
|
+
captureException,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function syncSubscriptionTrait({
|
|
27
|
+
organizationId,
|
|
28
|
+
hasSubscription,
|
|
29
|
+
}: {
|
|
30
|
+
organizationId: string;
|
|
31
|
+
hasSubscription: boolean;
|
|
32
|
+
}): Promise<void> {
|
|
33
|
+
const nurturing = getApp().nurturing;
|
|
34
|
+
if (!nurturing) return;
|
|
35
|
+
|
|
36
|
+
const orgUsers = await prisma.organizationUser.findMany({
|
|
37
|
+
where: { organizationId },
|
|
38
|
+
select: { userId: true },
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
await Promise.all(
|
|
42
|
+
orgUsers.map((ou) =>
|
|
43
|
+
nurturing.identifyUser({
|
|
44
|
+
userId: ou.userId,
|
|
45
|
+
traits: { has_subscription: hasSubscription },
|
|
46
|
+
}),
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
}
|