mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-24 02:47:53 -05:00
aa83ee336c
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
110 lines
3.9 KiB
TypeScript
110 lines
3.9 KiB
TypeScript
import { google } from "googleapis";
|
|
import { getServerSession } from "next-auth";
|
|
import { logger } from "@formbricks/logger";
|
|
import { TIntegrationGoogleSheetsConfig } from "@formbricks/types/integration/google-sheet";
|
|
import { responses } from "@/app/lib/api/response";
|
|
import {
|
|
GOOGLE_SHEETS_CLIENT_ID,
|
|
GOOGLE_SHEETS_CLIENT_SECRET,
|
|
GOOGLE_SHEETS_REDIRECT_URL,
|
|
WEBAPP_URL,
|
|
} from "@/lib/constants";
|
|
import { createOrUpdateIntegration, getIntegrationByType } from "@/lib/integration/service";
|
|
import { capturePostHogEvent } from "@/lib/posthog";
|
|
import { getOrganizationIdFromWorkspaceId } from "@/lib/utils/helper";
|
|
import { hasUserWorkspaceAccess } from "@/lib/workspace/auth";
|
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
|
|
export const GET = async (req: Request) => {
|
|
const url = new URL(req.url);
|
|
const workspaceId = url.searchParams.get("state");
|
|
const code = url.searchParams.get("code");
|
|
|
|
if (!workspaceId) {
|
|
return responses.badRequestResponse("Invalid workspaceId");
|
|
}
|
|
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) {
|
|
return responses.notAuthenticatedResponse();
|
|
}
|
|
|
|
const canUserAccessWorkspace = await hasUserWorkspaceAccess(session.user.id, workspaceId);
|
|
if (!canUserAccessWorkspace) {
|
|
return responses.unauthorizedResponse();
|
|
}
|
|
|
|
const basePath = `/workspaces/${workspaceId}/settings/workspace`;
|
|
|
|
if (code && typeof code !== "string") {
|
|
return responses.badRequestResponse("`code` must be a string");
|
|
}
|
|
|
|
const client_id = GOOGLE_SHEETS_CLIENT_ID;
|
|
const client_secret = GOOGLE_SHEETS_CLIENT_SECRET;
|
|
const redirect_uri = GOOGLE_SHEETS_REDIRECT_URL;
|
|
if (!client_id) return responses.internalServerErrorResponse("Google client id is missing");
|
|
if (!client_secret) return responses.internalServerErrorResponse("Google client secret is missing");
|
|
if (!redirect_uri) return responses.internalServerErrorResponse("Google redirect url is missing");
|
|
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
|
|
|
|
if (!code) {
|
|
return Response.redirect(`${WEBAPP_URL}${basePath}/integrations/google-sheets`);
|
|
}
|
|
|
|
const token = await oAuth2Client.getToken(code);
|
|
const key = token.res?.data;
|
|
if (!key) {
|
|
return Response.redirect(`${WEBAPP_URL}${basePath}/integrations/google-sheets`);
|
|
}
|
|
|
|
oAuth2Client.setCredentials({ access_token: key.access_token });
|
|
const oauth2 = google.oauth2({ auth: oAuth2Client, version: "v2" });
|
|
const userInfo = await oauth2.userinfo.get();
|
|
const userEmail = userInfo.data.email;
|
|
|
|
if (!userEmail) {
|
|
return responses.internalServerErrorResponse("Failed to get user email");
|
|
}
|
|
|
|
const integrationType = "googleSheets" as const;
|
|
const existingIntegration = await getIntegrationByType(workspaceId, integrationType);
|
|
const existingConfig = existingIntegration?.config as TIntegrationGoogleSheetsConfig;
|
|
|
|
const googleSheetIntegration = {
|
|
type: integrationType,
|
|
config: {
|
|
key,
|
|
data: existingConfig?.data ?? [],
|
|
email: userEmail,
|
|
},
|
|
};
|
|
|
|
const result = await createOrUpdateIntegration(workspaceId, googleSheetIntegration);
|
|
if (result) {
|
|
try {
|
|
const organizationId = await getOrganizationIdFromWorkspaceId(workspaceId);
|
|
capturePostHogEvent(session.user.id, "integration_connected", {
|
|
integration_type: "googleSheets",
|
|
organization_id: organizationId,
|
|
});
|
|
capturePostHogEvent(
|
|
session.user.id,
|
|
"integration_connected",
|
|
{
|
|
integration_type: "googleSheets",
|
|
organization_id: organizationId,
|
|
workspace_id: workspaceId,
|
|
},
|
|
{ organizationId, workspaceId }
|
|
);
|
|
} catch (err) {
|
|
logger.error({ error: err }, "Failed to capture PostHog integration_connected event for googleSheets");
|
|
}
|
|
|
|
return Response.redirect(`${WEBAPP_URL}${basePath}/integrations/google-sheets`);
|
|
}
|
|
|
|
return responses.internalServerErrorResponse("Failed to create or update Google Sheets integration");
|
|
};
|