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 <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-03-01 15:04:28 +00:00
1 parent 5b733765f4
commit 0970c77009
53 files changed
+375 -263

No files matched your search

+16 -10
View File
@@ -696,7 +696,7 @@ describe("orgMigration", () => {
});
describe("when user email matches orgAutoAcceptEmail", () => {
const orgMetadata = {
const orgSettings = {
orgAutoAcceptEmail: "org1.com",
} as const;
@@ -725,8 +725,10 @@ describe("orgMigration", () => {
const dbOrg = await createOrg({
slug: data.targetOrg.slug,
name: data.targetOrg.name,
metadata: {
...orgMetadata,
organizationSettings: {
create: {
...orgSettings,
},
},
});
@@ -784,8 +786,10 @@ describe("orgMigration", () => {
const dbOrg = await createOrg({
slug: data.targetOrg.slug,
name: data.targetOrg.name,
metadata: {
...orgMetadata,
organizationSettings: {
create: {
...orgSettings,
},
},
});
@@ -837,7 +841,11 @@ describe("orgMigration", () => {
name: data.targetOrg.name,
metadata: {
requestedSlug: data.targetOrg.requestedSlug,
...orgMetadata,
},
organizationSettings: {
create: {
...orgSettings,
},
},
});
@@ -901,9 +909,7 @@ describe("orgMigration", () => {
id: data.targetOrg.id,
slug: data.targetOrg.slug,
name: data.targetOrg.name,
metadata: {
isOrganization: true,
},
isOrganization: true,
},
});
@@ -1564,9 +1570,9 @@ async function createOrg(
return await prismock.team.create({
data: {
...data,
isOrganization: true,
metadata: {
...(data.metadata || {}),
isOrganization: true,
},
},
});
+15 -8
View File
@@ -1,5 +1,6 @@
import { getOrgUsernameFromEmail } from "@calcom/features/auth/signup/utils/getOrgUsernameFromEmail";
import { getOrgFullOrigin } from "@calcom/features/ee/organizations/lib/orgDomains";
import { isOrganization } from "@calcom/lib/entityPermissionUtils";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
@@ -47,7 +48,7 @@ export async function moveUserToOrg({
const teamMetadata = teamMetadataSchema.parse(team?.metadata);
if (!teamMetadata?.isOrganization) {
if (!isOrganization({ team })) {
throw new Error(`Team with ID:${targetOrgId} is not an Org`);
}
@@ -61,7 +62,7 @@ export async function moveUserToOrg({
if (!targetOrgUsername) {
targetOrgUsername = getOrgUsernameFromEmail(
userToMoveToOrg.email,
targetOrganization.metadata.orgAutoAcceptEmail || ""
targetOrganization.organizationSettings?.orgAutoAcceptEmail || ""
);
}
@@ -196,7 +197,7 @@ export async function moveTeamToOrg({
const teamMetadata = teamMetadataSchema.parse(possibleOrg?.metadata);
if (!teamMetadata?.isOrganization) {
if (!isOrganization({ team: possibleOrg })) {
throw new Error(`${targetOrg.id} is not an Org`);
}
@@ -205,7 +206,7 @@ export async function moveTeamToOrg({
await addTeamRedirect({
oldTeamSlug,
teamSlug: updatedTeam.slug,
orgSlug: targetOrganization.slug || orgMetadata.requestedSlug || null,
orgSlug: targetOrganization.slug || orgMetadata?.requestedSlug || null,
});
await setOrgSlugIfNotSet({ slug: targetOrganization.slug }, orgMetadata, targetOrg.id);
if (moveMembers) {
@@ -329,13 +330,14 @@ async function setOrgSlugIfNotSet(
},
orgMetadata: {
requestedSlug?: string | null | undefined;
},
} | null,
targetOrgId: number
) {
if (targetOrganization.slug) {
return;
}
if (!orgMetadata.requestedSlug) {
if (!orgMetadata?.requestedSlug) {
throw new HttpError({
statusCode: 400,
message: `Org with id: ${targetOrgId} doesn't have a slug. Tried using requestedSlug but that's also not present. So, all migration done but failed to set the Organization slug. Please set it manually`,
@@ -372,6 +374,9 @@ async function getTeamOrThrowError(targetOrgId: number) {
where: {
id: targetOrgId,
},
include: {
organizationSettings: true,
},
});
if (!team) {
@@ -650,6 +655,7 @@ async function moveTeamsWithoutMembersToOrg({
id: true,
slug: true,
metadata: true,
isOrganization: true,
},
});
@@ -661,7 +667,7 @@ async function moveTeamsWithoutMembersToOrg({
};
})
// Remove Orgs from the list
.filter((team) => !team.metadata?.isOrganization);
.filter((team) => !isOrganization({ team }));
const teamIdsToBeMovedToOrg = teamsToBeMovedToOrg.map((t) => t.id);
@@ -814,6 +820,7 @@ async function removeTeamsWithoutItsMemberFromOrg({ userToRemoveFromOrg }: { use
id: true,
slug: true,
metadata: true,
isOrganization: true,
},
});
@@ -825,7 +832,7 @@ async function removeTeamsWithoutItsMemberFromOrg({ userToRemoveFromOrg }: { use
};
})
// Remove Orgs from the list
.filter((team) => !team.metadata?.isOrganization);
.filter((team) => !isOrganization({ team }));
const teamIdsToBeRemovedFromOrg = teamsToBeRemovedFromOrg.map((t) => t.id);
+9 -8
View File
@@ -78,14 +78,17 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
team: {
select: {
metadata: true,
isOrganization: true,
parentId: true,
parent: {
select: {
slug: true,
metadata: true,
isOrganization: true,
organizationSettings: true,
},
},
slug: true,
organizationSettings: true,
},
},
},
@@ -134,8 +137,8 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
};
const isATeamInOrganization = tokenTeam?.parentId !== null;
const isOrganization = tokenTeam.metadata?.isOrganization;
// Detect if the team is an org by either the metadata flag or if it has a parent team
const isOrganization = tokenTeam.isOrganization;
const isOrganizationOrATeamInOrganization = isOrganization || isATeamInOrganization;
// If we are dealing with an org, the slug may come from the team itself or its parent
const orgSlug = isOrganizationOrATeamInOrganization
@@ -152,9 +155,7 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const isValidEmail = checkValidEmail(verificationToken.identifier);
const isOrgInviteByLink = isOrganizationOrATeamInOrganization && !isValidEmail;
const parentMetaDataForSubteam = tokenTeam?.parent?.metadata
? teamMetadataSchema.parse(tokenTeam.parent.metadata)
: null;
const parentOrgSettings = tokenTeam?.parent?.organizationSettings ?? null;
return {
props: {
@@ -167,15 +168,15 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
? getOrgUsernameFromEmail(
verificationToken.identifier,
(isOrganization
? tokenTeam.metadata?.orgAutoAcceptEmail
: parentMetaDataForSubteam?.orgAutoAcceptEmail) || ""
? tokenTeam.organizationSettings?.orgAutoAcceptEmail
: parentOrgSettings?.orgAutoAcceptEmail) || ""
)
: slugify(username),
}
: null,
orgSlug,
orgAutoAcceptEmail: isOrgInviteByLink
? tokenTeam?.metadata?.orgAutoAcceptEmail ?? parentMetaDataForSubteam?.orgAutoAcceptEmail ?? null
? tokenTeam?.organizationSettings?.orgAutoAcceptEmail ?? parentOrgSettings?.orgAutoAcceptEmail ?? null
: null,
},
};
@@ -72,7 +72,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
// Taking care of sub-teams and orgs
if (
(!isValidOrgDomain && team?.parent) ||
(!isValidOrgDomain && !!metadata?.isOrganization) ||
(!isValidOrgDomain && !!team?.isOrganization) ||
!organizationsEnabled
) {
return { notFound: true } as const;
+3 -3
View File
@@ -62,7 +62,7 @@ function TeamPage({
return (
<div className="flex h-full min-h-[100dvh] items-center justify-center">
<UnpublishedEntity
{...(metadata?.isOrganization || team.parentId ? { orgSlug: slug } : { teamSlug: slug })}
{...(team?.isOrganization || team.parentId ? { orgSlug: slug } : { teamSlug: slug })}
name={teamName}
/>
</div>
@@ -158,7 +158,7 @@ function TeamPage({
);
const profileImageSrc =
isValidOrgDomain || team.metadata?.isOrganization
isValidOrgDomain || team.isOrganization
? getOrgAvatarUrl({ slug: currentOrgDomain, logoUrl: team.logoUrl })
: getTeamAvatarUrl({ slug: team.slug, logoUrl: team.logoUrl, organizationId: team.parent?.id });
@@ -193,7 +193,7 @@ function TeamPage({
</>
)}
</div>
{metadata?.isOrganization ? (
{team.isOrganization ? (
<SubTeams />
) : (
<>
+4 -1
View File
@@ -48,8 +48,8 @@ export async function createOrgInDb({
data: {
name: name,
slug: slug,
isOrganization: true,
metadata: {
isOrganization: true,
...(requestedSlug
? {
requestedSlug,
@@ -57,5 +57,8 @@ export async function createOrgInDb({
: null),
},
},
include: {
organizationSettings: true,
},
});
}
+13 -13
View File
@@ -120,18 +120,21 @@ const createTeamAndAddUser = async (
const slug = `${isOrg ? "org" : "team"}-${workerInfo.workerIndex}-${Date.now()}`;
const data: PrismaType.TeamCreateInput = {
name: `user-id-${user.id}'s ${isOrg ? "Org" : "Team"}`,
isOrganization: isOrg,
};
data.metadata = {
...(isUnpublished ? { requestedSlug: slug } : {}),
...(isOrg
? {
isOrganization: true,
isOrganizationVerified: !!isOrgVerified,
orgAutoAcceptEmail: user.email.split("@")[1],
isOrganizationConfigured: false,
}
: {}),
};
if (isOrg) {
data.organizationSettings = {
create: {
isOrganizationVerified: !!isOrgVerified,
orgAutoAcceptEmail: user.email.split("@")[1],
isOrganizationConfigured: false,
},
};
}
data.slug = !isUnpublished ? slug : undefined;
if (isOrg && hasSubteam) {
const team = await createTeamAndAddUser({ user }, workerInfo);
@@ -593,7 +596,7 @@ const createUserFixture = (user: UserWithIncludes, page: Page) => {
},
};
})
.find((membership) => !membership.team?.metadata?.isOrganization);
.find((membership) => !membership.team.isOrganization);
if (!membership) {
throw new Error("No team found for user");
}
@@ -604,10 +607,7 @@ const createUserFixture = (user: UserWithIncludes, page: Page) => {
where: {
userId: user.id,
team: {
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
},
include: { team: { include: { children: true } } },
@@ -351,7 +351,7 @@ test.describe("Bookings", () => {
}
const usernameInOrg = getOrgUsernameFromEmail(
invitedUserEmail,
org.metadata?.orgAutoAcceptEmail ?? null
org.organizationSettings?.orgAutoAcceptEmail ?? null
);
const usernameOutsideOrg = userOutsideOrganization.username;
@@ -68,20 +68,10 @@ const checkIfUserShouldBelongToOrg = async (idP: IdentityProvider, email: string
if (!ORGANIZATIONS_AUTOLINK || idP !== "GOOGLE") return { orgUsername, orgId: undefined };
const existingOrg = await prisma.team.findFirst({
where: {
AND: [
{
metadata: {
path: ["isOrganizationVerified"],
equals: true,
},
},
{
metadata: {
path: ["orgAutoAcceptEmail"],
equals: apexDomain,
},
},
],
organizationSettings: {
isOrganizationVerified: true,
orgAutoAcceptEmail: apexDomain,
},
},
select: {
id: true,
@@ -14,7 +14,7 @@ import { closeComUpsertTeamUser } from "@calcom/lib/sync/SyncServiceManager";
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/lib/validateUsername";
import { prisma } from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums";
import { signupSchema, teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { signupSchema } from "@calcom/prisma/zod-utils";
import { joinAnyChildTeamOnOrgInvite } from "../utils/organization";
import {
@@ -128,12 +128,17 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
id: foundToken.teamId,
},
include: {
parent: true,
parent: {
select: {
id: true,
slug: true,
organizationSettings: true,
},
},
organizationSettings: true,
},
});
if (team) {
const teamMetadata = teamMetadataSchema.parse(team?.metadata);
const user = await prisma.user.upsert({
where: { email },
update: {
@@ -156,7 +161,6 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
});
// Wrapping in a transaction as if one fails we want to rollback the whole thing to preventa any data inconsistencies
const { membership } = await createOrUpdateMemberships({
teamMetadata,
user,
team,
});
@@ -12,7 +12,6 @@ import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/lib/validateUse
import prisma from "@calcom/prisma";
import { IdentityProvider } from "@calcom/prisma/enums";
import { signupSchema } from "@calcom/prisma/zod-utils";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { joinAnyChildTeamOnOrgInvite } from "../utils/organization";
import { prefillAvatar } from "../utils/prefillAvatar";
@@ -69,12 +68,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
id: foundToken.teamId,
},
include: {
parent: true,
parent: {
select: {
id: true,
slug: true,
organizationSettings: true,
},
},
organizationSettings: true,
},
});
if (team) {
const teamMetadata = teamMetadataSchema.parse(team?.metadata);
const user = await prisma.user.upsert({
where: { email: userEmail },
update: {
@@ -97,7 +101,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
const { membership } = await createOrUpdateMemberships({
teamMetadata,
user,
team,
});
@@ -1,25 +1,22 @@
import type z from "zod";
import { updateNewTeamMemberEventTypes } from "@calcom/lib/server/queries";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { prisma } from "@calcom/prisma";
import type { Team, User } from "@calcom/prisma/client";
import type { Team, User, OrganizationSettings } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import type { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { getOrgUsernameFromEmail } from "./getOrgUsernameFromEmail";
export const createOrUpdateMemberships = async ({
teamMetadata,
user,
team,
}: {
user: Pick<User, "id">;
team: Pick<Team, "id" | "parentId">;
teamMetadata: z.infer<typeof teamMetadataSchema>;
team: Pick<Team, "id" | "parentId" | "isOrganization"> & {
organizationSettings: OrganizationSettings | null;
};
}) => {
return await prisma.$transaction(async (tx) => {
if (teamMetadata?.isOrganization) {
if (team.isOrganization) {
const dbUser = await tx.user.update({
where: {
id: user.id,
@@ -36,7 +33,8 @@ export const createOrUpdateMemberships = async ({
// Ideally dbUser.username should never be null, but just in case.
// This method being called only during signup means that dbUser.username should be the correct org username
const orgUsername =
dbUser.username || getOrgUsernameFromEmail(dbUser.email, teamMetadata?.orgAutoAcceptEmail ?? null);
dbUser.username ||
getOrgUsernameFromEmail(dbUser.email, team.organizationSettings?.orgAutoAcceptEmail ?? null);
await tx.profile.upsert({
create: {
uid: ProfileRepository.generateProfileUid(),
@@ -1,12 +1,19 @@
import { updateNewTeamMemberEventTypes } from "@calcom/lib/server/queries";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { prisma } from "@calcom/prisma";
import type { Team } from "@calcom/prisma/client";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { Team, OrganizationSettings } from "@calcom/prisma/client";
import { getOrgUsernameFromEmail } from "./getOrgUsernameFromEmail";
export async function joinAnyChildTeamOnOrgInvite({ userId, org }: { userId: number; org: Team }) {
export async function joinAnyChildTeamOnOrgInvite({
userId,
org,
}: {
userId: number;
org: Pick<Team, "id"> & {
organizationSettings: OrganizationSettings | null;
};
}) {
const user = await prisma.user.findUnique({
where: {
id: userId,
@@ -16,10 +23,9 @@ export async function joinAnyChildTeamOnOrgInvite({ userId, org }: { userId: num
throw new Error("User not found");
}
const orgMetadata = teamMetadataSchema.parse(org.metadata);
const orgUsername =
user.username || getOrgUsernameFromEmail(user.email, orgMetadata?.orgAutoAcceptEmail ?? null);
user.username ||
getOrgUsernameFromEmail(user.email, org.organizationSettings?.orgAutoAcceptEmail ?? null);
await prisma.$transaction([
// Simply remove this update when we remove the `organizationId` field from the user table
@@ -4,7 +4,6 @@ import z from "zod";
import { HttpError } from "@calcom/lib/http-error";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import prisma from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
const querySchema = z.object({
org: z.string({ required_error: "org slug is required" }),
@@ -20,13 +19,16 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
} = parsedQuery;
if (!slug) return res.status(400).json({ message: "Org is needed" });
const org = await prisma.team.findFirst({ where: { slug }, select: { children: true, metadata: true } });
const org = await prisma.team.findFirst({
where: { slug },
select: { children: true, isOrganization: true },
});
if (!org) return res.status(400).json({ message: "Org doesn't exist" });
const metadata = teamMetadataSchema.parse(org?.metadata);
const isOrganization = org.isOrganization;
if (!metadata?.isOrganization) return res.status(400).json({ message: "Team is not an org" });
if (!isOrganization) return res.status(400).json({ message: "Team is not an org" });
return res.status(200).json({ slugs: org.children.map((ch) => ch.slug) });
}
@@ -139,10 +139,7 @@ export function whereClauseForOrgWithSlugOrRequestedSlug(slug: string) {
},
},
],
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
} satisfies Prisma.TeamWhereInput;
}
@@ -6,7 +6,7 @@ import { z } from "zod";
import NoSSR from "@calcom/core/components/NoSSR";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { useParamsWithFallback } from "@calcom/lib/hooks/useParamsWithFallback";
import type { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { orgSettingsSchema } from "@calcom/prisma/zod-utils";
import { trpc } from "@calcom/trpc/react";
import { Button, Form, Meta, TextField, showToast } from "@calcom/ui";
@@ -43,7 +43,7 @@ const OrgEditView = ({ orgId }: { orgId: number }) => {
type FormValues = {
name: Team["name"];
slug: Team["slug"];
metadata: z.infer<typeof teamMetadataSchema>;
organizationSettings: z.infer<typeof orgSettingsSchema>;
};
const OrgForm = ({
@@ -81,6 +81,10 @@ const OrgForm = ({
mutation.mutate({
id: org.id,
...values,
organizationSettings: {
...org.organizationSettings,
orgAutoAcceptEmail: values.organizationSettings?.orgAutoAcceptEmail,
},
});
};
@@ -96,7 +100,7 @@ const OrgForm = ({
label="Domain for which invitations are auto-accepted"
placeholder="abc.com"
required
{...form.register("metadata.orgAutoAcceptEmail")}
{...form.register("organizationSettings.orgAutoAcceptEmail")}
/>
<Button type="submit" color="primary" loading={mutation.isPending}>
{t("save")}
@@ -107,7 +107,7 @@ function AdminOrgTable() {
</Cell>
<Cell>
<div className="space-x-2">
{!org.metadata?.isOrganizationVerified ? (
{!org.organizationSettings?.isOrganizationVerified ? (
<Badge variant="red">{t("unverified")}</Badge>
) : (
<Badge variant="blue">{t("verified")}</Badge>
@@ -116,7 +116,7 @@ function AdminOrgTable() {
</Cell>
<Cell>
<div className="space-x-2">
{org.metadata?.isOrganizationConfigured ? (
{org.organizationSettings?.isOrganizationConfigured ? (
<Badge variant="blue">{t("dns_configured")}</Badge>
) : (
<Badge variant="red">{t("dns_missing")}</Badge>
@@ -136,7 +136,7 @@ function AdminOrgTable() {
<div className="flex w-full justify-end">
<DropdownActions
actions={[
...(!org.metadata?.isOrganizationVerified
...(!org.organizationSettings?.isOrganizationVerified
? [
{
id: "verify",
@@ -150,7 +150,7 @@ function AdminOrgTable() {
},
]
: []),
...(!org.metadata?.isOrganizationConfigured
...(!org.organizationSettings?.isOrganizationConfigured
? [
{
id: "dns",
@@ -158,7 +158,7 @@ function AdminOrgTable() {
onClick: () => {
updateMutation.mutate({
id: org.id,
metadata: {
organizationSettings: {
isOrganizationConfigured: true,
},
});
+1 -1
View File
@@ -87,7 +87,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
if (!session) return { message: "Team upgraded successfully" };
const redirectUrl = metadata?.data?.isOrganization
const redirectUrl = team?.isOrganization
? `${WEBAPP_URL}/settings/organizations/profile?upgraded=true`
: `${WEBAPP_URL}/settings/teams/${team.id}/profile?upgraded=true`;
@@ -5,7 +5,6 @@ import { useState } from "react";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { RouterOutputs } from "@calcom/trpc/react";
import { trpc } from "@calcom/trpc/react";
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
@@ -47,9 +46,7 @@ const useCurrentUserId = () => {
};
const checkIsOrg = (team: Props["team"]) => {
const metadata = teamMetadataSchema.safeParse(team.metadata);
if (metadata.success && metadata.data?.isOrganization) return true;
return false;
return team.isOrganization;
};
export default function MemberListItem(props: Props) {
+1 -1
View File
@@ -121,7 +121,7 @@ export const purchaseTeamSubscription = async (input: {
const getTeamWithPaymentMetadata = async (teamId: number) => {
const team = await prisma.team.findUniqueOrThrow({
where: { id: teamId },
select: { metadata: true, members: true },
select: { metadata: true, members: true, isOrganization: true },
});
const metadata = teamPaymentMetadataSchema.parse(team.metadata);
@@ -6,7 +6,6 @@ import { classNames } from "@calcom/lib";
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { useTypedQuery } from "@calcom/lib/hooks/useTypedQuery";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { trpc } from "@calcom/trpc/react";
import type { RouterOutputs } from "@calcom/trpc/react";
import { AnimatedPopover, Avatar, Divider, Tooltip, VerticalDivider } from "@calcom/ui";
@@ -97,7 +96,7 @@ export const TeamsFilter = ({
/>
<Divider />
{teams
?.filter((team) => !teamMetadataSchema.parse(team.metadata)?.isOrganization)
?.filter((team) => !team?.isOrganization)
.map((team) => (
<FilterCheckboxField
key={team.id}
@@ -25,7 +25,10 @@ export const createAProfileForAnExistingUser = async ({
if (!org) {
throw new Error(`Organization with id ${organizationId} not found`);
}
const usernameInOrg = getOrgUsernameFromEmail(user.email, org.metadata.orgAutoAcceptEmail ?? null);
const usernameInOrg = getOrgUsernameFromEmail(
user.email,
org.organizationSettings?.orgAutoAcceptEmail ?? null
);
const profile = await ProfileRepository.createForExistingUser({
userId: user.id,
organizationId,
+2 -3
View File
@@ -1,6 +1,5 @@
import type { Membership, Team } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
export const enum ENTITY_PERMISSION_LEVEL {
NONE,
@@ -20,8 +19,8 @@ export function canEditEntity(
);
}
export function isOrganization({ team }: { team: { metadata: Team["metadata"] } }) {
return teamMetadataSchema.parse(team.metadata)?.isOrganization;
export function isOrganization({ team }: { team: Pick<Team, "isOrganization"> }) {
return team.isOrganization;
}
export function getEntityPermissionLevel(
@@ -86,6 +86,7 @@ export async function getTeamWithMembers(args: {
id: true,
name: true,
slug: true,
isOrganization: true,
...(!!includeTeamLogo ? { logo: true } : {}),
logoUrl: true,
bio: true,
+13 -23
View File
@@ -30,9 +30,7 @@ describe("getOrg", () => {
id: 101,
name: "Test Team",
slug: "test-slug",
metadata: {
isOrganization: true,
},
isOrganization: true,
},
]);
@@ -52,18 +50,16 @@ describe("getOrg", () => {
expect(firstFindManyCallArguments[0]).toEqual({
where: {
slug: "test-slug",
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
select: {
id: true,
slug: true,
metadata: true,
isOrganization: true,
},
});
expect(org?.metadata?.isOrganization).toBe(true);
expect(org?.isOrganization).toBe(true);
});
it("should not return an org result if metadata.isOrganization isn't true", async () => {
@@ -93,15 +89,13 @@ describe("getOrg", () => {
expect(firstFindManyCallArguments[0]).toEqual({
where: {
slug: "test-slug",
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
select: {
id: true,
slug: true,
metadata: true,
isOrganization: true,
},
});
expect(org).toBe(null);
@@ -171,6 +165,7 @@ describe("getTeam", () => {
slug: true,
name: true,
metadata: true,
isOrganization: true,
},
});
expect(team).not.toBeNull();
@@ -185,9 +180,7 @@ describe("getTeam", () => {
id: 101,
name: "Test Team",
slug: "test-slug",
metadata: {
isOrganization: true,
},
isOrganization: true,
},
]);
@@ -214,6 +207,7 @@ describe("getTeam", () => {
slug: true,
name: true,
metadata: true,
isOrganization: true,
},
});
expect(team).toBe(null);
@@ -260,10 +254,7 @@ describe("getTeam", () => {
},
},
],
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
},
select: {
@@ -271,6 +262,7 @@ describe("getTeam", () => {
name: true,
slug: true,
metadata: true,
isOrganization: true,
},
});
});
@@ -294,10 +286,7 @@ describe("getTeam", () => {
where: {
slug: "test-team",
parent: {
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
OR: [
{
slug: "test-org",
@@ -316,6 +305,7 @@ describe("getTeam", () => {
slug: true,
name: true,
metadata: true,
isOrganization: true,
},
});
});
+20 -14
View File
@@ -9,8 +9,9 @@ import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { getParsedTeam } from "./teamUtils";
type TeamGetPayloadWithParsedMetadata<TeamSelect extends Prisma.TeamSelect> =
| (Omit<Prisma.TeamGetPayload<{ select: TeamSelect }>, "metadata"> & {
| (Omit<Prisma.TeamGetPayload<{ select: TeamSelect }>, "metadata" | "isOrganization"> & {
metadata: z.infer<typeof teamMetadataSchema>;
isOrganization: boolean;
})
| null;
@@ -51,7 +52,8 @@ async function getTeamOrOrg<TeamSelect extends Prisma.TeamSelect>({
teamSelect = {
...teamSelect,
metadata: true,
};
isOrganization: true,
} satisfies TeamSelect;
if (lookupBy.havingMemberWithId) where.members = { some: { userId: lookupBy.havingMemberWithId } };
if ("id" in lookupBy) {
@@ -65,10 +67,7 @@ async function getTeamOrOrg<TeamSelect extends Prisma.TeamSelect>({
// Note that an organization and a team that doesn't belong to an organization, both have parentId null
// If the organization has null slug(but requestedSlug is 'test') and the team also has slug 'test', we can't distinguish them without explicitly checking the metadata.isOrganization
// Note that, this isn't possible now to have same requestedSlug as the slug of a team not part of an organization. This is legacy teams handling mostly. But it is still safer to be sure that you are fetching an Organization only in case of isOrgView
where.metadata = {
path: ["isOrganization"],
equals: true,
};
where.isOrganization = true;
// We must fetch only the team here.
} else {
if (forOrgWithSlug) {
@@ -83,23 +82,28 @@ async function getTeamOrOrg<TeamSelect extends Prisma.TeamSelect>({
where,
});
// teamSelect extends Prisma.TeamSelect but still teams doesn't contain a valid team as per TypeScript and thus it doesn't consider it having team.metadata, team.id and other fields
// This is the reason below code is using a lot of assertions.
const teams = await prisma.team.findMany({
where,
select: teamSelect,
});
const teamsWithParsedMetadata = teams
.map((team) => ({
...team,
// Using Type assertion here because we know that the metadata is present and Prisma and TypeScript aren't playing well together
metadata: teamMetadataSchema.parse((team as { metadata: z.infer<typeof teamMetadataSchema> }).metadata),
}))
.map((team) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore ts types are way too complciated for this now
const parsedMetadata = teamMetadataSchema.parse(team.metadata ?? {});
return {
...team,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore It does exist
isOrganization: team.isOrganization as boolean,
metadata: parsedMetadata,
};
})
// In cases where there are many teams with the same slug, we need to find out the one and only one that matches our criteria
.filter((team) => {
// We need an org if isOrgView otherwise we need a team
return isOrg ? team.metadata?.isOrganization : !team.metadata?.isOrganization;
return isOrg ? team.isOrganization : !team.isOrganization;
});
if (teamsWithParsedMetadata.length > 1) {
@@ -156,6 +160,8 @@ const teamSelect = Prisma.validator<Prisma.TeamSelect>()({
logoUrl: true,
parentId: true,
metadata: true,
isOrganization: true,
organizationSettings: true,
});
export class TeamRepository {
+2
View File
@@ -20,6 +20,8 @@ const teamSelect = Prisma.validator<Prisma.TeamSelect>()({
slug: true,
metadata: true,
logoUrl: true,
organizationSettings: true,
isOrganization: true,
});
const userSelect = Prisma.validator<Prisma.UserSelect>()({
+1 -4
View File
@@ -195,10 +195,7 @@ const usernameCheckForSignup = async ({
where: {
userId: user.id,
team: {
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
},
});
+6 -7
View File
@@ -1,6 +1,5 @@
import { getOrgUsernameFromEmail } from "@calcom/features/auth/signup/utils/getOrgUsernameFromEmail";
import prisma from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
export const getUsernameForOrgMember = async ({
email,
@@ -92,10 +91,12 @@ export const validateAndGetCorrectedUsernameInTeam = async (
},
select: {
metadata: true,
isOrganization: true,
parentId: true,
organizationSettings: true,
parent: {
select: {
metadata: true,
organizationSettings: true,
},
},
},
@@ -105,17 +106,15 @@ export const validateAndGetCorrectedUsernameInTeam = async (
teamId,
team,
});
const teamData = { ...team, metadata: teamMetadataSchema.parse(team?.metadata) };
const organization = teamData.metadata?.isOrganization ? teamData : teamData.parent;
const organization = team?.isOrganization ? team : team?.parent;
if (organization) {
const orgMetadata = teamMetadataSchema.parse(organization.metadata);
// Organization context -> org-context username check
const orgId = teamData.parentId || teamId;
const orgId = team?.parentId || teamId;
return validateAndGetCorrectedUsernameAndEmail({
username,
email,
organizationId: orgId,
orgAutoAcceptEmail: orgMetadata?.orgAutoAcceptEmail || "",
orgAutoAcceptEmail: organization?.organizationSettings?.orgAutoAcceptEmail || "",
isSignup,
});
} else {
@@ -0,0 +1,46 @@
-- AlterTable
ALTER TABLE "Team" ADD COLUMN "isOrganization" BOOLEAN NOT NULL DEFAULT false;
-- CreateTable
CREATE TABLE "OrganizationSettings" (
"id" SERIAL NOT NULL,
"organizationId" INTEGER NOT NULL,
"isOrganizationConfigured" BOOLEAN NOT NULL DEFAULT false,
"isOrganizationVerified" BOOLEAN NOT NULL DEFAULT false,
"orgAutoAcceptEmail" TEXT NOT NULL,
CONSTRAINT "OrganizationSettings_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "OrganizationSettings_organizationId_key" ON "OrganizationSettings"("organizationId");
-- AddForeignKey
ALTER TABLE "OrganizationSettings" ADD CONSTRAINT "OrganizationSettings_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Manually written queries below
--
-- Set team field to notify if it is an organization -> Easier than metadata.parse(X).isOrganization
UPDATE "Team"
SET "isOrganization" = (metadata ->> 'isOrganization')::BOOLEAN
WHERE
metadata ->> 'isOrganization' = 'true';
-- Insert data into org settings
INSERT INTO "OrganizationSettings" ("organizationId", "isOrganizationConfigured", "orgAutoAcceptEmail", "isOrganizationVerified")
SELECT
t.id,
COALESCE((t.metadata ->> 'isOrganizationConfigured')::BOOLEAN, false) AS "isOrganizationConfigured",
COALESCE((t.metadata ->> 'orgAutoAcceptEmail'), '') AS "orgAutoAcceptEmail",
COALESCE((t.metadata ->> 'isOrganizationVerified')::BOOLEAN, false) AS "isOrganizationVerified"
FROM (
SELECT
id,
metadata
FROM
"Team"
WHERE
metadata ->> 'isOrganization' = 'true'
) AS t;
+12 -1
View File
@@ -297,7 +297,7 @@ model User {
@@map(name: "users")
}
// It holds Organization Profiles
// It holds Organization Profiles as well as User Profiles for users that have been added to an organization
model Profile {
id Int @id @default(autoincrement())
// uid allows us to set an identifier chosen by us which is helpful in migration when we create the Profile from User directly.
@@ -356,6 +356,8 @@ model Team {
apiKeys ApiKey[]
credentials Credential[]
accessCodes AccessCode[]
isOrganization Boolean @default(false)
organizationSettings OrganizationSettings?
instantMeetingTokens InstantMeetingToken[]
orgProfiles Profile[]
pendingPayment Boolean @default(false)
@@ -363,6 +365,15 @@ model Team {
@@unique([slug, parentId])
}
model OrganizationSettings {
id Int @id @default(autoincrement())
organization Team @relation(fields: [organizationId], references: [id], onDelete: Cascade)
organizationId Int @unique
isOrganizationConfigured Boolean @default(false)
isOrganizationVerified Boolean @default(false)
orgAutoAcceptEmail String
}
enum MembershipRole {
MEMBER
ADMIN
+7 -4
View File
@@ -346,6 +346,13 @@ export const userMetadata = z
})
.nullable();
export const orgSettingsSchema = z
.object({
isOrganizationVerified: z.boolean().optional(),
isOrganizationConfigured: z.boolean().optional(),
orgAutoAcceptEmail: z.string().optional(),
})
.nullable();
export type userMetadataType = z.infer<typeof userMetadata>;
export const teamMetadataSchema = z
@@ -354,10 +361,6 @@ export const teamMetadataSchema = z
paymentId: z.string(),
subscriptionId: z.string().nullable(),
subscriptionItemId: z.string().nullable(),
isOrganization: z.boolean().nullable(),
isOrganizationVerified: z.boolean().nullable(),
isOrganizationConfigured: z.boolean().nullable(),
orgAutoAcceptEmail: z.string().nullable(),
migratedToOrgFrom: z
.object({
teamSlug: z.string().or(z.null()).optional(),
@@ -1,4 +1,4 @@
import { isOrganization, withRoleCanCreateEntity } from "@calcom/lib/entityPermissionUtils";
import { withRoleCanCreateEntity } from "@calcom/lib/entityPermissionUtils";
import { getTeamAvatarUrl, getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
import type { PrismaClient } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
@@ -36,6 +36,7 @@ export const teamsAndUserProfilesQuery = async ({ ctx }: TeamsAndUserProfileOpti
team: {
select: {
id: true,
isOrganization: true,
name: true,
slug: true,
metadata: true,
@@ -56,7 +57,7 @@ export const teamsAndUserProfilesQuery = async ({ ctx }: TeamsAndUserProfileOpti
}
const nonOrgTeams = user.teams
.filter((membership) => !isOrganization({ team: membership.team }))
.filter((membership) => !membership.team.isOrganization)
.map((membership) => ({
...membership,
team: {
@@ -19,10 +19,7 @@ export const adminDeleteHandler = async ({ input }: AdminDeleteOption) => {
const foundOrg = await prisma.team.findUnique({
where: {
id: input.orgId,
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
include: {
members: {
@@ -23,6 +23,7 @@ export const adminGetHandler = async ({ input }: AdminGetOptions) => {
name: true,
slug: true,
metadata: true,
isOrganization: true,
members: {
where: {
role: "OWNER",
@@ -37,6 +38,13 @@ export const adminGetHandler = async ({ input }: AdminGetOptions) => {
},
},
},
organizationSettings: {
select: {
isOrganizationConfigured: true,
isOrganizationVerified: true,
orgAutoAcceptEmail: true,
},
},
},
});
@@ -47,7 +55,7 @@ export const adminGetHandler = async ({ input }: AdminGetOptions) => {
});
}
const parsedMetadata = teamMetadataSchema.parse(org.metadata);
if (!parsedMetadata?.isOrganization) {
if (!org?.isOrganization) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Organization not found",
@@ -12,20 +12,14 @@ type AdminGetAllOptions = {
export const adminGetUnverifiedHandler = async ({}: AdminGetAllOptions) => {
const allOrgs = await prisma.team.findMany({
where: {
AND: [
{
metadata: {
path: ["isOrganization"],
equals: true,
},
},
],
isOrganization: true,
},
select: {
id: true,
name: true,
slug: true,
metadata: true,
organizationSettings: true,
members: {
where: {
role: "OWNER",
@@ -17,11 +17,14 @@ type AdminUpdateOptions = {
};
export const adminUpdateHandler = async ({ input }: AdminUpdateOptions) => {
const { id, ...restInput } = input;
const { id, organizationSettings, ...restInput } = input;
const existingOrg = await prisma.team.findUnique({
where: {
id: id,
},
include: {
organizationSettings: true,
},
});
if (!existingOrg) {
@@ -32,10 +35,8 @@ export const adminUpdateHandler = async ({ input }: AdminUpdateOptions) => {
}
const { mergeMetadata } = getMetadataHelpers(teamMetadataSchema.unwrap(), existingOrg.metadata);
const data: Prisma.TeamUpdateArgs["data"] = {
...restInput,
metadata: mergeMetadata({ ...restInput.metadata }),
};
const data: Prisma.TeamUpdateArgs["data"] = restInput;
if (restInput.slug) {
await throwIfSlugConflicts({ id, slug: restInput.slug });
@@ -49,16 +50,36 @@ export const adminUpdateHandler = async ({ input }: AdminUpdateOptions) => {
data.metadata = mergeMetadata({
// If we save slug, we don't need the requestedSlug anymore
requestedSlug: undefined,
...input.metadata,
});
}
const updatedOrganisation = await prisma.team.update({
where: { id },
data,
const updatedOrganization = await prisma.$transaction(async (tx) => {
const updatedOrganization = await tx.team.update({
where: { id },
data,
});
if (organizationSettings || existingOrg.organizationSettings) {
await tx.organizationSettings.update({
where: {
organizationId: updatedOrganization.id,
},
data: {
isOrganizationConfigured:
organizationSettings?.isOrganizationConfigured ||
existingOrg.organizationSettings?.isOrganizationConfigured,
isOrganizationVerified:
organizationSettings?.isOrganizationVerified ||
existingOrg.organizationSettings?.isOrganizationVerified,
orgAutoAcceptEmail:
organizationSettings?.orgAutoAcceptEmail || existingOrg.organizationSettings?.orgAutoAcceptEmail,
},
});
}
return updatedOrganization;
});
return updatedOrganisation;
return updatedOrganization;
};
export default adminUpdateHandler;
@@ -1,12 +1,12 @@
import { z } from "zod";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { orgSettingsSchema } from "@calcom/prisma/zod-utils";
export const ZAdminUpdate = z.object({
id: z.number(),
name: z.string().optional(),
slug: z.string().nullish(),
metadata: teamMetadataSchema.optional(),
organizationSettings: orgSettingsSchema.unwrap().optional(),
});
export type TAdminUpdate = z.infer<typeof ZAdminUpdate>;
@@ -1,6 +1,5 @@
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { prisma } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@trpc/server";
@@ -18,10 +17,7 @@ export const adminVerifyHandler = async ({ input }: AdminVerifyOptions) => {
const foundOrg = await prisma.team.findFirst({
where: {
id: input.orgId,
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
include: {
members: {
@@ -43,17 +39,12 @@ export const adminVerifyHandler = async ({ input }: AdminVerifyOptions) => {
const acceptedEmailDomain = foundOrg.members[0].user.email.split("@")[1];
const existingMetadataParsed = teamMetadataSchema.parse(foundOrg.metadata);
await prisma.team.update({
await prisma.organizationSettings.update({
where: {
id: input.orgId,
organizationId: input.orgId,
},
data: {
metadata: {
...existingMetadataParsed,
isOrganizationVerified: true,
},
isOrganizationVerified: true,
},
});
@@ -72,12 +63,18 @@ export const adminVerifyHandler = async ({ input }: AdminVerifyOptions) => {
id: true,
username: true,
email: true,
profiles: {
where: {
organizationId: foundOrg.id,
},
},
},
});
const users = foundUsersWithMatchingEmailDomain;
const userIds = users.map((user) => user.id);
const usersNotHavingProfileWithTheOrg = users.filter((user) => user.profiles.length === 0);
await prisma.$transaction([
prisma.membership.updateMany({
where: {
@@ -112,7 +109,7 @@ export const adminVerifyHandler = async ({ input }: AdminVerifyOptions) => {
}),
ProfileRepository.createMany({
users: users.map((user) => {
users: usersNotHavingProfileWithTheOrg.map((user) => {
return {
id: user.id,
username: user.username || user.email.split("@")[0],
@@ -33,7 +33,7 @@ export async function checkIfOrgNeedsUpgradeHandler({ ctx }: GetUpgradeableOptio
/** 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 (metadata.success && !metadata.data?.isOrganization) return false;
if (!m.team.isOrganization) return false;
if (metadata.success && metadata.data?.subscriptionId) return false;
return true;
});
@@ -48,10 +48,7 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
where: {
slug: slug,
parentId: null,
metadata: {
path: ["isOrganization"],
equals: true,
},
isOrganization: true,
},
});
@@ -103,13 +100,17 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
const organization = await tx.team.create({
data: {
name,
isOrganization: true,
...(!IS_TEAM_BILLING_ENABLED ? { slug } : {}),
organizationSettings: {
create: {
isOrganizationVerified: true,
isOrganizationConfigured,
orgAutoAcceptEmail: emailDomain,
},
},
metadata: {
...(IS_TEAM_BILLING_ENABLED ? { requestedSlug: slug } : {}),
isOrganization: true,
isOrganizationVerified: true,
isOrganizationConfigured,
orgAutoAcceptEmail: emailDomain,
},
},
});
@@ -31,7 +31,7 @@ export const ZUpdateInputSchema = z.object({
timeZone: z.string().optional(),
weekStart: z.string().optional(),
timeFormat: z.number().optional(),
metadata: teamMetadataSchema.unwrap().pick({ isOrganizationConfigured: true }).optional(),
metadata: teamMetadataSchema.unwrap().optional(),
});
export type TUpdateInputSchema = z.infer<typeof ZUpdateInputSchema>;
@@ -3,7 +3,6 @@ import { randomBytes } from "crypto";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
import { prisma } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@calcom/trpc/server";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
@@ -21,8 +20,7 @@ export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) =
const membership = await isTeamAdmin(ctx.user.id, teamId);
if (!membership || !membership?.team) throw new TRPCError({ code: "UNAUTHORIZED" });
const teamMetadata = teamMetadataSchema.parse(membership.team.metadata);
const isOrganizationOrATeamInOrganization = !!(membership.team?.parentId || teamMetadata?.isOrganization);
const isOrganizationOrATeamInOrganization = !!(membership.team?.parentId || membership.team.isOrganization);
if (input.token) {
const existingToken = await prisma.verificationToken.findFirst({
@@ -4,7 +4,6 @@ import { deleteDomain } from "@calcom/lib/domainManager/organization";
import { isTeamOwner } from "@calcom/lib/server/queries/teams";
import { closeComDeleteTeam } from "@calcom/lib/sync/SyncServiceManager";
import { prisma } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@trpc/server";
@@ -39,9 +38,7 @@ export const deleteHandler = async ({ ctx, input }: DeleteOptions) => {
return deletedTeam;
});
const deletedTeamMetadata = teamMetadataSchema.parse(deletedTeam.metadata);
if (deletedTeamMetadata?.isOrganization && deletedTeam.slug) deleteDomain(deletedTeam.slug);
if (deletedTeam?.isOrganization && deletedTeam.slug) deleteDomain(deletedTeam.slug);
// Sync Services: Close.cm
closeComDeleteTeam(deletedTeam);
@@ -37,7 +37,7 @@ export const getUpgradeableHandler = async ({ ctx }: GetUpgradeableOptions) => {
teams = teams.filter((m) => {
const metadata = teamMetadataSchema.safeParse(m.team.metadata);
if (metadata.success && metadata.data?.subscriptionId) return false;
if (metadata.success && metadata.data?.isOrganization) return false; // We also dont return ORGs as it will be handled in OrgUpgradeBanner
if (m.team.isOrganization) return false; // We also dont return ORGs as it will be handled in OrgUpgradeBanner
if (m.team.children.length > 0) return false; // We also dont return ORGs as it will be handled in OrgUpgradeBanner
return true;
});
@@ -46,23 +46,23 @@ export const inviteMemberHandler = async ({ ctx, input }: InviteMemberOptions) =
await checkRateLimitAndThrowError({
identifier: `invitedBy:${ctx.user.id}`,
});
const team = await getTeamOrThrow(input.teamId);
const isOrg = team.isOrganization;
// Only owners can award owner role in an organization.
if (isOrg && input.role === MembershipRole.OWNER && !(await isOrganisationOwner(ctx.user.id, input.teamId)))
throw new TRPCError({ code: "UNAUTHORIZED" });
await checkPermissions({
userId: ctx.user.id,
teamId:
ctx.user.organization.id && ctx.user.organization.isOrgAdmin ? ctx.user.organization.id : input.teamId,
isOrg: input.isOrg,
isOrg,
});
// Only owners can award owner role in an organization.
if (
input.isOrg &&
input.role === MembershipRole.OWNER &&
!(await isOrganisationOwner(ctx.user.id, input.teamId))
)
throw new TRPCError({ code: "UNAUTHORIZED" });
const { autoAcceptEmailDomain, orgVerified } = getIsOrgVerified(isOrg, team);
const team = await getTeamOrThrow(input.teamId, input.isOrg);
const { autoAcceptEmailDomain, orgVerified } = getIsOrgVerified(input.isOrg, team);
const usernameOrEmailsToInvite = await getUsernameOrEmailsToInvite(input.usernameOrEmail);
const orgConnectInfoByUsernameOrEmail = usernameOrEmailsToInvite.reduce((acc, usernameOrEmail) => {
return {
@@ -72,13 +72,13 @@ export const inviteMemberHandler = async ({ ctx, input }: InviteMemberOptions) =
orgAutoAcceptDomain: autoAcceptEmailDomain,
usersEmail: usernameOrEmail,
team,
isOrg: input.isOrg,
isOrg: isOrg,
}),
};
}, {} as Record<string, ReturnType<typeof getOrgConnectionInfo>>);
const existingUsersWithMembersips = await getUsersToInvite({
usernamesOrEmails: usernameOrEmailsToInvite,
isInvitedToOrg: input.isOrg,
isInvitedToOrg: isOrg,
team,
});
@@ -86,6 +86,7 @@ const mockedRegularTeam: TeamWithParent = {
parent: null,
isPrivate: false,
logoUrl: "",
isOrganization: false,
calVideoLogo: "",
};
@@ -239,7 +240,10 @@ describe("Invite Member Utils", () => {
describe("getIsOrgVerified", () => {
it("should return the correct values when isOrg is true and teamMetadata.orgAutoAcceptEmail is true", () => {
const team = {
metadata: {
organizationSettings: {
id: 1,
teamId: 1,
isOrganizationConfigured: false,
isOrganizationVerified: true,
orgAutoAcceptEmail: "example.com",
},
@@ -258,7 +262,10 @@ describe("Invite Member Utils", () => {
metadata: {},
parent: {
...mockedRegularTeam,
metadata: {
organizationSettings: {
id: 1,
teamId: 1,
isOrganizationConfigured: false,
isOrganizationVerified: false,
orgAutoAcceptEmail: "example.com",
},
@@ -12,7 +12,7 @@ import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { UserRepository } from "@calcom/lib/server/repository/user";
import slugify from "@calcom/lib/slugify";
import { prisma } from "@calcom/prisma";
import type { Membership, Team } from "@calcom/prisma/client";
import type { Membership, OrganizationSettings, Team } from "@calcom/prisma/client";
import { Prisma, type User as UserType, type UserPassword } from "@calcom/prisma/client";
import type { Profile as ProfileType } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
@@ -62,18 +62,26 @@ export function checkInputEmailIsValid(email: string) {
});
}
export async function getTeamOrThrow(teamId: number, isOrg?: boolean) {
export async function getTeamOrThrow(teamId: number) {
const team = await prisma.team.findFirst({
where: {
id: teamId,
},
include: {
parent: true,
organizationSettings: true,
parent: {
include: {
organizationSettings: true,
},
},
},
});
if (!team)
throw new TRPCError({ code: "NOT_FOUND", message: `${isOrg ? "Organization" : "Team"} not found` });
throw new TRPCError({
code: "NOT_FOUND",
message: `Team not found`,
});
return { ...team, metadata: teamMetadataSchema.parse(team.metadata) };
}
@@ -404,27 +412,29 @@ export async function sendSignupToOrganizationEmail({
});
}
type TeamAndOrganizationSettings = Team & {
organizationSettings?: OrganizationSettings | null;
};
export function getIsOrgVerified(
isOrg: boolean,
team: Team & {
parent: Team | null;
team: TeamAndOrganizationSettings & {
parent: TeamAndOrganizationSettings | null;
}
) {
const teamMetadata = teamMetadataSchema.parse(team.metadata);
const orgMetadataSafeParse = teamMetadataSchema.safeParse(team.parent?.metadata);
const orgMetadataIfExists = orgMetadataSafeParse.success ? orgMetadataSafeParse.data : null;
const parentSettings = team.parent?.organizationSettings;
if (isOrg && teamMetadata?.orgAutoAcceptEmail) {
if (isOrg && team.organizationSettings?.orgAutoAcceptEmail) {
return {
isInOrgScope: true,
orgVerified: teamMetadata.isOrganizationVerified,
autoAcceptEmailDomain: teamMetadata.orgAutoAcceptEmail,
orgVerified: team.organizationSettings.isOrganizationVerified,
autoAcceptEmailDomain: team.organizationSettings.orgAutoAcceptEmail,
};
} else if (orgMetadataIfExists?.orgAutoAcceptEmail) {
} else if (parentSettings?.orgAutoAcceptEmail) {
return {
isInOrgScope: true,
orgVerified: orgMetadataIfExists.isOrganizationVerified,
autoAcceptEmailDomain: orgMetadataIfExists.orgAutoAcceptEmail,
orgVerified: parentSettings.isOrganizationVerified,
autoAcceptEmailDomain: parentSettings.orgAutoAcceptEmail,
};
}
@@ -22,9 +22,17 @@ export const listHandler = async ({ ctx, input }: ListOptions) => {
},
include: {
team: {
include: {
select: {
id: true,
name: true,
slug: true,
logo: true,
logoUrl: true,
isOrganization: true,
metadata: true,
inviteTokens: true,
parent: true,
parentId: true,
},
},
},
@@ -34,8 +42,7 @@ export const listHandler = async ({ ctx, input }: ListOptions) => {
return memberships
.filter((mmship) => {
if (input?.includeOrgs) return true;
const metadata = teamMetadataSchema.parse(mmship.team.metadata);
return !metadata?.isOrganization;
return !mmship.team.isOrganization;
})
.map(({ team: { inviteTokens, logo, logoUrl, ..._team }, ...membership }) => ({
role: membership.role,
@@ -1,6 +1,5 @@
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "../../../trpc";
@@ -33,8 +32,7 @@ export const listOwnedTeamsHandler = async ({ ctx }: ListOptions) => {
return user?.teams
?.filter((m) => {
const metadata = teamMetadataSchema.parse(m.team.metadata);
return !metadata?.isOrganization;
return !m.team.isOrganization;
})
?.map(({ team }) => team);
};
@@ -82,10 +82,13 @@ export const removeMemberHandler = async ({ ctx, input }: RemoveMemberOptions) =
const orgInfo = await ctx.prisma.team.findUnique({
where: { id: input.teamId },
select: {
isOrganization: true,
organizationSettings: true,
id: true,
metadata: true,
},
});
const orgMetadata = orgInfo?.organizationSettings;
if (!foundUser || !orgInfo) throw new TRPCError({ code: "NOT_FOUND" });
@@ -15,7 +15,7 @@ type InviteMemberOptions = {
};
export const resendInvitationHandler = async ({ ctx, input }: InviteMemberOptions) => {
const team = await getTeamOrThrow(input.teamId, input.isOrg);
const team = await getTeamOrThrow(input.teamId);
await checkPermissions({
userId: ctx.user.id,
@@ -2,7 +2,6 @@ import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
import { prisma } from "@calcom/prisma";
import type { Webhook } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { TRPCError } from "@trpc/server";
@@ -67,6 +66,7 @@ export const getByViewerHandler = async ({ ctx }: GetByViewerOptions) => {
team: {
select: {
id: true,
isOrganization: true,
name: true,
slug: true,
parentId: true,
@@ -114,8 +114,7 @@ export const getByViewerHandler = async ({ ctx }: GetByViewerOptions) => {
const teamWebhookGroups: WebhookGroup[] = user.teams
.filter((mmship) => {
const metadata = teamMetadataSchema.parse(mmship.team.metadata);
return !metadata?.isOrganization;
return !mmship.team.isOrganization;
})
.map((membership) => {
const orgMembership = teamMemberships.find(