diff --git a/src/app/api/surveys/[surveyId]/route.ts b/src/app/api/surveys/[surveyId]/route.ts new file mode 100644 index 0000000..308275c --- /dev/null +++ b/src/app/api/surveys/[surveyId]/route.ts @@ -0,0 +1,18 @@ +import { NextRequest, NextResponse } from "next/server"; +import { getSurveyByID } from "@/lib/firebase/database/surveys"; + +export async function GET( + req: NextRequest, + { params }: { params: { surveyId: string } } +) { + const { surveyId } = params; + try { + const survey = await getSurveyByID(surveyId); + return NextResponse.json({ data: survey }, { status: 200 }); + } catch (err) { + return NextResponse.json( + { error: "error getting survey by id" }, + { status: 500 } + ); + } +} diff --git a/src/lib/firebase/database/surveys.ts b/src/lib/firebase/database/surveys.ts index a99ea74..ec9e16f 100644 --- a/src/lib/firebase/database/surveys.ts +++ b/src/lib/firebase/database/surveys.ts @@ -1,6 +1,7 @@ +import { Survey } from '@/types/survey-types'; import { forms } from '../../googleAuthorization'; import { db } from "../firebaseConfig"; -import { doc, setDoc } from 'firebase/firestore'; +import { doc, getDoc, setDoc } from 'firebase/firestore'; export async function createSurvey(body: {title: string, documentTitle: string}) { let form = null; @@ -42,4 +43,13 @@ export async function updateSurvey(id: string) { } catch (err) { throw Error('unable to update survey in firestore'); } +} + +export async function getSurveyByID(id: string) { + try { + const snapshot = await getDoc(doc(db, 'surveys', id)); + return snapshot.exists() ? snapshot.data() as Survey : null + } catch (err) { + throw Error('survey with given id not found') + } } \ No newline at end of file