fix: Org creation for self-hosters (#21994)
* Restart form if admin is not org admin * Show pricing is admin and hosted * Only show billing specific fields when enabled * Create user onboarding only if valid valid license when self hosting * Reset store when org creation is completed * Create schema for trpc input when creating an org * Add `organization.createSelfHosted` trpc endpoint * `createOrganizationFromOnboarding` do not require stripe data * Create organization without billing if self-hosted admin * Update organizationOnboarding record as completed * Handle updating subscription for self hosters * Refactor `isBillingEnabled` to store * Type fix * Fix admin org creation for hosted * Hide UI from admin * Add tests * fix: Fix LicenseKeySingleton mocking in organization tests - Add proper vi.mock() module-level mocking for LicenseKeySingleton - Replace incorrect casting syntax with vi.mocked() approach - Add LicenseKeySingleton mock to hosted tests in intentToCreateOrg.handler.test.ts - Fix IS_SELF_HOSTED mocking for hosted vs self-hosted test scenarios - All organization creation tests now pass locally Co-Authored-By: [email protected] <[email protected]> * Fix tests * Fix tests * Test fixes * Type fix * Type fix * Type fix * Update packages/features/ee/organizations/lib/server/createOrganizationFromOnboarding.test.ts Fix typo Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * fix: Add IS_SELF_HOSTED mocking for E2E organization creation tests Co-Authored-By: [email protected] <[email protected]> * Fix E2E test * Address feedback * Type fix * Fix e2e tests * Remove unused code * Small fixes * fix unit tests * review fixes --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Benny Joo <[email protected]> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: Hariom <[email protected]>
This commit is contained in:
15 files changed
+802
-431
No files matched your search
@@ -7,7 +7,7 @@ import { useState } from "react";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
|
||||
import { subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
import { MINIMUM_NUMBER_OF_ORG_SEATS } from "@calcom/lib/constants";
|
||||
import { MINIMUM_NUMBER_OF_ORG_SEATS, IS_SELF_HOSTED } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
import { CreationSource } from "@calcom/prisma/enums";
|
||||
@@ -55,8 +55,9 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
|
||||
const router = useRouter();
|
||||
const [serverErrorMessage, setServerErrorMessage] = useState<string | null>(null);
|
||||
const isAdmin = session.data.user.role === UserPermissionRole.ADMIN;
|
||||
const defaultOrgOwnerEmail = session.data.user.email ?? "";
|
||||
const { useOnboardingStore } = useOnboarding({ step: "start" });
|
||||
// Let self-hosters create an organization with their own email. Hosted's Admin already has an organization for their email
|
||||
const defaultOrgOwnerEmail = (!isAdmin || IS_SELF_HOSTED ? session.data.user.email : null) ?? "";
|
||||
const { useOnboardingStore, isBillingEnabled } = useOnboarding({ step: "start" });
|
||||
const { slug, name, orgOwnerEmail, billingPeriod, pricePerSeat, seats, onboardingId, reset } =
|
||||
useOnboardingStore();
|
||||
|
||||
@@ -71,7 +72,7 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
|
||||
defaultValues: {
|
||||
billingPeriod: billingPeriod ?? BillingPeriod.MONTHLY,
|
||||
slug: slug ?? (!isAdmin ? deriveSlugFromEmail(defaultOrgOwnerEmail) : undefined),
|
||||
orgOwnerEmail: orgOwnerEmail || (!isAdmin ? defaultOrgOwnerEmail : undefined),
|
||||
orgOwnerEmail: orgOwnerEmail || defaultOrgOwnerEmail,
|
||||
name: name ?? (!isAdmin ? deriveOrgNameFromEmail(defaultOrgOwnerEmail) : undefined),
|
||||
seats: seats ?? null,
|
||||
pricePerSeat: pricePerSeat ?? null,
|
||||
@@ -92,7 +93,7 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
|
||||
slug: data.slug,
|
||||
});
|
||||
|
||||
if (isAdmin) {
|
||||
if (isAdmin && data.userId !== session.data.user.id) {
|
||||
router.push("/settings/organizations/new/handover");
|
||||
} else {
|
||||
router.push("/settings/organizations/new/about");
|
||||
@@ -133,7 +134,7 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
|
||||
<Alert severity="error" message={serverErrorMessage} />
|
||||
</div>
|
||||
)}
|
||||
{isAdmin && (
|
||||
{isBillingEnabled && isAdmin && (
|
||||
<div className="mb-5">
|
||||
<Controller
|
||||
name="billingPeriod"
|
||||
@@ -251,7 +252,7 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
|
||||
/>
|
||||
</div>
|
||||
|
||||
{isAdmin && (
|
||||
{isBillingEnabled && isAdmin && (
|
||||
<>
|
||||
<section className="grid grid-cols-2 gap-2">
|
||||
<div className="w-full">
|
||||
@@ -305,7 +306,7 @@ const CreateANewOrganizationFormChild = ({ session }: { session: Ensure<SessionC
|
||||
)}
|
||||
|
||||
{/* This radio group does nothing - its just for visual purposes */}
|
||||
{!isAdmin && (
|
||||
{isBillingEnabled && !isAdmin && (
|
||||
<>
|
||||
<div className="bg-subtle space-y-5 rounded-lg p-5">
|
||||
<h3 className="font-cal text-default text-lg font-semibold leading-4">
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useEffect } from "react";
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { WEBAPP_URL, IS_SELF_HOSTED } from "@calcom/lib/constants";
|
||||
import { UserPermissionRole } from "@calcom/prisma/enums";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
@@ -102,6 +102,10 @@ export const useOnboardingStore = create<OnboardingStoreState>()(
|
||||
if (state) {
|
||||
set(state);
|
||||
} else {
|
||||
// Clear the localStorage entry for this store
|
||||
if (typeof window !== "undefined") {
|
||||
localStorage.removeItem("org-creation-onboarding");
|
||||
}
|
||||
set(initialState);
|
||||
}
|
||||
},
|
||||
@@ -117,6 +121,7 @@ export const useOnboarding = (params?: { step?: "start" | "status" | null }) =>
|
||||
const router = useRouter();
|
||||
const path = usePathname();
|
||||
const isAdmin = session.data?.user?.role === UserPermissionRole.ADMIN;
|
||||
const isBillingEnabled = !(IS_SELF_HOSTED && isAdmin) || process.env.NEXT_PUBLIC_IS_E2E;
|
||||
const searchParams = useSearchParams();
|
||||
const { data: organizationOnboarding, isPending: isLoadingOrgOnboarding } =
|
||||
trpc.viewer.organizations.getOrganizationOnboarding.useQuery();
|
||||
@@ -127,6 +132,10 @@ export const useOnboarding = (params?: { step?: "start" | "status" | null }) =>
|
||||
return;
|
||||
}
|
||||
|
||||
if (organizationOnboarding?.isComplete) {
|
||||
reset();
|
||||
}
|
||||
|
||||
if (organizationOnboarding) {
|
||||
// Must not keep resetting state on each step change as every step doesn't save at the moment and this would reset user's changes
|
||||
if (!window.isOrgOnboardingSynced) {
|
||||
@@ -143,6 +152,9 @@ export const useOnboarding = (params?: { step?: "start" | "status" | null }) =>
|
||||
bio: organizationOnboarding.bio,
|
||||
logo: organizationOnboarding.logo,
|
||||
});
|
||||
if (isAdmin && organizationOnboarding?.orgOwnerEmail !== session.data?.user.email) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// First step doesn't require onboardingId
|
||||
@@ -154,11 +166,6 @@ export const useOnboarding = (params?: { step?: "start" | "status" | null }) =>
|
||||
router.push("/settings/organizations/new");
|
||||
}
|
||||
}
|
||||
|
||||
// Admin must start fresh, so that they can create organization with any email and data doesn't mix from different org creations
|
||||
if (isAdmin) {
|
||||
reset();
|
||||
}
|
||||
}, [organizationOnboarding, isLoadingOrgOnboarding, isAdmin, onboardingId, reset, step, router]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -170,5 +177,10 @@ export const useOnboarding = (params?: { step?: "start" | "status" | null }) =>
|
||||
router.push(`/auth/login?callbackUrl=${WEBAPP_URL}${path}${searchString ? `?${searchString}` : ""}`);
|
||||
}
|
||||
}, [session, router, path, searchParams]);
|
||||
return { useOnboardingStore, isLoadingOrgOnboarding, dbOnboarding: organizationOnboarding };
|
||||
return {
|
||||
useOnboardingStore,
|
||||
isLoadingOrgOnboarding,
|
||||
dbOnboarding: organizationOnboarding,
|
||||
isBillingEnabled,
|
||||
};
|
||||
};
|
||||
+320
-232
@@ -6,13 +6,22 @@ import prismock from "../../../../../../tests/libs/__mocks__/prisma";
|
||||
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import * as constants from "@calcom/lib/constants";
|
||||
import { createDomain } from "@calcom/lib/domainManager/organization";
|
||||
import type { OrganizationOnboarding } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { createTeamsHandler } from "@calcom/trpc/server/routers/viewer/organizations/createTeams.handler";
|
||||
import { inviteMembersWithNoInviterPermissionCheck } from "@calcom/trpc/server/routers/viewer/teams/inviteMember/inviteMember.handler";
|
||||
|
||||
import { createOrganizationFromOnboarding } from "./createOrganizationFromOnboarding";
|
||||
|
||||
vi.mock("@calcom/ee/common/server/LicenseKeyService", () => ({
|
||||
LicenseKeySingleton: {
|
||||
getInstance: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock the external functions
|
||||
vi.mock("@calcom/features/auth/lib/verifyEmail", () => ({
|
||||
sendEmailVerification: vi.fn(),
|
||||
@@ -147,6 +156,19 @@ async function createTestMembership(data: { userId: number; teamId: number; role
|
||||
});
|
||||
}
|
||||
|
||||
async function expectOrganizationOnboardingToHaveDataContaining({
|
||||
onboardingId,
|
||||
expectedData,
|
||||
}: {
|
||||
onboardingId: string;
|
||||
expectedData: Partial<OrganizationOnboarding>;
|
||||
}) {
|
||||
const updatedOrganizationOnboarding = await prismock.organizationOnboarding.findUnique({
|
||||
where: { id: onboardingId },
|
||||
});
|
||||
expect(updatedOrganizationOnboarding).toEqual(expect.objectContaining(expectedData));
|
||||
}
|
||||
|
||||
describe("createOrganizationFromOnboarding", () => {
|
||||
beforeEach(async () => {
|
||||
vi.resetAllMocks();
|
||||
@@ -154,260 +176,326 @@ describe("createOrganizationFromOnboarding", () => {
|
||||
await prismock.reset();
|
||||
});
|
||||
|
||||
// Skipped because non-existend user creation support isn't there through onboarding now.
|
||||
it.skip("should create an organization with a non-existent user as owner", async () => {
|
||||
const organizationOnboarding = await createTestOrganizationOnboarding();
|
||||
|
||||
const { organization, owner } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
describe("hosted", () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(constants, "IS_SELF_HOSTED", "get").mockReturnValue(false);
|
||||
});
|
||||
|
||||
// Verify organization creation
|
||||
expect(organization).toBeDefined();
|
||||
expect(organization.name).toBe(organizationOnboarding.name);
|
||||
expect(organization.slug).toBe(organizationOnboarding.slug);
|
||||
expect(organization.metadata).toEqual({
|
||||
orgSeats: 5,
|
||||
orgPricePerSeat: 20,
|
||||
billingPeriod: "MONTHLY",
|
||||
subscriptionId: "mock_subscription_id",
|
||||
subscriptionItemId: "mock_subscription_item_id",
|
||||
// Skipped because non-existend user creation support isn't there through onboarding now.
|
||||
it.skip("should create an organization with a non-existent user as owner", async () => {
|
||||
const organizationOnboarding = await createTestOrganizationOnboarding();
|
||||
|
||||
const { organization, owner } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
// Verify organization creation
|
||||
expect(organization).toBeDefined();
|
||||
expect(organization.name).toBe(organizationOnboarding.name);
|
||||
expect(organization.slug).toBe(organizationOnboarding.slug);
|
||||
expect(organization.metadata).toEqual({
|
||||
orgSeats: 5,
|
||||
orgPricePerSeat: 20,
|
||||
billingPeriod: "MONTHLY",
|
||||
subscriptionId: "mock_subscription_id",
|
||||
subscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
// Verify owner creation
|
||||
expect(owner).toBeDefined();
|
||||
expect(owner.email).toBe(organizationOnboarding.orgOwnerEmail);
|
||||
});
|
||||
|
||||
// Verify owner creation
|
||||
expect(owner).toBeDefined();
|
||||
expect(owner.email).toBe(organizationOnboarding.orgOwnerEmail);
|
||||
});
|
||||
|
||||
it("should create an organization with an existing user as owner", async () => {
|
||||
const orgOwnerEmail = "[email protected]";
|
||||
const existingUser = await createTestUser({
|
||||
email: orgOwnerEmail,
|
||||
name: "Existing User",
|
||||
username: "existinguser",
|
||||
onboardingCompleted: true,
|
||||
emailVerified: new Date(),
|
||||
});
|
||||
|
||||
const organizationOnboarding = await createTestOrganizationOnboarding({
|
||||
orgOwnerEmail,
|
||||
});
|
||||
|
||||
const { organization, owner } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
// Verify organization creation
|
||||
expect(organization).toBeDefined();
|
||||
expect(organization.name).toBe(organizationOnboarding.name);
|
||||
expect(organization.slug).toBe(organizationOnboarding.slug);
|
||||
|
||||
// Verify owner is the existing user
|
||||
expect(owner.id).toBe(existingUser.id);
|
||||
expect(owner.email).toBe(existingUser.email);
|
||||
|
||||
expect(createDomain).toHaveBeenCalledWith(organization.slug);
|
||||
// Verify team creation and member invites still work
|
||||
expect(inviteMembersWithNoInviterPermissionCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
orgSlug: organization.slug,
|
||||
teamId: organization.id,
|
||||
invitations: organizationOnboarding.invitedMembers?.map((member) => ({
|
||||
usernameOrEmail: member.email,
|
||||
role: MembershipRole.MEMBER,
|
||||
})),
|
||||
})
|
||||
);
|
||||
expect(createTeamsHandler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
input: expect.objectContaining({
|
||||
orgId: organization.id,
|
||||
teamNames: ["New Team"],
|
||||
moveTeams: expect.arrayContaining([expect.objectContaining({ id: 1, newSlug: "new-team-slug" })]),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should reuse existing organization if organizationId is already set in onboarding. Covers the retry scenario where org got created but then error occurred", async () => {
|
||||
const orgName = "Existing Org";
|
||||
const orgSlug = "existing-org";
|
||||
const existingOrg = await createTestOrganization({
|
||||
name: orgName,
|
||||
slug: orgSlug,
|
||||
});
|
||||
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
it("should create an organization with an existing user as owner", async () => {
|
||||
const orgOwnerEmail = "[email protected]";
|
||||
const existingUser = await createTestUser({
|
||||
email: orgOwnerEmail,
|
||||
name: "Existing User",
|
||||
username: "existinguser",
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
organizationId: existingOrg.id,
|
||||
onboardingCompleted: true,
|
||||
emailVerified: new Date(),
|
||||
});
|
||||
|
||||
const organizationOnboarding = await createTestOrganizationOnboarding({
|
||||
orgOwnerEmail,
|
||||
});
|
||||
|
||||
const { organization, owner } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
// Verify organization creation
|
||||
expect(organization).toBeDefined();
|
||||
expect(organization.name).toBe(organizationOnboarding.name);
|
||||
expect(organization.slug).toBe(organizationOnboarding.slug);
|
||||
|
||||
// Verify owner is the existing user
|
||||
expect(owner.id).toBe(existingUser.id);
|
||||
expect(owner.email).toBe(existingUser.email);
|
||||
|
||||
expect(createDomain).toHaveBeenCalledWith(organization.slug);
|
||||
// Verify team creation and member invites still work
|
||||
expect(inviteMembersWithNoInviterPermissionCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
orgSlug: organization.slug,
|
||||
teamId: organization.id,
|
||||
invitations: organizationOnboarding.invitedMembers?.map((member) => ({
|
||||
usernameOrEmail: member.email,
|
||||
role: MembershipRole.MEMBER,
|
||||
})),
|
||||
})
|
||||
);
|
||||
expect(createTeamsHandler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
input: expect.objectContaining({
|
||||
orgId: organization.id,
|
||||
teamNames: ["New Team"],
|
||||
moveTeams: expect.arrayContaining([expect.objectContaining({ id: 1, newSlug: "new-team-slug" })]),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should reuse existing organization if organizationId is already set in onboarding. Covers the retry scenario where org got created but then error occurred", async () => {
|
||||
const orgName = "Existing Org";
|
||||
const orgSlug = "existing-org";
|
||||
const existingOrg = await createTestOrganization({
|
||||
name: orgName,
|
||||
slug: orgSlug,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const { organization } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
name: "Existing User",
|
||||
username: "existinguser",
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
organizationId: existingOrg.id,
|
||||
name: orgName,
|
||||
slug: orgSlug,
|
||||
},
|
||||
});
|
||||
|
||||
// Verify the existing organization was reused
|
||||
expect(organization.id).toBe(existingOrg.id);
|
||||
});
|
||||
|
||||
it("should throw error if organization with same slug exists", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
await createTestOrganization({
|
||||
name: "Conflicting Org with same slug",
|
||||
slug: organizationOnboarding.slug,
|
||||
});
|
||||
|
||||
await expect(
|
||||
createOrganizationFromOnboarding({
|
||||
const { organization } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
})
|
||||
).rejects.toThrow("organization_url_taken");
|
||||
});
|
||||
});
|
||||
|
||||
it("should create team with slugified slug", async () => {
|
||||
const teamToMove = await createTestTeam({
|
||||
name: "Sales",
|
||||
slug: "Company Sales",
|
||||
// Verify the existing organization was reused
|
||||
expect(organization.id).toBe(existingOrg.id);
|
||||
});
|
||||
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "UPPERCASE-SLUG",
|
||||
teams: [{ id: teamToMove.id, name: "Sales", isBeingMigrated: true, slug: "sales-team" }],
|
||||
},
|
||||
it("should throw error if organization with same slug exists", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
await createTestOrganization({
|
||||
name: "Conflicting Org with same slug",
|
||||
slug: organizationOnboarding.slug,
|
||||
});
|
||||
|
||||
await expect(
|
||||
createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
})
|
||||
).rejects.toThrow("organization_url_taken");
|
||||
});
|
||||
|
||||
const { organization } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
it("should create team with slugified slug", async () => {
|
||||
const teamToMove = await createTestTeam({
|
||||
name: "Sales",
|
||||
slug: "Company Sales",
|
||||
});
|
||||
|
||||
expect(createTeamsHandler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
input: expect.objectContaining({
|
||||
orgId: organization.id,
|
||||
teamNames: [],
|
||||
moveTeams: expect.arrayContaining([expect.objectContaining({ id: 1, newSlug: "sales-team" })]),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "UPPERCASE-SLUG",
|
||||
teams: [{ id: teamToMove.id, name: "Sales", isBeingMigrated: true, slug: "sales-team" }],
|
||||
},
|
||||
});
|
||||
|
||||
it("should update stripe customer ID for existing user", async () => {
|
||||
const { user: existingUser, organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
// Verify user's stripe customer ID was updated
|
||||
const updatedUser = await prismock.user.findUnique({
|
||||
where: { id: existingUser.id },
|
||||
});
|
||||
|
||||
expect(updatedUser?.metadata).toEqual(
|
||||
expect.objectContaining({
|
||||
stripeCustomerId: organizationOnboarding.stripeCustomerId,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should create organization in with slug set even if there is a team with same slug of which orgOwner(to-be) is a member", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
const teamWithConflictingSlug = await createTestTeam({
|
||||
name: "TestTeamWithConflictingSlug",
|
||||
slug: organizationOnboarding.slug,
|
||||
});
|
||||
|
||||
// Make the orgOwner a member of the team
|
||||
await createTestMembership({
|
||||
userId: organizationOnboarding.createdById,
|
||||
teamId: teamWithConflictingSlug.id,
|
||||
role: MembershipRole.ADMIN,
|
||||
});
|
||||
|
||||
const { organization } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
expect(organization.slug).toBe(organizationOnboarding.slug);
|
||||
});
|
||||
|
||||
it("should not create organization if there is a team with same slug of which orgOwner(to-be) is not a member", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
await createTestTeam({
|
||||
name: "TestTeamWithConflictingSlugNotOwnedByOrgOwner",
|
||||
slug: organizationOnboarding.slug,
|
||||
});
|
||||
|
||||
// Don't make the orgOwner a member of the team
|
||||
// await createTestMembership({
|
||||
// userId: organizationOnboarding.createdById,
|
||||
// teamId: teamWithConflictingSlug.id,
|
||||
// role: MembershipRole.ADMIN,
|
||||
// });
|
||||
|
||||
await expect(
|
||||
createOrganizationFromOnboarding({
|
||||
const { organization } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
})
|
||||
).rejects.toThrow("organization_url_taken");
|
||||
});
|
||||
|
||||
expect(createTeamsHandler).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
input: expect.objectContaining({
|
||||
orgId: organization.id,
|
||||
teamNames: [],
|
||||
moveTeams: expect.arrayContaining([expect.objectContaining({ id: 1, newSlug: "sales-team" })]),
|
||||
}),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should update stripe customer ID for existing user", async () => {
|
||||
const { user: existingUser, organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
// Verify user's stripe customer ID was updated
|
||||
const updatedUser = await prismock.user.findUnique({
|
||||
where: { id: existingUser.id },
|
||||
});
|
||||
|
||||
expect(updatedUser?.metadata).toEqual(
|
||||
expect.objectContaining({
|
||||
stripeCustomerId: organizationOnboarding.stripeCustomerId,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it("should create organization in with slug set even if there is a team with same slug of which orgOwner(to-be) is a member", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
const teamWithConflictingSlug = await createTestTeam({
|
||||
name: "TestTeamWithConflictingSlug",
|
||||
slug: organizationOnboarding.slug,
|
||||
});
|
||||
|
||||
// Make the orgOwner a member of the team
|
||||
await createTestMembership({
|
||||
userId: organizationOnboarding.createdById,
|
||||
teamId: teamWithConflictingSlug.id,
|
||||
role: MembershipRole.ADMIN,
|
||||
});
|
||||
|
||||
const { organization } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
expect(organization.slug).toBe(organizationOnboarding.slug);
|
||||
});
|
||||
|
||||
it("should not create organization if there is a team with same slug of which orgOwner(to-be) is not a member", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
organizationOnboarding: {
|
||||
slug: "conflicting-org-with-same-slug",
|
||||
},
|
||||
});
|
||||
|
||||
await createTestTeam({
|
||||
name: "TestTeamWithConflictingSlugNotOwnedByOrgOwner",
|
||||
slug: organizationOnboarding.slug,
|
||||
});
|
||||
|
||||
// Don't make the orgOwner a member of the team
|
||||
// await createTestMembership({
|
||||
// userId: organizationOnboarding.createdById,
|
||||
// teamId: teamWithConflictingSlug.id,
|
||||
// role: MembershipRole.ADMIN,
|
||||
// });
|
||||
|
||||
await expect(
|
||||
createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
})
|
||||
).rejects.toThrow("organization_url_taken");
|
||||
});
|
||||
|
||||
it("should not mark the Onboarding as completed(isComplete=true)", async () => {
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
await expectOrganizationOnboardingToHaveDataContaining({
|
||||
onboardingId: organizationOnboarding.id,
|
||||
expectedData: {
|
||||
isComplete: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("self-hosted", () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(constants, "IS_SELF_HOSTED", "get").mockReturnValue(true);
|
||||
});
|
||||
|
||||
it("should fail if the license is invalid", async () => {
|
||||
vi.mocked(LicenseKeySingleton.getInstance as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
checkLicense: vi.fn().mockResolvedValue(false),
|
||||
});
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
await expect(
|
||||
createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
})
|
||||
).rejects.toThrow("Self hosted license not valid");
|
||||
});
|
||||
|
||||
it("should create an organization with a valid license", async () => {
|
||||
vi.mocked(LicenseKeySingleton.getInstance).mockResolvedValue({
|
||||
checkLicense: vi.fn().mockResolvedValue(true),
|
||||
});
|
||||
const { organizationOnboarding } = await createOnboardingEligibleUserAndOnboarding({
|
||||
user: {
|
||||
email: "[email protected]",
|
||||
},
|
||||
});
|
||||
|
||||
const { organization, owner } = await createOrganizationFromOnboarding({
|
||||
organizationOnboarding,
|
||||
paymentSubscriptionId: "mock_subscription_id",
|
||||
paymentSubscriptionItemId: "mock_subscription_item_id",
|
||||
});
|
||||
|
||||
expect(organization).toBeDefined();
|
||||
expect(owner).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { TFunction } from "i18next";
|
||||
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { sendOrganizationCreationEmail } from "@calcom/emails/email-manager";
|
||||
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
||||
import { getOrgFullOrigin } from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
@@ -10,9 +11,11 @@ import {
|
||||
} from "@calcom/features/ee/organizations/lib/server/orgCreationUtils";
|
||||
import { DEFAULT_SCHEDULE } from "@calcom/lib/availability";
|
||||
import { getAvailabilityFromSchedule } from "@calcom/lib/availability";
|
||||
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { DeploymentRepository } from "@calcom/lib/server/repository/deployment";
|
||||
import { OrganizationRepository } from "@calcom/lib/server/repository/organization";
|
||||
import { OrganizationOnboardingRepository } from "@calcom/lib/server/repository/organizationOnboarding";
|
||||
import { UserRepository } from "@calcom/lib/server/repository/user";
|
||||
@@ -333,9 +336,13 @@ async function backwardCompatibilityForSubscriptionDetails({
|
||||
id: number;
|
||||
metadata: Prisma.JsonValue;
|
||||
};
|
||||
paymentSubscriptionId: string;
|
||||
paymentSubscriptionItemId: string;
|
||||
paymentSubscriptionId?: string;
|
||||
paymentSubscriptionItemId?: string;
|
||||
}) {
|
||||
if (!paymentSubscriptionId || !paymentSubscriptionItemId) {
|
||||
return organization;
|
||||
}
|
||||
|
||||
const existingMetadata = teamMetadataSchema.parse(organization.metadata);
|
||||
const updatedOrganization = await OrganizationRepository.updateStripeSubscriptionDetails({
|
||||
id: organization.id,
|
||||
@@ -387,9 +394,19 @@ async function handleOrganizationCreation({
|
||||
}: {
|
||||
organizationOnboarding: OrganizationOnboardingArg;
|
||||
owner: OrgOwner;
|
||||
paymentSubscriptionId: string;
|
||||
paymentSubscriptionItemId: string;
|
||||
paymentSubscriptionId?: string;
|
||||
paymentSubscriptionItemId?: string;
|
||||
}) {
|
||||
if (IS_SELF_HOSTED) {
|
||||
const deploymentRepo = new DeploymentRepository(prisma);
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance(deploymentRepo);
|
||||
const hasValidLicense = await licenseKeyService.checkLicense();
|
||||
|
||||
if (!hasValidLicense) {
|
||||
throw new Error("Self hosted license not valid");
|
||||
}
|
||||
}
|
||||
|
||||
let organization;
|
||||
const orgData = {
|
||||
id: organizationOnboarding.organizationId,
|
||||
@@ -468,8 +485,8 @@ export const createOrganizationFromOnboarding = async ({
|
||||
paymentSubscriptionItemId,
|
||||
}: {
|
||||
organizationOnboarding: OrganizationOnboardingArg;
|
||||
paymentSubscriptionId: string;
|
||||
paymentSubscriptionItemId: string;
|
||||
paymentSubscriptionId?: string;
|
||||
paymentSubscriptionItemId?: string;
|
||||
}) => {
|
||||
log.info(
|
||||
"createOrganizationFromOnboarding",
|
||||
@@ -478,6 +495,11 @@ export const createOrganizationFromOnboarding = async ({
|
||||
orgSlug: organizationOnboarding.slug,
|
||||
})
|
||||
);
|
||||
|
||||
if (!IS_SELF_HOSTED && (!paymentSubscriptionId || !paymentSubscriptionItemId)) {
|
||||
throw new Error("payment_subscription_id_and_payment_subscription_item_id_are_required");
|
||||
}
|
||||
|
||||
if (
|
||||
await hasConflictingOrganization({
|
||||
slug: organizationOnboarding.slug,
|
||||
@@ -491,10 +513,13 @@ export const createOrganizationFromOnboarding = async ({
|
||||
const userFromEmail = await findUserToBeOrgOwner(organizationOnboarding.orgOwnerEmail);
|
||||
const orgOwnerTranslation = await getTranslation(userFromEmail?.locale || "en", "common");
|
||||
|
||||
await handleDomainSetup({
|
||||
organizationOnboarding,
|
||||
orgOwnerTranslation,
|
||||
});
|
||||
// TODO: We need to send emails to admin in the case of SELF_HOSTING where NEXT_PUBLIC_SINGLE_ORG_SLUG isn't set
|
||||
if (!process.env.NEXT_PUBLIC_SINGLE_ORG_SLUG) {
|
||||
await handleDomainSetup({
|
||||
organizationOnboarding,
|
||||
orgOwnerTranslation,
|
||||
});
|
||||
}
|
||||
|
||||
const { organization, owner } = await handleOrganizationCreation({
|
||||
organizationOnboarding,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import z from "zod";
|
||||
|
||||
import { orgOnboardingInvitedMembersSchema, orgOnboardingTeamsSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
// Base user schema - fields that any user can set
|
||||
export const createOrganizationSchema = z.object({
|
||||
language: z.string().optional(),
|
||||
logo: z.string().nullish(),
|
||||
bio: z.string().nullish(),
|
||||
onboardingId: z.string(),
|
||||
invitedMembers: orgOnboardingInvitedMembersSchema.optional(),
|
||||
teams: orgOnboardingTeamsSchema.optional(),
|
||||
});
|
||||
Reference in new issue
Block a user