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:
46 files changed
+323
-249
No files matched your search
@@ -61,20 +61,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 {
|
||||
@@ -129,8 +129,6 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
|
||||
},
|
||||
});
|
||||
if (team) {
|
||||
const teamMetadata = teamMetadataSchema.parse(team?.metadata);
|
||||
|
||||
const user = await prisma.user.upsert({
|
||||
where: { email },
|
||||
update: {
|
||||
@@ -149,7 +147,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 {
|
||||
@@ -69,8 +68,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
},
|
||||
});
|
||||
if (team) {
|
||||
const teamMetadata = teamMetadataSchema.parse(team?.metadata);
|
||||
|
||||
const user = await prisma.user.upsert({
|
||||
where: { email: userEmail },
|
||||
update: {
|
||||
@@ -88,7 +85,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
|
||||
const { membership } = await createOrUpdateMemberships({
|
||||
teamMetadata,
|
||||
user,
|
||||
team,
|
||||
});
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
import type z from "zod";
|
||||
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Team, User } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import type { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
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">;
|
||||
}) => {
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
if (teamMetadata?.isOrganization) {
|
||||
if (team.isOrganization) {
|
||||
await tx.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
|
||||
@@ -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.isLoading}>
|
||||
{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,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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";
|
||||
@@ -88,7 +87,7 @@ export const TeamsFilter = ({
|
||||
/>
|
||||
<Divider />
|
||||
{teams
|
||||
?.filter((team) => !teamMetadataSchema.parse(team.metadata)?.isOrganization)
|
||||
?.filter((team) => !team?.isOrganization)
|
||||
.map((team) => (
|
||||
<FilterCheckboxField
|
||||
key={team.id}
|
||||
|
||||
Reference in new issue
Block a user