-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ChangePlusPlusVandy/API-endpoints-applica…
…tion-response API endpoints application response
- Loading branch information
Showing
4 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters