Files
calendar/packages/app-store/googlecalendar/api/webhook.ts
T
MorganandGitHub 524c0d81a7 chore: rename DWD to DelegationCredential (#19744)
* Revert "Revert "chore: rename DWD to DelegationCredential (#19703)" (#19734)"

This reverts commit 340b5ab061.

* chore: fix schema and types for now

* fix: domainWideDelegationCredentialId error type
2025-03-05 10:42:20 -03:00

52 lines
2.2 KiB
TypeScript

import type { NextApiRequest } from "next";
import { buildNonDelegationCredential } from "@calcom/lib/delegationCredential/server";
import { HttpError } from "@calcom/lib/http-error";
import { defaultHandler } from "@calcom/lib/server/defaultHandler";
import { defaultResponder } from "@calcom/lib/server/defaultResponder";
import { SelectedCalendarRepository } from "@calcom/lib/server/repository/selectedCalendar";
import { getCalendar } from "../../_utils/getCalendar";
async function postHandler(req: NextApiRequest) {
if (req.headers["x-goog-channel-token"] !== process.env.GOOGLE_WEBHOOK_TOKEN) {
throw new HttpError({ statusCode: 403, message: "Invalid API key" });
}
if (typeof req.headers["x-goog-channel-id"] !== "string") {
throw new HttpError({ statusCode: 403, message: "Missing Channel ID" });
}
// There could be multiple selected calendars for the same googleChannelId for different eventTypes and same user
// Every such record has their googleChannel related fields set which are same
// So, it is enough to get the first selected calendar for this googleChannelId
// Further code gets all the selected calendars for this calendar's credential
const selectedCalendar = await SelectedCalendarRepository.findFirstByGoogleChannelId(
req.headers["x-goog-channel-id"]
);
if (!selectedCalendar) {
throw new HttpError({
statusCode: 200,
message: `No selected calendar found for googleChannelId: ${req.headers["x-goog-channel-id"]}`,
});
}
const { credential } = selectedCalendar;
if (!credential)
throw new HttpError({
statusCode: 200,
message: `No credential found for selected calendar for googleChannelId: ${req.headers["x-goog-channel-id"]}`,
});
const { selectedCalendars } = credential;
const calendar = await getCalendar(buildNonDelegationCredential(credential));
// Make sure to pass unique SelectedCalendars to avoid unnecessary third party api calls
// Necessary to do here so that it is ensure for all calendar apps
await calendar?.fetchAvailabilityAndSetCache?.(selectedCalendars);
return { message: "ok" };
}
export default defaultHandler({
POST: Promise.resolve({ default: defaultResponder(postHandler) }),
});