diff --git a/pages/api/applications.ts b/pages/api/applications.ts index f3e0887..5abf141 100644 --- a/pages/api/applications.ts +++ b/pages/api/applications.ts @@ -8,20 +8,15 @@ import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse'; import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; import { getServerSession } from 'next-auth'; +import { makeSessionForAPITest } from '@/utils/api-testing'; export default async function handler( req: NextApiRequest, res: NextApiResponse ) { // Get session user - const session = await getServerSession(req, res, authOptions); - // for API testing only - // TODO extract this logic - // const session = { - // user: { - // id: '60f3f5f8f0f6f6f6f6f6f6f6', - // }, - // }; + const session = + (await getServerSession(req, res, authOptions)) || makeSessionForAPITest(); // Get request parameter const { @@ -46,10 +41,6 @@ export default async function handler( userId: session.user.id, }).populate('eventId'); - // const appliedEvents = applicationResponses.map((applicationResponse) => { - // return applicationResponse.eventId; - // }; - return res.status(200).json({ message: applicationResponses }); } catch (error) { console.error(error); diff --git a/pages/api/event/[id]/apply.ts b/pages/api/event/[id]/apply.ts index 6484b4c..2619810 100644 --- a/pages/api/event/[id]/apply.ts +++ b/pages/api/event/[id]/apply.ts @@ -7,20 +7,15 @@ import { ApplicationResponseData } from 'bookem-shared/src/types/database'; import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; import { getServerSession } from 'next-auth'; +import { makeSessionForAPITest } from '@/utils/api-testing'; export default async function handler( req: NextApiRequest, res: NextApiResponse ) { // Get session user - const session = await getServerSession(req, res, authOptions); - // for API testing only - // TODO extract this logic - // const session = { - // user: { - // id: '60f3f5f8f0f6f6f6f6f6f6f6', - // }, - // } + let session = + (await getServerSession(req, res, authOptions)) || makeSessionForAPITest(); // Get request parameter const { @@ -39,7 +34,6 @@ export default async function handler( try { await dbConnect(); - // TODO - refactor common id checking to a common util if (!id) return res.status(400).json({ message: 'Missing id' }); // check if id is a valid mongoose id @@ -51,7 +45,6 @@ export default async function handler( eventId: id, }); - // TODO - refactor a common util for when something is not found if (!volunteerApplication) { return res .status(404) @@ -77,13 +70,9 @@ export default async function handler( try { await dbConnect(); - // start a try catch block to catch any errors in parsing the request body - // TODO maybe define a DTO for this - console.log(req.body); const response = req.body as ApplicationResponseData; const { answers } = response; - // Declare the following ops to be an atomic transaction const mongoSession = await mongoose.startSession(); await mongoSession.withTransaction(async () => { @@ -95,7 +84,7 @@ export default async function handler( answers, }); - await newResponse.save() + await newResponse.save(); // use the id of the saved response to update the volunteerApplications data await VolunteerApplications.updateOne( diff --git a/pages/api/event/[id]/submitted-application.ts b/pages/api/event/[id]/submitted-application.ts index 421f24f..afa53d8 100644 --- a/pages/api/event/[id]/submitted-application.ts +++ b/pages/api/event/[id]/submitted-application.ts @@ -4,19 +4,15 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; +import { makeSessionForAPITest } from '@/utils/api-testing'; export default async function handler( req: NextApiRequest, res: NextApiResponse ) { // Get session user - const session = await getServerSession(req, res, authOptions); - // for API testing only - // const session = { - // user: { - // id: '60f3f5f8f0f6f6f6f6f6f6f6', - // }, - // }; + let session = + (await getServerSession(req, res, authOptions)) || makeSessionForAPITest(); // Get request parameter const { @@ -35,7 +31,6 @@ export default async function handler( try { await dbConnect(); - // TODO - refactor common id checking to a common util if (!id) return res.status(400).json({ message: 'Missing id' }); // check if id is a valid mongoose id