Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(sfdc): link org owner as member on create + sync starter org (#14486)

Steffen Schmitz0e0d1beac847
diff --git a/web/src/__tests__/server/unit/sfdc-sync.servertest.ts b/web/src/__tests__/server/unit/sfdc-sync.servertest.ts
index 2353c9ee76..27a365107b 100644
--- a/web/src/__tests__/server/unit/sfdc-sync.servertest.ts
+++ b/web/src/__tests__/server/unit/sfdc-sync.servertest.ts
@@ -34,6 +34,9 @@ const { envMock, prismaMock, loggerMock, fetchMock } = vi.hoisted(() => {
async (): Promise<{ sfdcOrgId: string | null } | null> => null,
),
update: vi.fn(async () => ({})),
+ create: vi.fn(
+ async (): Promise<unknown> => ({ id: "org-new", name: "New Org" }),
+ ),
},
organizationMembership: {
findFirst: vi.fn(async (): Promise<unknown> => null),
@@ -44,6 +47,12 @@ const { envMock, prismaMock, loggerMock, fetchMock } = vi.hoisted(() => {
auditLog: {
create: vi.fn(async () => ({})),
},
+ // tRPC organizations.create wraps its writes in a transaction; run the
+ // callback with prismaMock standing in for the tx client.
+ $transaction: vi.fn(
+ async (cb: (tx: unknown) => Promise<unknown>): Promise<unknown> =>
+ cb(prismaMock),
+ ),
},
loggerMock: {
trace: vi.fn(),
@@ -640,3 +649,41 @@ describe("call site: admin API handleUpdateMembership", () => {
});
});
});
+
+describe("call site: tRPC organizations.create", () => {
+ // upsertOrg creates the SFDC Account but does not link the creator; the
+ // create flow must also fire setUserRole to establish the OWNER bridge.
+ it("creates the account AND links the OWNER (upsertOrg + setUserRole)", async () => {
+ prismaMock.organization.create.mockResolvedValueOnce({
+ id: "org-new",
+ name: "New Org",
+ });
+ // [0] upsertOrg (Account), [1] setUserRole (OWNER member bridge)
+ fetchMock
+ .mockResolvedValueOnce(emptyOkResponse())
+ .mockResolvedValueOnce(emptyOkResponse());
+
+ await createOwnerCaller("org-1").organizations.create({ name: "New Org" });
+
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+
+ const upsertOrgBody = JSON.parse(fetchMock.mock.calls[0][1].body as string);
+ expect(upsertOrgBody).toMatchObject({
+ type: "updateOrg",
+ orgId: "org-new",
+ orgName: "New Org",
+ userId: "owner-user",
+ email: "[email protected]",
+ role: "ADMIN", // OWNER -> ADMIN
+ });
+
+ const setRoleBody = JSON.parse(fetchMock.mock.calls[1][1].body as string);
+ expect(setRoleBody).toMatchObject({
+ type: "setUserRole",
+ orgId: "org-new",
+ userId: "owner-user",
+ email: "[email protected]",
+ role: "ADMIN",
+ });
+ });
+});
diff --git a/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts b/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts
index 4f49a103c3..4b84e9e7ac 100644
--- a/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts
+++ b/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts
@@ -181,11 +181,27 @@ export async function createProjectMembershipsOnSignup(
canCreateOrganizations(user.email) &&
(options?.userWasJustCreated || isNewUser)
) {
- await provisionStarterOrganizationForNewUser({
+ const starterOrg = await provisionStarterOrganizationForNewUser({
prisma,
userId: user.id,
userName: user.name,
});
+
+ if (starterOrg) {
+ await getSfdcService()?.upsertOrg({
+ orgId: starterOrg.organization.id,
+ orgName: starterOrg.organization.name,
+ userId: user.id,
+ email: user.email,
+ role: "OWNER",
+ });
+ await getSfdcService()?.setUserRole({
+ orgId: starterOrg.organization.id,
+ userId: user.id,
+ email: user.email,
+ role: "OWNER",
+ });
+ }
}
if (isCloudDeployment && (options?.userWasJustCreated || isNewUser)) {
diff --git a/web/src/features/organizations/server/organizationRouter.ts b/web/src/features/organizations/server/organizationRouter.ts
index e3f0d671b8..935d3f2926 100644
--- a/web/src/features/organizations/server/organizationRouter.ts
+++ b/web/src/features/organizations/server/organizationRouter.ts
@@ -122,6 +122,12 @@ export const organizationsRouter = createTRPCRouter({
email: ctx.session.user.email,
role: "OWNER",
});
+ await getSfdcService()?.setUserRole({
+ orgId: organization.id,
+ userId: ctx.session.user.id,
+ email: ctx.session.user.email,
+ role: "OWNER",
+ });
return {
id: organization.id,