Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: emit OrganizationCreated from route handlers post-transaction

Devin AI1832499580dc
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index b44b1b4416..192aac6ccd 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1161,8 +1161,7 @@ export const registerRoutes = async (
membershipDAL,
roleDAL,
userGroupMembershipDAL,
- additionalPrivilegeDAL,
- telemetryService
+ additionalPrivilegeDAL
});
const subOrgService = subOrgServiceFactory({
diff --git a/backend/src/server/routes/v1/admin-router.ts b/backend/src/server/routes/v1/admin-router.ts
index c01dd083e9..948472a69a 100644
--- a/backend/src/server/routes/v1/admin-router.ts
+++ b/backend/src/server/routes/v1/admin-router.ts
@@ -661,6 +661,15 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
}
});
+ void server.services.telemetry.sendPostHogEvents({
+ event: PostHogEventTypes.OrganizationCreated,
+ distinctId: user.user.username ?? "",
+ organizationId: organization.id,
+ properties: {
+ name: organization.name
+ }
+ });
+
const adminDistinctId = user.user.username ?? user.user.email ?? "";
if (adminDistinctId) {
void server.services.telemetry.identifyUser(
@@ -831,6 +840,15 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
}
});
+ void server.services.telemetry.sendPostHogEvents({
+ event: PostHogEventTypes.OrganizationCreated,
+ distinctId: user.user.username ?? "",
+ organizationId: organization.id,
+ properties: {
+ name: organization.name
+ }
+ });
+
const bootstrapDistinctId = user.user.username ?? user.user.email ?? "";
if (bootstrapDistinctId) {
void server.services.telemetry.identifyUser(
@@ -946,6 +964,16 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
},
handler: async (req) => {
const organization = await server.services.superAdmin.createOrganization(req.body, req.permission);
+
+ void server.services.telemetry.sendPostHogEvents({
+ event: PostHogEventTypes.OrganizationCreated,
+ distinctId: getTelemetryDistinctId(req),
+ organizationId: organization.id,
+ properties: {
+ name: organization.name
+ }
+ });
+
return { organization };
}
});
diff --git a/backend/src/server/routes/v2/organization-router.ts b/backend/src/server/routes/v2/organization-router.ts
index ece8f3cb8e..2798567359 100644
--- a/backend/src/server/routes/v2/organization-router.ts
+++ b/backend/src/server/routes/v2/organization-router.ts
@@ -6,9 +6,11 @@ import { getConfig } from "@app/lib/config/env";
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { addAuthOriginDomainCookie } from "@app/server/lib/cookie";
import { GenericResourceNameSchema } from "@app/server/lib/schemas";
+import { getTelemetryDistinctId } from "@app/server/lib/telemetry";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode } from "@app/services/auth/auth-type";
import { sanitizedOrganizationSchema } from "@app/services/org/org-schema";
+import { PostHogEventTypes } from "@app/services/telemetry/telemetry-types";
import { SanitizedUserSchema } from "../sanitizedSchemas";
@@ -402,6 +404,15 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
orgName: req.body.name
});
+ void server.services.telemetry.sendPostHogEvents({
+ event: PostHogEventTypes.OrganizationCreated,
+ distinctId: getTelemetryDistinctId(req),
+ organizationId: organization.id,
+ properties: {
+ name: req.body.name
+ }
+ });
+
return { organization };
}
});
diff --git a/backend/src/server/routes/v3/signup-router.ts b/backend/src/server/routes/v3/signup-router.ts
index f2b3e3dd5e..2309fd9f70 100644
--- a/backend/src/server/routes/v3/signup-router.ts
+++ b/backend/src/server/routes/v3/signup-router.ts
@@ -162,6 +162,17 @@ export const registerSignupRouter = async (server: FastifyZodProvider) => {
}
});
+ if (organizationId) {
+ void server.services.telemetry.sendPostHogEvents({
+ event: PostHogEventTypes.OrganizationCreated,
+ distinctId: user.username ?? "",
+ organizationId,
+ properties: {
+ name: "organizationName" in req.body ? (req.body.organizationName as string) : ""
+ }
+ });
+ }
+
const signupDistinctId = user.username ?? user.email ?? "";
if (signupDistinctId) {
void server.services.telemetry.identifyUser(
diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts
index cdcf4dc3f5..b6ae56ca8f 100644
--- a/backend/src/services/org/org-service.ts
+++ b/backend/src/services/org/org-service.ts
@@ -61,8 +61,6 @@ import { fnDeleteProjectSecretReminders } from "../secret/secret-fns";
import { TSecretFolderDALFactory } from "../secret-folder/secret-folder-dal";
import { TSecretV2BridgeDALFactory } from "../secret-v2-bridge/secret-v2-bridge-dal";
import { SmtpTemplates, TSmtpService } from "../smtp/smtp-service";
-import { TTelemetryServiceFactory } from "../telemetry/telemetry-service";
-import { PostHogEventTypes } from "../telemetry/telemetry-types";
import { TUserDALFactory } from "../user/user-dal";
import { TIncidentContactsDALFactory } from "./incident-contacts-dal";
import { TOrgDALFactory } from "./org-dal";
@@ -123,7 +121,6 @@ type TOrgServiceFactoryDep = {
reminderService: Pick<TReminderServiceFactory, "deleteReminderBySecretId">;
userGroupMembershipDAL: TUserGroupMembershipDALFactory;
additionalPrivilegeDAL: TAdditionalPrivilegeDALFactory;
- telemetryService: Pick<TTelemetryServiceFactory, "sendPostHogEvents">;
};
export type TOrgServiceFactory = ReturnType<typeof orgServiceFactory>;
@@ -157,8 +154,7 @@ export const orgServiceFactory = ({
membershipUserDAL,
membershipDAL,
userGroupMembershipDAL,
- additionalPrivilegeDAL,
- telemetryService
+ additionalPrivilegeDAL
}: TOrgServiceFactoryDep) => {
/*
* Get organization details by the organization id
@@ -700,15 +696,6 @@ export const orgServiceFactory = ({
await licenseService.updateSubscriptionOrgMemberCount(organization.id, trx);
- void telemetryService.sendPostHogEvents({
- event: PostHogEventTypes.OrganizationCreated,
- distinctId: userEmail ?? "",
- organizationId: organization.id,
- properties: {
- name: orgName
- }
- });
-
return organization;
};