From 534e14740ea83533de6b665d174d57b5c1b67dfc Mon Sep 17 00:00:00 2001 From: Naresh <58628794+ortin779@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:06:59 +0530 Subject: [PATCH] fix: duplicate survey not revalidating the cache key PAIR: Bilal Mirza (#1229) Co-authored-by: Matti Nannt --- .../environments/[environmentId]/actions.ts | 57 ++---------------- packages/lib/survey/service.ts | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/apps/web/app/(app)/environments/[environmentId]/actions.ts b/apps/web/app/(app)/environments/[environmentId]/actions.ts index b3768dc653..1a98e6a9d0 100644 --- a/apps/web/app/(app)/environments/[environmentId]/actions.ts +++ b/apps/web/app/(app)/environments/[environmentId]/actions.ts @@ -1,20 +1,19 @@ "use server"; -import { authOptions } from "@formbricks/lib/authOptions"; import { prisma } from "@formbricks/database"; +import { authOptions } from "@formbricks/lib/authOptions"; import { SHORT_SURVEY_BASE_URL, SURVEY_BASE_URL } from "@formbricks/lib/constants"; import { hasUserEnvironmentAccess } from "@formbricks/lib/environment/auth"; import { createMembership } from "@formbricks/lib/membership/service"; import { createProduct } from "@formbricks/lib/product/service"; import { createShortUrl } from "@formbricks/lib/shortUrl/service"; import { canUserAccessSurvey } from "@formbricks/lib/survey/auth"; -import { deleteSurvey, getSurvey } from "@formbricks/lib/survey/service"; +import { deleteSurvey, duplicateSurvey } from "@formbricks/lib/survey/service"; import { createTeam, getTeamByEnvironmentId } from "@formbricks/lib/team/service"; import { AuthenticationError, AuthorizationError, ResourceNotFoundError } from "@formbricks/types/v1/errors"; import { Team } from "@prisma/client"; import { Prisma as prismaClient } from "@prisma/client/"; import { getServerSession } from "next-auth"; -import { getActionClasses } from "@formbricks/lib/actionClass/service"; export const createShortUrlAction = async (url: string) => { const session = await getServerSession(authOptions); @@ -57,56 +56,8 @@ export async function duplicateSurveyAction(environmentId: string, surveyId: str const isAuthorized = await canUserAccessSurvey(session.user.id, surveyId); if (!isAuthorized) throw new AuthorizationError("Not authorized"); - const existingSurvey = await getSurvey(surveyId); - - if (!existingSurvey) { - throw new ResourceNotFoundError("Survey", surveyId); - } - - const actionClasses = await getActionClasses(environmentId); - - // create new survey with the data of the existing survey - const newSurvey = await prisma.survey.create({ - data: { - ...existingSurvey, - id: undefined, // id is auto-generated - environmentId: undefined, // environmentId is set below - name: `${existingSurvey.name} (copy)`, - status: "draft", - questions: JSON.parse(JSON.stringify(existingSurvey.questions)), - thankYouCard: JSON.parse(JSON.stringify(existingSurvey.thankYouCard)), - triggers: { - create: existingSurvey.triggers.map((trigger) => ({ - eventClassId: actionClasses.find((actionClass) => actionClass.name === trigger)!.id, - })), - }, - attributeFilters: { - create: existingSurvey.attributeFilters.map((attributeFilter) => ({ - attributeClassId: attributeFilter.attributeClassId, - condition: attributeFilter.condition, - value: attributeFilter.value, - })), - }, - environment: { - connect: { - id: environmentId, - }, - }, - surveyClosedMessage: existingSurvey.surveyClosedMessage - ? JSON.parse(JSON.stringify(existingSurvey.surveyClosedMessage)) - : prismaClient.JsonNull, - singleUse: existingSurvey.singleUse - ? JSON.parse(JSON.stringify(existingSurvey.singleUse)) - : prismaClient.JsonNull, - productOverwrites: existingSurvey.productOverwrites - ? JSON.parse(JSON.stringify(existingSurvey.productOverwrites)) - : prismaClient.JsonNull, - verifyEmail: existingSurvey.verifyEmail - ? JSON.parse(JSON.stringify(existingSurvey.verifyEmail)) - : prismaClient.JsonNull, - }, - }); - return newSurvey; + const duplicatedSurvey = await duplicateSurvey(environmentId,surveyId) + return duplicatedSurvey; } export async function copyToOtherEnvironmentAction( diff --git a/packages/lib/survey/service.ts b/packages/lib/survey/service.ts index 8a0793dc22..f7229fe5b4 100644 --- a/packages/lib/survey/service.ts +++ b/packages/lib/survey/service.ts @@ -617,3 +617,61 @@ export async function createSurvey(environmentId: string, surveyBody: TSurveyInp return transformedSurvey; } + + +export async function duplicateSurvey(environmentId:string,surveyId:string){ + const existingSurvey = await getSurvey(surveyId); + + if (!existingSurvey) { + throw new ResourceNotFoundError("Survey", surveyId); + } + + const actionClasses = await getActionClasses(environmentId); + + // create new survey with the data of the existing survey + const newSurvey = await prisma.survey.create({ + data: { + ...existingSurvey, + id: undefined, // id is auto-generated + environmentId: undefined, // environmentId is set below + name: `${existingSurvey.name} (copy)`, + status: "draft", + questions: JSON.parse(JSON.stringify(existingSurvey.questions)), + thankYouCard: JSON.parse(JSON.stringify(existingSurvey.thankYouCard)), + triggers: { + create: existingSurvey.triggers.map((trigger) => ({ + eventClassId: actionClasses.find((actionClass) => actionClass.name === trigger)!.id, + })), + }, + attributeFilters: { + create: existingSurvey.attributeFilters.map((attributeFilter) => ({ + attributeClassId: attributeFilter.attributeClassId, + condition: attributeFilter.condition, + value: attributeFilter.value, + })), + }, + environment: { + connect: { + id: environmentId, + }, + }, + surveyClosedMessage: existingSurvey.surveyClosedMessage + ? JSON.parse(JSON.stringify(existingSurvey.surveyClosedMessage)) + : Prisma.JsonNull, + singleUse: existingSurvey.singleUse + ? JSON.parse(JSON.stringify(existingSurvey.singleUse)) + : Prisma.JsonNull, + productOverwrites: existingSurvey.productOverwrites + ? JSON.parse(JSON.stringify(existingSurvey.productOverwrites)) + : Prisma.JsonNull, + verifyEmail: existingSurvey.verifyEmail + ? JSON.parse(JSON.stringify(existingSurvey.verifyEmail)) + : Prisma.JsonNull, + }, + }); + + revalidateTag(getSurveysCacheTag(environmentId)); + revalidateTag(getSurveyCacheTag(surveyId)); + + return newSurvey; +} \ No newline at end of file