Scheduling infrastructure for absolutely everyone.
feat: Add Google Profile Photo when connecting Google Calendar (#13627)
Zeeshan Bhati9455d14ad5a4
diff --git a/packages/app-store/googlecalendar/api/add.ts b/packages/app-store/googlecalendar/api/add.tsindex 6d2c65385a7..a898fed2830 100644--- a/packages/app-store/googlecalendar/api/add.ts+++ b/packages/app-store/googlecalendar/api/add.ts@@ -11,6 +11,7 @@ import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";export const scopes = ["https://www.googleapis.com/auth/calendar.readonly","https://www.googleapis.com/auth/calendar.events",+ "https://www.googleapis.com/auth/userinfo.profile",];let client_id = "";diff --git a/packages/app-store/googlecalendar/api/callback.ts b/packages/app-store/googlecalendar/api/callback.tsindex 87fbb2ee2b4..1aec90fbe00 100644--- a/packages/app-store/googlecalendar/api/callback.ts+++ b/packages/app-store/googlecalendar/api/callback.ts@@ -1,9 +1,11 @@+import type { Auth } from "googleapis";import { google } from "googleapis";import type { NextApiRequest, NextApiResponse } from "next";import { WEBAPP_URL_FOR_OAUTH, WEBAPP_URL } from "@calcom/lib/constants";import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";import { HttpError } from "@calcom/lib/http-error";+import logger from "@calcom/lib/logger";import { defaultHandler, defaultResponder } from "@calcom/lib/server";import prisma from "@calcom/prisma";import { Prisma } from "@calcom/prisma/client";@@ -91,6 +93,10 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {throw new HttpError({ message: "Internal Error", statusCode: 500 });}+ // Update the user's profile photo with google profile photo if it's null+ // Since we don't want to block the user from using the app if this fails, we don't await this+ updateProfilePhoto(oAuth2Client, req.session.user.id);+const credential = await prisma.credential.create({data: {type: "google_calendar",@@ -156,6 +162,23 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {);}+async function updateProfilePhoto(oAuth2Client: Auth.OAuth2Client, userId: number) {+ try {+ const oauth2 = google.oauth2({ version: "v2", auth: oAuth2Client });+ const userDetails = await oauth2.userinfo.get();+ if (userDetails.data?.picture) {+ await prisma.user.update({+ where: { id: userId, avatarUrl: null, avatar: null },+ data: {+ avatarUrl: userDetails.data.picture,+ },+ });+ }+ } catch (error) {+ logger.error("Error updating avatarUrl from google calendar connect", error);+ }+}+export default defaultHandler({GET: Promise.resolve({ default: defaultResponder(getHandler) }),});