chore: Chore team metadata table + isOrganization migration from metadata (#12828)

* Migration+schema changes

* Remove org utils from schema

* Users fixture

* Auth signup handlers

* checkIsOrg fn

* use team.isOrganization > metadata

* Add isOrganization to getTeamWithMembers query

* list handler + optimise queries

* Update trpc handlers to use team.isOrganization && orgSettings table

* Update fixture

* Fix typo in prisma.schema

* next auth options use organizationSettings instread of tema metadata

* Admin tooling

* Updating components to use orgsettings over metadata

* Add unique index + fix org lib files

* validateUsername

* Add a seperate unique constraint between isOrg and slug

* Invite Member Logic

* fix resend file

* force isOrg to exist

* Team settings

* update createOrUpdateMemberships

* fix: admin get handler

* fix: team list handler - improve select

* fix: update admin update handler

* fix: settings layout - team get select

* organziationSettings Schema + adminupdate

* organziationSettings Schema + adminupdate

* typefix onSubtmi

* Fix:team repo throwing error if no metadata is found

* fix: add onDelete to be cascade instead of restri

* fix: org - team invite e2e

* fix: team invites not working in subteam context

* rename to organizationID

* fix user fixture team names

* Fix the organization created in tests

* fix: update organization metadata

* variable rename

* Reuse isOrganization

* variable rename

* Remove unnecessary optional chaining

* Update packages/trpc/server/routers/viewer/organizations/adminUpdate.handler.ts

Co-authored-by: Hariom Balhara <[email protected]>

* Update packages/trpc/server/routers/viewer/organizations/adminUpdate.handler.ts

Co-authored-by: Hariom Balhara <[email protected]>

* Update packages/trpc/server/routers/viewer/organizations/create.handler.ts

---------

Co-authored-by: Hariom <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-01-16 10:18:17 +00:00
1 parent 60e0bbf4b7
commit aabfd3b3f6
46 files changed
+323 -249

No files matched your search

+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(
@@ -83,6 +83,7 @@ export async function getTeamWithMembers(args: {
id: true,
name: true,
slug: true,
isOrganization: true,
...(!!includeTeamLogo ? { logo: true } : {}),
bio: true,
hideBranding: 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,
},
});
});
+22 -12
View File
@@ -7,8 +7,9 @@ import prisma from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
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;
@@ -49,7 +50,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) {
@@ -63,10 +65,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,21 +82,32 @@ async function getTeamOrOrg<TeamSelect extends Prisma.TeamSelect>({
// 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 select = teamSelect;
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) => {
const teamX = 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) {
+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,
@@ -94,10 +93,12 @@ export const validateAndGetCorrectedUsernameInTeam = async (
},
select: {
metadata: true,
isOrganization: true,
parentId: true,
organizationSettings: true,
parent: {
select: {
metadata: true,
organizationSettings: true,
},
},
},
@@ -107,17 +108,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 {