feat: Populate gCal calendar cache via webhooks (#11928)
* WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * WIP * Update CalendarService.ts * Type fixes * WIP * fix: improve cache hits * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Update CalendarService.ts * Feedback * Update CalendarService.ts * Update CalendarService.ts * Update _router.ts * feedback * WIP * WIP * Update schema.prisma * feedback * typefixes * Update Calendar.d.ts * fix: watches when adding a calendar * Discard changes to packages/app-store/googlecalendar/api/add.ts * WIP Signed-off-by: Omar López <[email protected]> * WIP Signed-off-by: Omar López <[email protected]> * WP Signed-off-by: Omar López <[email protected]> * Update calendar.ts * Update calendar.ts * Update callback.ts * Update callback.ts * Conflicts * WIP * WIP * Update CalendarService.ts * Cleanup * Discard changes to packages/features/settings/layouts/SettingsLayout.tsx * Update calendar-cache.repository.ts * WIP * Update getSelectedCalendarsToWatch.sql * WIP * Update CalendarService.ts * Cleanup * Discard changes to packages/app-store/googlecalendar/lib/CalendarService.ts * Create CalendarService.wip.ts * WIP * Update CalendarService.ts * Update getSelectedCalendarsToWatch.sql * Delete CalendarService.wip.ts * test updates * cleanup Signed-off-by: Omar López <[email protected]> * Update CalendarService.test.ts * Update CalendarService.ts * type fixes * Update OAuthManager.ts * Update CalendarService.ts * Almost there * Update CalendarService.test.ts * Update calendar.ts * Update callback.ts * Update toggleFeatureFlag.handler.ts * fix: feedback * Update getSelectedCalendarsToWatch.sql * Fix unit tests --------- Signed-off-by: Omar López <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
5012a16794
commit
d294a74aad
40 files changed
+963
-240
No files matched your search
@@ -0,0 +1,7 @@
|
||||
export { GET } from "@calcom/features/calendar-cache/api/cron";
|
||||
|
||||
/**
|
||||
* This runs each minute and we need fresh data each time
|
||||
* @see https://nextjs.org/docs/app/building-your-application/caching#opting-out-2
|
||||
**/
|
||||
export const revalidate = 0;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { _generateMetadata } from "app/_utils";
|
||||
|
||||
export const generateMetadata = async () =>
|
||||
await _generateMetadata(
|
||||
(t) => t("admin"),
|
||||
() => ""
|
||||
);
|
||||
|
||||
const Page = () => <h1>Calendar cache index</h1>;
|
||||
|
||||
export default Page;
|
||||
@@ -1,9 +1,12 @@
|
||||
import { CronJob } from "cron";
|
||||
import dotEnv from "dotenv";
|
||||
|
||||
dotEnv.config({ path: "../../.env" });
|
||||
|
||||
async function fetchCron(endpoint: string) {
|
||||
const apiKey = process.env.CRON_API_KEY;
|
||||
|
||||
const res = await fetch(`http://localhost:3000/api${endpoint}?${apiKey}`, {
|
||||
const res = await fetch(`http://localhost:3000/api${endpoint}?apiKey=${apiKey}`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
authorization: `Bearer ${process.env.CRON_SECRET}`,
|
||||
@@ -20,7 +23,7 @@ try {
|
||||
"*/5 * * * * *",
|
||||
async function () {
|
||||
await Promise.allSettled([
|
||||
fetchCron("/tasks/cron"),
|
||||
fetchCron("/calendar-cache/cron"),
|
||||
// fetchCron("/cron/calVideoNoShowWebhookTriggers"),
|
||||
//
|
||||
// fetchCron("/tasks/cleanup"),
|
||||
|
||||
@@ -3,98 +3,92 @@ import { z } from "zod";
|
||||
|
||||
import { getCalendarCredentials, getConnectedCalendars } from "@calcom/core/CalendarManager";
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import { CalendarCache } from "@calcom/features/calendar-cache/calendar-cache";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import notEmpty from "@calcom/lib/notEmpty";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||
import { SelectedCalendarRepository } from "@calcom/lib/server/repository/selectedCalendar";
|
||||
import { UserRepository } from "@calcom/lib/server/repository/user";
|
||||
|
||||
const selectedCalendarSelectSchema = z.object({
|
||||
integration: z.string(),
|
||||
externalId: z.string(),
|
||||
credentialId: z.number().optional(),
|
||||
credentialId: z.coerce.number(),
|
||||
});
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession({ req, res });
|
||||
/** Shared authentication middleware for GET, DELETE and POST requests */
|
||||
async function authMiddleware(req: CustomNextApiRequest) {
|
||||
const session = await getServerSession({ req });
|
||||
|
||||
if (!session?.user?.id) {
|
||||
res.status(401).json({ message: "Not authenticated" });
|
||||
return;
|
||||
throw new HttpError({ statusCode: 401, message: "Not authenticated" });
|
||||
}
|
||||
|
||||
const userWithCredentials = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: session.user.id,
|
||||
},
|
||||
select: {
|
||||
credentials: {
|
||||
select: credentialForCalendarServiceSelect,
|
||||
},
|
||||
timeZone: true,
|
||||
id: true,
|
||||
selectedCalendars: true,
|
||||
},
|
||||
});
|
||||
const userWithCredentials = await UserRepository.findUserWithCredentials({ id: session.user.id });
|
||||
|
||||
if (!userWithCredentials) {
|
||||
res.status(401).json({ message: "Not authenticated" });
|
||||
return;
|
||||
}
|
||||
const { credentials, ...user } = userWithCredentials;
|
||||
|
||||
if (req.method === "POST") {
|
||||
const { integration, externalId, credentialId } = selectedCalendarSelectSchema.parse(req.body);
|
||||
await prisma.selectedCalendar.upsert({
|
||||
where: {
|
||||
userId_integration_externalId: {
|
||||
userId: user.id,
|
||||
integration,
|
||||
externalId,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
userId: user.id,
|
||||
integration,
|
||||
externalId,
|
||||
credentialId,
|
||||
},
|
||||
// already exists
|
||||
update: {},
|
||||
});
|
||||
res.status(200).json({ message: "Calendar Selection Saved" });
|
||||
}
|
||||
|
||||
if (req.method === "DELETE") {
|
||||
const { integration, externalId } = selectedCalendarSelectSchema.parse(req.query);
|
||||
await prisma.selectedCalendar.delete({
|
||||
where: {
|
||||
userId_integration_externalId: {
|
||||
userId: user.id,
|
||||
externalId,
|
||||
integration,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({ message: "Calendar Selection Saved" });
|
||||
}
|
||||
|
||||
if (req.method === "GET") {
|
||||
const selectedCalendarIds = await prisma.selectedCalendar.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
select: {
|
||||
externalId: true,
|
||||
},
|
||||
});
|
||||
|
||||
// get user's credentials + their connected integrations
|
||||
const calendarCredentials = getCalendarCredentials(credentials);
|
||||
// get all the connected integrations' calendars (from third party)
|
||||
const { connectedCalendars } = await getConnectedCalendars(calendarCredentials, user.selectedCalendars);
|
||||
const calendars = connectedCalendars.flatMap((c) => c.calendars).filter(notEmpty);
|
||||
const selectableCalendars = calendars.map((cal) => {
|
||||
return { selected: selectedCalendarIds.findIndex((s) => s.externalId === cal.externalId) > -1, ...cal };
|
||||
});
|
||||
res.status(200).json(selectableCalendars);
|
||||
throw new HttpError({ statusCode: 401, message: "Not authenticated" });
|
||||
}
|
||||
req.userWithCredentials = userWithCredentials;
|
||||
return userWithCredentials;
|
||||
}
|
||||
|
||||
type CustomNextApiRequest = NextApiRequest & {
|
||||
userWithCredentials?: Awaited<ReturnType<typeof authMiddleware>>;
|
||||
};
|
||||
|
||||
async function postHandler(req: CustomNextApiRequest) {
|
||||
if (!req.userWithCredentials) throw new HttpError({ statusCode: 401, message: "Not authenticated" });
|
||||
const user = req.userWithCredentials;
|
||||
const { integration, externalId, credentialId } = selectedCalendarSelectSchema.parse(req.body);
|
||||
await SelectedCalendarRepository.upsert({
|
||||
userId: user.id,
|
||||
integration,
|
||||
externalId,
|
||||
credentialId,
|
||||
});
|
||||
|
||||
return { message: "Calendar Selection Saved" };
|
||||
}
|
||||
|
||||
async function deleteHandler(req: CustomNextApiRequest) {
|
||||
if (!req.userWithCredentials) throw new HttpError({ statusCode: 401, message: "Not authenticated" });
|
||||
const user = req.userWithCredentials;
|
||||
const { integration, externalId, credentialId } = selectedCalendarSelectSchema.parse(req.query);
|
||||
const calendarCacheRepository = await CalendarCache.initFromCredentialId(credentialId);
|
||||
await calendarCacheRepository.unwatchCalendar({ calendarId: externalId });
|
||||
await SelectedCalendarRepository.delete({
|
||||
userId: user.id,
|
||||
externalId,
|
||||
integration,
|
||||
});
|
||||
|
||||
return { message: "Calendar Selection Saved" };
|
||||
}
|
||||
|
||||
async function getHandler(req: CustomNextApiRequest) {
|
||||
if (!req.userWithCredentials) throw new HttpError({ statusCode: 401, message: "Not authenticated" });
|
||||
const user = req.userWithCredentials;
|
||||
const selectedCalendarIds = await SelectedCalendarRepository.findMany({
|
||||
where: { userId: user.id },
|
||||
select: { externalId: true },
|
||||
});
|
||||
// get user's credentials + their connected integrations
|
||||
const calendarCredentials = getCalendarCredentials(user.credentials);
|
||||
// get all the connected integrations' calendars (from third party)
|
||||
const { connectedCalendars } = await getConnectedCalendars(calendarCredentials, user.selectedCalendars);
|
||||
const calendars = connectedCalendars.flatMap((c) => c.calendars).filter(notEmpty);
|
||||
const selectableCalendars = calendars.map((cal) => {
|
||||
return { selected: selectedCalendarIds.findIndex((s) => s.externalId === cal.externalId) > -1, ...cal };
|
||||
});
|
||||
return selectableCalendars;
|
||||
}
|
||||
|
||||
export default defaultResponder(async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
await authMiddleware(req);
|
||||
return defaultHandler({
|
||||
GET: Promise.resolve({ default: defaultResponder(getHandler) }),
|
||||
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
|
||||
DELETE: Promise.resolve({ default: defaultResponder(deleteHandler) }),
|
||||
})(req, res);
|
||||
});
|
||||
@@ -3,6 +3,12 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const apiKey = req.headers.authorization || req.query.apiKey;
|
||||
if (![process.env.CRON_API_KEY, `Bearer ${process.env.CRON_SECRET}`].includes(`${apiKey}`)) {
|
||||
res.status(401).json({ message: "Not authenticated" });
|
||||
return;
|
||||
}
|
||||
|
||||
const deleted = await prisma.calendarCache.deleteMany({
|
||||
where: {
|
||||
// Delete all cache entries that expired before now
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
"path": "/api/tasks/cron",
|
||||
"schedule": "* * * * *"
|
||||
},
|
||||
{
|
||||
"path": "/api/calendar-cache/cron",
|
||||
"schedule": "* * * * *"
|
||||
},
|
||||
{
|
||||
"path": "/api/tasks/cleanup",
|
||||
"schedule": "0 0 * * *"
|
||||
|
||||
Reference in new issue
Block a user