feat: Zapier For Teams (#9851)
Co-authored-by: alannnc <[email protected]> Co-authored-by: zomars <[email protected]>
This commit is contained in:
18 files changed
+247
-62
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
export async function checkPermissions(args: {
|
||||
userId: number;
|
||||
teamId?: number;
|
||||
role: Prisma.MembershipWhereInput["role"];
|
||||
}) {
|
||||
const { teamId, userId, role } = args;
|
||||
if (!teamId) return;
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId,
|
||||
role,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!team) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
}
|
||||
@@ -2,8 +2,10 @@ import { v4 } from "uuid";
|
||||
|
||||
import { generateUniqueAPIKey } from "@calcom/ee/api-keys/lib/apiKeys";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
import { checkPermissions } from "./_auth-middleware";
|
||||
import type { TCreateInputSchema } from "./create.schema";
|
||||
|
||||
type CreateHandlerOptions = {
|
||||
@@ -17,12 +19,17 @@ export const createHandler = async ({ ctx, input }: CreateHandlerOptions) => {
|
||||
const [hashedApiKey, apiKey] = generateUniqueAPIKey();
|
||||
|
||||
// Here we snap never expires before deleting it so it's not passed to prisma create call.
|
||||
const { neverExpires, ...rest } = input;
|
||||
const { neverExpires, teamId, ...rest } = input;
|
||||
const userId = ctx.user.id;
|
||||
|
||||
/** Only admin or owner can create apiKeys of team (if teamId is passed) */
|
||||
await checkPermissions({ userId, teamId, role: { in: [MembershipRole.OWNER, MembershipRole.ADMIN] } });
|
||||
|
||||
await prisma.apiKey.create({
|
||||
data: {
|
||||
id: v4(),
|
||||
userId: ctx.user.id,
|
||||
teamId,
|
||||
...rest,
|
||||
// And here we pass a null to expiresAt if never expires is true. otherwise just pass expiresAt from input
|
||||
expiresAt: neverExpires ? null : rest.expiresAt,
|
||||
|
||||
@@ -5,6 +5,7 @@ export const ZCreateInputSchema = z.object({
|
||||
expiresAt: z.date().optional().nullable(),
|
||||
neverExpires: z.boolean().optional(),
|
||||
appId: z.string().optional().nullable(),
|
||||
teamId: z.number().optional(),
|
||||
});
|
||||
|
||||
export type TCreateInputSchema = z.infer<typeof ZCreateInputSchema>;
|
||||
@@ -1,6 +1,8 @@
|
||||
import prisma from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
import { checkPermissions } from "./_auth-middleware";
|
||||
import type { TFindKeyOfTypeInputSchema } from "./findKeyOfType.schema";
|
||||
|
||||
type FindKeyOfTypeOptions = {
|
||||
@@ -11,16 +13,16 @@ type FindKeyOfTypeOptions = {
|
||||
};
|
||||
|
||||
export const findKeyOfTypeHandler = async ({ ctx, input }: FindKeyOfTypeOptions) => {
|
||||
return await prisma.apiKey.findFirst({
|
||||
const { teamId, appId } = input;
|
||||
const userId = ctx.user.id;
|
||||
/** Only admin or owner can create apiKeys of team (if teamId is passed) */
|
||||
await checkPermissions({ userId, teamId, role: { in: [MembershipRole.OWNER, MembershipRole.ADMIN] } });
|
||||
|
||||
return await prisma.apiKey.findMany({
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
{
|
||||
appId: input.appId,
|
||||
},
|
||||
],
|
||||
teamId,
|
||||
userId,
|
||||
appId,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod";
|
||||
|
||||
export const ZFindKeyOfTypeInputSchema = z.object({
|
||||
appId: z.string().optional(),
|
||||
teamId: z.number().optional(),
|
||||
});
|
||||
|
||||
export type TFindKeyOfTypeInputSchema = z.infer<typeof ZFindKeyOfTypeInputSchema>;
|
||||
@@ -22,6 +22,7 @@ import { ZUpdateMembershipInputSchema } from "./updateMembership.schema";
|
||||
type TeamsRouterHandlerCache = {
|
||||
get?: typeof import("./get.handler").getHandler;
|
||||
list?: typeof import("./list.handler").listHandler;
|
||||
listOwnedTeams?: typeof import("./listOwnedTeams.handler").listOwnedTeamsHandler;
|
||||
create?: typeof import("./create.handler").createHandler;
|
||||
update?: typeof import("./update.handler").updateHandler;
|
||||
delete?: typeof import("./delete.handler").deleteHandler;
|
||||
@@ -79,6 +80,23 @@ export const viewerTeamsRouter = router({
|
||||
ctx,
|
||||
});
|
||||
}),
|
||||
// Returns Teams I am a owner/admin of
|
||||
listOwnedTeams: authedProcedure.query(async ({ ctx }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOwnedTeams) {
|
||||
UNSTABLE_HANDLER_CACHE.listOwnedTeams = await import("./listOwnedTeams.handler").then(
|
||||
(mod) => mod.listOwnedTeamsHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.listOwnedTeams) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.listOwnedTeams({
|
||||
ctx,
|
||||
});
|
||||
}),
|
||||
|
||||
create: authedProcedure.input(ZCreateInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.create) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../trpc";
|
||||
|
||||
type ListOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
};
|
||||
|
||||
export const listOwnedTeamsHandler = async ({ ctx }: ListOptions) => {
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
id: ctx.user.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
teams: {
|
||||
where: {
|
||||
accepted: true,
|
||||
role: {
|
||||
in: [MembershipRole.OWNER, MembershipRole.ADMIN],
|
||||
},
|
||||
},
|
||||
select: {
|
||||
team: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return user?.teams
|
||||
?.filter((m) => {
|
||||
const metadata = teamMetadataSchema.parse(m.team.metadata);
|
||||
return !metadata?.isOrganization;
|
||||
})
|
||||
?.map(({ team }) => team);
|
||||
};
|
||||
Reference in new issue
Block a user