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

@@ -28,6 +28,7 @@ import { ZGetRoundRobinHostsInputSchema } from "./roundRobin/getRoundRobinHostsT
import { ZRoundRobinManualReassignInputSchema } from "./roundRobin/roundRobinManualReassign.schema";
import { ZRoundRobinReassignInputSchema } from "./roundRobin/roundRobinReassign.schema";
import { ZSetInviteExpirationInputSchema } from "./setInviteExpiration.schema";
import { ZSkipTeamTrialsInputSchema } from "./skipTeamTrials.schema";
import { ZUpdateInputSchema } from "./update.schema";
import { ZUpdateInternalNotesPresetsInputSchema } from "./updateInternalNotesPresets.schema";
import { ZUpdateMembershipInputSchema } from "./updateMembership.schema";
@@ -190,4 +191,8 @@ export const viewerTeamsRouter = router({
const { default: handler } = await import("./hasActiveTeamPlan.handler");
return handler(opts);
}),
skipTeamTrials: authedProcedure.input(ZSkipTeamTrialsInputSchema).mutation(async (opts) => {
const { default: handler } = await import("./skipTeamTrials.handler");
return handler(opts);
}),
});
@@ -0,0 +1,62 @@
import { InternalTeamBilling } from "@calcom/ee/billing/teams/internal-team-billing";
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import logger from "@calcom/lib/logger";
import { prisma } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import type { TSkipTeamTrialsInputSchema } from "./skipTeamTrials.schema";
const log = logger.getSubLogger({ prefix: ["skipTeamTrials"] });
type SkipTeamTrialsOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TSkipTeamTrialsInputSchema;
};
export const skipTeamTrialsHandler = async ({ ctx }: SkipTeamTrialsOptions) => {
// If self-hosted, no need to skip trials as they're already handled differently
if (IS_SELF_HOSTED) return { success: true };
try {
await prisma.user.update({
where: {
id: ctx.user.id,
},
data: {
trialEndsAt: null,
},
});
const ownedTeams = await prisma.team.findMany({
where: {
members: {
some: {
userId: ctx.user.id,
accepted: true,
role: "OWNER",
},
},
},
});
for (const team of ownedTeams) {
const teamBillingService = new InternalTeamBilling(team);
const subscriptionStatus = await teamBillingService.getSubscriptionStatus();
if (subscriptionStatus === "trialing") {
await teamBillingService.endTrial();
log.info(`Ended trial for team ${team.id}`);
}
}
return { success: true };
} catch (error) {
log.error("Error skipping team trials", error);
return { success: false, error: "Failed to skip team trials" };
}
};
export default skipTeamTrialsHandler;
@@ -0,0 +1,5 @@
import { z } from "zod";
export const ZSkipTeamTrialsInputSchema = z.object({});
export type TSkipTeamTrialsInputSchema = z.infer<typeof ZSkipTeamTrialsInputSchema>;
@@ -0,0 +1,134 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { InternalTeamBilling } from "@calcom/ee/billing/teams/internal-team-billing";
import { prisma } from "@calcom/prisma";
import { skipTeamTrialsHandler } from "./skipTeamTrials.handler";
// Instead of completely mocking the handler, we'll use the real implementation
// and mock its dependencies
vi.mock("./skipTeamTrials.handler", async () => {
const actual = await vi.importActual<typeof import("./skipTeamTrials.handler")>("./skipTeamTrials.handler");
return {
...actual,
// We keep this for spying purposes
skipTeamTrialsHandler: vi.fn().mockImplementation(actual.skipTeamTrialsHandler),
};
});
// Mock dependencies
vi.mock("@calcom/lib/constants", () => ({
IS_SELF_HOSTED: false,
IS_PRODUCTION: false,
CALCOM_ENV: "test",
WEBAPP_URL: "http://localhost:3000",
IS_CALCOM: false,
HOSTED_CAL_FEATURES: true,
}));
vi.mock("@calcom/prisma", () => ({
prisma: {
user: {
update: vi.fn().mockResolvedValue({}),
},
team: {
findMany: vi.fn(),
},
},
}));
vi.mock("@calcom/lib/logger", () => ({
default: {
getSubLogger: () => ({
info: vi.fn(),
error: vi.fn(),
}),
},
}));
const mockGetSubscriptionStatus = vi.fn();
const mockEndTrial = vi.fn().mockResolvedValue(true);
vi.mock("@calcom/ee/billing/teams/internal-team-billing", () => ({
InternalTeamBilling: vi.fn().mockImplementation(() => ({
getSubscriptionStatus: mockGetSubscriptionStatus,
endTrial: mockEndTrial,
})),
}));
describe("skipTeamTrialsHandler", () => {
const mockCtx = {
user: {
id: 1,
email: "[email protected]",
},
};
beforeEach(() => {
vi.clearAllMocks();
});
it("should set user's trialEndsAt to null", async () => {
vi.mocked(prisma.team.findMany).mockResolvedValueOnce([]);
// @ts-expect-error - simplified context for testing
await skipTeamTrialsHandler({ ctx: mockCtx, input: {} });
expect(prisma.user.update).toHaveBeenCalledWith({
where: {
id: 1,
},
data: {
trialEndsAt: null,
},
});
});
it("should end trials for all teams where user is OWNER", async () => {
// Mock teams where user is owner
const mockTeams = [
{ id: 101, name: "Team 1" },
{ id: 102, name: "Team 2" },
];
vi.mocked(prisma.team.findMany).mockResolvedValueOnce(mockTeams);
mockGetSubscriptionStatus
.mockResolvedValueOnce("trialing") // First team is in trial
.mockResolvedValueOnce("active"); // Second team is active
// @ts-expect-error - simplified context for testing
await skipTeamTrialsHandler({ ctx: mockCtx, input: {} });
expect(prisma.user.update).toHaveBeenCalled();
expect(prisma.team.findMany).toHaveBeenCalledWith({
where: {
members: {
some: {
userId: mockCtx.user.id,
accepted: true,
role: "OWNER",
},
},
},
});
expect(InternalTeamBilling).toHaveBeenCalledTimes(2);
expect(InternalTeamBilling).toHaveBeenNthCalledWith(1, mockTeams[0]);
expect(InternalTeamBilling).toHaveBeenNthCalledWith(2, mockTeams[1]);
expect(mockGetSubscriptionStatus).toHaveBeenCalledTimes(2);
expect(mockEndTrial).toHaveBeenCalledTimes(1);
});
it("should handle errors gracefully", async () => {
vi.mocked(prisma.user.update).mockRejectedValueOnce(new Error("Database error"));
// @ts-expect-error - simplified context for testing
const result = await skipTeamTrialsHandler({ ctx: mockCtx, input: {} });
expect(result).toEqual({ success: false, error: "Failed to skip team trials" });
});
});