Files
calendar/packages/trpc/server/routers/viewer/organizations/checkIfOrgNeedsUpgrade.handler.ts
T
0970c77009 fix: Revert -> "Revert "chore: Chore team metadata table + isOrganization migration (#13700)
* Revert "Revert "chore: Chore team metadata table + isOrganization migration from metadata (#12828)""

This reverts commit 2408338ed4.

* Remove constraint slug,isOrganization and reset migration

* fix: conflicts

* change schema to bust cache

* Fix issues reported by TS

* change schema to bust cache

* Review fixes

* Colaesce for orgAutoAcceptEmail as well

* Fix missing negation

---------

Co-authored-by: zomars <zomars@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
2024-03-01 15:04:28 +00:00

45 lines
1.2 KiB
TypeScript

import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "../../../trpc";
type GetUpgradeableOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
export async function checkIfOrgNeedsUpgradeHandler({ ctx }: GetUpgradeableOptions) {
if (!IS_TEAM_BILLING_ENABLED) return [];
// Get all teams/orgs where the user is an owner
let teams = await prisma.membership.findMany({
where: {
user: {
id: ctx.user.id,
},
role: MembershipRole.OWNER,
team: {
parentId: null, // Since ORGS relay on their parent's subscription, we don't need to return them
},
},
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);
if (!m.team.isOrganization) return false;
if (metadata.success && metadata.data?.subscriptionId) return false;
return true;
});
return teams;
}
export default checkIfOrgNeedsUpgradeHandler;