2022-04-16 02:58:34 +00:00
|
|
|
import { v4 } from "uuid";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import { generateUniqueAPIKey } from "@calcom/features/ee/api-keys/lib/apiKeys";
|
2022-04-16 02:58:34 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
import { router, authedProcedure } from "../../trpc";
|
2022-04-16 02:58:34 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
export const apiKeysRouter = router({
|
|
|
|
list: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
return await ctx.prisma.apiKey.findMany({
|
|
|
|
where: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
NOT: {
|
|
|
|
appId: "zapier",
|
2022-05-06 14:48:27 +00:00
|
|
|
},
|
2022-11-10 23:40:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
appId: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
orderBy: { createdAt: "desc" },
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
findKeyOfType: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
appId: z.string().optional().nullable(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2022-05-03 23:16:59 +00:00
|
|
|
return await ctx.prisma.apiKey.findFirst({
|
|
|
|
where: {
|
|
|
|
AND: [
|
|
|
|
{
|
|
|
|
userId: ctx.user.id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
appId: input.appId,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2022-04-16 02:58:34 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
create: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
note: z.string().optional().nullish(),
|
|
|
|
expiresAt: z.date().optional().nullable(),
|
|
|
|
neverExpires: z.boolean().optional(),
|
|
|
|
appId: z.string().optional().nullable(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-04-16 02:58:34 +00:00
|
|
|
const [hashedApiKey, apiKey] = generateUniqueAPIKey();
|
|
|
|
// Here we snap never expires before deleting it so it's not passed to prisma create call.
|
|
|
|
const neverExpires = input.neverExpires;
|
|
|
|
delete input.neverExpires;
|
|
|
|
await ctx.prisma.apiKey.create({
|
|
|
|
data: {
|
|
|
|
id: v4(),
|
|
|
|
userId: ctx.user.id,
|
|
|
|
...input,
|
|
|
|
// And here we pass a null to expiresAt if never expires is true. otherwise just pass expiresAt from input
|
|
|
|
expiresAt: neverExpires ? null : input.expiresAt,
|
|
|
|
hashedKey: hashedApiKey,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const prefixedApiKey = `${process.env.API_KEY_PREFIX ?? "cal_"}${apiKey}`;
|
|
|
|
return prefixedApiKey;
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
edit: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.string(),
|
|
|
|
note: z.string().optional().nullish(),
|
|
|
|
expiresAt: z.date().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-04-16 02:58:34 +00:00
|
|
|
const { id, ...data } = input;
|
|
|
|
const {
|
|
|
|
apiKeys: [updatedApiKey],
|
|
|
|
} = await ctx.prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: ctx.user.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
apiKeys: {
|
|
|
|
update: {
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
data,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
apiKeys: {
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return updatedApiKey;
|
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
delete: authedProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
id: z.string(),
|
|
|
|
eventTypeId: z.number().optional(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.mutation(async ({ ctx, input }) => {
|
2022-04-16 02:58:34 +00:00
|
|
|
const { id } = input;
|
2022-08-15 20:18:41 +00:00
|
|
|
|
|
|
|
const apiKeyToDelete = await ctx.prisma.apiKey.findFirst({
|
|
|
|
where: {
|
2022-08-17 17:38:21 +00:00
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
2022-08-15 20:18:41 +00:00
|
|
|
|
2022-04-16 02:58:34 +00:00
|
|
|
await ctx.prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: ctx.user.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
apiKeys: {
|
|
|
|
delete: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-08-15 20:18:41 +00:00
|
|
|
|
|
|
|
//remove all existing zapier webhooks, as we always have only one zapier API key and the running zaps won't work any more if this key is deleted
|
2022-08-17 17:38:21 +00:00
|
|
|
if (apiKeyToDelete && apiKeyToDelete.appId === "zapier") {
|
2022-08-15 20:18:41 +00:00
|
|
|
await ctx.prisma.webhook.deleteMany({
|
|
|
|
where: {
|
2023-02-16 20:22:14 +00:00
|
|
|
userId: ctx.user.id,
|
2022-08-17 17:38:21 +00:00
|
|
|
appId: "zapier",
|
|
|
|
},
|
|
|
|
});
|
2022-08-15 20:18:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 02:58:34 +00:00
|
|
|
return {
|
|
|
|
id,
|
|
|
|
};
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
|
|
|
});
|