Revert "Revert "Feature/cal 274 add webhooks (#628)" (#854)" (#876)

This reverts commit 6868474c92.

Co-authored-by: Bailey Pumfleet <[email protected]>
This commit is contained in:
Omar LópezandBailey Pumfleet authored and GitHub committed 2021-10-07 15:14:47 +00:00
1 parent a9ee2ef9ae
commit 30f97117e8
19 files changed
+956 -62

No files matched your search

@@ -1,5 +1,6 @@
import { Credential } from "@prisma/client";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import ICAL from "ical.js";
import { createEvent, DurationObject, Attendee, Person } from "ics";
import {
@@ -19,6 +20,8 @@ import logger from "@lib/logger";
import { IntegrationCalendar, CalendarApiAdapter, CalendarEvent } from "../../calendarClient";
import { stripHtml } from "../../emails/helpers";
dayjs.extend(utc);
const log = logger.getChildLogger({ prefix: ["[[lib] apple calendar"] });
type EventBusyDate = Record<"start" | "end", Date>;
@@ -1,5 +1,6 @@
import { Credential } from "@prisma/client";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import ICAL from "ical.js";
import { Attendee, createEvent, DurationObject, Person } from "ics";
import {
@@ -19,6 +20,8 @@ import logger from "@lib/logger";
import { CalendarApiAdapter, CalendarEvent, IntegrationCalendar } from "../../calendarClient";
import { stripHtml } from "../../emails/helpers";
dayjs.extend(utc);
const log = logger.getChildLogger({ prefix: ["[lib] caldav"] });
type EventBusyDate = Record<"start" | "end", Date>;
+3
View File
@@ -0,0 +1,3 @@
import { Webhook as PrismaWebhook } from "@prisma/client";
export type Webhook = PrismaWebhook & { prevState: null };
+33
View File
@@ -0,0 +1,33 @@
import { CalendarEvent } from "@lib/calendarClient";
const sendPayload = (
triggerEvent: string,
createdAt: string,
subscriberUrl: string,
payload: CalendarEvent
): Promise<string | Response> =>
new Promise((resolve, reject) => {
if (!subscriberUrl || !payload) {
return reject("Missing required elements to send webhook payload.");
}
fetch(subscriberUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
triggerEvent: triggerEvent,
createdAt: createdAt,
payload: payload,
}),
})
.then((response) => {
resolve(response);
})
.catch((err) => {
reject(err);
});
});
export default sendPayload;
+27
View File
@@ -0,0 +1,27 @@
import { WebhookTriggerEvents } from "@prisma/client";
import prisma from "@lib/prisma";
const getSubscriberUrls = async (userId: number, triggerEvent: WebhookTriggerEvents): Promise<string[]> => {
const allWebhooks = await prisma.webhook.findMany({
where: {
userId: userId,
AND: {
eventTriggers: {
has: triggerEvent,
},
active: {
equals: true,
},
},
},
select: {
subscriberUrl: true,
},
});
const subscriberUrls = allWebhooks.map(({ subscriberUrl }) => subscriberUrl);
return subscriberUrls;
};
export default getSubscriberUrls;