Files
calendar/packages/trpc/server/routers/viewer/teams/update.handler.ts
T
871e17a865 feat: org owner-admin cant see the teams created by other owner-admin 9982 cal 2120 (#10430)
* WIP

* New files for other teams

* Update code domain model location

* Fixes for finding teams in orgs

* VerticalTabs remove other teams appearance and only display if org admin

* Added conditional to consider orgs admin when updating team profile

* team-member-list wip

* Update memberInviteModal with disableCopyLink prop

* cleaning up

* Undo changes to files

* remove logs

* clean up

* undo changes

* undo change

* Undo changes

* Reset files to main

* typo on import

* Update packages/features/ee/organizations/pages/components/OtherTeamListItem.tsx

Co-authored-by: Omar López <zomars@me.com>

* useSearchParams hook instead of useRouter

* remove unused code and clean up

* Fix type

---------

Co-authored-by: Omar López <zomars@me.com>
2023-08-02 21:09:43 +02:00

90 lines
2.4 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
import { closeComUpdateTeam } from "@calcom/lib/sync/SyncServiceManager";
import { prisma } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../trpc";
import type { TUpdateInputSchema } from "./update.schema";
type UpdateOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TUpdateInputSchema;
};
export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
const isOrgAdmin = ctx.user?.organization?.isOrgAdmin;
if (!isOrgAdmin) {
if (!(await isTeamAdmin(ctx.user?.id, input.id))) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
}
if (input.slug) {
const userConflict = await prisma.team.findMany({
where: {
slug: input.slug,
},
});
if (userConflict.some((t) => t.id !== input.id)) return;
}
const prevTeam = await prisma.team.findFirst({
where: {
id: input.id,
},
});
if (!prevTeam) throw new TRPCError({ code: "NOT_FOUND", message: "Team not found." });
const data: Prisma.TeamUpdateArgs["data"] = {
name: input.name,
logo: input.logo,
bio: input.bio,
hideBranding: input.hideBranding,
isPrivate: input.isPrivate,
hideBookATeamMember: input.hideBookATeamMember,
brandColor: input.brandColor,
darkBrandColor: input.darkBrandColor,
theme: input.theme,
};
if (
input.slug &&
IS_TEAM_BILLING_ENABLED &&
/** If the team doesn't have a slug we can assume that it hasn't been published yet. */
!prevTeam.slug
) {
// Save it on the metadata so we can use it later
data.metadata = {
requestedSlug: input.slug,
};
} else {
data.slug = input.slug;
// If we save slug, we don't need the requestedSlug anymore
const metadataParse = teamMetadataSchema.safeParse(prevTeam.metadata);
if (metadataParse.success) {
const { requestedSlug: _, ...cleanMetadata } = metadataParse.data || {};
data.metadata = {
...cleanMetadata,
};
}
}
const updatedTeam = await prisma.team.update({
where: { id: input.id },
data,
});
// Sync Services: Close.com
if (prevTeam) closeComUpdateTeam(prevTeam, updatedTeam);
};