2022-05-03 23:16:59 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
2021-10-28 22:52:39 +00:00
|
|
|
import { v4 } from "uuid";
|
2021-10-25 16:15:52 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-03-16 23:36:43 +00:00
|
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { getTranslation } from "@calcom/lib/server/i18n";
|
|
|
|
import { WEBHOOK_TRIGGER_EVENTS } from "@calcom/lib/webhooks/constants";
|
|
|
|
import sendPayload from "@calcom/lib/webhooks/sendPayload";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2022-05-26 13:52:25 +00:00
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
import { createProtectedRouter } from "../../createRouter";
|
|
|
|
|
2022-05-26 13:52:25 +00:00
|
|
|
// Common data for all endpoints under webhook
|
|
|
|
const webhookIdAndEventTypeIdSchema = z.object({
|
|
|
|
// Webhook ID
|
|
|
|
id: z.string().optional(),
|
|
|
|
// Event type ID
|
|
|
|
eventTypeId: z.number().optional(),
|
|
|
|
});
|
2021-10-25 16:15:52 +00:00
|
|
|
|
2021-10-28 22:52:39 +00:00
|
|
|
export const webhookRouter = createProtectedRouter()
|
2022-05-26 13:52:25 +00:00
|
|
|
.middleware(async ({ ctx, rawInput, next }) => {
|
|
|
|
// Endpoints that just read the logged in user's data - like 'list' don't necessary have any input
|
|
|
|
if (!rawInput) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const webhookIdAndEventTypeId = webhookIdAndEventTypeIdSchema.safeParse(rawInput);
|
|
|
|
if (!webhookIdAndEventTypeId.success) {
|
|
|
|
throw new TRPCError({ code: "PARSE_ERROR" });
|
|
|
|
}
|
|
|
|
const { eventTypeId, id } = webhookIdAndEventTypeId.data;
|
|
|
|
|
|
|
|
// A webhook is either linked to Event Type or to a user.
|
|
|
|
if (eventTypeId) {
|
|
|
|
const team = await ctx.prisma.team.findFirst({
|
|
|
|
where: {
|
|
|
|
eventTypes: {
|
|
|
|
some: {
|
|
|
|
id: eventTypeId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
members: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Team should be available and the user should be a member of the team
|
|
|
|
if (!team?.members.some((membership) => membership.userId === ctx.user.id)) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (id) {
|
|
|
|
const authorizedHook = await ctx.prisma.webhook.findFirst({
|
|
|
|
where: {
|
|
|
|
id: id,
|
|
|
|
userId: ctx.user.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!authorizedHook) {
|
|
|
|
throw new TRPCError({
|
|
|
|
code: "UNAUTHORIZED",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
})
|
2021-10-28 22:52:39 +00:00
|
|
|
.query("list", {
|
2022-03-02 16:24:57 +00:00
|
|
|
input: z
|
|
|
|
.object({
|
|
|
|
eventTypeId: z.number().optional(),
|
2022-07-20 18:30:57 +00:00
|
|
|
appId: z.string().optional(),
|
2022-03-02 16:24:57 +00:00
|
|
|
})
|
|
|
|
.optional(),
|
|
|
|
async resolve({ ctx, input }) {
|
2022-05-18 16:05:25 +00:00
|
|
|
const where: Prisma.WebhookWhereInput = {
|
2022-07-20 18:30:57 +00:00
|
|
|
/* Don't mixup zapier webhooks with normal ones */
|
|
|
|
AND: [{ appId: !input?.appId ? null : input.appId }],
|
2022-05-03 23:16:59 +00:00
|
|
|
};
|
|
|
|
if (Array.isArray(where.AND)) {
|
|
|
|
if (input?.eventTypeId) {
|
|
|
|
where.AND?.push({ eventTypeId: input.eventTypeId });
|
|
|
|
} else {
|
|
|
|
where.AND?.push({ userId: ctx.user.id });
|
|
|
|
}
|
2022-03-02 16:24:57 +00:00
|
|
|
}
|
2022-07-20 18:30:57 +00:00
|
|
|
|
2021-10-28 22:52:39 +00:00
|
|
|
return await ctx.prisma.webhook.findMany({
|
2022-05-03 23:16:59 +00:00
|
|
|
where,
|
2021-10-28 22:52:39 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
2022-09-12 19:07:52 +00:00
|
|
|
.query("get", {
|
|
|
|
input: z.object({
|
|
|
|
webhookId: z.string().optional(),
|
|
|
|
}),
|
|
|
|
resolve({ ctx, input }) {
|
|
|
|
return ctx.prisma.webhook.findUniqueOrThrow({
|
|
|
|
where: {
|
|
|
|
id: input.webhookId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
subscriberUrl: true,
|
|
|
|
payloadTemplate: true,
|
|
|
|
active: true,
|
|
|
|
eventTriggers: true,
|
|
|
|
secret: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
2021-10-28 22:52:39 +00:00
|
|
|
.mutation("create", {
|
|
|
|
input: z.object({
|
|
|
|
subscriberUrl: z.string().url(),
|
|
|
|
eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array(),
|
|
|
|
active: z.boolean(),
|
2022-02-18 17:49:39 +00:00
|
|
|
payloadTemplate: z.string().nullable(),
|
2022-03-02 16:24:57 +00:00
|
|
|
eventTypeId: z.number().optional(),
|
2022-05-03 23:16:59 +00:00
|
|
|
appId: z.string().optional().nullable(),
|
2022-06-16 16:21:48 +00:00
|
|
|
secret: z.string().optional().nullable(),
|
2021-10-28 22:52:39 +00:00
|
|
|
}),
|
2022-05-26 13:52:25 +00:00
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
if (input.eventTypeId) {
|
|
|
|
return await ctx.prisma.webhook.create({
|
|
|
|
data: {
|
|
|
|
id: v4(),
|
|
|
|
...input,
|
|
|
|
},
|
2022-03-02 16:24:57 +00:00
|
|
|
});
|
|
|
|
}
|
2022-07-20 18:30:57 +00:00
|
|
|
|
2022-05-26 13:52:25 +00:00
|
|
|
return await ctx.prisma.webhook.create({
|
|
|
|
data: {
|
|
|
|
id: v4(),
|
|
|
|
userId: ctx.user.id,
|
|
|
|
...input,
|
|
|
|
},
|
2021-10-28 22:52:39 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.mutation("edit", {
|
|
|
|
input: z.object({
|
|
|
|
id: z.string(),
|
|
|
|
subscriberUrl: z.string().url().optional(),
|
|
|
|
eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array().optional(),
|
|
|
|
active: z.boolean().optional(),
|
2021-11-22 11:37:07 +00:00
|
|
|
payloadTemplate: z.string().nullable(),
|
2022-03-02 16:24:57 +00:00
|
|
|
eventTypeId: z.number().optional(),
|
2022-05-03 23:16:59 +00:00
|
|
|
appId: z.string().optional().nullable(),
|
2022-06-16 16:21:48 +00:00
|
|
|
secret: z.string().optional().nullable(),
|
2021-10-28 22:52:39 +00:00
|
|
|
}),
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
const { id, ...data } = input;
|
2022-03-02 16:24:57 +00:00
|
|
|
const webhook = input.eventTypeId
|
|
|
|
? await ctx.prisma.webhook.findFirst({
|
|
|
|
where: {
|
|
|
|
eventTypeId: input.eventTypeId,
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
: await ctx.prisma.webhook.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
});
|
2021-10-28 22:52:39 +00:00
|
|
|
if (!webhook) {
|
|
|
|
// user does not own this webhook
|
2022-03-02 16:24:57 +00:00
|
|
|
// team event doesn't own this webhook
|
2021-10-28 22:52:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return await ctx.prisma.webhook.update({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.mutation("delete", {
|
|
|
|
input: z.object({
|
|
|
|
id: z.string(),
|
2022-03-02 16:24:57 +00:00
|
|
|
eventTypeId: z.number().optional(),
|
2021-10-28 22:52:39 +00:00
|
|
|
}),
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
const { id } = input;
|
2022-03-02 16:24:57 +00:00
|
|
|
input.eventTypeId
|
|
|
|
? await ctx.prisma.eventType.update({
|
|
|
|
where: {
|
|
|
|
id: input.eventTypeId,
|
2022-03-02 15:30:13 +00:00
|
|
|
},
|
2022-03-02 16:24:57 +00:00
|
|
|
data: {
|
|
|
|
webhooks: {
|
|
|
|
delete: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
: await ctx.prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: ctx.user.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
webhooks: {
|
|
|
|
delete: {
|
|
|
|
id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-10-25 16:15:52 +00:00
|
|
|
return {
|
2021-10-28 22:52:39 +00:00
|
|
|
id,
|
2021-10-25 16:15:52 +00:00
|
|
|
};
|
2021-10-28 22:52:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.mutation("testTrigger", {
|
|
|
|
input: z.object({
|
|
|
|
url: z.string().url(),
|
|
|
|
type: z.string(),
|
2021-11-22 11:37:07 +00:00
|
|
|
payloadTemplate: z.string().optional().nullable(),
|
2021-10-28 22:52:39 +00:00
|
|
|
}),
|
|
|
|
async resolve({ input }) {
|
2022-05-03 23:16:59 +00:00
|
|
|
const { url, type, payloadTemplate = null } = input;
|
2022-02-10 10:44:46 +00:00
|
|
|
const translation = await getTranslation("en", "common");
|
|
|
|
const language = {
|
|
|
|
locale: "en",
|
|
|
|
translate: translation,
|
|
|
|
};
|
2021-10-28 22:52:39 +00:00
|
|
|
|
2021-11-22 11:37:07 +00:00
|
|
|
const data = {
|
|
|
|
type: "Test",
|
|
|
|
title: "Test trigger event",
|
|
|
|
description: "",
|
|
|
|
startTime: new Date().toISOString(),
|
|
|
|
endTime: new Date().toISOString(),
|
|
|
|
attendees: [
|
|
|
|
{
|
|
|
|
email: "jdoe@example.com",
|
|
|
|
name: "John Doe",
|
|
|
|
timeZone: "Europe/London",
|
2022-02-10 10:44:46 +00:00
|
|
|
language,
|
2021-10-28 22:52:39 +00:00
|
|
|
},
|
2021-11-22 11:37:07 +00:00
|
|
|
],
|
|
|
|
organizer: {
|
|
|
|
name: "Cal",
|
2022-03-14 11:27:28 +00:00
|
|
|
email: "no-reply@cal.com",
|
2021-11-22 11:37:07 +00:00
|
|
|
timeZone: "Europe/London",
|
2022-02-10 10:44:46 +00:00
|
|
|
language,
|
2021-10-28 22:52:39 +00:00
|
|
|
},
|
2021-10-25 16:15:52 +00:00
|
|
|
};
|
2021-10-28 22:52:39 +00:00
|
|
|
|
|
|
|
try {
|
2022-06-16 16:21:48 +00:00
|
|
|
const webhook = { subscriberUrl: url, payloadTemplate, appId: null, secret: null };
|
|
|
|
return await sendPayload(null, type, new Date().toISOString(), webhook, data);
|
2021-10-28 22:52:39 +00:00
|
|
|
} catch (_err) {
|
2021-11-22 11:37:07 +00:00
|
|
|
const error = getErrorFromUnknown(_err);
|
2021-10-28 22:52:39 +00:00
|
|
|
return {
|
2021-11-22 11:37:07 +00:00
|
|
|
ok: false,
|
2021-10-28 22:52:39 +00:00
|
|
|
status: 500,
|
2021-11-22 11:37:07 +00:00
|
|
|
message: error.message,
|
2021-10-28 22:52:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|