* init members page for platform * add missing icon * add members tab to sidebar * use data from useMe query instead of useSession * return user profiles since platform users are stored there * small refactor * fixup * remove unused logs from console * fixup * resolve merge conflicts * add missing tabs for members and billing * update schema * fixup * memoise selection options for org and platform * update onSubmit handler * resolve merge conflicts * add members tab * improve team billing view when user not subscribed * update members tab * add function to find if user is admin of team * update invite members handler to handle platform user as well --------- Co-authored-by: Morgan <[email protected]>
78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
import { TeamBilling } from "@calcom/ee/billing/teams";
|
|
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
|
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
|
|
import { prisma } from "@calcom/prisma";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TrpcSessionUser } from "../../../trpc";
|
|
import type { TBulkUsersDelete } from "./bulkDeleteUsers.schema.";
|
|
|
|
type BulkDeleteUsersHandler = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TBulkUsersDelete;
|
|
};
|
|
|
|
export async function bulkDeleteUsersHandler({ ctx, input }: BulkDeleteUsersHandler) {
|
|
const currentUser = ctx.user;
|
|
const currentUserOrgId = currentUser.organizationId ?? currentUser.profiles[0].organizationId;
|
|
|
|
if (!currentUserOrgId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
// check if user is admin of organization
|
|
if (!(await isOrganisationAdmin(currentUser?.id, currentUserOrgId)))
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
// Loop over all users in input.userIds and remove all memberships for the organization including child teams
|
|
const deleteMany = prisma.membership.deleteMany({
|
|
where: {
|
|
userId: {
|
|
in: input.userIds,
|
|
},
|
|
team: {
|
|
OR: [
|
|
{
|
|
parentId: currentUser.organizationId,
|
|
},
|
|
{ id: currentUserOrgId },
|
|
],
|
|
},
|
|
},
|
|
});
|
|
|
|
const removeOrgrelation = prisma.user.updateMany({
|
|
where: {
|
|
id: {
|
|
in: input.userIds,
|
|
},
|
|
},
|
|
data: {
|
|
organizationId: null,
|
|
// Set username to null - to make sure there is no conflicts
|
|
username: null,
|
|
// Set completedOnboarding to false - to make sure the user has to complete onboarding again -> Setup a new username
|
|
completedOnboarding: false,
|
|
},
|
|
});
|
|
|
|
const removeProfiles = ProfileRepository.deleteMany({
|
|
userIds: input.userIds,
|
|
});
|
|
|
|
// We do this in a transaction to make sure that all memberships are removed before we remove the organization relation from the user
|
|
// We also do this to make sure that if one of the queries fail, the whole transaction fails
|
|
await prisma.$transaction([removeProfiles, deleteMany, removeOrgrelation]);
|
|
|
|
const teamBilling = await TeamBilling.findAndInit(currentUserOrgId);
|
|
await teamBilling.updateQuantity();
|
|
|
|
return {
|
|
success: true,
|
|
usersDeleted: input.userIds.length,
|
|
};
|
|
}
|
|
|
|
export default bulkDeleteUsersHandler;
|