feat: /skip trial for teams (#20753)

* skip team trials

* add loading state

* update mocks

* cleanup

* add import oops

* fix tests files

---------

Co-authored-by: Sean Brydon <[email protected]>
Co-authored-by: Carina Wollendorfer <[email protected]>
This commit is contained in:
authored and GitHub committed 2025-04-29 11:48:41 +00:00
1 parent 81b4651d6e
commit 3296255f80
9 files changed
+273

No files matched your search

@@ -9,6 +9,7 @@ export interface BillingService {
subscriptionItemId: string;
membershipCount: number;
}): Promise<void>;
handleEndTrial(subscriptionId: string): Promise<void>;
// Customer management
createCustomer(args: {
@@ -120,6 +120,18 @@ export class StripeBillingService implements BillingService {
});
}
async handleEndTrial(subscriptionId: string) {
const subscription = await this.stripe.subscriptions.retrieve(subscriptionId);
if (subscription.status !== "trialing") {
return; // Do nothing if not in trial
}
await this.stripe.subscriptions.update(subscriptionId, {
trial_end: "now",
});
}
async checkoutSessionIsPaid(paymentId: string) {
const checkoutSession = await this.stripe.checkout.sessions.retrieve(paymentId);
return checkoutSession.payment_status === "paid";
@@ -181,4 +181,28 @@ export class InternalTeamBilling implements TeamBilling {
if (!subscriptionId) return null;
return await billing.getSubscriptionStatus(subscriptionId);
}
/**
* Ends the trial period for a team subscription by converting it to a regular subscription
* @returns {Promise<boolean>} True if successful, false otherwise
*/
async endTrial() {
try {
const { subscriptionId } = this.team.metadata;
log.info(`Ending trial for subscription ${subscriptionId} of team ${this.team.id}`);
if (!subscriptionId) {
log.warn(`No subscription ID found for team ${this.team.id}`);
return false;
}
// End the trial by converting to regular subscription
await billing.handleEndTrial(subscriptionId);
log.info(`Successfully ended trial for team ${this.team.id}`);
return true;
} catch (error) {
this.logErrorFromUnknown(error);
return false;
}
}
}
@@ -1,7 +1,9 @@
import type { User as UserAuth } from "next-auth";
import { IS_DUB_REFERRALS_ENABLED } from "@calcom/lib/constants";
import { useHasActiveTeamPlan } from "@calcom/lib/hooks/useHasPaidPlan";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
import { showToast } from "@calcom/ui/components/toast";
import { type NavigationItemType } from "./navigation/NavigationItem";
@@ -18,8 +20,33 @@ export function useBottomNavItems({
user,
}: BottomNavItemsProps): NavigationItemType[] {
const { t } = useLocale();
const { isTrial } = useHasActiveTeamPlan();
const utils = trpc.useUtils();
const skipTeamTrialsMutation = trpc.viewer.teams.skipTeamTrials.useMutation({
onSuccess: () => {
utils.viewer.teams.hasActiveTeamPlan.invalidate();
showToast(t("team_trials_skipped_successfully"), "success");
},
onError: () => {
showToast(t("something_went_wrong"), "error");
},
});
return [
// Render above to prevent layout shift as much as possible
isTrial
? {
name: "skip_trial",
href: "",
isLoading: skipTeamTrialsMutation.isPending,
icon: "clock",
onClick: (e: { preventDefault: () => void }) => {
e.preventDefault();
skipTeamTrialsMutation.mutate({});
},
}
: null,
{
name: "view_public_page",
href: publicPageUrl,
@@ -43,6 +70,7 @@ export function useBottomNavItems({
icon: "gift",
}
: null,
isAdmin
? {
name: "impersonation",