Files
cal.diy/packages/features/ee/organizations/lib/service/onboarding/BillingEnabledOrgOnboardingService.ts
T
e29aa2097e feat: Ensure teams with conflicting slugs owned by the user are migrated(handled in backend, frontend already had this restriction) (#25291)
* feat: add backend validation for conflicting team slugs during org onboarding

- Added findOwnedTeamsByUserId method to TeamRepository
- Created buildTeamsAndInvites method in BaseOnboardingService that automatically:
  - Detects teams with same slug as organization
  - Marks conflicting teams for migration (isBeingMigrated: true)
  - Filters empty team names and invite emails
- Updated BillingEnabledOrgOnboardingService to use new method
- Updated SelfHostedOnboardingService to use new method
- Added comprehensive tests for slug conflict scenarios

This ensures backend validation even if frontend is bypassed, preventing
slug conflicts during organization creation. All inheriting classes
automatically get this validation without code changes.

* refactor: use TeamRepository in listOwnedTeamsHandler

Refactored listOwnedTeamsHandler to use TeamRepository.findOwnedTeamsByUserId
instead of direct Prisma queries. This:
- Reduces code duplication
- Ensures consistency across the codebase
- Follows repository pattern
- Makes the handler more maintainable

* fix: update tests to use renamed buildTeamsAndInvites method

- Renamed testFilterTeamsAndInvites to testBuildTeamsAndInvites
- Made test wrapper method async to match the async buildTeamsAndInvites
- Added orgSlug parameter to all test calls
- Updated all 9 test cases to use await with the new method signature
- Fixed lint warnings by using proper types instead of 'any'
- Imported OnboardingIntentResult and User types
- Used Pick<User> for mockUser type
- Removed all 'as any' type casts

Fixes test failures where filterTeamsAndInvites was renamed to buildTeamsAndInvites in the base service.

Co-Authored-By: [email protected] <[email protected]>

* fix: mock TeamRepository in tests to prevent database calls

Added vi.mock for TeamRepository to avoid database calls in unit tests.
The buildTeamsAndInvites method now calls ensureConflictingSlugTeamIsMigrated
which uses TeamRepository.findOwnedTeamsByUserId(). Mocking this prevents
Prisma errors in CI while keeping the tests focused on filtering logic.

The mock returns an empty array so no teams are found for migration,
allowing the tests to verify the filtering behavior without database access.

Co-Authored-By: [email protected] <[email protected]>

* refactor: improve readability of ensureConflictingSlugTeamIsMigrated

Refactored the conditional logic in ensureConflictingSlugTeamIsMigrated
for better readability while preserving exact behavior. Changed from
manual array manipulation to using .map() for updating team migration
status. This is a cosmetic change with no functional differences.

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Udit Takkar <[email protected]>
2025-11-22 09:07:28 +00:00

276 lines
9.2 KiB
TypeScript

import { findUserToBeOrgOwner } from "@calcom/features/ee/organizations/lib/server/orgCreationUtils";
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
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 { OrganizationOnboardingRepository } from "@calcom/lib/server/repository/organizationOnboarding";
import type { Team, User } from "@calcom/prisma/client";
import { orgOnboardingInvitedMembersSchema, orgOnboardingTeamsSchema } from "@calcom/prisma/zod-utils";
import { BaseOnboardingService } from "../onboarding/BaseOnboardingService";
import type {
CreateOnboardingIntentInput,
OnboardingIntentResult,
OrganizationOnboardingData,
OrganizationData,
} from "../onboarding/types";
const log = logger.getSubLogger({ prefix: ["BillingEnabledOrgOnboardingService"] });
const invitedMembersSchema = orgOnboardingInvitedMembersSchema;
const teamsSchema = orgOnboardingTeamsSchema;
/**
* Handles organization onboarding when billing is enabled (Stripe flow).
*
* Flow:
* 1. Create onboarding record
* 2. Store teams/invites in database
* 3. Create Stripe checkout session
* 4. Return checkout URL
* 5. Organization created later via Stripe webhook
*
* Exception: Admin creating org for self - immediately creates organization (no payment required)
*/
export class BillingEnabledOrgOnboardingService extends BaseOnboardingService {
async createOnboardingIntent(input: CreateOnboardingIntentInput): Promise<OnboardingIntentResult> {
log.debug(
"BillingEnabledOrgOnboardingService.createOnboardingIntent - INPUT",
safeStringify({
invitedMembers: input.invitedMembers,
teams: input.teams,
})
);
const { teamsData, invitedMembersData } = await this.buildTeamsAndInvites(
input.slug,
input.teams,
input.invitedMembers
);
log.debug(
"BillingEnabledOrgOnboardingService - After filterTeamsAndInvites",
safeStringify({
teamsData,
invitedMembersData,
})
);
// Create onboarding record with ALL data at once
const organizationOnboarding = await this.createOnboardingRecord({
...input,
teams: teamsData,
invitedMembers: invitedMembersData,
});
const onboardingId = organizationOnboarding.id;
// Check if this is an admin handover flow
const handoverResult = this.handleAdminHandoverIfNeeded(input, onboardingId);
if (handoverResult) {
return handoverResult;
}
// Check if admin is creating org for themselves - skip payment, create immediately
if (
this.isAdminCreatingForSelf({
orgOwnerEmail: organizationOnboarding.orgOwnerEmail,
})
) {
log.debug(
"Admin creating org for self - skipping payment and creating organization immediately",
safeStringify({ adminEmail: this.user.email, onboardingId })
);
const { organization } = await this.createOrganization(organizationOnboarding);
await OrganizationOnboardingRepository.markAsComplete(onboardingId);
log.debug(
"Organization created successfully for admin",
safeStringify({ onboardingId, organizationId: organization.id })
);
return {
userId: this.user.id,
orgOwnerEmail: input.orgOwnerEmail,
name: input.name,
slug: input.slug,
seats: input.seats ?? null,
pricePerSeat: input.pricePerSeat ?? null,
billingPeriod: input.billingPeriod,
isPlatform: input.isPlatform,
organizationOnboardingId: onboardingId,
checkoutUrl: null,
organizationId: organization.id,
};
}
// Regular flow - create payment intent
const paymentIntent = await this.paymentService.createPaymentIntent(
{
logo: organizationOnboarding.logo,
bio: input.bio ?? null,
brandColor: input.brandColor ?? null,
bannerUrl: organizationOnboarding.bannerUrl,
teams: teamsData,
invitedMembers: invitedMembersData,
},
{
id: organizationOnboarding.id,
pricePerSeat: organizationOnboarding.pricePerSeat,
billingPeriod: organizationOnboarding.billingPeriod,
seats: organizationOnboarding.seats,
isComplete: organizationOnboarding.isComplete,
orgOwnerEmail: organizationOnboarding.orgOwnerEmail,
slug: organizationOnboarding.slug,
stripeCustomerId: organizationOnboarding.stripeCustomerId,
}
);
return {
userId: this.user.id,
orgOwnerEmail: input.orgOwnerEmail,
name: input.name,
slug: input.slug,
seats: input.seats ?? null,
pricePerSeat: input.pricePerSeat ?? null,
billingPeriod: input.billingPeriod,
isPlatform: input.isPlatform,
organizationOnboardingId: onboardingId,
checkoutUrl: paymentIntent.checkoutUrl,
organizationId: null, // Organization not created yet (pending payment)
};
}
async createOrganization(
organizationOnboarding: OrganizationOnboardingData,
paymentDetails?: { subscriptionId: string; subscriptionItemId: string }
): Promise<{ organization: Team; owner: User }> {
const organizationRepository = getOrganizationRepository();
log.info(
"createOrganization (billing-enabled)",
safeStringify({
orgId: organizationOnboarding.organizationId,
orgSlug: organizationOnboarding.slug,
})
);
const isAdminForSelf = this.isAdminCreatingForSelf({
orgOwnerEmail: organizationOnboarding.orgOwnerEmail,
});
if (
!IS_SELF_HOSTED &&
!isAdminForSelf &&
(!paymentDetails?.subscriptionId || !paymentDetails?.subscriptionItemId)
) {
throw new Error("payment_subscription_id_and_payment_subscription_item_id_are_required");
}
if (
await this.hasConflictingOrganization({
slug: organizationOnboarding.slug,
onboardingId: organizationOnboarding.id,
})
) {
throw new Error("organization_already_exists_with_this_slug");
}
let owner = await findUserToBeOrgOwner(organizationOnboarding.orgOwnerEmail);
const orgOwnerTranslation = await getTranslation(owner?.locale || "en", "common");
if (!process.env.NEXT_PUBLIC_SINGLE_ORG_SLUG) {
await this.handleDomainSetup({
organizationOnboarding,
orgOwnerTranslation,
});
}
const orgData: OrganizationData = {
id: organizationOnboarding.organizationId,
name: organizationOnboarding.name,
slug: organizationOnboarding.slug,
isOrganizationConfigured: true,
isOrganizationAdminReviewed: true,
autoAcceptEmail: organizationOnboarding.orgOwnerEmail.split("@")[1],
seats: organizationOnboarding.seats,
pricePerSeat: organizationOnboarding.pricePerSeat,
isPlatform: false,
billingPeriod: organizationOnboarding.billingPeriod,
logoUrl: organizationOnboarding.logo,
bio: organizationOnboarding.bio,
brandColor: organizationOnboarding.brandColor,
bannerUrl: organizationOnboarding.bannerUrl,
};
let organization: Team;
if (!owner) {
const result = await this.createOrganizationWithNonExistentUserAsOwner({
email: organizationOnboarding.orgOwnerEmail,
orgData,
});
organization = result.organization;
owner = result.owner;
} else {
const result = await this.createOrganizationWithExistingUserAsOwner({
orgData,
owner,
});
organization = result.organization;
}
if (organizationOnboarding.stripeCustomerId) {
await this.ensureStripeCustomerIdIsUpdated({
owner,
stripeCustomerId: organizationOnboarding.stripeCustomerId,
});
}
await OrganizationOnboardingRepository.update(organizationOnboarding.id, {
organizationId: organization.id,
});
const updatedOrganization = await this.backwardCompatibilityForSubscriptionDetails({
organization,
paymentSubscriptionId: paymentDetails?.subscriptionId,
paymentSubscriptionItemId: paymentDetails?.subscriptionItemId,
});
organization.metadata = updatedOrganization.metadata;
const teamsData = teamsSchema.parse(organizationOnboarding.teams);
await this.createOrMoveTeamsToOrganization(teamsData, owner, organization.id);
await this.inviteMembers(
invitedMembersSchema.parse(organizationOnboarding.invitedMembers),
organization,
teamsData
);
if (!organization.slug) {
try {
const { slug } = await organizationRepository.setSlug({
id: organization.id,
slug: organizationOnboarding.slug,
});
organization.slug = slug;
} catch (error) {
log.error(
"RecoverableError: Error while setting slug for organization",
safeStringify(error),
safeStringify({
attemptedSlug: organizationOnboarding.slug,
organizationId: organization.id,
})
);
throw new Error(
`Unable to set slug '${organizationOnboarding.slug}' for organization ${organization.id}`
);
}
}
return { organization, owner };
}
}