fix: google sheet integration error message (#4899)

This commit is contained in:
Piyush Gupta
2025-03-16 21:40:51 +05:30
committed by GitHub
parent a371bdaedd
commit c2d237a99a
2 changed files with 46 additions and 22 deletions

View File

@@ -1,25 +1,41 @@
"use server"; "use server";
import { authOptions } from "@/modules/auth/lib/authOptions"; import { authenticatedActionClient } from "@/lib/utils/action-client";
import { getServerSession } from "next-auth"; import { checkAuthorizationUpdated } from "@/lib/utils/action-client-middleware";
import { hasUserEnvironmentAccess } from "@formbricks/lib/environment/auth"; import { getOrganizationIdFromEnvironmentId, getProjectIdFromEnvironmentId } from "@/lib/utils/helper";
import { z } from "zod";
import { getSpreadsheetNameById } from "@formbricks/lib/googleSheet/service"; import { getSpreadsheetNameById } from "@formbricks/lib/googleSheet/service";
import { AuthorizationError } from "@formbricks/types/errors"; import { ZIntegrationGoogleSheets } from "@formbricks/types/integration/google-sheet";
import { TIntegrationGoogleSheets } from "@formbricks/types/integration/google-sheet";
export async function getSpreadsheetNameByIdAction( const ZGetSpreadsheetNameByIdAction = z.object({
googleSheetIntegration: TIntegrationGoogleSheets, googleSheetIntegration: ZIntegrationGoogleSheets,
environmentId: string, environmentId: z.string(),
spreadsheetId: string spreadsheetId: z.string(),
) { });
const session = await getServerSession(authOptions);
if (!session) throw new AuthorizationError("Not authorized");
const isAuthorized = await hasUserEnvironmentAccess(session.user.id, environmentId); export const getSpreadsheetNameByIdAction = authenticatedActionClient
if (!isAuthorized) throw new AuthorizationError("Not authorized"); .schema(ZGetSpreadsheetNameByIdAction)
const integrationData = structuredClone(googleSheetIntegration); .action(async ({ ctx, parsedInput }) => {
integrationData.config.data.forEach((data) => { await checkAuthorizationUpdated({
data.createdAt = new Date(data.createdAt); userId: ctx.user.id,
organizationId: await getOrganizationIdFromEnvironmentId(parsedInput.environmentId),
access: [
{
type: "organization",
roles: ["owner", "manager"],
},
{
type: "projectTeam",
projectId: await getProjectIdFromEnvironmentId(parsedInput.environmentId),
minPermission: "readWrite",
},
],
});
const integrationData = structuredClone(parsedInput.googleSheetIntegration);
integrationData.config.data.forEach((data) => {
data.createdAt = new Date(data.createdAt);
});
return await getSpreadsheetNameById(integrationData, parsedInput.spreadsheetId);
}); });
return await getSpreadsheetNameById(integrationData, spreadsheetId);
}

View File

@@ -8,6 +8,7 @@ import {
isValidGoogleSheetsUrl, isValidGoogleSheetsUrl,
} from "@/app/(app)/environments/[environmentId]/integrations/google-sheets/lib/util"; } from "@/app/(app)/environments/[environmentId]/integrations/google-sheets/lib/util";
import GoogleSheetLogo from "@/images/googleSheetsLogo.png"; import GoogleSheetLogo from "@/images/googleSheetsLogo.png";
import { getFormattedErrorMessage } from "@/lib/utils/helper";
import { AdditionalIntegrationSettings } from "@/modules/ui/components/additional-integration-settings"; import { AdditionalIntegrationSettings } from "@/modules/ui/components/additional-integration-settings";
import { Button } from "@/modules/ui/components/button"; import { Button } from "@/modules/ui/components/button";
import { Checkbox } from "@/modules/ui/components/checkbox"; import { Checkbox } from "@/modules/ui/components/checkbox";
@@ -115,11 +116,18 @@ export const AddIntegrationModal = ({
throw new Error(t("environments.integrations.select_at_least_one_question_error")); throw new Error(t("environments.integrations.select_at_least_one_question_error"));
} }
const spreadsheetId = extractSpreadsheetIdFromUrl(spreadsheetUrl); const spreadsheetId = extractSpreadsheetIdFromUrl(spreadsheetUrl);
const spreadsheetName = await getSpreadsheetNameByIdAction( const spreadsheetNameResponse = await getSpreadsheetNameByIdAction({
googleSheetIntegration, googleSheetIntegration,
environmentId, environmentId,
spreadsheetId spreadsheetId,
); });
if (!spreadsheetNameResponse?.data) {
const errorMessage = getFormattedErrorMessage(spreadsheetNameResponse);
throw new Error(errorMessage);
}
const spreadsheetName = spreadsheetNameResponse.data;
setIsLinkingSheet(true); setIsLinkingSheet(true);
integrationData.spreadsheetId = spreadsheetId; integrationData.spreadsheetId = spreadsheetId;