fix: Onboarding v3 - create team then invite. (#25364)
## What does this PR do? This PR changes the order of creating the team -> paying -> invite users. The old approach was create team -> invite -> pay. But we are matching the current implementation of how billing works with usage based billing ## How should this be tested? Enable onboarding-v3 flag on your instance Ensure you have stripe enabled + stripe:listen running login as [email protected], onboarding Go through the team setup flow Pay Invite Ensure uers were added to team Create a new account disable billing Test again ## Checklist <!-- Remove bullet points below that don't apply to you --> - I haven't read the [contributing guide](https://github.com/calcom/cal.com/blob/main/CONTRIBUTING.md) - My code doesn't follow the style guidelines of this project - I haven't commented my code, particularly in hard-to-understand areas - I haven't checked if my changes generate no new warnings
This commit is contained in:
1 parent
2b2bf36703
commit
d03f45dd0f
7 files changed
+166
-37
No files matched your search
@@ -2,16 +2,21 @@ import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
|
||||
import { useFlagMap } from "@calcom/features/flags/context/provider";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { CreationSource } from "@calcom/prisma/enums";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
import type { OnboardingState } from "../store/onboarding-store";
|
||||
import { useOnboardingStore } from "../store/onboarding-store";
|
||||
|
||||
export function useCreateTeam() {
|
||||
const router = useRouter();
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const flags = useFlagMap();
|
||||
const { setTeamId, teamId } = useOnboardingStore();
|
||||
|
||||
const createTeamMutation = trpc.viewer.teams.create.useMutation();
|
||||
const inviteMemberMutation = trpc.viewer.teams.inviteMember.useMutation();
|
||||
|
||||
const createTeam = async (store: OnboardingState) => {
|
||||
setIsSubmitting(true);
|
||||
@@ -30,6 +35,7 @@ export function useCreateTeam() {
|
||||
const result = await createTeamMutation.mutateAsync({
|
||||
name: teamDetails.name,
|
||||
slug: teamDetails.slug,
|
||||
bio: teamDetails.bio,
|
||||
logo: teamBrand.logo,
|
||||
isOnboarding: true,
|
||||
});
|
||||
@@ -41,11 +47,9 @@ export function useCreateTeam() {
|
||||
}
|
||||
|
||||
if (result.team) {
|
||||
// Not sure we need this flag check - keeping it here for safe keeping as this is called only from v3 onboarding flow
|
||||
const gettingStartedPath = flags["onboarding-v3"]
|
||||
? "/onboarding/personal/settings"
|
||||
: "/getting-started";
|
||||
router.push(gettingStartedPath);
|
||||
// Store the teamId and redirect to invite flow after team creation
|
||||
setTeamId(result.team.id);
|
||||
router.push(`/onboarding/teams/invite?teamId=${result.team.id}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to create team:", error);
|
||||
@@ -55,9 +59,72 @@ export function useCreateTeam() {
|
||||
}
|
||||
};
|
||||
|
||||
const inviteMembers = async (
|
||||
invites: Array<{ email: string; role: "MEMBER" | "ADMIN" }>,
|
||||
language: string
|
||||
) => {
|
||||
if (!teamId) {
|
||||
throw new Error("Team ID is required to invite members");
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
|
||||
try {
|
||||
// Filter and validate invites
|
||||
const validInvites = invites.filter((invite) => invite.email && invite.email.trim().length > 0);
|
||||
|
||||
if (validInvites.length === 0) {
|
||||
throw new Error("At least one valid email address is required");
|
||||
}
|
||||
|
||||
// Group invites by role and send separate requests for each role
|
||||
// This is necessary because the schema validation expects array of strings when using bulk invites
|
||||
const invitesByRole = validInvites.reduce((acc, invite) => {
|
||||
const role = invite.role === "ADMIN" ? MembershipRole.ADMIN : MembershipRole.MEMBER;
|
||||
if (!acc[role]) {
|
||||
acc[role] = [];
|
||||
}
|
||||
acc[role].push(invite.email.trim().toLowerCase());
|
||||
return acc;
|
||||
}, {} as Record<MembershipRole, string[]>);
|
||||
|
||||
// Send invites for each role group
|
||||
await Promise.all(
|
||||
Object.entries(invitesByRole).map(([role, emails]) =>
|
||||
inviteMemberMutation.mutateAsync({
|
||||
teamId,
|
||||
usernameOrEmail: emails, // Array of strings, not objects
|
||||
role: role as MembershipRole,
|
||||
language,
|
||||
creationSource: CreationSource.WEBAPP,
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
// Redirect to personal settings after successful invite
|
||||
const gettingStartedPath = flags["onboarding-v3"]
|
||||
? "/onboarding/personal/settings"
|
||||
: "/getting-started";
|
||||
router.push(gettingStartedPath);
|
||||
} catch (error) {
|
||||
console.error("Failed to invite members:", error);
|
||||
// Extract error message from TRPC error
|
||||
if (error && typeof error === "object" && "message" in error) {
|
||||
throw new Error(error.message as string);
|
||||
}
|
||||
if (error instanceof Error) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error("Failed to invite members. Please try again.");
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
createTeam,
|
||||
inviteMembers,
|
||||
isSubmitting,
|
||||
error: createTeamMutation.error,
|
||||
error: createTeamMutation.error || inviteMemberMutation.error,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user