fix: update tempOrgRediret on updating org slug (#25838)
* fix: update tempOrgRediret on updating s lug * Update packages/trpc/server/routers/viewer/organizations/adminUpdate.handler.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * refactor * fix --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
176aeaafd8
commit
b20b52a1f8
3 files changed
+155
-101
No files matched your search
@@ -1,9 +1,6 @@
|
||||
import { renameDomain } from "@calcom/lib/domainManager/organization";
|
||||
import { getMetadataHelpers } from "@calcom/lib/getMetadataHelpers";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { getOrganizationRepository } from "@calcom/features/ee/organizations/di/OrganizationRepository.container";
|
||||
import { AdminOrganizationUpdateService } from "@calcom/features/ee/organizations/lib/service/AdminOrganizationUpdateService";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { teamMetadataStrictSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../types";
|
||||
import type { TAdminUpdate } from "./adminUpdate.schema";
|
||||
@@ -16,103 +13,13 @@ type AdminUpdateOptions = {
|
||||
};
|
||||
|
||||
export const adminUpdateHandler = async ({ input }: AdminUpdateOptions) => {
|
||||
const { id, organizationSettings, ...restInput } = input;
|
||||
const existingOrg = await prisma.team.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
include: {
|
||||
organizationSettings: true,
|
||||
},
|
||||
const organizationRepository = getOrganizationRepository();
|
||||
const adminOrganizationUpdateService = new AdminOrganizationUpdateService({
|
||||
prismaClient: prisma,
|
||||
organizationRepository,
|
||||
});
|
||||
|
||||
if (!existingOrg) {
|
||||
throw new HttpError({
|
||||
message: "Organization not found",
|
||||
statusCode: 404,
|
||||
});
|
||||
}
|
||||
|
||||
const { mergeMetadata } = getMetadataHelpers(teamMetadataStrictSchema.unwrap(), existingOrg.metadata || {});
|
||||
|
||||
const data: Prisma.TeamUpdateArgs["data"] = restInput;
|
||||
|
||||
if (restInput.slug) {
|
||||
await throwIfSlugConflicts({ id, slug: restInput.slug });
|
||||
const isSlugChanged = restInput.slug !== existingOrg.slug;
|
||||
if (isSlugChanged) {
|
||||
// If slug is changed, we need to rename the domain first
|
||||
// If renaming fails, we don't want to update the new slug in DB
|
||||
await renameDomain(existingOrg.slug, restInput.slug);
|
||||
}
|
||||
data.slug = input.slug;
|
||||
data.metadata = mergeMetadata({
|
||||
// If we save slug, we don't need the requestedSlug anymore
|
||||
requestedSlug: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
isAdminReviewed: organizationSettings?.isAdminReviewed,
|
||||
orgAutoAcceptEmail:
|
||||
organizationSettings?.orgAutoAcceptEmail || existingOrg.organizationSettings?.orgAutoAcceptEmail,
|
||||
isAdminAPIEnabled: !!(
|
||||
organizationSettings?.isAdminAPIEnabled ?? existingOrg.organizationSettings?.isAdminAPIEnabled
|
||||
),
|
||||
},
|
||||
});
|
||||
}
|
||||
return updatedOrganization;
|
||||
});
|
||||
|
||||
return updatedOrganization;
|
||||
return adminOrganizationUpdateService.updateOrganization(input);
|
||||
};
|
||||
|
||||
export default adminUpdateHandler;
|
||||
|
||||
async function throwIfSlugConflicts({ id, slug }: { id: number; slug: string }) {
|
||||
const organizationsWithSameSlug = await prisma.team.findMany({
|
||||
where: {
|
||||
slug: slug,
|
||||
parentId: null,
|
||||
},
|
||||
});
|
||||
|
||||
if (organizationsWithSameSlug.length > 1) {
|
||||
throw new HttpError({
|
||||
message: "There can only be one organization with a given slug",
|
||||
statusCode: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const foundOrg = organizationsWithSameSlug[0];
|
||||
if (!foundOrg) {
|
||||
// No org with same slug found - So, no conflict.
|
||||
return;
|
||||
}
|
||||
|
||||
// If foundOrg isn't same as the org being edited
|
||||
if (foundOrg.id !== id) {
|
||||
throw new HttpError({
|
||||
message: "Organization or a Team with same slug already exists",
|
||||
statusCode: 400,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user