feat: install gcal + gvideo by default for google signups (#17773)

* feat: install gcal + gvideo by default for google signups

* add upsertSelectedCalendar

* fix type check
This commit is contained in:
Benny Joo authored and GitHub committed 2024-11-20 23:54:11 +00:00
1 parent 57bbba3d95
commit fcd06c80e3
13 files changed
+249 -170

No files matched your search

+7
View File
@@ -192,3 +192,10 @@ 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",
];
-45
View File
@@ -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 } });
}
-48
View File
@@ -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,
+138
View File
@@ -0,0 +1,138 @@
import type { Prisma } from "@prisma/client";
import type { Credentials } from "google-auth-library";
import type { OAuth2Client } from "google-auth-library";
import type { calendar_v3 } from "googleapis";
import { google } from "googleapis";
import logger from "@calcom/lib/logger";
import { CredentialRepository } from "../repository/credential";
import { SelectedCalendarRepository } from "../repository/selectedCalendar";
import { UserRepository } from "../repository/user";
export class GoogleService {
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: Omit<Prisma.SelectedCalendarUncheckedCreateInput, "integration">
) {
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",
});
}
static async findFirstGoogleCalendarCredential({ userId }: { userId: number }) {
return await CredentialRepository.findFirstByAppIdAndUserId({
appId: "google-calendar",
userId,
});
}
static 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;
}
}
static 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;
}
}
static async 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);
}
}
}