Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
fix: When Installing Calendar App, Set Primary Calendar As Selected C…
Browse files Browse the repository at this point in the history
…alendar (calcom#12291)

* GCal set primary calendar on install

* Outlook set default calendar as selected cal

* Zoho create selected calendar on install

* Lark create selected cal on install
  • Loading branch information
joeauyeung authored Nov 9, 2023
1 parent 529054a commit 26637cd
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
29 changes: 28 additions & 1 deletion packages/app-store/googlecalendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,41 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const token = await oAuth2Client.getToken(code);
key = token.res?.data;

await prisma.credential.create({
const credential = await prisma.credential.create({
data: {
type: "google_calendar",
key,
userId: req.session.user.id,
appId: "google-calendar",
},
});

// Set the primary calendar as the first selected calendar

// We can ignore this type error because we just validated the key when we init oAuth2Client
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
oAuth2Client.setCredentials(key);

const calendar = google.calendar({
version: "v3",
auth: oAuth2Client,
});

const cals = await calendar.calendarList.list({ fields: "items(id,summary,primary,accessRole)" });

const primaryCal = cals.data.items?.find((cal) => cal.primary);

if (primaryCal?.id) {
await prisma.selectedCalendar.create({
data: {
userId: req.session.user.id,
externalId: primaryCal.id,
credentialId: credential.id,
integration: "google_calendar",
},
});
}
}

if (state?.installGoogleVideo) {
Expand Down
26 changes: 26 additions & 0 deletions packages/app-store/larkcalendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
});
}

const primaryCalendarResponse = await fetch(
`https://${LARK_HOST}/open-apis/calendar/v4/calendars/primary`,
{
method: "GET",
headers: {
Authorization: `Bearer ${key.access_token}`,
"Content-Type": "application/json",
},
}
);

if (primaryCalendarResponse.status === 200) {
const primaryCalendar = await primaryCalendarResponse.json();

if (primaryCalendar.data.calendars.calendar.calendar_id && req.session?.user?.id) {
await prisma.selectedCalendar.create({
data: {
userId: req.session?.user.id,
integration: "lark_calendar",
externalId: primaryCalendar.data.calendars.calendar.calendar_id as string,
credentialId: currentCredential?.id,
},
});
}
}

res.redirect(
getSafeRedirectUrl(state?.returnTo) ??
getInstalledAppPath({ variant: "calendar", slug: "lark-calendar" })
Expand Down
48 changes: 47 additions & 1 deletion packages/app-store/office365calendar/api/callback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Calendar as OfficeCalendar } from "@microsoft/microsoft-graph-types-beta";
import type { NextApiRequest, NextApiResponse } from "next";

import { WEBAPP_URL } from "@calcom/lib/constants";
import { handleErrorsJson } from "@calcom/lib/errors";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import prisma from "@calcom/prisma";

Expand Down Expand Up @@ -65,7 +67,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
responseBody.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in); // set expiry date in seconds
delete responseBody.expires_in;

await prisma.credential.create({
const credential = await prisma.credential.create({
data: {
type: "office365_calendar",
key: responseBody,
Expand All @@ -74,6 +76,50 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
});

// Set the isDefaultCalendar as selectedCalendar
// If a user has multiple calendars, keep on making calls until we find the default calendar
let defaultCalendar: OfficeCalendar | undefined = undefined;
let requestUrl = "https://graph.microsoft.com/v1.0/me/calendars?$select=id,isDefaultCalendar";
let finishedParsingCalendars = false;

while (!finishedParsingCalendars) {
const calRequest = await fetch(requestUrl, {
method: "GET",
headers: {
Authorization: `Bearer ${responseBody.access_token}`,
"Content-Type": "application/json",
},
});
let calBody = await handleErrorsJson<{ value: OfficeCalendar[]; "@odata.nextLink"?: string }>(calRequest);

if (typeof responseBody === "string") {
calBody = JSON.parse(responseBody) as { value: OfficeCalendar[] };
}

const findDefaultCalendar = calBody.value.find((calendar) => calendar.isDefaultCalendar);

if (findDefaultCalendar) {
defaultCalendar = findDefaultCalendar;
}

if (calBody["@odata.nextLink"]) {
requestUrl = calBody["@odata.nextLink"];
} else {
finishedParsingCalendars = true;
}
}

if (defaultCalendar?.id && req.session?.user?.id) {
await prisma.selectedCalendar.create({
data: {
userId: req.session?.user.id,
integration: "office365_calendar",
externalId: defaultCalendar.id,
credentialId: credential.id,
},
});
}

const state = decodeOAuthState(req);
return res.redirect(
getSafeRedirectUrl(state?.returnTo) ??
Expand Down
33 changes: 31 additions & 2 deletions packages/app-store/zohocalendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { WEBAPP_URL } from "@calcom/lib/constants";
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
import logger from "@calcom/lib/logger";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import prisma from "@calcom/prisma";

import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
import createOAuthAppCredential from "../../_utils/oauth/createOAuthAppCredential";
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
import config from "../config.json";
import type { ZohoAuthCredentials } from "../types/ZohoCalendar";
Expand Down Expand Up @@ -64,7 +64,36 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
expires_in: Math.round(+new Date() / 1000 + responseBody.expires_in),
};

await createOAuthAppCredential({ appId: config.slug, type: config.type }, key, req);
const credential = await prisma.credential.create({
data: {
type: config.type,
key,
userId: req.session.user.id,
appId: config.slug,
},
});

const calendarResponse = await fetch("https://calendar.zoho.com/api/v1/calendars", {
method: "GET",
headers: {
Authorization: `Bearer ${key.access_token}`,
"Content-Type": "application/json",
},
});
const data = await calendarResponse.json();

const primaryCalendar = data.calendars.find((calendar: any) => calendar.isdefault);

if (primaryCalendar.uid) {
await prisma.selectedCalendar.create({
data: {
userId: req.session.user.id,
integration: config.type,
externalId: primaryCalendar.uid,
credentialId: credential.id,
},
});
}

res.redirect(
getSafeRedirectUrl(state?.returnTo) ?? getInstalledAppPath({ variant: config.variant, slug: config.slug })
Expand Down

0 comments on commit 26637cd

Please sign in to comment.