Skip to content

Commit

Permalink
Merge pull request #42 from ChangePlusPlusVandy/API-endpoints-applica…
Browse files Browse the repository at this point in the history
…tion-response

API endpoints application response
  • Loading branch information
JiashuHarryHuang authored Feb 24, 2024
2 parents 2fd16ad + dace433 commit f628198
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
87 changes: 87 additions & 0 deletions pages/api/event/[id]/application/[responseId].ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
37 changes: 37 additions & 0 deletions pages/api/event/[id]/applications.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 4 additions & 4 deletions utils/api-testing.ts
Original file line number Diff line number Diff line change
@@ -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;
};
5 changes: 5 additions & 0 deletions utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit f628198

Please sign in to comment.