[Feat] Bulk Invite of Users + Google Workspace (#8969)
* Base UI work * Bulk invite users * WIP workspace oauth implementation * Seperate components - add existing gcal check * Move callback session to getServerSession * Implementation of state redirect back to teams on login * Add callback to populate text field * Change error message * Redirect from callback and open modal * Fix bulk translations * Fix translations for google button * Delete Query * Feature flag this * Update packages/trpc/server/routers/viewer/teams/inviteMember.handler.ts * Check if Gcal in installed globally * Add new router * Add missing [trpc] route * Feedback * Update packages/trpc/server/routers/viewer/googleWorkspace/googleWorkspace.handler.ts * Typefixes * More typefixes --------- Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: zomars <[email protected]>
This commit is contained in:
22 files changed
+738
-170
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { google } from "googleapis";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
|
||||
const scopes = [
|
||||
"https://www.googleapis.com/auth/admin.directory.user.readonly",
|
||||
"https://www.googleapis.com/auth/admin.directory.customer.readonly",
|
||||
];
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") {
|
||||
// Get appKeys from google-calendar
|
||||
const { client_id, client_secret } = await getAppKeysFromSlug("google-calendar");
|
||||
if (!client_id || typeof client_id !== "string")
|
||||
return res.status(400).json({ message: "Google client_id missing." });
|
||||
if (!client_secret || typeof client_secret !== "string")
|
||||
return res.status(400).json({ message: "Google client_secret missing." });
|
||||
|
||||
// use differnt callback to normal calendar connection
|
||||
const redirect_uri = WEBAPP_URL + "/api/teams/googleworkspace/callback";
|
||||
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
||||
|
||||
const authUrl = oAuth2Client.generateAuthUrl({
|
||||
access_type: "offline",
|
||||
scope: scopes,
|
||||
|
||||
prompt: "consent",
|
||||
state: JSON.stringify({ teamId: req.query.teamId }),
|
||||
});
|
||||
|
||||
res.status(200).json({ url: authUrl });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { google } from "googleapis";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { z } from "zod";
|
||||
|
||||
import getAppKeysFromSlug from "@calcom/app-store/_utils/getAppKeysFromSlug";
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
const stateSchema = z.object({
|
||||
teamId: z.string(),
|
||||
});
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getServerSession({ req, res });
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return res.status(401).json({ message: "You must be logged in to do this" });
|
||||
}
|
||||
|
||||
const { code, state } = req.query;
|
||||
const parsedState = stateSchema.parse(JSON.parse(state as string));
|
||||
const { teamId } = parsedState;
|
||||
|
||||
if (code && typeof code !== "string") {
|
||||
res.status(400).json({ message: "`code` must be a string" });
|
||||
return;
|
||||
}
|
||||
|
||||
const { client_id, client_secret } = await getAppKeysFromSlug("google-calendar");
|
||||
|
||||
if (!client_id || typeof client_id !== "string")
|
||||
return res.status(400).json({ message: "Google client_id missing." });
|
||||
if (!client_secret || typeof client_secret !== "string")
|
||||
return res.status(400).json({ message: "Google client_secret missing." });
|
||||
|
||||
const redirect_uri = WEBAPP_URL + "/api/teams/googleworkspace/callback";
|
||||
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
||||
|
||||
if (!code) {
|
||||
throw new Error("No code provided");
|
||||
}
|
||||
|
||||
const credentials = await oAuth2Client.getToken(code);
|
||||
|
||||
await prisma.credential.create({
|
||||
data: {
|
||||
type: "google_workspace_directory",
|
||||
key: credentials.res?.data,
|
||||
userId: session.user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!teamId) {
|
||||
res.redirect(getSafeRedirectUrl(WEBAPP_URL + "/settings") ?? `${WEBAPP_URL}/teams`);
|
||||
}
|
||||
|
||||
res.redirect(
|
||||
getSafeRedirectUrl(WEBAPP_URL + `/settings/teams/${teamId}/members?inviteModal=true&bulk=true`) ??
|
||||
`${WEBAPP_URL}/teams`
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createNextApiHandler } from "@calcom/trpc/server/createNextApiHandler";
|
||||
import { googleWorkspaceRouter } from "@calcom/trpc/server/routers/viewer/googleWorkspace/_router";
|
||||
|
||||
export default createNextApiHandler(googleWorkspaceRouter);
|
||||
Reference in new issue
Block a user