clean up and move to new action -> service structure

This commit is contained in:
Matthias Nannt
2023-07-19 19:00:04 +02:00
parent e385638c14
commit feadefa90d
10 changed files with 249 additions and 154 deletions
@@ -2,7 +2,7 @@
import { prisma } from "@formbricks/database";
import { ResourceNotFoundError } from "@formbricks/errors";
import { captureTelemetry } from "@formbricks/lib/telemetry";
import { deleteSurvey, getSurvey } from "@formbricks/lib/services/survey";
import { QuestionType } from "@formbricks/types/questions";
import { createId } from "@paralleldrive/cuid2";
import { Team } from "@prisma/client";
@@ -1376,46 +1376,13 @@ export async function addDemoData(teamId: string): Promise<void> {
);
}
export async function deleteSurveyAction(surveyId: string) {
const deletedSurvey = await prisma.survey.delete({
where: {
id: surveyId,
},
});
return deletedSurvey;
}
export async function createSurveyAction(environmentId: string, surveyBody: any) {
const survey = await prisma.survey.create({
data: {
...surveyBody,
environment: {
connect: {
id: environmentId,
},
},
},
});
captureTelemetry("survey created");
return survey;
}
export async function duplicateSurveyAction(environmentId: string, surveyId: string) {
const existingSurvey = await prisma.survey.findFirst({
where: {
id: surveyId,
environmentId,
},
include: {
triggers: true,
attributeFilters: true,
},
});
const existingSurvey = await getSurvey(surveyId);
if (!existingSurvey) {
throw new ResourceNotFoundError("Survey", surveyId);
}
// create new survey with the data of the existing survey
const newSurvey = await prisma.survey.create({
data: {
@@ -1428,7 +1395,7 @@ export async function duplicateSurveyAction(environmentId: string, surveyId: str
thankYouCard: JSON.parse(JSON.stringify(existingSurvey.thankYouCard)),
triggers: {
create: existingSurvey.triggers.map((trigger) => ({
eventClassId: trigger.eventClassId,
eventClassId: trigger.id,
})),
},
attributeFilters: {
@@ -1575,3 +1542,7 @@ export async function copyToOtherEnvironmentAction(
});
return newSurvey;
}
export const deleteSurveyAction = async (surveyId: string) => {
await deleteSurvey(surveyId);
};
@@ -14,21 +14,21 @@ import {
DropdownMenuTrigger,
} from "@/components/shared/DropdownMenu";
import LoadingSpinner from "@/components/shared/LoadingSpinner";
import type { TEnvironment } from "@formbricks/types/v1/environment";
import type { TSurveyWithAnalytics } from "@formbricks/types/v1/surveys";
import {
ArrowUpOnSquareStackIcon,
DocumentDuplicateIcon,
EllipsisHorizontalIcon,
EyeIcon,
LinkIcon,
PencilSquareIcon,
EyeIcon,
TrashIcon,
ArrowUpOnSquareStackIcon,
} from "@heroicons/react/24/solid";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useState } from "react";
import toast from "react-hot-toast";
import type { TEnvironment } from "@formbricks/types/v1/environment";
interface SurveyDropDownMenuProps {
environmentId: string;
@@ -151,7 +151,7 @@ export default function SurveyDropDownMenu({
<DropdownMenuItem>
<Link
className="flex w-full items-center"
href={`${window.location.protocol}//${window.location.host}/s/${survey.id}?preview=true`}
href={`/s/${survey.id}?preview=true`}
target="_blank">
<EyeIcon className="mr-2 h-4 w-4" />
Preview Survey
@@ -1,6 +1,6 @@
"use client";
import { Template } from "@/../../packages/types/templates";
import { createSurveyAction } from "@/app/(app)/environments/[environmentId]/actions";
import { createSurveyAction } from "./actions";
import TemplateList from "@/app/(app)/environments/[environmentId]/surveys/templates/TemplateList";
import LoadingSpinner from "@/components/shared/LoadingSpinner";
import type { TEnvironment } from "@formbricks/types/v1/environment";
@@ -1,11 +1,11 @@
import { IS_FORMBRICKS_CLOUD, RESPONSES_LIMIT_FREE } from "@formbricks/lib/constants";
import { getSurveyResponses } from "@formbricks/lib/services/response";
import { getSurvey } from "@formbricks/lib/services/survey";
import { getSurveyWithAnalytics } from "@formbricks/lib/services/survey";
import { getTeamByEnvironmentId } from "@formbricks/lib/services/team";
export const getAnalysisData = async (surveyId: string, environmentId: string) => {
const [survey, team, allResponses] = await Promise.all([
getSurvey(surveyId),
getSurveyWithAnalytics(surveyId),
getTeamByEnvironmentId(environmentId),
getSurveyResponses(surveyId),
]);
@@ -0,0 +1,7 @@
"use server";
import { createSurvey } from "@formbricks/lib/services/survey";
export async function createSurveyAction(environmentId: string, surveyBody: any) {
return await createSurvey(environmentId, surveyBody);
}
@@ -1,21 +1,27 @@
"use client";
import { createSurvey } from "@/lib/surveys/surveys";
import LoadingSpinner from "@/components/shared/LoadingSpinner";
import { useProfile } from "@/lib/profile";
import { replacePresetPlaceholders } from "@/lib/templates";
import { cn } from "@formbricks/lib/cn";
import type { Template } from "@formbricks/types/templates";
import { Button, ErrorComponent } from "@formbricks/ui";
import { PlusCircleIcon } from "@heroicons/react/24/outline";
import { SparklesIcon } from "@heroicons/react/24/solid";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { customSurvey, templates } from "./templates";
import { SplitIcon } from "lucide-react";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@formbricks/ui";
import type { TEnvironment } from "@formbricks/types/v1/environment";
import type { TProduct } from "@formbricks/types/v1/product";
import { useProfile } from "@/lib/profile";
import LoadingSpinner from "@/components/shared/LoadingSpinner";
import {
Button,
ErrorComponent,
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@formbricks/ui";
import { PlusCircleIcon } from "@heroicons/react/24/outline";
import { SparklesIcon } from "@heroicons/react/24/solid";
import { SplitIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { createSurveyAction } from "./actions";
import { customSurvey, templates } from "./templates";
type TemplateList = {
environmentId: string;
@@ -59,7 +65,7 @@ export default function TemplateList({ environmentId, onTemplateClick, product,
...activeTemplate.preset,
type: environment?.widgetSetupCompleted ? "web" : "link",
};
const survey = await createSurvey(environmentId, augmentedTemplate);
const survey = await createSurveyAction(environmentId, augmentedTemplate);
router.push(`/environments/${environmentId}/surveys/${survey.id}/edit`);
};
@@ -0,0 +1,7 @@
"use server";
import { createSurvey } from "@formbricks/lib/services/survey";
export async function createSurveyAction(environmentId: string, surveyBody: any) {
return await createSurvey(environmentId, surveyBody);
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { prisma } from "@formbricks/database";
import { select } from "@formbricks/lib/services/survey";
import { selectSurvey } from "@formbricks/lib/services/survey";
import { TPerson } from "@formbricks/types/v1/people";
import { TSurvey } from "@formbricks/types/v1/surveys";
@@ -48,7 +48,7 @@ export const getSurveys = async (environmentId: string, person: TPerson): Promis
],
},
select: {
...select,
...selectSurvey,
attributeFilters: {
select: {
id: true,