feat: Org user table - bulk actions (#10504)
Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: Leo Giovanetti <[email protected]> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: zomars <[email protected]> Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Keith Williams <[email protected]> Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Syed Ali Shahbaz <[email protected]> Co-authored-by: gitstart-calcom <[email protected]> Co-authored-by: Shivam Kalra <[email protected]> Co-authored-by: cherish2003 <[email protected]> Co-authored-by: sean-brydon <[email protected]> Co-authored-by: rkreddy99 <[email protected]> Co-authored-by: varun thummar <Varun> Co-authored-by: Crowdin Bot <[email protected]> Co-authored-by: Pradumn Kumar <[email protected]> Co-authored-by: Richard Poelderl <[email protected]> Co-authored-by: mohammed hussam <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: Anik Dhabal Babu <[email protected]> Co-authored-by: nicktrn <[email protected]> Co-authored-by: sydwardrae <[email protected]> Co-authored-by: Janakiram Yellapu <[email protected]> Co-authored-by: GitStart-Cal.com <[email protected]> Co-authored-by: sajanlamsal <[email protected]> Co-authored-by: Cherish <[email protected]> Co-authored-by: Danila <[email protected]> Co-authored-by: Neel Patel <[email protected]> Co-authored-by: Rama Krishna Reddy <[email protected]> Co-authored-by: Varun Thummar <[email protected]> Co-authored-by: Bhargav <[email protected]> Co-authored-by: Pratik Kumar <[email protected]> Co-authored-by: Ritesh Patil <[email protected]>
This commit is contained in:
+27









sean-brydon
Udit Takkar
Hariom Balhara
Leo Giovanetti
Alex van Andel
CarinaWolli
zomars
Peer Richelsen
Joe Au-Yeung
Udit Takkar
Keith Williams
Peer Richelsen
Syed Ali Shahbaz
gitstart-calcom
Shivam Kalra
cherish2003
rkreddy99
varun thummar
Crowdin Bot
Pradumn Kumar
Richard Poelderl
mohammed hussam
Carina Wollendorfer
Anik Dhabal Babu
nicktrn
sydwardrae
Janakiram Yellapu
GitStart-Cal.com
sajanlamsal
Cherish
Danila
Neel Patel
Rama Krishna Reddy
Varun Thummar
Bhargav
Pratik Kumar
Ritesh Patil authored and
GitHub committed 2023-08-15 14:07:38 -07:00
1 parent
20c45b5d88
commit
10ffd9bacd
47 files changed
+1282
-336
No files matched your search
@@ -4,287 +4,123 @@ import authedProcedure, {
|
||||
authedAdminProcedure,
|
||||
authedOrgAdminProcedure,
|
||||
} from "../../../procedures/authedProcedure";
|
||||
import { router } from "../../../trpc";
|
||||
import { importHandler, router } from "../../../trpc";
|
||||
import { ZAddBulkTeams } from "./addBulkTeams.schema";
|
||||
import { ZAdminVerifyInput } from "./adminVerify.schema";
|
||||
import { ZBulkUsersDelete } from "./bulkDeleteUsers.schema.";
|
||||
import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZCreateTeamsSchema } from "./createTeams.schema";
|
||||
import { ZGetMembersInput } from "./getMembers.schema";
|
||||
import { ZGetOtherTeamInputSchema } from "./getOtherTeam.handler";
|
||||
import { ZGetUserInput } from "./getUser.schema";
|
||||
import { ZListMembersSchema } from "./listMembers.schema";
|
||||
import { ZListOtherTeamMembersSchema } from "./listOtherTeamMembers.handler";
|
||||
import { ZSetPasswordSchema } from "./setPassword.schema";
|
||||
import { ZUpdateInputSchema } from "./update.schema";
|
||||
import { ZUpdateUserInputSchema } from "./updateUser.schema";
|
||||
|
||||
type OrganizationsRouterHandlerCache = {
|
||||
create?: typeof import("./create.handler").createHandler;
|
||||
listCurrent?: typeof import("./list.handler").listHandler;
|
||||
publish?: typeof import("./publish.handler").publishHandler;
|
||||
checkIfOrgNeedsUpgrade?: typeof import("./checkIfOrgNeedsUpgrade.handler").checkIfOrgNeedsUpgradeHandler;
|
||||
update?: typeof import("./update.handler").updateHandler;
|
||||
verifyCode?: typeof import("./verifyCode.handler").verifyCodeHandler;
|
||||
createTeams?: typeof import("./createTeams.handler").createTeamsHandler;
|
||||
setPassword?: typeof import("./setPassword.handler").setPasswordHandler;
|
||||
adminGetUnverified?: typeof import("./adminGetUnverified.handler").adminGetUnverifiedHandler;
|
||||
adminVerify?: typeof import("./adminVerify.handler").adminVerifyHandler;
|
||||
listMembers?: typeof import("./listMembers.handler").listMembersHandler;
|
||||
getBrand?: typeof import("./getBrand.handler").getBrandHandler;
|
||||
getMembers?: typeof import("./getMembers.handler").getMembersHandler;
|
||||
listOtherTeams?: typeof import("./listOtherTeams.handler").listOtherTeamHandler;
|
||||
getOtherTeam?: typeof import("./getOtherTeam.handler").getOtherTeamHandler;
|
||||
listOtherTeamMembers?: typeof import("./listOtherTeamMembers.handler").listOtherTeamMembers;
|
||||
};
|
||||
const NAMESPACE = "organizations";
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: OrganizationsRouterHandlerCache = {};
|
||||
const namespaced = (s: string) => `${NAMESPACE}.${s}`;
|
||||
|
||||
export const viewerOrganizationsRouter = router({
|
||||
create: authedProcedure.input(ZCreateInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.create) {
|
||||
UNSTABLE_HANDLER_CACHE.create = await import("./create.handler").then((mod) => mod.createHandler);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.create) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.create({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
create: authedProcedure.input(ZCreateInputSchema).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("create"), () => import("./create.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
update: authedProcedure.input(ZUpdateInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.update) {
|
||||
UNSTABLE_HANDLER_CACHE.update = await import("./update.handler").then((mod) => mod.updateHandler);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.update) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.update({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
update: authedProcedure.input(ZUpdateInputSchema).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("update"), () => import("./update.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
verifyCode: authedProcedure.input(ZVerifyCodeInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.verifyCode) {
|
||||
UNSTABLE_HANDLER_CACHE.verifyCode = await import("./verifyCode.handler").then(
|
||||
(mod) => mod.verifyCodeHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.verifyCode) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.verifyCode({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
verifyCode: authedProcedure.input(ZVerifyCodeInputSchema).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("verifyCode"), () => import("./verifyCode.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
createTeams: authedProcedure.input(ZCreateTeamsSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.createTeams) {
|
||||
UNSTABLE_HANDLER_CACHE.createTeams = await import("./createTeams.handler").then(
|
||||
(mod) => mod.createTeamsHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.createTeams) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.createTeams({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
createTeams: authedProcedure.input(ZCreateTeamsSchema).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("createTeams"), () => import("./createTeams.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
listCurrent: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listCurrent) {
|
||||
UNSTABLE_HANDLER_CACHE.listCurrent = await import("./list.handler").then((mod) => mod.listHandler);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listCurrent) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listCurrent({
|
||||
ctx,
|
||||
});
|
||||
listCurrent: authedProcedure.query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("listCurrent"), () => import("./list.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
checkIfOrgNeedsUpgrade: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.checkIfOrgNeedsUpgrade) {
|
||||
UNSTABLE_HANDLER_CACHE.checkIfOrgNeedsUpgrade = await import("./checkIfOrgNeedsUpgrade.handler").then(
|
||||
(mod) => mod.checkIfOrgNeedsUpgradeHandler
|
||||
);
|
||||
}
|
||||
|
||||
if (!UNSTABLE_HANDLER_CACHE.checkIfOrgNeedsUpgrade) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.checkIfOrgNeedsUpgrade({ ctx });
|
||||
checkIfOrgNeedsUpgrade: authedProcedure.query(async (opts) => {
|
||||
const handler = await importHandler(
|
||||
namespaced("checkIfOrgNeedsUpgrade"),
|
||||
() => import("./checkIfOrgNeedsUpgrade.handler")
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
publish: authedProcedure.mutation(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.publish) {
|
||||
UNSTABLE_HANDLER_CACHE.publish = await import("./publish.handler").then((mod) => mod.publishHandler);
|
||||
}
|
||||
|
||||
if (!UNSTABLE_HANDLER_CACHE.publish) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.publish({ ctx });
|
||||
publish: authedProcedure.mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("publish"), () => import("./publish.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
setPassword: authedProcedure.input(ZSetPasswordSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.setPassword) {
|
||||
UNSTABLE_HANDLER_CACHE.setPassword = await import("./setPassword.handler").then(
|
||||
(mod) => mod.setPasswordHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.setPassword) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.setPassword({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
setPassword: authedProcedure.input(ZSetPasswordSchema).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("setPassword"), () => import("./setPassword.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
getMembers: authedProcedure.input(ZGetMembersInput).query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getMembers) {
|
||||
UNSTABLE_HANDLER_CACHE.getMembers = await import("./getMembers.handler").then(
|
||||
(mod) => mod.getMembersHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.getMembers) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.getMembers({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
getMembers: authedProcedure.input(ZGetMembersInput).query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("getMembers"), () => import("./getMembers.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
adminGetUnverified: authedAdminProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.adminGetUnverified) {
|
||||
UNSTABLE_HANDLER_CACHE.adminGetUnverified = await import("./adminGetUnverified.handler").then(
|
||||
(mod) => mod.adminGetUnverifiedHandler
|
||||
);
|
||||
}
|
||||
if (!UNSTABLE_HANDLER_CACHE.adminGetUnverified) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.adminGetUnverified({
|
||||
ctx,
|
||||
});
|
||||
adminGetUnverified: authedAdminProcedure.query(async (opts) => {
|
||||
const handler = await importHandler(
|
||||
namespaced("adminGetUnverified"),
|
||||
() => import("./adminGetUnverified.handler")
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
adminVerify: authedAdminProcedure.input(ZAdminVerifyInput).mutation(async ({ input, ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.adminVerify) {
|
||||
UNSTABLE_HANDLER_CACHE.adminVerify = await import("./adminVerify.handler").then(
|
||||
(mod) => mod.adminVerifyHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.adminVerify) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.adminVerify({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
adminVerify: authedAdminProcedure.input(ZAdminVerifyInput).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("adminVerify"), () => import("./adminVerify.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
listMembers: authedProcedure.input(ZListMembersSchema).query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listMembers) {
|
||||
UNSTABLE_HANDLER_CACHE.listMembers = await import("./listMembers.handler").then(
|
||||
(mod) => mod.listMembersHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listMembers) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listMembers({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
listMembers: authedProcedure.input(ZListMembersSchema).query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("listMembers"), () => import("./listMembers.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
getBrand: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getBrand) {
|
||||
UNSTABLE_HANDLER_CACHE.getBrand = await import("./getBrand.handler").then((mod) => mod.getBrandHandler);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.getBrand) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.getBrand({
|
||||
ctx,
|
||||
});
|
||||
getBrand: authedProcedure.query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("getBrand"), () => import("./getBrand.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
listOtherTeams: authedOrgAdminProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeams) {
|
||||
UNSTABLE_HANDLER_CACHE.listOtherTeams = await import("./listOtherTeams.handler").then(
|
||||
(mod) => mod.listOtherTeamHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeams) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listOtherTeams({
|
||||
ctx,
|
||||
});
|
||||
getUser: authedProcedure.input(ZGetUserInput).query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("getUser"), () => import("./getUser.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
getOtherTeam: authedOrgAdminProcedure.input(ZGetOtherTeamInputSchema).query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.getOtherTeam) {
|
||||
UNSTABLE_HANDLER_CACHE.getOtherTeam = await import("./getOtherTeam.handler").then(
|
||||
(mod) => mod.getOtherTeamHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.getOtherTeam) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.getOtherTeam({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
updateUser: authedProcedure.input(ZUpdateUserInputSchema).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("updateUser"), () => import("./updateUser.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
getTeams: authedProcedure.query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("getTeams"), () => import("./getTeams.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
bulkAddToTeams: authedProcedure.input(ZAddBulkTeams).mutation(async (opts) => {
|
||||
const handler = await importHandler(namespaced("addBulkTeams"), () => import("./addBulkTeams.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
bulkDeleteUsers: authedProcedure.input(ZBulkUsersDelete).mutation(async (opts) => {
|
||||
const handler = await importHandler(
|
||||
namespaced("bulkDeleteUsers"),
|
||||
() => import("./bulkDeleteUsers.handler")
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
listOtherTeamMembers: authedOrgAdminProcedure.input(ZListOtherTeamMembersSchema).query(async (opts) => {
|
||||
const handler = await importHandler(
|
||||
namespaced("listOtherTeamMembers"),
|
||||
() => import("./listOtherTeamMembers.handler")
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
getOtherTeam: authedOrgAdminProcedure.input(ZGetOtherTeamInputSchema).query(async (opts) => {
|
||||
const handler = await importHandler(namespaced("getOtherTeam"), () => import("./getOtherTeam.handler"));
|
||||
return handler(opts);
|
||||
}),
|
||||
listOtherTeams: authedOrgAdminProcedure.query(async (opts) => {
|
||||
const handler = await importHandler(
|
||||
namespaced("listOtherTeams"),
|
||||
() => import("./listOtherTeams.handler")
|
||||
);
|
||||
return handler(opts);
|
||||
}),
|
||||
listOtherTeamMembers: authedOrgAdminProcedure
|
||||
.input(ZListOtherTeamMembersSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeamMembers) {
|
||||
UNSTABLE_HANDLER_CACHE.listOtherTeamMembers = await import("./listOtherTeamMembers.handler").then(
|
||||
(mod) => mod.listOtherTeamMembers
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOtherTeamMembers) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listOtherTeamMembers({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
import type { TAddBulkTeams } from "./addBulkTeams.schema";
|
||||
|
||||
type AddBulkTeamsHandler = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TAddBulkTeams;
|
||||
};
|
||||
|
||||
export async function addBulkTeamsHandler({ ctx, input }: AddBulkTeamsHandler) {
|
||||
const currentUser = ctx.user;
|
||||
|
||||
if (!currentUser.organizationId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// check if user is admin of organization
|
||||
if (!(await isOrganisationAdmin(currentUser?.id, currentUser.organizationId)))
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// Loop over all users and check if they are already in the organization
|
||||
const usersInOrganization = await prisma.membership.findMany({
|
||||
where: {
|
||||
teamId: currentUser.organizationId,
|
||||
user: {
|
||||
id: {
|
||||
in: input.userIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
distinct: ["userId"],
|
||||
});
|
||||
|
||||
// Throw error if any of the users are not in the organization. They should be invited to the organization via the onboaring flow first.
|
||||
if (usersInOrganization.length !== input.userIds.length) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "One or more users are not in the organization",
|
||||
});
|
||||
}
|
||||
|
||||
// loop over all users and check if they are already in team they are being invited to
|
||||
const usersInTeams = await prisma.membership.findMany({
|
||||
where: {
|
||||
userId: {
|
||||
in: input.userIds,
|
||||
},
|
||||
teamId: {
|
||||
in: input.teamIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Filter out users who are already in teams they are being invited to
|
||||
const filteredUserIds = input.userIds.filter((userId) => {
|
||||
return !usersInTeams.some((membership) => membership.userId === userId);
|
||||
});
|
||||
|
||||
// TODO: might need to come back to this is people are doing ALOT of invites with bulk actions.
|
||||
// Loop over all users and add them to all teams in the array
|
||||
const membershipData = filteredUserIds.flatMap((userId) =>
|
||||
input.teamIds.map((teamId) => {
|
||||
const userMembership = usersInOrganization.find((membership) => membership.userId === userId);
|
||||
const accepted = userMembership && userMembership.accepted;
|
||||
return {
|
||||
userId,
|
||||
teamId,
|
||||
role: MembershipRole.MEMBER,
|
||||
accepted: accepted || false,
|
||||
} as Prisma.MembershipCreateManyInput;
|
||||
})
|
||||
);
|
||||
|
||||
await prisma.membership.createMany({
|
||||
data: membershipData,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
invitedTotalUsers: input.userIds.length,
|
||||
};
|
||||
}
|
||||
|
||||
export default addBulkTeamsHandler;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZAddBulkTeams = z.object({
|
||||
userIds: z.array(z.number()),
|
||||
teamIds: z.array(z.number()),
|
||||
});
|
||||
|
||||
export type TAddBulkTeams = z.infer<typeof ZAddBulkTeams>;
|
||||
@@ -49,3 +49,5 @@ export const adminGetUnverifiedHandler = async ({ ctx }: AdminGetUnverifiedOptio
|
||||
|
||||
return unVerifiedTeams;
|
||||
};
|
||||
|
||||
export default adminGetUnverifiedHandler;
|
||||
@@ -114,3 +114,5 @@ export const adminVerifyHandler = async ({ input }: AdminVerifyOptions) => {
|
||||
message: `Verified Organization - Auto accepted all members ending in ${acceptedEmailDomain}`,
|
||||
};
|
||||
};
|
||||
|
||||
export default adminVerifyHandler;
|
||||
@@ -0,0 +1,67 @@
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
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;
|
||||
|
||||
if (!currentUser.organizationId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// check if user is admin of organization
|
||||
if (!(await isOrganisationAdmin(currentUser?.id, currentUser.organizationId)))
|
||||
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: currentUser.organizationId },
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const removeOrgrelation = prisma.user.updateMany({
|
||||
where: {
|
||||
id: {
|
||||
in: input.userIds,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
// Remove organization relation
|
||||
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,
|
||||
},
|
||||
});
|
||||
// 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([deleteMany, removeOrgrelation]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
usersDeleted: input.userIds.length,
|
||||
};
|
||||
}
|
||||
|
||||
export default bulkDeleteUsersHandler;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZBulkUsersDelete = z.object({
|
||||
userIds: z.array(z.number()),
|
||||
});
|
||||
|
||||
export type TBulkUsersDelete = z.infer<typeof ZBulkUsersDelete>;
|
||||
@@ -40,3 +40,5 @@ export async function checkIfOrgNeedsUpgradeHandler({ ctx }: GetUpgradeableOptio
|
||||
|
||||
return teams;
|
||||
}
|
||||
|
||||
export default checkIfOrgNeedsUpgradeHandler;
|
||||
@@ -160,3 +160,5 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
|
||||
|
||||
return { checked: true };
|
||||
};
|
||||
|
||||
export default createHandler;
|
||||
@@ -135,3 +135,5 @@ class NoOrganizationSlugError extends TRPCError {
|
||||
super({ code: "BAD_REQUEST", message: "no_organization_slug" });
|
||||
}
|
||||
}
|
||||
|
||||
export default createTeamsHandler;
|
||||
@@ -14,3 +14,5 @@ export const getBrandHandler = async ({ ctx }: VerifyCodeOptions) => {
|
||||
|
||||
return await getBrand(user.organizationId);
|
||||
};
|
||||
|
||||
export default getBrandHandler;
|
||||
@@ -52,3 +52,5 @@ export const getMembersHandler = async ({ input, ctx }: CreateOptions) => {
|
||||
});
|
||||
return teamQuery?.members || [];
|
||||
};
|
||||
|
||||
export default getMembersHandler;
|
||||
@@ -52,3 +52,5 @@ export const getOtherTeamHandler = async ({ input }: GetOptions) => {
|
||||
safeBio: markdownToSafeHTML(team.bio),
|
||||
};
|
||||
};
|
||||
|
||||
export default getOtherTeamHandler;
|
||||
@@ -0,0 +1,36 @@
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
|
||||
type GetTeamsHandler = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
};
|
||||
|
||||
export async function getTeamsHandler({ ctx }: GetTeamsHandler) {
|
||||
const currentUser = ctx.user;
|
||||
|
||||
if (!currentUser.organizationId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// check if user is admin of organization
|
||||
if (!(await isOrganisationAdmin(currentUser?.id, currentUser.organizationId)))
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
const allOrgTeams = await prisma.team.findMany({
|
||||
where: {
|
||||
parentId: currentUser.organizationId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
|
||||
return allOrgTeams;
|
||||
}
|
||||
|
||||
export default getTeamsHandler;
|
||||
@@ -0,0 +1,89 @@
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
import type { TGetUserInput } from "./getUser.schema";
|
||||
|
||||
type AdminVerifyOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TGetUserInput;
|
||||
};
|
||||
|
||||
export async function getUserHandler({ input, ctx }: AdminVerifyOptions) {
|
||||
const currentUser = ctx.user;
|
||||
|
||||
if (!currentUser.organizationId) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// check if user is admin of organization
|
||||
if (!(await isOrganisationAdmin(currentUser?.id, currentUser.organizationId)))
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// get requested user from database and ensure they are in the same organization
|
||||
const [requestedUser, membership, teams] = await prisma.$transaction([
|
||||
prisma.user.findFirst({
|
||||
where: { id: input.userId, organizationId: currentUser.organizationId },
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
username: true,
|
||||
name: true,
|
||||
bio: true,
|
||||
timeZone: true,
|
||||
schedules: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
// Query on accepted as we don't want the user to be able to get this much info on a user that hasn't accepted the invite
|
||||
prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: input.userId,
|
||||
teamId: currentUser.organizationId,
|
||||
accepted: true,
|
||||
},
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
}),
|
||||
prisma.membership.findMany({
|
||||
where: {
|
||||
userId: input.userId,
|
||||
team: {
|
||||
parentId: currentUser.organizationId,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
team: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
accepted: true,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!requestedUser || !membership)
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "user_not_exist_or_not_in_org" });
|
||||
|
||||
const foundUser = {
|
||||
...requestedUser,
|
||||
teams: teams.map((team) => ({
|
||||
...team.team,
|
||||
accepted: team.accepted,
|
||||
})),
|
||||
role: membership.role,
|
||||
};
|
||||
|
||||
return foundUser;
|
||||
}
|
||||
|
||||
export default getUserHandler;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZGetUserInput = z.object({
|
||||
userId: z.number().optional(),
|
||||
});
|
||||
|
||||
export type TGetUserInput = z.infer<typeof ZGetUserInput>;
|
||||
@@ -40,3 +40,5 @@ export const listHandler = async ({ ctx }: ListHandlerInput) => {
|
||||
metadata,
|
||||
};
|
||||
};
|
||||
|
||||
export default listHandler;
|
||||
@@ -102,3 +102,5 @@ export const listMembersHandler = async ({ ctx, input }: GetOptions) => {
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default listMembersHandler;
|
||||
@@ -79,3 +79,5 @@ export const listOtherTeamMembers = async ({ ctx, input }: ListOptions) => {
|
||||
|
||||
return members;
|
||||
};
|
||||
|
||||
export default listOtherTeamMembers;
|
||||
@@ -41,3 +41,5 @@ export const listOtherTeamHandler = async ({ ctx }: ListOptions) => {
|
||||
...team,
|
||||
}));
|
||||
};
|
||||
|
||||
export default listOtherTeamHandler;
|
||||
@@ -82,3 +82,5 @@ export const publishHandler = async ({ ctx }: PublishOptions) => {
|
||||
message: "Team published successfully",
|
||||
};
|
||||
};
|
||||
|
||||
export default publishHandler;
|
||||
@@ -55,3 +55,5 @@ export const setPasswordHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
return { update: true };
|
||||
};
|
||||
|
||||
export default setPasswordHandler;
|
||||
@@ -100,3 +100,5 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
return { update: true, userId: ctx.user.id };
|
||||
};
|
||||
|
||||
export default updateHandler;
|
||||
@@ -0,0 +1,69 @@
|
||||
import { isOrganisationAdmin } from "@calcom/lib/server/queries/organisations";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TUpdateUserInputSchema } from "./updateUser.schema";
|
||||
|
||||
type UpdateUserOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TUpdateUserInputSchema;
|
||||
};
|
||||
|
||||
export const updateUserHandler = async ({ ctx, input }: UpdateUserOptions) => {
|
||||
const { user } = ctx;
|
||||
const { id: userId, organizationId } = user;
|
||||
if (!organizationId)
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "You must be a memeber of an organizaiton" });
|
||||
|
||||
if (!(await isOrganisationAdmin(userId, organizationId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
// Is requested user a member of the organization?
|
||||
const requestedMember = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: input.userId,
|
||||
teamId: organizationId,
|
||||
accepted: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!requestedMember)
|
||||
throw new TRPCError({ code: "UNAUTHORIZED", message: "User does not belong to your organization" });
|
||||
|
||||
// Update user
|
||||
await prisma.$transaction([
|
||||
prisma.user.update({
|
||||
where: {
|
||||
id: input.userId,
|
||||
},
|
||||
data: {
|
||||
bio: input.bio,
|
||||
email: input.email,
|
||||
name: input.name,
|
||||
timeZone: input.timeZone,
|
||||
},
|
||||
}),
|
||||
prisma.membership.update({
|
||||
where: {
|
||||
userId_teamId: {
|
||||
userId: input.userId,
|
||||
teamId: organizationId,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
role: input.role,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
// TODO: audit log this
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
};
|
||||
|
||||
export default updateUserHandler;
|
||||
@@ -0,0 +1,12 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZUpdateUserInputSchema = z.object({
|
||||
userId: z.number(),
|
||||
bio: z.string().optional(),
|
||||
name: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
role: z.enum(["ADMIN", "MEMBER"]),
|
||||
timeZone: z.string(),
|
||||
});
|
||||
|
||||
export type TUpdateUserInputSchema = z.infer<typeof ZUpdateUserInputSchema>;
|
||||
@@ -38,3 +38,5 @@ export const verifyCodeHandler = async ({ ctx, input }: VerifyCodeOptions) => {
|
||||
|
||||
return isValidToken;
|
||||
};
|
||||
|
||||
export default verifyCodeHandler;
|
||||
@@ -15,3 +15,41 @@ export const middleware = tRPCContext.middleware;
|
||||
export const procedure = tRPCContext.procedure;
|
||||
|
||||
export type TrpcSessionUser = UserFromSession;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
const UNSTABLE_HANDLER_CACHE: Record<string, Function> = {};
|
||||
|
||||
/**
|
||||
* This function will import the module defined in importer just once and then cache the default export of that module.
|
||||
*
|
||||
* It gives you the default export of the module.
|
||||
*
|
||||
* **Note: It is your job to ensure that the name provided is unique across all routes.**
|
||||
* @example
|
||||
* ```ts
|
||||
const handler = await importHandler("myUniqueNameSpace", () => import("./getUser.handler"));
|
||||
return handler({ ctx, input });
|
||||
* ```
|
||||
*/
|
||||
export const importHandler = async <
|
||||
T extends {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
default: Function;
|
||||
}
|
||||
>(
|
||||
/**
|
||||
* The name of the handler in cache. It has to be unique across all routes
|
||||
*/
|
||||
name: string,
|
||||
importer: () => Promise<T>
|
||||
) => {
|
||||
const nameInCache = name as keyof typeof UNSTABLE_HANDLER_CACHE;
|
||||
|
||||
if (!UNSTABLE_HANDLER_CACHE[nameInCache]) {
|
||||
const importedModule = await importer();
|
||||
UNSTABLE_HANDLER_CACHE[nameInCache] = importedModule.default;
|
||||
return importedModule.default as T["default"];
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE[nameInCache] as unknown as T["default"];
|
||||
};
|
||||
Reference in new issue
Block a user