Skip to content

Commit

Permalink
fix: custom templates on team plan (#18792)
Browse files Browse the repository at this point in the history
Co-authored-by: CarinaWolli <[email protected]>
  • Loading branch information
CarinaWolli and CarinaWolli authored Jan 21, 2025
1 parent 65b70d6 commit f523ce7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default function WorkflowStepContainer(props: WorkflowStepProps) {
{ enabled: !!teamId }
);

const { hasActiveTeamPlan } = useHasActiveTeamPlan(teamId);
const { hasActiveTeamPlan } = useHasActiveTeamPlan();

const { data: _verifiedEmails } = trpc.viewer.workflows.getVerifiedEmails.useQuery({ teamId });

Expand Down
2 changes: 1 addition & 1 deletion packages/lib/hooks/useHasPaidPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function useHasEnterprisePlan() {
export function useHasActiveTeamPlan(teamId?: number) {
if (IS_SELF_HOSTED) return { isPending: false, hasActiveTeamPlan: true };

const { data, isPending } = trpc.viewer.teams.hasActiveTeamPlan.useQuery({ teamId });
const { data, isPending } = trpc.viewer.teams.hasActiveTeamPlan.useQuery();

return { isPending, hasActiveTeamPlan: !!data };
}
Expand Down
3 changes: 1 addition & 2 deletions packages/trpc/server/routers/viewer/teams/_router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { ZGetSchema } from "./get.schema";
import { ZGetMemberAvailabilityInputSchema } from "./getMemberAvailability.schema";
import { ZGetMembershipbyUserInputSchema } from "./getMembershipbyUser.schema";
import { ZGetUserConnectedAppsInputSchema } from "./getUserConnectedApps.schema";
import { ZHasActiveTeamPlanSchema } from "./hasActiveTeamPlan.schema";
import { ZHasEditPermissionForUserSchema } from "./hasEditPermissionForUser.schema";
import { ZInviteMemberInputSchema } from "./inviteMember/inviteMember.schema";
import { ZInviteMemberByTokenSchemaInputSchema } from "./inviteMemberByToken.schema";
Expand Down Expand Up @@ -223,7 +222,7 @@ export const viewerTeamsRouter = router({
);
return handler(opts);
}),
hasActiveTeamPlan: authedProcedure.input(ZHasActiveTeamPlanSchema).query(async (opts) => {
hasActiveTeamPlan: authedProcedure.query(async (opts) => {
const handler = await importHandler(
namespaced("hasActiveTeamPlan"),
() => import("./hasActiveTeamPlan.handler")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,37 @@ import { IS_SELF_HOSTED } from "@calcom/lib/constants";
import { prisma } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";

import type { THasActiveTeamPlanSchema } from "./hasActiveTeamPlan.schema";

type HasActiveTeamPlanOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: THasActiveTeamPlanSchema;
};

export const hasActiveTeamPlanHandler = async ({ input, ctx }: HasActiveTeamPlanOptions) => {
export const hasActiveTeamPlanHandler = async ({ ctx }: HasActiveTeamPlanOptions) => {
if (IS_SELF_HOSTED) return true;

if (!input.teamId) return false;

const userId = ctx.user.id;

// Check if the user is a member of the requested team
const team = await prisma.team.findFirst({
const teams = await prisma.team.findMany({
where: {
id: input.teamId,
members: {
some: {
userId: userId,
userId: ctx.user.id,
accepted: true,
},
},
},
});
if (!team) return false;

// Get the current team's subscription
const teamBillingService = new InternalTeamBilling(team);
return await teamBillingService.checkIfTeamHasActivePlan();
if (!teams.length) return false;

// check if user has at least on membership with an active plan
for (const team of teams) {
const teamBillingService = new InternalTeamBilling(team);
const isPlanActive = await teamBillingService.checkIfTeamHasActivePlan();
if (isPlanActive) {
return true;
}
}

return false;
};

export default hasActiveTeamPlanHandler;
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
import { z } from "zod";

export const ZHasActiveTeamPlanSchema = z.object({
teamId: z.number().optional(),
});

export type THasActiveTeamPlanSchema = z.infer<typeof ZHasActiveTeamPlanSchema>;
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
if (!isCurrentUsernamePremium) {
isTeamsPlan = await hasActiveTeamPlanHandler({
ctx,
input: { teamId: userWorkflow?.teamId ?? undefined },
});
}
const hasPaidPlan = IS_SELF_HOSTED || isCurrentUsernamePremium || isTeamsPlan;
Expand Down

0 comments on commit f523ce7

Please sign in to comment.