Files
cal.diy/packages/trpc/server/routers/viewer/organizations/createSelfHosted.handler.ts
T
6ed197d35c 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: joe@cal.com <joe@cal.com>

* 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: joe@cal.com <joe@cal.com>

* 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: joe@cal.com <joe@cal.com>
Co-authored-by: Benny Joo <sldisek783@gmail.com>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Co-authored-by: Hariom <hariombalhara@gmail.com>
2025-06-25 16:58:18 +03:00

76 lines
2.3 KiB
TypeScript

import { createOrganizationFromOnboarding } from "@calcom/features/ee/organizations/lib/server/createOrganizationFromOnboarding";
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import { OrganizationOnboardingRepository } from "@calcom/lib/server/repository/organizationOnboarding";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../types";
import type { TCreateSelfHostedInputSchema } from "./createSelfHosted.schema";
type CreateSelfHostedOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TCreateSelfHostedInputSchema;
};
export const createSelfHostedHandler = async ({ input, ctx }: CreateSelfHostedOptions) => {
if (!IS_SELF_HOSTED) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Self-hosted is not enabled",
});
}
if (!input.onboardingId) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "onboardingId is required",
});
}
const organizationOnboarding = await OrganizationOnboardingRepository.findById(input.onboardingId);
if (!organizationOnboarding) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Organization onboarding not found",
});
}
if (!organizationOnboarding.orgOwnerEmail) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Organization owner email not found",
});
}
const { organization } = await createOrganizationFromOnboarding({
organizationOnboarding: {
id: input.onboardingId,
logo: input.logo ?? null,
bio: input.bio ?? null,
invitedMembers: input.invitedMembers ?? [],
teams: input.teams ?? [],
orgOwnerEmail: organizationOnboarding.orgOwnerEmail,
slug: organizationOnboarding.slug,
name: organizationOnboarding.name,
billingPeriod: organizationOnboarding.billingPeriod,
seats: organizationOnboarding.seats,
pricePerSeat: organizationOnboarding.pricePerSeat,
stripeCustomerId: organizationOnboarding.stripeCustomerId,
isPlatform: organizationOnboarding.isPlatform,
isDomainConfigured: organizationOnboarding.isDomainConfigured,
organizationId: organizationOnboarding.organizationId,
},
});
await OrganizationOnboardingRepository.markAsComplete(organizationOnboarding.id);
return {
organization,
};
};
export default createSelfHostedHandler;