add SurveyId filter option to responses endpoint (#517)

This commit is contained in:
Matti Nannt
2023-07-10 17:03:36 +02:00
committed by GitHub
parent 04ec0a6827
commit 7be6879afb
5 changed files with 27 additions and 8 deletions
+22 -4
View File
@@ -1,10 +1,11 @@
import { responses } from "@/lib/api/response";
import { getApiKeyFromKey } from "@formbricks/lib/services/apiKey";
import { getEnvironmentResponses } from "@formbricks/lib/services/response";
import { getEnvironmentResponses, getSurveyResponses } from "@formbricks/lib/services/response";
import { headers } from "next/headers";
import { DatabaseError } from "@formbricks/errors";
import { getSurvey } from "@formbricks/lib/services/survey";
export async function GET() {
export async function GET(request: Request) {
const apiKey = headers().get("x-api-key");
if (!apiKey) {
return responses.notAuthenticatedResponse();
@@ -19,10 +20,27 @@ export async function GET() {
return responses.notAuthenticatedResponse();
}
// get surveyId from searchParams
const { searchParams } = new URL(request.url);
const surveyId = searchParams.get("surveyId");
// get responses from database
try {
const environmentResponses = await getEnvironmentResponses(apiKeyData.environmentId);
return responses.successResponse(environmentResponses);
if (!surveyId) {
const environmentResponses = await getEnvironmentResponses(apiKeyData.environmentId);
return responses.successResponse(environmentResponses);
}
// check if survey is part of environment
const survey = await getSurvey(surveyId);
if (!survey) {
return responses.notFoundResponse(surveyId, "survey");
}
if (survey.environmentId !== apiKeyData.environmentId) {
return responses.notFoundResponse(surveyId, "survey");
}
// get responses for survey
const surveyResponses = await getSurveyResponses(surveyId);
return responses.successResponse(surveyResponses);
} catch (error) {
if (error instanceof DatabaseError) {
return responses.badRequestResponse(error.message);
@@ -14,7 +14,7 @@ export default async function ResponsesLimitReachedBanner({
environmentId,
session,
}: ResponsesLimitReachedBannerProps) {
const { responsesCount, limitReached } = await getAnalysisData(session, surveyId);
const { responsesCount, limitReached } = await getAnalysisData(session, surveyId, environmentId);
return (
<>
{limitReached && (
@@ -16,7 +16,7 @@ export default async function ResponsesPage({ params }) {
if (!session) {
throw new Error("Unauthorized");
}
const { responses, survey } = await getAnalysisData(session, params.surveyId);
const { responses, survey } = await getAnalysisData(session, params.surveyId, params.environmentId);
return (
<>
<SurveyResultsTabs
@@ -27,7 +27,7 @@ interface SummaryListProps {
}
export default async function SummaryList({ environmentId, surveyId, session }: SummaryListProps) {
const { survey, responses } = await getAnalysisData(session, surveyId);
const { survey, responses } = await getAnalysisData(session, surveyId, environmentId);
const getSummaryData = (): QuestionSummary<TSurveyQuestion>[] =>
survey.questions.map((question) => {
@@ -3,9 +3,10 @@ import { getSurveyResponses } from "@formbricks/lib/services/response";
import { getSurvey } from "@formbricks/lib/services/survey";
import { Session } from "next-auth";
export const getAnalysisData = async (session: Session, surveyId: string) => {
export const getAnalysisData = async (session: Session, surveyId: string, environmentId: string) => {
const survey = await getSurvey(surveyId);
if (!survey) throw new Error(`Survey not found: ${surveyId}`);
if (survey.environmentId !== environmentId) throw new Error(`Survey not found: ${surveyId}`);
const allResponses = await getSurveyResponses(surveyId);
const limitReached =
IS_FORMBRICKS_CLOUD && session?.user.plan === "free" && allResponses.length >= RESPONSES_LIMIT_FREE;