2023-04-25 22:39:47 +00:00
|
|
|
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
|
|
|
import { prisma } from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
2023-04-25 22:39:47 +00:00
|
|
|
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
|
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
|
|
|
|
type GetUpgradeableOptions = {
|
|
|
|
ctx: {
|
|
|
|
user: NonNullable<TrpcSessionUser>;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getUpgradeableHandler = async ({ ctx }: GetUpgradeableOptions) => {
|
|
|
|
if (!IS_TEAM_BILLING_ENABLED) return [];
|
|
|
|
let { teams } = await prisma.user.findUniqueOrThrow({
|
|
|
|
where: { id: ctx.user.id },
|
|
|
|
include: { teams: { where: { role: MembershipRole.OWNER }, include: { team: true } } },
|
|
|
|
});
|
|
|
|
/** We only need to return teams that don't have a `subscriptionId` on their metadata */
|
|
|
|
teams = teams.filter((m) => {
|
|
|
|
const metadata = teamMetadataSchema.safeParse(m.team.metadata);
|
2023-06-06 23:34:14 +00:00
|
|
|
return !(metadata.success && metadata.data?.subscriptionId);
|
2023-04-25 22:39:47 +00:00
|
|
|
});
|
|
|
|
return teams;
|
|
|
|
};
|