* 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 <zomars@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
82 lines
2.0 KiB
TypeScript
82 lines
2.0 KiB
TypeScript
import { deleteDomain } from "@calcom/lib/domainManager/organization";
|
|
import logger from "@calcom/lib/logger";
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../trpc";
|
|
import type { TAdminDeleteInput } from "./adminDelete.schema";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["organizations/adminDelete"] });
|
|
type AdminDeleteOption = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TAdminDeleteInput;
|
|
};
|
|
|
|
export const adminDeleteHandler = async ({ input }: AdminDeleteOption) => {
|
|
const foundOrg = await prisma.team.findUnique({
|
|
where: {
|
|
id: input.orgId,
|
|
isOrganization: true,
|
|
},
|
|
include: {
|
|
members: {
|
|
select: {
|
|
user: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!foundOrg)
|
|
throw new TRPCError({
|
|
code: "FORBIDDEN",
|
|
message: "Organization not found",
|
|
});
|
|
|
|
if (foundOrg.slug) {
|
|
try {
|
|
await deleteDomain(foundOrg.slug);
|
|
} catch (e) {
|
|
log.error(`Failed to delete domain ${foundOrg.slug}. Do a manual deletion if needed`);
|
|
}
|
|
}
|
|
|
|
await renameUsersToAvoidUsernameConflicts(foundOrg.members.map((member) => member.user));
|
|
await prisma.team.delete({
|
|
where: {
|
|
id: input.orgId,
|
|
},
|
|
});
|
|
|
|
return {
|
|
ok: true,
|
|
message: `Organization ${foundOrg.name} deleted.`,
|
|
};
|
|
};
|
|
|
|
export default adminDeleteHandler;
|
|
|
|
async function renameUsersToAvoidUsernameConflicts(users: { id: number; username: string | null }[]) {
|
|
for (const user of users) {
|
|
let currentUsername = user.username;
|
|
|
|
if (!currentUsername) {
|
|
currentUsername = "no-username";
|
|
log.warn(`User ${user.id} has no username, defaulting to ${currentUsername}`);
|
|
}
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
id: user.id,
|
|
},
|
|
data: {
|
|
// user.id being auto-incremented, we can safely assume that the username will be unique
|
|
username: `${currentUsername}-${user.id}`,
|
|
},
|
|
});
|
|
}
|
|
}
|