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

@@ -1,5 +1,6 @@
import type { Membership, Team, UserPermissionRole } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { google } from "googleapis";
import type { AuthOptions, Session, User } from "next-auth";
import type { JWT } from "next-auth/jwt";
import { encode } from "next-auth/jwt";
@@ -15,7 +16,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";
@@ -25,6 +31,7 @@ import { randomString } from "@calcom/lib/random";
import { safeStringify } from "@calcom/lib/safeStringify";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import { UserRepository } from "@calcom/lib/server/repository/user";
import { GoogleService } from "@calcom/lib/server/service/google";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { IdentityProvider, MembershipRole } from "@calcom/prisma/enums";
@@ -251,6 +258,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 +622,55 @@ export const getOptions = ({
return await autoMergeIdentities();
}
const grantedScopes = account.scope?.split(" ") ?? [];
if (
account.provider === "google" &&
!(await GoogleService.findFirstGoogleCalendarCredential({
userId: user.id as number,
})) &&
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 GoogleService.createGoogleCalendarCredential({
userId: user.id as number,
key: credentialkey,
});
if (
!(await GoogleService.findGoogleMeetCredential({
userId: user.id as number,
}))
) {
await GoogleService.createGoogleMeetsCredential({
userId: user.id as number,
});
}
const oAuth2Client = new google.auth.OAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET);
oAuth2Client.setCredentials(credentialkey);
const calendar = google.calendar({
version: "v3",
auth: oAuth2Client,
});
const primaryCal = await GoogleService.getPrimaryCalendar(calendar);
if (primaryCal?.id) {
await GoogleService.createSelectedCalendar({
credentialId: gcalCredential.id,
userId: user.id as number,
externalId: primaryCal.id,
});
}
await GoogleService.updateProfilePhoto(oAuth2Client, user.id as number);
}
return {
...token,
id: existingUser.id,
@@ -632,7 +695,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,