* 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>
111 lines
4.1 KiB
TypeScript
111 lines
4.1 KiB
TypeScript
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
|
import { OrganizationPaymentService } from "@calcom/features/ee/organizations/lib/OrganizationPaymentService";
|
|
import {
|
|
assertCanCreateOrg,
|
|
findUserToBeOrgOwner,
|
|
} from "@calcom/features/ee/organizations/lib/server/orgCreationUtils";
|
|
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import { DeploymentRepository } from "@calcom/lib/server/repository/deployment";
|
|
import { OrganizationOnboardingRepository } from "@calcom/lib/server/repository/organizationOnboarding";
|
|
import { prisma } from "@calcom/prisma";
|
|
import { UserPermissionRole } from "@calcom/prisma/enums";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../types";
|
|
import type { TIntentToCreateOrgInputSchema } from "./intentToCreateOrg.schema";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["intentToCreateOrgHandler"] });
|
|
|
|
type CreateOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TIntentToCreateOrgInputSchema;
|
|
};
|
|
|
|
export const intentToCreateOrgHandler = async ({ input, ctx }: CreateOptions) => {
|
|
const { slug, name, orgOwnerEmail, seats, pricePerSeat, billingPeriod, isPlatform } = input;
|
|
log.debug(
|
|
"Starting organization creation intent",
|
|
safeStringify({ slug, name, orgOwnerEmail, isPlatform })
|
|
);
|
|
|
|
if (IS_SELF_HOSTED) {
|
|
const deploymentRepo = new DeploymentRepository(prisma);
|
|
const licenseKeyService = await LicenseKeySingleton.getInstance(deploymentRepo);
|
|
const hasValidLicense = await licenseKeyService.checkLicense();
|
|
|
|
if (!hasValidLicense) {
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
// TODO: We need to send translation keys from here and frontend should translate it
|
|
message: "License is not valid",
|
|
});
|
|
}
|
|
}
|
|
|
|
const loggedInUser = ctx.user;
|
|
if (!loggedInUser) throw new TRPCError({ code: "UNAUTHORIZED", message: "You are not authorized." });
|
|
|
|
const IS_USER_ADMIN = loggedInUser.role === UserPermissionRole.ADMIN;
|
|
log.debug("User authorization check", safeStringify({ userId: loggedInUser.id, isAdmin: IS_USER_ADMIN }));
|
|
|
|
if (!IS_USER_ADMIN && loggedInUser.email !== orgOwnerEmail && !isPlatform) {
|
|
log.warn(
|
|
"Unauthorized organization creation attempt",
|
|
safeStringify({ loggedInUserEmail: loggedInUser.email, orgOwnerEmail })
|
|
);
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "You can only create organization where you are the owner",
|
|
});
|
|
}
|
|
|
|
const orgOwner = await findUserToBeOrgOwner(orgOwnerEmail);
|
|
if (!orgOwner) {
|
|
// The flow exists to create the user through the stripe webhook invoice.paid but there could be a possible security issue with the approach. So, we avoid it currently.
|
|
// Issue: As the onboarding link(which has onboardingId) could be used by unwanted person to pay and then invite some unwanted members to the organization.
|
|
throw new TRPCError({
|
|
code: "BAD_REQUEST",
|
|
message: `No user found with email ${orgOwnerEmail}`,
|
|
});
|
|
}
|
|
log.debug("Found organization owner", safeStringify({ orgOwnerId: orgOwner.id, email: orgOwner.email }));
|
|
|
|
let organizationOnboarding = await OrganizationOnboardingRepository.findByOrgOwnerEmail(orgOwner.email);
|
|
if (organizationOnboarding) {
|
|
throw new Error("organization_onboarding_already_exists");
|
|
}
|
|
|
|
await assertCanCreateOrg({
|
|
slug,
|
|
isPlatform,
|
|
orgOwner,
|
|
restrictBasedOnMinimumPublishedTeams: !IS_USER_ADMIN,
|
|
});
|
|
|
|
const paymentService = new OrganizationPaymentService(ctx.user);
|
|
organizationOnboarding = await paymentService.createOrganizationOnboarding({
|
|
...input,
|
|
createdByUserId: loggedInUser.id,
|
|
});
|
|
|
|
log.debug("Organization creation intent successful", safeStringify({ slug, orgOwnerId: orgOwner.id }));
|
|
return {
|
|
userId: orgOwner.id,
|
|
orgOwnerEmail,
|
|
name,
|
|
slug,
|
|
seats,
|
|
pricePerSeat,
|
|
billingPeriod,
|
|
isPlatform,
|
|
organizationOnboardingId: organizationOnboarding.id,
|
|
};
|
|
};
|
|
|
|
export default intentToCreateOrgHandler;
|