Files
calendar/packages/trpc/server/routers/viewer/organizations/createWithPaymentIntent.handler.ts
sean-brydonGitHubcoderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>Hariom Balhara
fa35cc5210 chore: organization onboarding refactor (#24381)
* feat: redirect to new onboarding flow

* Getting started

* Brand details

* Preview organization brands

* Orgs team pages

* Invite team steps

* Move to global zustand store

* Few darkmdoe fixes

* Wip onboarding + stripe flow

* Default plan state

Server Action for gettting slug satus of org

* Remove onboardingId

* Confirmation prompt

* Update old onboarding flow handlers to handle new fields

* update onboarding hook

* Filter out organization section for none -company emails

* Match placeholders to users domain

* Drop migration

* Wip new onboarding intent

* WIP flow for self-hosted. Same service call just split logic

* WIP

* Add TODO

* Use onboarding user type instead of trpc session

* WIP

* WIP

* pass role and team name from onboarding to save in schema

* Add test to ensure role + name + team are persisted into onboarding table

* migrate roles to enum values

* Update ENUM

* Fix type error

* Redirect if flag is disabled

* Remove web

* WIP

* WIP

* Fix migration

* Fix calls

* User onboarding User types instead of trpc session

* Fix factory tests

* Fix flow for self hoste

* Type error

* More type fixes

* Fix handler tests

* Fix enum return type being different

* Use consistant types across the oganization stuff

* Fix

* Use TEAM_BILLING for e2e test

* Refactor is not company email and add tests

* Fix

* Fix

* Refactor flow to submit after form complete

* Fix flow with billing disabled

* Fix tests

* Apply suggestion from @coderabbitai[bot]

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Rename and move test files

* WIP

* Fix types

* Update repo paths + tests

* Move to service folder

* Fix tests

* Fix types

* Remove old test files

* Restore lock

* Fix path

* Fix tests with new paths and factory logic

* Fix updaetdAt

* WIP onboardingID isolation

* Fix e2e test

* verify test

* Code rabbit

* Rename SelfHostedOnboardongService -> SelfHostedOrganizationOnboardingService

* Fix stores

* Fix type error

* Fix types

* remove tsignore

* Apply suggestion from @coderabbitai[bot]

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* NITS

* Add the logic to auto complete admin org when billing enabled

* Fix store being weird

* We need to return the parsed value

* fixes

* sync from db always

* Add onboardingSgtore tests

* fix test

* remove step and status

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Hariom Balhara <[email protected]>
2025-10-18 14:13:30 +00:00

93 lines
3.1 KiB
TypeScript

/**
* @deprecated This endpoint is deprecated. Use `intentToCreateOrg` instead, which now handles
* teams and invites in a single mutation call. This endpoint will be removed in a future release.
*
* Migration guide:
* - Instead of calling `intentToCreateOrg` followed by `createWithPaymentIntent`,
* pass teams and invitedMembers directly to `intentToCreateOrg`
* - The new endpoint returns checkoutUrl directly in the response
*/
import { OrganizationPaymentService } from "@calcom/features/ee/organizations/lib/OrganizationPaymentService";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { OrganizationOnboardingRepository } from "@calcom/lib/server/repository/organizationOnboarding";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../types";
import type { TCreateWithPaymentIntentInputSchema } from "./createWithPaymentIntent.schema";
type CreateOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TCreateWithPaymentIntentInputSchema;
};
const log = logger.getSubLogger({ prefix: ["viewer", "organizations", "createWithPaymentIntent"] });
/**
* @deprecated Use intentToCreateOrg with teams and invitedMembers instead
*/
export const createHandler = async ({ input, ctx }: CreateOptions) => {
log.warn(
"DEPRECATED: createWithPaymentIntent is deprecated. Use intentToCreateOrg with teams and invitedMembers instead."
);
const paymentService = new OrganizationPaymentService({
id: ctx.user.id,
email: ctx.user.email,
role: ctx.user.role,
name: ctx.user.name ?? undefined,
});
const isAdmin = ctx.user.role === "ADMIN";
// Regular user can send onboardingId if the onboarding was started by ADMIN/someone else and they shared the link with them.
// ADMIN flow doesn't send onboardingId
const organizationOnboarding = await OrganizationOnboardingRepository.findById(input.onboardingId);
if (!organizationOnboarding) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "organization_onboarding_not_found",
});
}
if (organizationOnboarding.stripeSubscriptionId) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "organization_has_subscription_already",
});
}
if (!isAdmin && organizationOnboarding.orgOwnerEmail !== ctx.user.email) {
log.warn(
"Organization onboarding there but not have access",
safeStringify({
orgOwnerEmail: organizationOnboarding?.orgOwnerEmail,
userEmail: ctx.user.email,
})
);
// Intentionally throw vague error to avoid leaking information
throw new TRPCError({
code: "BAD_REQUEST",
message: "organization_onboarding_not_found",
});
}
const paymentIntent = await paymentService.createPaymentIntent(
{
...input,
logo: input.logo ?? null,
bio: input.bio ?? null,
brandColor: input.brandColor ?? null,
bannerUrl: input.bannerUrl ?? null,
},
organizationOnboarding
);
return {
checkoutUrl: paymentIntent.checkoutUrl,
};
};
export default createHandler;