feat: Reduce bundle size via better imports + install gcal + gvideo by default for google signups (#17810)
* wip * use light imports * fix * perf: Reduce bundle size via better imports with `googleapis` (#17811) * fix test * refactor * update vite.config.js * fix * fix unit test failing
This commit is contained in:
1 parent
f75415e414
commit
b8219c273d
27 files changed
+383
-279
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import { oauth2_v2 } from "@googleapis/oauth2";
|
||||
import type { OAuth2Client } from "googleapis-common";
|
||||
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { UserRepository } from "@calcom/lib/server/repository/user";
|
||||
|
||||
export async function updateProfilePhotoGoogle(oAuth2Client: OAuth2Client, userId: number) {
|
||||
try {
|
||||
const oauth2 = new oauth2_v2.Oauth2({ auth: oAuth2Client });
|
||||
const userDetails = await oauth2.userinfo.get();
|
||||
if (userDetails.data?.picture) {
|
||||
await UserRepository.updateAvatar({ id: userId, avatarUrl: userDetails.data.picture });
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Error updating avatarUrl from google calendar connect", error);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,21 @@
|
||||
import { google } from "googleapis";
|
||||
import { OAuth2Client } from "googleapis-common";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { WEBAPP_URL_FOR_OAUTH } from "@calcom/lib/constants";
|
||||
import { GOOGLE_CALENDAR_SCOPES, SCOPE_USERINFO_PROFILE, WEBAPP_URL_FOR_OAUTH } from "@calcom/lib/constants";
|
||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||
|
||||
import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";
|
||||
import { SCOPES } from "../lib/constants";
|
||||
import { getGoogleAppKeys } from "../lib/getGoogleAppKeys";
|
||||
|
||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
// Get token from Google Calendar API
|
||||
const { client_id, client_secret } = await getGoogleAppKeys();
|
||||
const redirect_uri = `${WEBAPP_URL_FOR_OAUTH}/api/integrations/googlecalendar/callback`;
|
||||
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
||||
const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uri);
|
||||
|
||||
const authUrl = oAuth2Client.generateAuthUrl({
|
||||
access_type: "offline",
|
||||
scope: SCOPES,
|
||||
scope: [SCOPE_USERINFO_PROFILE, ...GOOGLE_CALENDAR_SCOPES],
|
||||
// A refresh token is only returned the first time the user
|
||||
// consents to providing access. For illustration purposes,
|
||||
// setting the prompt to 'consent' will force this consent
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
import { google } from "googleapis";
|
||||
import { calendar_v3 } from "@googleapis/calendar";
|
||||
import { OAuth2Client } from "googleapis-common";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { updateProfilePhotoGoogle } from "@calcom/app-store/_utils/oauth/updateProfilePhotoGoogle";
|
||||
import GoogleCalendarService from "@calcom/app-store/googlecalendar/lib/CalendarService";
|
||||
import { renewSelectedCalendarCredentialId } from "@calcom/lib/connectedCalendar";
|
||||
import { WEBAPP_URL, WEBAPP_URL_FOR_OAUTH } from "@calcom/lib/constants";
|
||||
import {
|
||||
GOOGLE_CALENDAR_SCOPES,
|
||||
SCOPE_USERINFO_PROFILE,
|
||||
WEBAPP_URL,
|
||||
WEBAPP_URL_FOR_OAUTH,
|
||||
} from "@calcom/lib/constants";
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
import { getAllCalendars, updateProfilePhoto } from "@calcom/lib/google";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
||||
import { GoogleRepository } from "@calcom/lib/server/repository/google";
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
||||
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
|
||||
import { REQUIRED_SCOPES, SCOPE_USERINFO_PROFILE } from "../lib/constants";
|
||||
import { getGoogleAppKeys } from "../lib/getGoogleAppKeys";
|
||||
|
||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -40,14 +45,14 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const redirect_uri = `${WEBAPP_URL_FOR_OAUTH}/api/integrations/googlecalendar/callback`;
|
||||
|
||||
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
||||
const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uri);
|
||||
|
||||
if (code) {
|
||||
const token = await oAuth2Client.getToken(code);
|
||||
const key = token.tokens;
|
||||
const grantedScopes = token.tokens.scope?.split(" ") ?? [];
|
||||
// Check if we have granted all required permissions
|
||||
const hasMissingRequiredScopes = REQUIRED_SCOPES.some((scope) => !grantedScopes.includes(scope));
|
||||
const hasMissingRequiredScopes = GOOGLE_CALENDAR_SCOPES.some((scope) => !grantedScopes.includes(scope));
|
||||
if (hasMissingRequiredScopes) {
|
||||
if (!state?.fromApp) {
|
||||
throw new HttpError({
|
||||
@@ -63,28 +68,25 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the primary calendar as the first selected calendar
|
||||
|
||||
oAuth2Client.setCredentials(key);
|
||||
|
||||
const calendar = google.calendar({
|
||||
version: "v3",
|
||||
const gcalCredential = await CredentialRepository.create({
|
||||
userId: req.session.user.id,
|
||||
key,
|
||||
appId: "google-calendar",
|
||||
type: "google_calendar",
|
||||
});
|
||||
|
||||
const gCalService = new GoogleCalendarService({
|
||||
...gcalCredential,
|
||||
user: null,
|
||||
});
|
||||
|
||||
const calendar = new calendar_v3.Calendar({
|
||||
auth: oAuth2Client,
|
||||
});
|
||||
|
||||
const cals = await getAllCalendars(calendar);
|
||||
|
||||
const primaryCal = cals.find((cal) => cal.primary) ?? cals[0];
|
||||
|
||||
// Only attempt to update the user's profile photo if the user has granted the required scope
|
||||
if (grantedScopes.includes(SCOPE_USERINFO_PROFILE)) {
|
||||
await updateProfilePhoto(oAuth2Client, req.session.user.id);
|
||||
}
|
||||
|
||||
const gcalCredential = await GoogleRepository.createGoogleCalendarCredential({
|
||||
key,
|
||||
userId: req.session.user.id,
|
||||
});
|
||||
const primaryCal = await gCalService.getPrimaryCalendar(calendar);
|
||||
|
||||
// If we still don't have a primary calendar skip creating the selected calendar.
|
||||
// It can be toggled on later.
|
||||
@@ -96,6 +98,11 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only attempt to update the user's profile photo if the user has granted the required scope
|
||||
if (grantedScopes.includes(SCOPE_USERINFO_PROFILE)) {
|
||||
await updateProfilePhotoGoogle(oAuth2Client, req.session.user.id);
|
||||
}
|
||||
|
||||
const selectedCalendarWhereUnique = {
|
||||
userId: req.session.user.id,
|
||||
externalId: primaryCal.id,
|
||||
@@ -105,10 +112,8 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
// Wrapping in a try/catch to reduce chance of race conditions-
|
||||
// also this improves performance for most of the happy-paths.
|
||||
try {
|
||||
await GoogleRepository.upsertSelectedCalendar({
|
||||
credentialId: gcalCredential.id,
|
||||
await gCalService.upsertSelectedCalendar({
|
||||
externalId: selectedCalendarWhereUnique.externalId,
|
||||
userId: selectedCalendarWhereUnique.userId,
|
||||
});
|
||||
} catch (error) {
|
||||
let errorMessage = "something_went_wrong";
|
||||
@@ -145,8 +150,9 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingGoogleMeetCredential = await GoogleRepository.findGoogleMeetCredential({
|
||||
const existingGoogleMeetCredential = await CredentialRepository.findFirstByUserIdAndType({
|
||||
userId: req.session.user.id,
|
||||
type: "google_video",
|
||||
});
|
||||
|
||||
// If the user already has a google meet credential, there's nothing to do in here
|
||||
@@ -159,7 +165,12 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
// Create a new google meet credential
|
||||
await GoogleRepository.createGoogleMeetsCredential({ userId: req.session.user.id });
|
||||
await CredentialRepository.create({
|
||||
userId: req.session.user.id,
|
||||
type: "google_video",
|
||||
key: {},
|
||||
appId: "google-meet",
|
||||
});
|
||||
res.redirect(
|
||||
getSafeRedirectUrl(`${WEBAPP_URL}/apps/installed/conferencing?hl=google-meet`) ??
|
||||
getInstalledAppPath({ variant: "conferencing", slug: "google-meet" })
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import prismock from "../../../../tests/libs/__mocks__/prisma";
|
||||
import oAuthManagerMock, { defaultMockOAuthManager } from "../../tests/__mocks__/OAuthManager";
|
||||
import { googleapisMock, setCredentialsMock } from "./__mocks__/googleapis";
|
||||
import { adminMock, calendarMock, setCredentialsMock } from "./__mocks__/googleapis";
|
||||
|
||||
import { expect, test, vi } from "vitest";
|
||||
import { expect, test, beforeEach, vi } from "vitest";
|
||||
import "vitest-fetch-mock";
|
||||
|
||||
import { CalendarCache } from "@calcom/features/calendar-cache/calendar-cache";
|
||||
@@ -22,7 +22,21 @@ vi.mock("./getGoogleAppKeys", () => ({
|
||||
redirect_uris: ["http://localhost:3000/api/integrations/googlecalendar/callback"],
|
||||
}),
|
||||
}));
|
||||
googleapisMock.google;
|
||||
|
||||
vi.mock("googleapis-common", () => ({
|
||||
OAuth2Client: vi.fn().mockImplementation(() => ({
|
||||
setCredentials: setCredentialsMock,
|
||||
})),
|
||||
}));
|
||||
vi.mock("@googleapis/admin", () => adminMock);
|
||||
vi.mock("@googleapis/calendar", () => calendarMock);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
setCredentialsMock.mockClear();
|
||||
calendarMock.calendar_v3.Calendar.mockClear();
|
||||
adminMock.admin_directory_v1.Admin.mockClear();
|
||||
});
|
||||
|
||||
const googleTestCredentialKey = {
|
||||
scope: "https://www.googleapis.com/auth/calendar.events",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { calendar_v3 } from "@googleapis/calendar";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import type { calendar_v3 } from "googleapis";
|
||||
import { google } from "googleapis";
|
||||
import { OAuth2Client } from "googleapis-common";
|
||||
import { RRule } from "rrule";
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
@@ -18,10 +18,9 @@ import {
|
||||
CREDENTIAL_SYNC_SECRET_HEADER_NAME,
|
||||
} from "@calcom/lib/constants";
|
||||
import { formatCalEvent } from "@calcom/lib/formatCalendarEvent";
|
||||
import { getAllCalendars } from "@calcom/lib/google";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { GoogleRepository } from "@calcom/lib/server/repository/google";
|
||||
import { SelectedCalendarRepository } from "@calcom/lib/server/repository/selectedCalendar";
|
||||
import prisma from "@calcom/prisma";
|
||||
import type {
|
||||
Calendar,
|
||||
@@ -164,8 +163,7 @@ export default class GoogleCalendarService implements Calendar {
|
||||
|
||||
public authedCalendar = async () => {
|
||||
const myGoogleAuth = await this.auth.getMyGoogleAuthWithRefreshedToken();
|
||||
const calendar = google.calendar({
|
||||
version: "v3",
|
||||
const calendar = new calendar_v3.Calendar({
|
||||
auth: myGoogleAuth,
|
||||
});
|
||||
return calendar;
|
||||
@@ -541,12 +539,12 @@ export default class GoogleCalendarService implements Calendar {
|
||||
// Only calendars of other integrations selected
|
||||
return [];
|
||||
}
|
||||
async function getCalIds() {
|
||||
const getCalIds = async () => {
|
||||
if (selectedCalendarIds.length !== 0) return selectedCalendarIds;
|
||||
const cals = await getAllCalendars(calendar, ["id"]);
|
||||
const cals = await this.getAllCalendars(calendar, ["id"]);
|
||||
if (!cals.length) return [];
|
||||
return cals.reduce((c, cal) => (cal.id ? [...c, cal.id] : c), [] as string[]);
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const calsIds = await getCalIds();
|
||||
@@ -607,7 +605,7 @@ export default class GoogleCalendarService implements Calendar {
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
data: {
|
||||
items: await getAllCalendars(calendar),
|
||||
items: await this.getAllCalendars(calendar),
|
||||
},
|
||||
})
|
||||
);
|
||||
@@ -652,10 +650,8 @@ export default class GoogleCalendarService implements Calendar {
|
||||
},
|
||||
});
|
||||
const response = res.data;
|
||||
await GoogleRepository.upsertSelectedCalendar({
|
||||
userId: this.credential.userId!,
|
||||
await this.upsertSelectedCalendar({
|
||||
externalId: calendarId,
|
||||
credentialId: this.credential.id,
|
||||
googleChannelId: response?.id,
|
||||
googleChannelKind: response?.kind,
|
||||
googleChannelResourceId: response?.resourceId,
|
||||
@@ -686,10 +682,8 @@ export default class GoogleCalendarService implements Calendar {
|
||||
.catch((err) => {
|
||||
console.warn(JSON.stringify(err));
|
||||
});
|
||||
await GoogleRepository.upsertSelectedCalendar({
|
||||
userId: this.credential.userId!,
|
||||
await this.upsertSelectedCalendar({
|
||||
externalId: calendarId,
|
||||
credentialId: this.credential.id,
|
||||
googleChannelId: null,
|
||||
googleChannelKind: null,
|
||||
googleChannelResourceId: null,
|
||||
@@ -715,9 +709,97 @@ export default class GoogleCalendarService implements Calendar {
|
||||
const data = await this.fetchAvailability(parsedArgs);
|
||||
await this.setAvailabilityInCache(parsedArgs, data);
|
||||
}
|
||||
|
||||
async createSelectedCalendar(
|
||||
data: Omit<Prisma.SelectedCalendarUncheckedCreateInput, "integration" | "credentialId">
|
||||
) {
|
||||
return await SelectedCalendarRepository.create({
|
||||
...data,
|
||||
integration: this.integrationName,
|
||||
credentialId: this.credential.id,
|
||||
});
|
||||
}
|
||||
|
||||
async upsertSelectedCalendar(
|
||||
data: Omit<Prisma.SelectedCalendarUncheckedCreateInput, "integration" | "credentialId" | "userId">
|
||||
) {
|
||||
if (!this.credential.userId) {
|
||||
logger.error("upsertSelectedCalendar failed. userId is missing.");
|
||||
return;
|
||||
}
|
||||
return await SelectedCalendarRepository.upsert({
|
||||
...data,
|
||||
integration: this.integrationName,
|
||||
credentialId: this.credential.id,
|
||||
userId: this.credential.userId,
|
||||
});
|
||||
}
|
||||
|
||||
async getAllCalendars(
|
||||
calendar: calendar_v3.Calendar,
|
||||
fields: string[] = ["id", "summary", "primary", "accessRole"]
|
||||
): Promise<calendar_v3.Schema$CalendarListEntry[]> {
|
||||
let allCalendars: calendar_v3.Schema$CalendarListEntry[] = [];
|
||||
let pageToken: string | undefined;
|
||||
|
||||
try {
|
||||
do {
|
||||
const response: any = await calendar.calendarList.list({
|
||||
fields: `items(${fields.join(",")}),nextPageToken`,
|
||||
pageToken,
|
||||
maxResults: 250, // 250 is max
|
||||
});
|
||||
|
||||
allCalendars = [...allCalendars, ...(response.data.items ?? [])];
|
||||
pageToken = response.data.nextPageToken;
|
||||
} while (pageToken);
|
||||
|
||||
return allCalendars;
|
||||
} catch (error) {
|
||||
logger.error("Error fetching all Google Calendars", { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getPrimaryCalendar(
|
||||
calendar: calendar_v3.Calendar,
|
||||
fields: string[] = ["id", "summary", "primary", "accessRole"]
|
||||
): Promise<calendar_v3.Schema$CalendarListEntry | null> {
|
||||
let pageToken: string | undefined;
|
||||
let firstCalendar: calendar_v3.Schema$CalendarListEntry | undefined;
|
||||
|
||||
try {
|
||||
do {
|
||||
const response: any = await calendar.calendarList.list({
|
||||
fields: `items(${fields.join(",")}),nextPageToken`,
|
||||
pageToken,
|
||||
maxResults: 250, // 250 is max
|
||||
});
|
||||
|
||||
const cals = response.data.items ?? [];
|
||||
const primaryCal = cals.find((cal: calendar_v3.Schema$CalendarListEntry) => cal.primary);
|
||||
if (primaryCal) {
|
||||
return primaryCal;
|
||||
}
|
||||
|
||||
// Store the first calendar in case no primary is found
|
||||
if (cals.length > 0 && !firstCalendar) {
|
||||
firstCalendar = cals[0];
|
||||
}
|
||||
|
||||
pageToken = response.data.nextPageToken;
|
||||
} while (pageToken);
|
||||
|
||||
// should not be reached because Google Cal always has a primary cal
|
||||
return firstCalendar ?? null;
|
||||
} catch (error) {
|
||||
logger.error("Error in `getPrimaryCalendar`", { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MyGoogleAuth extends google.auth.OAuth2 {
|
||||
class MyGoogleAuth extends OAuth2Client {
|
||||
constructor(client_id: string, client_secret: string, redirect_uri: string) {
|
||||
super(client_id, client_secret, redirect_uri);
|
||||
}
|
||||
|
||||
@@ -1,50 +1,33 @@
|
||||
import type * as googleapis from "googleapis";
|
||||
import { beforeEach, vi } from "vitest";
|
||||
import { mockDeep, mockReset } from "vitest-mock-extended";
|
||||
|
||||
vi.mock("googleapis", () => googleapisMock);
|
||||
|
||||
beforeEach(() => {
|
||||
mockReset(googleapisMock);
|
||||
});
|
||||
|
||||
const googleapisMock = mockDeep<typeof googleapis>({
|
||||
fallbackMockImplementation: (...args) => {
|
||||
console.log(args);
|
||||
throw new Error("Unimplemented");
|
||||
},
|
||||
});
|
||||
import { vi } from "vitest";
|
||||
|
||||
const setCredentialsMock = vi.fn();
|
||||
googleapisMock.google = {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
calendar: vi.fn().mockReturnValue({
|
||||
channels: {
|
||||
stop: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
events: {
|
||||
watch: vi.fn().mockResolvedValue({
|
||||
data: {
|
||||
kind: "api#channel",
|
||||
id: "mock-channel-id",
|
||||
resourceId: "mock-resource-id",
|
||||
resourceUri: "mock-resource-uri",
|
||||
expiration: "1111111111",
|
||||
},
|
||||
}),
|
||||
},
|
||||
}),
|
||||
auth: {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
OAuth2: function MockGoogleOAuth2() {
|
||||
return {
|
||||
setCredentials: setCredentialsMock,
|
||||
};
|
||||
},
|
||||
|
||||
const calendarMockImplementation = {
|
||||
channels: {
|
||||
stop: vi.fn().mockResolvedValue(undefined),
|
||||
},
|
||||
events: {
|
||||
watch: vi.fn().mockResolvedValue({
|
||||
data: {
|
||||
kind: "api#channel",
|
||||
id: "mock-channel-id",
|
||||
resourceId: "mock-resource-id",
|
||||
resourceUri: "mock-resource-uri",
|
||||
expiration: "1111111111",
|
||||
},
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
export default googleapisMock;
|
||||
export { googleapisMock, setCredentialsMock };
|
||||
const calendarMock = {
|
||||
calendar_v3: {
|
||||
Calendar: vi.fn().mockImplementation(() => calendarMockImplementation),
|
||||
},
|
||||
};
|
||||
const adminMock = {
|
||||
admin_directory_v1: {
|
||||
Admin: vi.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
export { calendarMock, adminMock, setCredentialsMock };
|
||||
@@ -1,7 +0,0 @@
|
||||
export const SCOPE_USERINFO_PROFILE = "https://www.googleapis.com/auth/userinfo.profile";
|
||||
export const SCOPE_CALENDAR_READONLY = "https://www.googleapis.com/auth/calendar.readonly";
|
||||
export const SCOPE_CALENDAR_EVENT = "https://www.googleapis.com/auth/calendar.events";
|
||||
|
||||
export const REQUIRED_SCOPES = [SCOPE_CALENDAR_READONLY, SCOPE_CALENDAR_EVENT];
|
||||
|
||||
export const SCOPES = [...REQUIRED_SCOPES, SCOPE_USERINFO_PROFILE];
|
||||
@@ -1,29 +0,0 @@
|
||||
import type { calendar_v3 } from "googleapis";
|
||||
|
||||
import logger from "@calcom/lib/logger";
|
||||
|
||||
export async function getAllCalendars(
|
||||
calendar: calendar_v3.Calendar,
|
||||
fields: string[] = ["id", "summary", "primary", "accessRole"]
|
||||
): Promise<calendar_v3.Schema$CalendarListEntry[]> {
|
||||
let allCalendars: calendar_v3.Schema$CalendarListEntry[] = [];
|
||||
let pageToken: string | undefined;
|
||||
|
||||
try {
|
||||
do {
|
||||
const response: any = await calendar.calendarList.list({
|
||||
fields: `items(${fields.join(",")}),nextPageToken`,
|
||||
pageToken,
|
||||
maxResults: 250, // 250 is max
|
||||
});
|
||||
|
||||
allCalendars = [...allCalendars, ...(response.data.items ?? [])];
|
||||
pageToken = response.data.nextPageToken;
|
||||
} while (pageToken);
|
||||
|
||||
return allCalendars;
|
||||
} catch (error) {
|
||||
logger.error("Error fetching all Google Calendars", { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
"description": "Google Calendar is a time management and scheduling service developed by Google. Allows users to create and edit events, with options available for type and time. Available to anyone that has a Gmail account on both mobile and web versions.",
|
||||
"dependencies": {
|
||||
"@calcom/prisma": "*",
|
||||
"googleapis": "^84.0.0"
|
||||
"@googleapis/calendar": "^9.7.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@calcom/types": "*"
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
"main": "./index.ts",
|
||||
"description": "Google Meet is Google's web-based video conferencing platform, designed to compete with major conferencing platforms.",
|
||||
"dependencies": {
|
||||
"@calcom/prisma": "*",
|
||||
"googleapis": "^84.0.0"
|
||||
"@calcom/prisma": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@calcom/types": "*"
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { calendar_v3 } from "@googleapis/calendar";
|
||||
import type { Membership, Team, UserPermissionRole } from "@prisma/client";
|
||||
import { waitUntil } from "@vercel/functions";
|
||||
import { OAuth2Client } from "googleapis-common";
|
||||
import type { AuthOptions, Session, User } from "next-auth";
|
||||
import type { JWT } from "next-auth/jwt";
|
||||
import { encode } from "next-auth/jwt";
|
||||
@@ -8,6 +10,8 @@ import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import EmailProvider from "next-auth/providers/email";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
|
||||
import { updateProfilePhotoGoogle } from "@calcom/app-store/_utils/oauth/updateProfilePhotoGoogle";
|
||||
import GoogleCalendarService from "@calcom/app-store/googlecalendar/lib/CalendarService";
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
|
||||
import postHogClient from "@calcom/features/ee/event-tracking/lib/posthog/postHogClient";
|
||||
@@ -15,7 +19,12 @@ import ImpersonationProvider from "@calcom/features/ee/impersonation/lib/Imperso
|
||||
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
import { clientSecretVerifier, hostedCal, isSAMLLoginEnabled } from "@calcom/features/ee/sso/lib/saml";
|
||||
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
|
||||
import { HOSTED_CAL_FEATURES, IS_CALCOM } from "@calcom/lib/constants";
|
||||
import {
|
||||
GOOGLE_CALENDAR_SCOPES,
|
||||
GOOGLE_OAUTH_SCOPES,
|
||||
HOSTED_CAL_FEATURES,
|
||||
IS_CALCOM,
|
||||
} from "@calcom/lib/constants";
|
||||
import { ENABLE_PROFILE_SWITCHER, IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { symmetricDecrypt, symmetricEncrypt } from "@calcom/lib/crypto";
|
||||
import { defaultCookies } from "@calcom/lib/default-cookies";
|
||||
@@ -23,6 +32,7 @@ import { isENVDev } from "@calcom/lib/env";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { randomString } from "@calcom/lib/random";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
|
||||
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
|
||||
import { UserRepository } from "@calcom/lib/server/repository/user";
|
||||
import slugify from "@calcom/lib/slugify";
|
||||
@@ -251,6 +261,13 @@ if (IS_GOOGLE_LOGIN_ENABLED) {
|
||||
clientId: GOOGLE_CLIENT_ID,
|
||||
clientSecret: GOOGLE_CLIENT_SECRET,
|
||||
allowDangerousEmailAccountLinking: true,
|
||||
authorization: {
|
||||
params: {
|
||||
scope: [...GOOGLE_OAUTH_SCOPES, ...GOOGLE_CALENDAR_SCOPES].join(" "),
|
||||
access_type: "offline",
|
||||
prompt: "consent",
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -608,6 +625,63 @@ export const getOptions = ({
|
||||
return await autoMergeIdentities();
|
||||
}
|
||||
|
||||
const grantedScopes = account.scope?.split(" ") ?? [];
|
||||
if (
|
||||
account.provider === "google" &&
|
||||
!(await CredentialRepository.findFirstByAppIdAndUserId({
|
||||
userId: user.id as number,
|
||||
appId: "google-calendar",
|
||||
})) &&
|
||||
GOOGLE_CALENDAR_SCOPES.every((scope) => grantedScopes.includes(scope))
|
||||
) {
|
||||
// Installing Google Calendar by default
|
||||
const credentialkey = {
|
||||
access_token: account.access_token,
|
||||
refresh_token: account.refresh_token,
|
||||
id_token: account.id_token,
|
||||
token_type: account.token_type,
|
||||
expires_at: account.expires_at,
|
||||
};
|
||||
const gcalCredential = await CredentialRepository.create({
|
||||
userId: user.id as number,
|
||||
key: credentialkey,
|
||||
appId: "google-calendar",
|
||||
type: "google_calendar",
|
||||
});
|
||||
const gCalService = new GoogleCalendarService({
|
||||
...gcalCredential,
|
||||
user: null,
|
||||
});
|
||||
|
||||
if (
|
||||
!(await CredentialRepository.findFirstByUserIdAndType({
|
||||
userId: user.id as number,
|
||||
type: "google_video",
|
||||
}))
|
||||
) {
|
||||
await CredentialRepository.create({
|
||||
type: "google_video",
|
||||
key: {},
|
||||
userId: user.id as number,
|
||||
appId: "google-meet",
|
||||
});
|
||||
}
|
||||
|
||||
const oAuth2Client = new OAuth2Client(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET);
|
||||
oAuth2Client.setCredentials(credentialkey);
|
||||
const calendar = new calendar_v3.Calendar({
|
||||
auth: oAuth2Client,
|
||||
});
|
||||
const primaryCal = await gCalService.getPrimaryCalendar(calendar);
|
||||
if (primaryCal?.id) {
|
||||
await gCalService.createSelectedCalendar({
|
||||
externalId: primaryCal.id,
|
||||
userId: user.id as number,
|
||||
});
|
||||
}
|
||||
await updateProfilePhotoGoogle(oAuth2Client, user.id as number);
|
||||
}
|
||||
|
||||
return {
|
||||
...token,
|
||||
id: existingUser.id,
|
||||
@@ -632,7 +706,6 @@ export const getOptions = ({
|
||||
log.debug("callbacks:session - Session callback called", safeStringify({ session, token, user }));
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance();
|
||||
const hasValidLicense = await licenseKeyService.checkLicense();
|
||||
|
||||
const profileId = token.profileId;
|
||||
const calendsoSession: Session = {
|
||||
...session,
|
||||
|
||||
@@ -189,3 +189,11 @@ export const RECORDING_DEFAULT_ICON = IS_PRODUCTION
|
||||
export const RECORDING_IN_PROGRESS_ICON = IS_PRODUCTION
|
||||
? `${WEBAPP_URL}/stop-recording.svg`
|
||||
: `https://app.cal.com/stop-recording.svg`;
|
||||
|
||||
export const SCOPE_USERINFO_PROFILE = "https://www.googleapis.com/auth/userinfo.profile";
|
||||
export const SCOPE_USERINFO_EMAIL = "https://www.googleapis.com/auth/userinfo.email";
|
||||
export const GOOGLE_OAUTH_SCOPES = [SCOPE_USERINFO_PROFILE, SCOPE_USERINFO_EMAIL];
|
||||
export const GOOGLE_CALENDAR_SCOPES = [
|
||||
"https://www.googleapis.com/auth/calendar.events",
|
||||
"https://www.googleapis.com/auth/calendar.readonly",
|
||||
];
|
||||
@@ -1,45 +0,0 @@
|
||||
import type { OAuth2Client } from "google-auth-library";
|
||||
import type { calendar_v3 } from "googleapis";
|
||||
import { google } from "googleapis";
|
||||
|
||||
import logger from "@calcom/lib/logger";
|
||||
|
||||
import { UserRepository } from "./server/repository/user";
|
||||
|
||||
export async function getAllCalendars(
|
||||
calendar: calendar_v3.Calendar,
|
||||
fields: string[] = ["id", "summary", "primary", "accessRole"]
|
||||
): Promise<calendar_v3.Schema$CalendarListEntry[]> {
|
||||
let allCalendars: calendar_v3.Schema$CalendarListEntry[] = [];
|
||||
let pageToken: string | undefined;
|
||||
|
||||
try {
|
||||
do {
|
||||
const response: any = await calendar.calendarList.list({
|
||||
fields: `items(${fields.join(",")}),nextPageToken`,
|
||||
pageToken,
|
||||
maxResults: 250, // 250 is max
|
||||
});
|
||||
|
||||
allCalendars = [...allCalendars, ...(response.data.items ?? [])];
|
||||
pageToken = response.data.nextPageToken;
|
||||
} while (pageToken);
|
||||
|
||||
return allCalendars;
|
||||
} catch (error) {
|
||||
logger.error("Error fetching all Google Calendars", { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProfilePhoto(oAuth2Client: OAuth2Client, userId: number) {
|
||||
try {
|
||||
const oauth2 = google.oauth2({ version: "v2", auth: oAuth2Client });
|
||||
const userDetails = await oauth2.userinfo.get();
|
||||
if (userDetails.data?.picture) {
|
||||
await UserRepository.updateAvatar({ id: userId, avatarUrl: userDetails.data.picture });
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Error updating avatarUrl from google calendar connect", error);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,15 @@ export class CredentialRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findFirstByAppIdAndUserId({ appId, userId }: { appId: string; userId: number }) {
|
||||
return await prisma.credential.findFirst({
|
||||
where: {
|
||||
appId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static async findFirstByUserIdAndType({ userId, type }: { userId: number; type: string }) {
|
||||
return await prisma.credential.findFirst({ where: { userId, type } });
|
||||
}
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import type { Credentials } from "google-auth-library";
|
||||
|
||||
import { CredentialRepository } from "./credential";
|
||||
import { SelectedCalendarRepository } from "./selectedCalendar";
|
||||
|
||||
export class GoogleRepository {
|
||||
static async createGoogleCalendarCredential({ userId, key }: { userId: number; key: Credentials }) {
|
||||
return await CredentialRepository.create({
|
||||
type: "google_calendar",
|
||||
key,
|
||||
userId,
|
||||
appId: "google-calendar",
|
||||
});
|
||||
}
|
||||
|
||||
static async createGoogleMeetsCredential({ userId }: { userId: number }) {
|
||||
return await CredentialRepository.create({
|
||||
type: "google_video",
|
||||
key: {},
|
||||
userId,
|
||||
appId: "google-meet",
|
||||
});
|
||||
}
|
||||
|
||||
static async createSelectedCalendar(data: { credentialId: number; userId: number; externalId: string }) {
|
||||
return await SelectedCalendarRepository.create({
|
||||
...data,
|
||||
integration: "google_calendar",
|
||||
});
|
||||
}
|
||||
|
||||
static async upsertSelectedCalendar(
|
||||
data: Omit<Prisma.SelectedCalendarUncheckedCreateInput, "integration">
|
||||
) {
|
||||
return await SelectedCalendarRepository.upsert({
|
||||
...data,
|
||||
integration: "google_calendar",
|
||||
});
|
||||
}
|
||||
|
||||
static async findGoogleMeetCredential({ userId }: { userId: number }) {
|
||||
return await CredentialRepository.findFirstByUserIdAndType({
|
||||
userId,
|
||||
type: "google_video",
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,8 @@ import type { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
|
||||
type SelectedCalendarCreateInput = {
|
||||
credentialId: number;
|
||||
userId: number;
|
||||
externalId: string;
|
||||
integration: string;
|
||||
};
|
||||
|
||||
export class SelectedCalendarRepository {
|
||||
static async create(data: SelectedCalendarCreateInput) {
|
||||
static async create(data: Prisma.SelectedCalendarUncheckedCreateInput) {
|
||||
return await prisma.selectedCalendar.create({
|
||||
data: {
|
||||
...data,
|
||||
|
||||
@@ -54,7 +54,7 @@ export default defineConfig({
|
||||
"ical.js",
|
||||
"ics",
|
||||
"tsdav",
|
||||
"googleapis",
|
||||
"@googleapis/calendar",
|
||||
"rrule",
|
||||
"@hubspot/api-client",
|
||||
"querystring",
|
||||
@@ -125,7 +125,7 @@ export default defineConfig({
|
||||
"ical.js": "ical.js",
|
||||
ics: "ics",
|
||||
tsdav: "tsdav",
|
||||
googleapis: "googleapis",
|
||||
"@googleapis/calendar": "@googleapis/calendar",
|
||||
rrule: "rrule",
|
||||
"@hubspot/api-client": "@hubspot/api-client",
|
||||
querystring: "querystring",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { google } from "googleapis";
|
||||
import { admin_directory_v1 } from "@googleapis/admin";
|
||||
import { OAuth2Client } from "googleapis-common";
|
||||
import { z } from "zod";
|
||||
|
||||
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
|
||||
@@ -48,14 +49,15 @@ export const getUsersFromGWorkspace = async ({}: CheckForGCalOptions) => {
|
||||
|
||||
const credentials = credentialsSchema.parse(hasExistingCredentials.key);
|
||||
|
||||
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret);
|
||||
const oAuth2Client = new OAuth2Client(client_id, client_secret);
|
||||
|
||||
// Set users credentials instead of our app credentials - allowing us to make requests on their behalf
|
||||
oAuth2Client.setCredentials(credentials);
|
||||
|
||||
// Create a new instance of the Admin SDK directory API
|
||||
const directory = google.admin({ version: "directory_v1", auth: oAuth2Client });
|
||||
|
||||
const directory = new admin_directory_v1.Admin({
|
||||
auth: oAuth2Client as any,
|
||||
});
|
||||
const { data } = await directory.users.list({
|
||||
maxResults: 200, // Up this if we ever need to get more than 200 users
|
||||
customer: "my_customer", // This only works for single domain setups - we'll need to change this if we ever support multi-domain setups (unlikely we'll ever need to)
|
||||
|
||||
Vendored
+1
-1
@@ -5,7 +5,7 @@ import type {
|
||||
SelectedCalendar as _SelectedCalendar,
|
||||
} from "@prisma/client";
|
||||
import type { Dayjs } from "dayjs";
|
||||
import type { calendar_v3 } from "googleapis";
|
||||
import type { calendar_v3 } from "@googleapis/calendar";
|
||||
import type { Time } from "ical.js";
|
||||
import type { TFunction } from "next-i18next";
|
||||
import type z from "zod";
|
||||
|
||||
Reference in new issue
Block a user