From 0e0d3780d3765b3e00cfd2fcd6f88ed591e583d9 Mon Sep 17 00:00:00 2001 From: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com> Date: Thu, 6 Mar 2025 14:40:51 +0530 Subject: [PATCH] fix: removed completed surveys from survey list in integrations (#4838) Co-authored-by: Piyush Gupta --- .../integrations/airtable/page.tsx | 2 +- .../integrations/google-sheets/page.tsx | 2 +- .../integrations/lib/surveys.ts | 48 ++++++++++++++ .../integrations/notion/page.tsx | 2 +- .../integrations/slack/page.tsx | 2 +- packages/lib/survey/service.ts | 23 ++----- packages/lib/survey/utils.ts | 64 +------------------ 7 files changed, 59 insertions(+), 84 deletions(-) create mode 100644 apps/web/app/(app)/environments/[environmentId]/integrations/lib/surveys.ts diff --git a/apps/web/app/(app)/environments/[environmentId]/integrations/airtable/page.tsx b/apps/web/app/(app)/environments/[environmentId]/integrations/airtable/page.tsx index f426f0cbae..c172f367c9 100644 --- a/apps/web/app/(app)/environments/[environmentId]/integrations/airtable/page.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/integrations/airtable/page.tsx @@ -1,4 +1,5 @@ import { AirtableWrapper } from "@/app/(app)/environments/[environmentId]/integrations/airtable/components/AirtableWrapper"; +import { getSurveys } from "@/app/(app)/environments/[environmentId]/integrations/lib/surveys"; import { authOptions } from "@/modules/auth/lib/authOptions"; import { getProjectPermissionByUserId } from "@/modules/ee/teams/lib/roles"; import { getTeamPermissionFlags } from "@/modules/ee/teams/utils/teams"; @@ -15,7 +16,6 @@ import { getIntegrations } from "@formbricks/lib/integration/service"; import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/service"; import { getAccessFlags } from "@formbricks/lib/membership/utils"; import { getProjectByEnvironmentId } from "@formbricks/lib/project/service"; -import { getSurveys } from "@formbricks/lib/survey/service"; import { findMatchingLocale } from "@formbricks/lib/utils/locale"; import { TIntegrationItem } from "@formbricks/types/integration"; import { TIntegrationAirtable } from "@formbricks/types/integration/airtable"; diff --git a/apps/web/app/(app)/environments/[environmentId]/integrations/google-sheets/page.tsx b/apps/web/app/(app)/environments/[environmentId]/integrations/google-sheets/page.tsx index 99e56933eb..19828a3876 100644 --- a/apps/web/app/(app)/environments/[environmentId]/integrations/google-sheets/page.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/integrations/google-sheets/page.tsx @@ -1,4 +1,5 @@ import { GoogleSheetWrapper } from "@/app/(app)/environments/[environmentId]/integrations/google-sheets/components/GoogleSheetWrapper"; +import { getSurveys } from "@/app/(app)/environments/[environmentId]/integrations/lib/surveys"; import { authOptions } from "@/modules/auth/lib/authOptions"; import { getProjectPermissionByUserId } from "@/modules/ee/teams/lib/roles"; import { getTeamPermissionFlags } from "@/modules/ee/teams/utils/teams"; @@ -19,7 +20,6 @@ import { getIntegrations } from "@formbricks/lib/integration/service"; import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/service"; import { getAccessFlags } from "@formbricks/lib/membership/utils"; import { getProjectByEnvironmentId } from "@formbricks/lib/project/service"; -import { getSurveys } from "@formbricks/lib/survey/service"; import { findMatchingLocale } from "@formbricks/lib/utils/locale"; import { TIntegrationGoogleSheets } from "@formbricks/types/integration/google-sheet"; diff --git a/apps/web/app/(app)/environments/[environmentId]/integrations/lib/surveys.ts b/apps/web/app/(app)/environments/[environmentId]/integrations/lib/surveys.ts new file mode 100644 index 0000000000..3039099837 --- /dev/null +++ b/apps/web/app/(app)/environments/[environmentId]/integrations/lib/surveys.ts @@ -0,0 +1,48 @@ +import "server-only"; +import { Prisma } from "@prisma/client"; +import { cache as reactCache } from "react"; +import { prisma } from "@formbricks/database"; +import { cache } from "@formbricks/lib/cache"; +import { surveyCache } from "@formbricks/lib/survey/cache"; +import { selectSurvey } from "@formbricks/lib/survey/service"; +import { transformPrismaSurvey } from "@formbricks/lib/survey/utils"; +import { validateInputs } from "@formbricks/lib/utils/validate"; +import { ZId } from "@formbricks/types/common"; +import { DatabaseError } from "@formbricks/types/errors"; +import { TSurvey } from "@formbricks/types/surveys/types"; + +export const getSurveys = reactCache( + async (environmentId: string): Promise => + cache( + async () => { + validateInputs([environmentId, ZId]); + + try { + const surveysPrisma = await prisma.survey.findMany({ + where: { + environmentId, + status: { + not: "completed", + }, + }, + select: selectSurvey, + orderBy: { + updatedAt: "desc", + }, + }); + + return surveysPrisma.map((surveyPrisma) => transformPrismaSurvey(surveyPrisma)); + } catch (error) { + if (error instanceof Prisma.PrismaClientKnownRequestError) { + console.error(error); + throw new DatabaseError(error.message); + } + throw error; + } + }, + [`getSurveys-${environmentId}`], + { + tags: [surveyCache.tag.byEnvironmentId(environmentId)], + } + )() +); diff --git a/apps/web/app/(app)/environments/[environmentId]/integrations/notion/page.tsx b/apps/web/app/(app)/environments/[environmentId]/integrations/notion/page.tsx index 9e65336db7..130fddf8c7 100644 --- a/apps/web/app/(app)/environments/[environmentId]/integrations/notion/page.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/integrations/notion/page.tsx @@ -1,3 +1,4 @@ +import { getSurveys } from "@/app/(app)/environments/[environmentId]/integrations/lib/surveys"; import { NotionWrapper } from "@/app/(app)/environments/[environmentId]/integrations/notion/components/NotionWrapper"; import { authOptions } from "@/modules/auth/lib/authOptions"; import { getProjectPermissionByUserId } from "@/modules/ee/teams/lib/roles"; @@ -21,7 +22,6 @@ import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/ import { getAccessFlags } from "@formbricks/lib/membership/utils"; import { getNotionDatabases } from "@formbricks/lib/notion/service"; import { getProjectByEnvironmentId } from "@formbricks/lib/project/service"; -import { getSurveys } from "@formbricks/lib/survey/service"; import { findMatchingLocale } from "@formbricks/lib/utils/locale"; import { TIntegrationNotion, TIntegrationNotionDatabase } from "@formbricks/types/integration/notion"; diff --git a/apps/web/app/(app)/environments/[environmentId]/integrations/slack/page.tsx b/apps/web/app/(app)/environments/[environmentId]/integrations/slack/page.tsx index b9d4b0f964..1a72b35784 100644 --- a/apps/web/app/(app)/environments/[environmentId]/integrations/slack/page.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/integrations/slack/page.tsx @@ -1,3 +1,4 @@ +import { getSurveys } from "@/app/(app)/environments/[environmentId]/integrations/lib/surveys"; import { SlackWrapper } from "@/app/(app)/environments/[environmentId]/integrations/slack/components/SlackWrapper"; import { authOptions } from "@/modules/auth/lib/authOptions"; import { getProjectPermissionByUserId } from "@/modules/ee/teams/lib/roles"; @@ -14,7 +15,6 @@ import { getIntegrationByType } from "@formbricks/lib/integration/service"; import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/service"; import { getAccessFlags } from "@formbricks/lib/membership/utils"; import { getProjectByEnvironmentId } from "@formbricks/lib/project/service"; -import { getSurveys } from "@formbricks/lib/survey/service"; import { findMatchingLocale } from "@formbricks/lib/utils/locale"; import { TIntegrationSlack } from "@formbricks/types/integration/slack"; diff --git a/packages/lib/survey/service.ts b/packages/lib/survey/service.ts index d9da25a217..b9c2027e40 100644 --- a/packages/lib/survey/service.ts +++ b/packages/lib/survey/service.ts @@ -9,7 +9,6 @@ import { TSegment, ZSegmentFilters } from "@formbricks/types/segment"; import { TSurvey, TSurveyCreateInput, - TSurveyFilterCriteria, TSurveyOpenTextQuestion, TSurveyQuestions, ZSurvey, @@ -27,13 +26,7 @@ import { capturePosthogEnvironmentEvent } from "../posthogServer"; import { getIsAIEnabled } from "../utils/ai"; import { validateInputs } from "../utils/validate"; import { surveyCache } from "./cache"; -import { - buildOrderByClause, - buildWhereClause, - doesSurveyHasOpenTextQuestion, - getInsightsEnabled, - transformPrismaSurvey, -} from "./utils"; +import { doesSurveyHasOpenTextQuestion, getInsightsEnabled, transformPrismaSurvey } from "./utils"; interface TriggerUpdate { create?: Array<{ actionClassId: string }>; @@ -272,12 +265,7 @@ export const getSurveysByActionClassId = reactCache( ); export const getSurveys = reactCache( - async ( - environmentId: string, - limit?: number, - offset?: number, - filterCriteria?: TSurveyFilterCriteria - ): Promise => + async (environmentId: string, limit?: number, offset?: number): Promise => cache( async () => { validateInputs([environmentId, ZId], [limit, ZOptionalNumber], [offset, ZOptionalNumber]); @@ -286,10 +274,11 @@ export const getSurveys = reactCache( const surveysPrisma = await prisma.survey.findMany({ where: { environmentId, - ...buildWhereClause(filterCriteria), }, select: selectSurvey, - orderBy: buildOrderByClause(filterCriteria?.sortBy), + orderBy: { + updatedAt: "desc", + }, take: limit, skip: offset, }); @@ -303,7 +292,7 @@ export const getSurveys = reactCache( throw error; } }, - [`getSurveys-${environmentId}-${limit}-${offset}-${JSON.stringify(filterCriteria)}`], + [`getSurveys-${environmentId}-${limit}-${offset}`], { tags: [surveyCache.tag.byEnvironmentId(environmentId)], } diff --git a/packages/lib/survey/utils.ts b/packages/lib/survey/utils.ts index 45ad28d5da..8ef9a140dc 100644 --- a/packages/lib/survey/utils.ts +++ b/packages/lib/survey/utils.ts @@ -1,15 +1,9 @@ import "server-only"; -import { Prisma } from "@prisma/client"; import { generateObject } from "ai"; import { z } from "zod"; import { TJsEnvironmentStateSurvey } from "@formbricks/types/js"; import { TSegment } from "@formbricks/types/segment"; -import { - TSurvey, - TSurveyFilterCriteria, - TSurveyQuestion, - TSurveyQuestions, -} from "@formbricks/types/surveys/types"; +import { TSurvey, TSurveyQuestion, TSurveyQuestions } from "@formbricks/types/surveys/types"; import { llmModel } from "../aiModels"; export const transformPrismaSurvey = ( @@ -33,62 +27,6 @@ export const transformPrismaSurvey = { - const whereClause: Prisma.SurveyWhereInput["AND"] = []; - - // for name - if (filterCriteria?.name) { - whereClause.push({ name: { contains: filterCriteria.name, mode: "insensitive" } }); - } - - // for status - if (filterCriteria?.status && filterCriteria?.status?.length) { - whereClause.push({ status: { in: filterCriteria.status } }); - } - - // for type - if (filterCriteria?.type && filterCriteria?.type?.length) { - whereClause.push({ type: { in: filterCriteria.type } }); - } - - // for createdBy - if (filterCriteria?.createdBy?.value && filterCriteria?.createdBy?.value?.length) { - if (filterCriteria.createdBy.value.length === 1) { - if (filterCriteria.createdBy.value[0] === "you") { - whereClause.push({ createdBy: filterCriteria.createdBy.userId }); - } - if (filterCriteria.createdBy.value[0] === "others") { - whereClause.push({ - OR: [ - { - createdBy: { - not: filterCriteria.createdBy.userId, - }, - }, - { - createdBy: null, - }, - ], - }); - } - } - } - - return { AND: whereClause }; -}; - -export const buildOrderByClause = ( - sortBy?: TSurveyFilterCriteria["sortBy"] -): Prisma.SurveyOrderByWithRelationInput[] | undefined => { - const orderMapping: { [key: string]: Prisma.SurveyOrderByWithRelationInput } = { - name: { name: "asc" }, - createdAt: { createdAt: "desc" }, - updatedAt: { updatedAt: "desc" }, - }; - - return sortBy ? [orderMapping[sortBy] || { updatedAt: "desc" }] : undefined; -}; - export const anySurveyHasFilters = (surveys: TSurvey[]): boolean => { return surveys.some((survey) => { if ("segment" in survey && survey.segment) {