Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API endpoints application response #42

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading