mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
* add eslint config * migrate autoLock to features * migrate teamService to features * migrate userCreationService * migrate insights services to features * migrate ProfileRepository * update imports * migrate filter segmen tests * migrate filter segment repository * migrate getBusyTimes * migrate getLocaleFromRequest * refactor csvUtils * make filename clearer * migrate getLuckyUser integration test * migrate autoLock test to features * wip * refactors * migrate useBookerUrl * migrate more * wip * Migrate eventTypeRepository * membership repository * update imports * update imports * migrate * move organization repository * update imports * update imports * wip * wip * wip * wip * wip * wip * wip * wip * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix tests * fix type checks * fix * fix * migrate * update imports * fix tests * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import { InternalTeamBilling } from "@calcom/ee/billing/teams/internal-team-billing";
|
|
import { IS_SELF_HOSTED } from "@calcom/lib/constants";
|
|
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
|
import { prisma } from "@calcom/prisma";
|
|
import type { Prisma } from "@calcom/prisma/client";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
|
|
|
import type { THasActiveTeamPlanInputSchema } from "./hasActiveTeamPlan.schema";
|
|
|
|
type HasActiveTeamPlanOptions = {
|
|
ctx: {
|
|
user: Pick<NonNullable<TrpcSessionUser>, "id">;
|
|
};
|
|
input: THasActiveTeamPlanInputSchema;
|
|
};
|
|
|
|
export const hasActiveTeamPlanHandler = async ({ ctx, input }: HasActiveTeamPlanOptions) => {
|
|
if (IS_SELF_HOSTED) return { isActive: true, isTrial: false };
|
|
|
|
const whereClause: Prisma.MembershipWhereInput = { userId: ctx.user.id, accepted: true };
|
|
|
|
if (input?.ownerOnly) {
|
|
whereClause.role = "OWNER";
|
|
}
|
|
|
|
const teams = await MembershipRepository.findAllAcceptedTeamMemberships(ctx.user.id, whereClause);
|
|
|
|
if (!teams.length) return { isActive: false, isTrial: false };
|
|
|
|
let isTrial = false;
|
|
// check if user has at least on membership with an active plan
|
|
for (const team of teams) {
|
|
if (team.isPlatform && team.isOrganization) {
|
|
const platformBilling = await prisma.platformBilling.findUnique({ where: { id: team.id } });
|
|
if (platformBilling && platformBilling.plan !== "none" && platformBilling.plan !== "FREE") {
|
|
return { isActive: true, isTrial: false };
|
|
}
|
|
}
|
|
const teamBillingService = new InternalTeamBilling(team);
|
|
const subscriptionStatus = await teamBillingService.getSubscriptionStatus();
|
|
|
|
if (subscriptionStatus === "active" || subscriptionStatus === "past_due") {
|
|
return { isActive: true, isTrial: false };
|
|
}
|
|
if (subscriptionStatus === "trialing") {
|
|
isTrial = true;
|
|
}
|
|
}
|
|
|
|
return { isActive: false, isTrial };
|
|
};
|
|
|
|
export default hasActiveTeamPlanHandler;
|