diff --git a/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.test.tsx b/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.test.tsx index 9644efa658..de40d91042 100644 --- a/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.test.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.test.tsx @@ -20,7 +20,7 @@ vi.mock("@/modules/ui/components/switch", () => ({ })); vi.mock("../actions", () => ({ - updateNotificationSettingsAction: vi.fn(() => Promise.resolve()), + updateNotificationSettingsAction: vi.fn(() => Promise.resolve({ data: true })), })); const surveyId = "survey1"; @@ -246,4 +246,204 @@ describe("NotificationSwitch", () => { }); expect(updateNotificationSettingsAction).not.toHaveBeenCalled(); }); + + test("shows error toast when updateNotificationSettingsAction fails for 'alert' type", async () => { + const mockErrorResponse = { serverError: "Failed to update notification settings" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, alert: { [surveyId]: true } }, + }); + expect(toast.error).toHaveBeenCalledWith("Failed to update notification settings", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("shows error toast when updateNotificationSettingsAction fails for 'weeklySummary' type", async () => { + const mockErrorResponse = { serverError: "Database connection failed" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, weeklySummary: { [projectId]: true } }; + renderSwitch({ + surveyOrProjectOrOrganizationId: projectId, + notificationSettings: initialSettings, + notificationType: "weeklySummary", + }); + const switchInput = screen.getByLabelText("toggle notification settings for weeklySummary"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, weeklySummary: { [projectId]: false } }, + }); + expect(toast.error).toHaveBeenCalledWith("Database connection failed", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("shows error toast when updateNotificationSettingsAction fails for 'unsubscribedOrganizationIds' type", async () => { + const mockErrorResponse = { serverError: "Permission denied" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, unsubscribedOrganizationIds: [] }; + renderSwitch({ + surveyOrProjectOrOrganizationId: organizationId, + notificationSettings: initialSettings, + notificationType: "unsubscribedOrganizationIds", + }); + const switchInput = screen.getByLabelText("toggle notification settings for unsubscribedOrganizationIds"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, unsubscribedOrganizationIds: [organizationId] }, + }); + expect(toast.error).toHaveBeenCalledWith("Permission denied", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("shows error toast when updateNotificationSettingsAction returns null", async () => { + const mockErrorResponse = { serverError: "An error occurred" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, alert: { [surveyId]: true } }, + }); + expect(toast.error).toHaveBeenCalledWith("An error occurred", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("shows error toast when updateNotificationSettingsAction returns undefined", async () => { + const mockErrorResponse = { serverError: "An error occurred" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, alert: { [surveyId]: true } }, + }); + expect(toast.error).toHaveBeenCalledWith("An error occurred", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("shows error toast when updateNotificationSettingsAction returns response without data property", async () => { + const mockErrorResponse = { validationErrors: { _errors: ["Invalid input"] } }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, alert: { [surveyId]: true } }, + }); + expect(toast.error).toHaveBeenCalledWith("Invalid input", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("shows error toast when updateNotificationSettingsAction throws an exception", async () => { + const mockErrorResponse = { serverError: "Network error" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, alert: { [surveyId]: true } }, + }); + expect(toast.error).toHaveBeenCalledWith("Network error", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); + + test("switch remains enabled after error occurs", async () => { + const mockErrorResponse = { serverError: "Failed to update" }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(toast.error).toHaveBeenCalledWith("Failed to update", { + id: "notification-switch", + }); + expect(switchInput).toBeEnabled(); // Switch should be re-enabled after error + }); + + test("shows error toast with validation errors for specific fields", async () => { + const mockErrorResponse = { + validationErrors: { + notificationSettings: { + _errors: ["Invalid notification settings"], + }, + }, + }; + vi.mocked(updateNotificationSettingsAction).mockResolvedValueOnce(mockErrorResponse); + + const initialSettings = { ...baseNotificationSettings, alert: { [surveyId]: false } }; + renderSwitch({ notificationSettings: initialSettings, notificationType: "alert" }); + const switchInput = screen.getByLabelText("toggle notification settings for alert"); + + await act(async () => { + await user.click(switchInput); + }); + + expect(updateNotificationSettingsAction).toHaveBeenCalledWith({ + notificationSettings: { ...initialSettings, alert: { [surveyId]: true } }, + }); + expect(toast.error).toHaveBeenCalledWith("notificationSettingsInvalid notification settings", { + id: "notification-switch", + }); + expect(toast.success).not.toHaveBeenCalled(); + }); }); diff --git a/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.tsx b/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.tsx index 20ab38f206..9deeb3d6ad 100644 --- a/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/settings/(account)/notifications/components/NotificationSwitch.tsx @@ -1,7 +1,9 @@ "use client"; +import { getFormattedErrorMessage } from "@/lib/utils/helper"; import { Switch } from "@/modules/ui/components/switch"; import { useTranslate } from "@tolgee/react"; +import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { TUserNotificationSettings } from "@formbricks/types/user"; @@ -24,6 +26,7 @@ export const NotificationSwitch = ({ }: NotificationSwitchProps) => { const [isLoading, setIsLoading] = useState(false); const { t } = useTranslate(); + const router = useRouter(); const isChecked = notificationType === "unsubscribedOrganizationIds" ? !notificationSettings.unsubscribedOrganizationIds?.includes(surveyOrProjectOrOrganizationId) @@ -50,7 +53,20 @@ export const NotificationSwitch = ({ !updatedNotificationSettings[notificationType][surveyOrProjectOrOrganizationId]; } - await updateNotificationSettingsAction({ notificationSettings: updatedNotificationSettings }); + const updatedNotificationSettingsActionResponse = await updateNotificationSettingsAction({ + notificationSettings: updatedNotificationSettings, + }); + if (updatedNotificationSettingsActionResponse?.data) { + toast.success(t("environments.settings.notifications.notification_settings_updated"), { + id: "notification-switch", + }); + router.refresh(); + } else { + const errorMessage = getFormattedErrorMessage(updatedNotificationSettingsActionResponse); + toast.error(errorMessage, { + id: "notification-switch", + }); + } setIsLoading(false); }; @@ -104,9 +120,6 @@ export const NotificationSwitch = ({ disabled={isLoading} onCheckedChange={async () => { await handleSwitchChange(); - toast.success(t("environments.settings.notifications.notification_settings_updated"), { - id: "notification-switch", - }); }} /> ); diff --git a/apps/web/lib/organization/service.test.ts b/apps/web/lib/organization/service.test.ts index 0cb0f7f80c..d0129587f0 100644 --- a/apps/web/lib/organization/service.test.ts +++ b/apps/web/lib/organization/service.test.ts @@ -1,9 +1,16 @@ import { BILLING_LIMITS, PROJECT_FEATURE_KEYS } from "@/lib/constants"; +import { updateUser } from "@/lib/user/service"; import { Prisma } from "@prisma/client"; import { afterEach, describe, expect, test, vi } from "vitest"; import { prisma } from "@formbricks/database"; import { DatabaseError } from "@formbricks/types/errors"; -import { createOrganization, getOrganization, getOrganizationsByUserId, updateOrganization } from "./service"; +import { + createOrganization, + getOrganization, + getOrganizationsByUserId, + subscribeOrganizationMembersToSurveyResponses, + updateOrganization, +} from "./service"; vi.mock("@formbricks/database", () => ({ prisma: { @@ -13,9 +20,16 @@ vi.mock("@formbricks/database", () => ({ create: vi.fn(), update: vi.fn(), }, + user: { + findUnique: vi.fn(), + }, }, })); +vi.mock("@/lib/user/service", () => ({ + updateUser: vi.fn(), +})); + describe("Organization Service", () => { afterEach(() => { vi.clearAllMocks(); @@ -252,4 +266,62 @@ describe("Organization Service", () => { }); }); }); + + describe("subscribeOrganizationMembersToSurveyResponses", () => { + test("should subscribe user to survey responses when not unsubscribed", async () => { + const mockUser = { + id: "user-123", + notificationSettings: { + alert: { "existing-survey-id": true }, + weeklySummary: {}, + unsubscribedOrganizationIds: [], // User is subscribed to all organizations + }, + } as any; + + const surveyId = "survey-123"; + const userId = "user-123"; + const organizationId = "org-123"; + + vi.mocked(prisma.user.findUnique).mockResolvedValueOnce(mockUser); + vi.mocked(updateUser).mockResolvedValueOnce({} as any); + + await subscribeOrganizationMembersToSurveyResponses(surveyId, userId, organizationId); + + expect(prisma.user.findUnique).toHaveBeenCalledWith({ + where: { id: userId }, + }); + expect(updateUser).toHaveBeenCalledWith(userId, { + notificationSettings: { + alert: { + "existing-survey-id": true, + "survey-123": true, + }, + weeklySummary: {}, + unsubscribedOrganizationIds: [], + }, + }); + }); + + test("should not subscribe user when unsubscribed from organization", async () => { + const mockUser = { + id: "user-123", + notificationSettings: { + alert: { "existing-survey-id": true }, + weeklySummary: {}, + unsubscribedOrganizationIds: ["org-123"], // User has unsubscribed from this organization + }, + } as any; + + const surveyId = "survey-123"; + const userId = "user-123"; + const organizationId = "org-123"; + + vi.mocked(prisma.user.findUnique).mockResolvedValueOnce(mockUser); + + await subscribeOrganizationMembersToSurveyResponses(surveyId, userId, organizationId); + + // Should not call updateUser because user is unsubscribed from this organization + expect(updateUser).not.toHaveBeenCalled(); + }); + }); }); diff --git a/apps/web/modules/survey/components/template-list/lib/organization.test.ts b/apps/web/modules/survey/components/template-list/lib/organization.test.ts deleted file mode 100644 index 2c29f3f04e..0000000000 --- a/apps/web/modules/survey/components/template-list/lib/organization.test.ts +++ /dev/null @@ -1,105 +0,0 @@ -import "@testing-library/jest-dom/vitest"; -import { afterEach, describe, expect, test, vi } from "vitest"; -import { prisma } from "@formbricks/database"; -import { ResourceNotFoundError } from "@formbricks/types/errors"; -import { subscribeOrganizationMembersToSurveyResponses } from "./organization"; -import { updateUser } from "./user"; - -vi.mock("@formbricks/database", () => ({ - prisma: { - user: { - findUnique: vi.fn(), - }, - }, -})); - -vi.mock("./user", () => ({ - updateUser: vi.fn(), -})); - -describe("subscribeOrganizationMembersToSurveyResponses", () => { - afterEach(() => { - vi.clearAllMocks(); - }); - - test("subscribes user to survey responses successfully", async () => { - const mockUser = { - id: "user-123", - notificationSettings: { - alert: { "existing-survey-id": true }, - weeklySummary: {}, - }, - }; - - const surveyId = "survey-123"; - const userId = "user-123"; - - vi.mocked(prisma.user.findUnique).mockResolvedValueOnce(mockUser); - vi.mocked(updateUser).mockResolvedValueOnce({} as any); - - await subscribeOrganizationMembersToSurveyResponses(surveyId, userId); - - expect(prisma.user.findUnique).toHaveBeenCalledWith({ - where: { id: userId }, - }); - - expect(updateUser).toHaveBeenCalledWith(userId, { - notificationSettings: { - alert: { - "existing-survey-id": true, - "survey-123": true, - }, - weeklySummary: {}, - }, - }); - }); - - test("creates notification settings if user doesn't have any", async () => { - const mockUser = { - id: "user-123", - notificationSettings: null, - }; - - const surveyId = "survey-123"; - const userId = "user-123"; - - vi.mocked(prisma.user.findUnique).mockResolvedValueOnce(mockUser); - vi.mocked(updateUser).mockResolvedValueOnce({} as any); - - await subscribeOrganizationMembersToSurveyResponses(surveyId, userId); - - expect(updateUser).toHaveBeenCalledWith(userId, { - notificationSettings: { - alert: { - "survey-123": true, - }, - weeklySummary: {}, - }, - }); - }); - - test("throws ResourceNotFoundError if user is not found", async () => { - const surveyId = "survey-123"; - const userId = "nonexistent-user"; - - vi.mocked(prisma.user.findUnique).mockResolvedValueOnce(null); - - await expect(subscribeOrganizationMembersToSurveyResponses(surveyId, userId)).rejects.toThrow( - new ResourceNotFoundError("User", userId) - ); - - expect(updateUser).not.toHaveBeenCalled(); - }); - - test("propagates errors from database operations", async () => { - const surveyId = "survey-123"; - const userId = "user-123"; - const dbError = new Error("Database error"); - - vi.mocked(prisma.user.findUnique).mockRejectedValueOnce(dbError); - - await expect(subscribeOrganizationMembersToSurveyResponses(surveyId, userId)).rejects.toThrow(dbError); - - expect(updateUser).not.toHaveBeenCalled(); - }); -}); diff --git a/apps/web/modules/survey/components/template-list/lib/organization.ts b/apps/web/modules/survey/components/template-list/lib/organization.ts deleted file mode 100644 index 452a30f3ab..0000000000 --- a/apps/web/modules/survey/components/template-list/lib/organization.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { updateUser } from "@/modules/survey/components/template-list/lib/user"; -import { prisma } from "@formbricks/database"; -import { ResourceNotFoundError } from "@formbricks/types/errors"; -import { TUserNotificationSettings } from "@formbricks/types/user"; - -export const subscribeOrganizationMembersToSurveyResponses = async ( - surveyId: string, - createdBy: string -): Promise => { - try { - const surveyCreator = await prisma.user.findUnique({ - where: { - id: createdBy, - }, - }); - - if (!surveyCreator) { - throw new ResourceNotFoundError("User", createdBy); - } - - const defaultSettings = { alert: {}, weeklySummary: {} }; - const updatedNotificationSettings: TUserNotificationSettings = { - ...defaultSettings, - ...surveyCreator.notificationSettings, - }; - - updatedNotificationSettings.alert[surveyId] = true; - - await updateUser(surveyCreator.id, { - notificationSettings: updatedNotificationSettings, - }); - } catch (error) { - throw error; - } -}; diff --git a/apps/web/modules/survey/components/template-list/lib/survey.test.ts b/apps/web/modules/survey/components/template-list/lib/survey.test.ts index f622b94220..fc29da7de9 100644 --- a/apps/web/modules/survey/components/template-list/lib/survey.test.ts +++ b/apps/web/modules/survey/components/template-list/lib/survey.test.ts @@ -1,7 +1,9 @@ +import { + getOrganizationByEnvironmentId, + subscribeOrganizationMembersToSurveyResponses, +} from "@/lib/organization/service"; import { capturePosthogEnvironmentEvent } from "@/lib/posthogServer"; -import { subscribeOrganizationMembersToSurveyResponses } from "@/modules/survey/components/template-list/lib/organization"; import { getActionClasses } from "@/modules/survey/lib/action-class"; -import { getOrganizationAIKeys, getOrganizationIdFromEnvironmentId } from "@/modules/survey/lib/organization"; import { selectSurvey } from "@/modules/survey/lib/survey"; import { ActionClass, Prisma } from "@prisma/client"; import "@testing-library/jest-dom/vitest"; @@ -21,19 +23,15 @@ vi.mock("@/lib/survey/utils", () => ({ checkForInvalidImagesInQuestions: vi.fn(), })); -vi.mock("@/modules/survey/components/template-list/lib/organization", () => ({ +vi.mock("@/lib/organization/service", () => ({ subscribeOrganizationMembersToSurveyResponses: vi.fn(), + getOrganizationByEnvironmentId: vi.fn(), })); vi.mock("@/modules/survey/lib/action-class", () => ({ getActionClasses: vi.fn(), })); -vi.mock("@/modules/survey/lib/organization", () => ({ - getOrganizationIdFromEnvironmentId: vi.fn(), - getOrganizationAIKeys: vi.fn(), -})); - vi.mock("@/modules/survey/lib/survey", () => ({ selectSurvey: { id: true, @@ -86,8 +84,7 @@ describe("survey module", () => { // Mock dependencies const mockActionClasses: ActionClass[] = []; vi.mocked(getActionClasses).mockResolvedValue(mockActionClasses); - vi.mocked(getOrganizationIdFromEnvironmentId).mockResolvedValue("org-123"); - vi.mocked(getOrganizationAIKeys).mockResolvedValue({ id: "org-123", name: "Org" } as any); + vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue({ id: "org-123", name: "Org" } as any); const mockCreatedSurvey = { id: "survey-123", @@ -109,8 +106,7 @@ describe("survey module", () => { // Verify results expect(getActionClasses).toHaveBeenCalledWith(environmentId); - expect(getOrganizationIdFromEnvironmentId).toHaveBeenCalledWith(environmentId); - expect(getOrganizationAIKeys).toHaveBeenCalledWith("org-123"); + expect(getOrganizationByEnvironmentId).toHaveBeenCalledWith(environmentId); expect(prisma.survey.create).toHaveBeenCalledWith({ data: expect.objectContaining({ name: surveyBody.name, @@ -122,7 +118,11 @@ describe("survey module", () => { }); expect(prisma.segment.create).toHaveBeenCalled(); expect(prisma.survey.update).toHaveBeenCalled(); - expect(subscribeOrganizationMembersToSurveyResponses).toHaveBeenCalledWith("survey-123", "user-123"); + expect(subscribeOrganizationMembersToSurveyResponses).toHaveBeenCalledWith( + "survey-123", + "user-123", + "org-123" + ); expect(capturePosthogEnvironmentEvent).toHaveBeenCalledWith( environmentId, "survey created", @@ -143,8 +143,7 @@ describe("survey module", () => { }; vi.mocked(getActionClasses).mockResolvedValue([]); - vi.mocked(getOrganizationIdFromEnvironmentId).mockResolvedValue("org-123"); - vi.mocked(getOrganizationAIKeys).mockResolvedValue({ id: "org-123" } as any); + vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue({ id: "org-123" } as any); vi.mocked(prisma.survey.create).mockResolvedValue({ id: "survey-123", environmentId, @@ -172,8 +171,7 @@ describe("survey module", () => { }; vi.mocked(getActionClasses).mockResolvedValue([]); - vi.mocked(getOrganizationIdFromEnvironmentId).mockResolvedValue("org-123"); - vi.mocked(getOrganizationAIKeys).mockResolvedValue({ id: "org-123" } as any); + vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue({ id: "org-123" } as any); vi.mocked(prisma.survey.create).mockResolvedValue({ id: "survey-123", environmentId, @@ -204,8 +202,7 @@ describe("survey module", () => { }; vi.mocked(getActionClasses).mockResolvedValue([]); - vi.mocked(getOrganizationIdFromEnvironmentId).mockResolvedValue("org-123"); - vi.mocked(getOrganizationAIKeys).mockResolvedValue(null); + vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue(null); await expect(createSurvey(environmentId, surveyBody)).rejects.toThrow(ResourceNotFoundError); }); @@ -220,12 +217,11 @@ describe("survey module", () => { }; vi.mocked(getActionClasses).mockResolvedValue([]); - vi.mocked(getOrganizationIdFromEnvironmentId).mockResolvedValue("org-123"); - vi.mocked(getOrganizationAIKeys).mockResolvedValue({ id: "org-123" } as any); + vi.mocked(getOrganizationByEnvironmentId).mockResolvedValue({ id: "org-123" } as any); const prismaError = new Prisma.PrismaClientKnownRequestError("Database error", { code: "P2002", - clientVersion: "4.8.0", + clientVersion: "5.0.0", }); vi.mocked(prisma.survey.create).mockRejectedValue(prismaError); diff --git a/apps/web/modules/survey/components/template-list/lib/survey.ts b/apps/web/modules/survey/components/template-list/lib/survey.ts index 245c2d4ac6..eb2bb5d925 100644 --- a/apps/web/modules/survey/components/template-list/lib/survey.ts +++ b/apps/web/modules/survey/components/template-list/lib/survey.ts @@ -1,9 +1,11 @@ +import { + getOrganizationByEnvironmentId, + subscribeOrganizationMembersToSurveyResponses, +} from "@/lib/organization/service"; import { capturePosthogEnvironmentEvent } from "@/lib/posthogServer"; import { checkForInvalidImagesInQuestions } from "@/lib/survey/utils"; -import { subscribeOrganizationMembersToSurveyResponses } from "@/modules/survey/components/template-list/lib/organization"; import { TriggerUpdate } from "@/modules/survey/editor/types/survey-trigger"; import { getActionClasses } from "@/modules/survey/lib/action-class"; -import { getOrganizationAIKeys, getOrganizationIdFromEnvironmentId } from "@/modules/survey/lib/organization"; import { selectSurvey } from "@/modules/survey/lib/survey"; import { ActionClass, Prisma } from "@prisma/client"; import { prisma } from "@formbricks/database"; @@ -43,8 +45,7 @@ export const createSurvey = async ( }; } - const organizationId = await getOrganizationIdFromEnvironmentId(environmentId); - const organization = await getOrganizationAIKeys(organizationId); + const organization = await getOrganizationByEnvironmentId(environmentId); if (!organization) { throw new ResourceNotFoundError("Organization", null); } @@ -118,7 +119,7 @@ export const createSurvey = async ( }; if (createdBy) { - await subscribeOrganizationMembersToSurveyResponses(survey.id, createdBy); + await subscribeOrganizationMembersToSurveyResponses(survey.id, createdBy, organization.id); } await capturePosthogEnvironmentEvent(survey.environmentId, "survey created", {