diff --git a/pages/api/event/[id]/application/[responseId].ts b/pages/api/event/[id]/application/[responseId].ts new file mode 100644 index 0000000..740d81c --- /dev/null +++ b/pages/api/event/[id]/application/[responseId].ts @@ -0,0 +1,87 @@ +import dbConnect from '@/lib/dbConnect'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse'; +import { + ApplicationStatus, + ApplicationResponseData, +} from 'bookem-shared/src/types/database'; +import { enumChecking as checkEnum } from '@/utils/utils'; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + // Get request parameter + const { + query: { id, responseId }, + method, + } = req; + + switch (method) { + /** + * @route GET /api/event/[id]/application/[responseId] + * @desc Get specific response + * @req event: id, response: responseId + * @res a specific response + */ + case 'GET': + try { + await dbConnect(); + + const applicationResponse = await ApplicationResponse.findOne({ + _id: responseId, + }); + + if (!applicationResponse) { + return res.status(404).json({ message: 'Response not found' }); + } + + return res.status(200).json(applicationResponse); + } catch (error: any) { + console.error(error); + res.status(500).json({ message: error.message }); + } + break; + + /** + * @route PUT /api/event/[id]/application/[responseId] + * @desc update the status of a response + * @req event: id, response: responseId + * @res a message telling whether the response is updated + */ + case 'PUT': + try { + await dbConnect(); + + const applicationResponse = await ApplicationResponse.findOne({ + _id: responseId, + }); + + if (!applicationResponse) { + return res.status(404).json({ message: 'Response not found' }); + } + + const responseUpdate = req.body as ApplicationResponseData; + + const updateStatus = responseUpdate.status as ApplicationStatus; + // enum checking to ensure status is valid + if (!checkEnum(responseUpdate.status, ApplicationStatus)) { + return res.status(400).json({ message: 'Invalid status' }); + } + + // update the status + await ApplicationResponse.findOneAndUpdate( + { _id: responseId }, + { + status: updateStatus, + } + ); + + return res.status(200).json({ message: 'Response updated' }); + } catch (error: any) { + console.error(error); + res.status(500).json({ message: error.message }); + } + break; + } +} diff --git a/pages/api/event/[id]/applications.ts b/pages/api/event/[id]/applications.ts new file mode 100644 index 0000000..8da682e --- /dev/null +++ b/pages/api/event/[id]/applications.ts @@ -0,0 +1,37 @@ +import dbConnect from '@/lib/dbConnect'; +import type { NextApiRequest, NextApiResponse } from 'next'; +import VolunteerApplications from 'bookem-shared/src/models/VolunteerApplications'; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + // Get request parameter + const { + query: { id }, + method, + } = req; + + switch (method) { + /** + * @route GET /api/event/applications + * @desc Return a list of questions + a list of responses + * @req event id + * @res a list of questions + a list of responses + */ + case 'GET': + try { + await dbConnect(); + + const applicationWithResponses = await VolunteerApplications.find({ + eventId: id, + }); + + return res.status(200).json(applicationWithResponses); + } catch (error: any) { + console.error(error); + res.status(500).json({ message: error.message }); + } + break; + } +} diff --git a/utils/api-testing.ts b/utils/api-testing.ts index 151c101..bb7648c 100644 --- a/utils/api-testing.ts +++ b/utils/api-testing.ts @@ -1,15 +1,15 @@ // common utils for simple API testing in development phase // change this var to true when you are testing the API endpoints without logging in -export const testingAPI = false; +export const testingAPI = true; -const fakeUserSession = { +const fakeAdminSession = { user: { - _id: '65ae7bbe24dc37492f2581c2', + id: '65ae7bbe24dc37492f2581c2', }, }; // returns a fake user session when testing the API export const makeSessionForAPITest = () => { - return testingAPI ? fakeUserSession : null; + return testingAPI ? fakeAdminSession : null; }; diff --git a/utils/utils.ts b/utils/utils.ts index 95b0e9d..19814ad 100644 --- a/utils/utils.ts +++ b/utils/utils.ts @@ -1,5 +1,10 @@ import * as XLSX from 'xlsx'; import { saveAs } from 'file-saver'; + +export const enumChecking = (value: string, enumType: any) => { + return Object.values(enumType).includes(value); +}; + export const convertToDate = (str: string) => { if (str === '') return str; const date = new Date(str);