* feat: add skip trial feature for teams/orgs billing page Co-Authored-By: [email protected] <[email protected]> * feat: add translation strings for skip trial feature and fix lint-staged config Co-Authored-By: [email protected] <[email protected]> * fix: use IS_TEAM_BILLING_ENABLED instead of IS_SELF_HOSTED Co-Authored-By: [email protected] <[email protected]> * fix: use correct DialogHeader props in billing-view.tsx Co-Authored-By: [email protected] <[email protected]> * restore lint staged config file * invalidate trpc cache * fix: update role filter and error message in skipTrialForTeam handler - Changed role filter to include both OWNER and ADMIN roles to match authorization logic - Updated error message to reflect that both owners and admins can skip the trial Addresses Cubic AI review feedback on PR #26584 Co-Authored-By: unknown <> * fix: optimize skipTrialForTeam to query only specific team by ID Address Cubic AI review feedback: Replace findAllAcceptedTeamMemberships (which fetches all teams) with TeamRepository.findById (which fetches only the specific team needed). Authorization is already validated earlier in the handler, so this redundant query was inefficient. Co-Authored-By: unknown <> * fix static methods * feat: use pbac * fix: remove redundant team query in getSubscriptionStatus handler Co-Authored-By: [email protected] <[email protected]> * add pbac to skip team handler --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
import { getTeamBillingServiceFactory } from "@calcom/ee/billing/di/containers/Billing";
|
|
import { SubscriptionStatus } from "@calcom/ee/billing/repository/billing/IBillingRepository";
|
|
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
|
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
|
import { TeamService } from "@calcom/features/ee/teams/services/teamService";
|
|
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
|
import logger from "@calcom/lib/logger";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../types";
|
|
import type { TGetSubscriptionStatusInputSchema } from "./getSubscriptionStatus.schema";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["getSubscriptionStatus"] });
|
|
|
|
type GetSubscriptionStatusOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TGetSubscriptionStatusInputSchema;
|
|
};
|
|
|
|
export const getSubscriptionStatusHandler = async ({ ctx, input }: GetSubscriptionStatusOptions) => {
|
|
if (!IS_TEAM_BILLING_ENABLED) {
|
|
return { status: null, isTrialing: false };
|
|
}
|
|
|
|
const { teamId } = input;
|
|
|
|
const membershipRepository = new MembershipRepository();
|
|
const membership = await membershipRepository.findUniqueByUserIdAndTeamId({
|
|
userId: ctx.user.id,
|
|
teamId,
|
|
});
|
|
|
|
if (!membership || !membership.accepted) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "You are not a member of this team",
|
|
});
|
|
}
|
|
|
|
const team = await TeamService.fetchTeamOrThrow(teamId);
|
|
const permissionService = new PermissionCheckService();
|
|
const hasManageBillingPermission = await permissionService.checkPermission({
|
|
userId: ctx.user.id,
|
|
teamId,
|
|
permission: team.isOrganization ? "organization.manageBilling" : "team.manageBilling",
|
|
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
|
});
|
|
|
|
if (!hasManageBillingPermission) {
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "Only team owners and admins can view subscription status",
|
|
});
|
|
}
|
|
|
|
try {
|
|
const teamBillingServiceFactory = getTeamBillingServiceFactory();
|
|
const teamBillingService = await teamBillingServiceFactory.findAndInit(teamId);
|
|
|
|
const subscriptionStatus = await teamBillingService.getSubscriptionStatus();
|
|
|
|
log.debug(`Subscription status for team ${teamId}: ${subscriptionStatus}`);
|
|
|
|
return {
|
|
status: subscriptionStatus,
|
|
isTrialing: subscriptionStatus === SubscriptionStatus.TRIALING,
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof TRPCError) {
|
|
throw error;
|
|
}
|
|
log.error("Error getting subscription status", error);
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: "Failed to get subscription status",
|
|
});
|
|
}
|
|
};
|
|
|
|
export default getSubscriptionStatusHandler;
|