From ae5fe799804adbeed715bfdb4bc0f669a680afe7 Mon Sep 17 00:00:00 2001 From: Matthew Laux Date: Mon, 4 Nov 2024 09:27:19 -0600 Subject: [PATCH 1/2] Delete unused API route --- src/pages/api/hello.ts | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 src/pages/api/hello.ts diff --git a/src/pages/api/hello.ts b/src/pages/api/hello.ts deleted file mode 100644 index d13a609..0000000 --- a/src/pages/api/hello.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Data = { - name: string; -}; - -export default function handler( - req: NextApiRequest, - res: NextApiResponse, -): void { - res.status(200).json({ name: 'John Doe' }); -} From 03f9113d9c4bdba1db02b302daba3f601a920790 Mon Sep 17 00:00:00 2001 From: Matthew Laux Date: Mon, 4 Nov 2024 09:27:35 -0600 Subject: [PATCH 2/2] Modify try catch for API routes --- src/pages/api/endVoting.ts | 4 ++-- src/pages/api/getPoll/[pollId].ts | 2 +- src/pages/api/getPollResults/[pollId].ts | 2 +- src/pages/api/getPollVote/[...params].ts | 28 +++++++++++----------- src/pages/api/getPollVoteCount/[pollId].ts | 2 +- src/pages/api/newPoll.ts | 4 ++-- src/pages/api/newPollVote.ts | 6 ++--- src/pages/api/startVoting.ts | 4 ++-- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/pages/api/endVoting.ts b/src/pages/api/endVoting.ts index fb6b218..eacc432 100644 --- a/src/pages/api/endVoting.ts +++ b/src/pages/api/endVoting.ts @@ -19,9 +19,9 @@ export default async function endVoting( req: NextApiRequest, res: NextApiResponse, ): Promise { - const { pollId } = req.body; - // TODO: Add session check to verify it is coordinator. Also additional security step of verifying coordinator's signature before updating poll? try { + const { pollId } = req.body; + // TODO: Add session check to verify it is coordinator. Also additional security step of verifying coordinator's signature before updating poll? const findPoll = await prisma.poll.findFirst({ where: { id: BigInt(pollId), diff --git a/src/pages/api/getPoll/[pollId].ts b/src/pages/api/getPoll/[pollId].ts index 1e7b2cc..5507e7a 100644 --- a/src/pages/api/getPoll/[pollId].ts +++ b/src/pages/api/getPoll/[pollId].ts @@ -21,8 +21,8 @@ export default async function getPoll( req: NextApiRequest, res: NextApiResponse, ): Promise { - const pollId = req.query.pollId; try { + const pollId = req.query.pollId; if (typeof pollId !== 'string') { return res.status(400).json({ poll: null, diff --git a/src/pages/api/getPollResults/[pollId].ts b/src/pages/api/getPollResults/[pollId].ts index 780c0d1..9c6061f 100644 --- a/src/pages/api/getPollResults/[pollId].ts +++ b/src/pages/api/getPollResults/[pollId].ts @@ -28,8 +28,8 @@ export default async function getPollResults( req: NextApiRequest, res: NextApiResponse, ): Promise { - const pollId = req.query.pollId; try { + const pollId = req.query.pollId; if (typeof pollId !== 'string') { return res.status(400).json({ votes: null, diff --git a/src/pages/api/getPollVote/[...params].ts b/src/pages/api/getPollVote/[...params].ts index 542933a..126497f 100644 --- a/src/pages/api/getPollVote/[...params].ts +++ b/src/pages/api/getPollVote/[...params].ts @@ -18,22 +18,22 @@ export default async function getPollVoteCount( req: NextApiRequest, res: NextApiResponse, ): Promise { - const { params } = req.query; + try { + const { params } = req.query; - // Ensures params are an array of strings - if (Array.isArray(params)) { - params.forEach((param) => { - if (typeof param !== 'string') { - return res.status(400).json({ vote: '', message: 'Invalid params' }); - } - }); - } else { - return res.status(400).json({ vote: '', message: 'Invalid params' }); - } - const userId = params[0]; - const pollId = params[1]; + // Ensures params are an array of strings + if (Array.isArray(params)) { + params.forEach((param) => { + if (typeof param !== 'string') { + return res.status(400).json({ vote: '', message: 'Invalid params' }); + } + }); + } else { + return res.status(400).json({ vote: '', message: 'Invalid params' }); + } + const userId = params[0]; + const pollId = params[1]; - try { const vote = await prisma.poll_vote.findFirst({ where: { poll_id: BigInt(pollId), diff --git a/src/pages/api/getPollVoteCount/[pollId].ts b/src/pages/api/getPollVoteCount/[pollId].ts index 53e27ea..0365f84 100644 --- a/src/pages/api/getPollVoteCount/[pollId].ts +++ b/src/pages/api/getPollVoteCount/[pollId].ts @@ -18,8 +18,8 @@ export default async function getPollVoteCount( req: NextApiRequest, res: NextApiResponse, ): Promise { - const pollId = req.query.pollId; try { + const pollId = req.query.pollId; if (typeof pollId !== 'string') { return res.status(400).json({ count: 0, diff --git a/src/pages/api/newPoll.ts b/src/pages/api/newPoll.ts index 649c483..c2a3740 100644 --- a/src/pages/api/newPoll.ts +++ b/src/pages/api/newPoll.ts @@ -19,9 +19,9 @@ export default async function newPoll( req: NextApiRequest, res: NextApiResponse, ): Promise { - const { name, description } = req.body; - // TODO: Add session check to verify it is coordinator. Also additional security step of verifying coordinator's signature before creating poll? try { + const { name, description } = req.body; + // TODO: Add session check to verify it is coordinator. Also additional security step of verifying coordinator's signature before creating poll? // TODO: Add data sanitization check. If fails sanitization return a message to the user. // validate name if (!name) { diff --git a/src/pages/api/newPollVote.ts b/src/pages/api/newPollVote.ts index f011830..3266df4 100644 --- a/src/pages/api/newPollVote.ts +++ b/src/pages/api/newPollVote.ts @@ -22,10 +22,10 @@ export default async function newPollVote( req: NextApiRequest, res: NextApiResponse, ): Promise { - const { pollId, vote } = req.body; - // TODO: Add session check to verify it is delegator/alternate. Also additional security step of verifying delegator/alternate's signature before casting vote - // TODO: Add check that the delegate/alternate is the active voter for the convention location try { + const { pollId, vote } = req.body; + // TODO: Add session check to verify it is delegator/alternate. Also additional security step of verifying delegator/alternate's signature before casting vote + // TODO: Add check that the delegate/alternate is the active voter for the convention location // TODO: Add data sanitization check. If fails sanitization return a message to the user. // validate poll id if (!pollId) { diff --git a/src/pages/api/startVoting.ts b/src/pages/api/startVoting.ts index 8e032ba..d9d8716 100644 --- a/src/pages/api/startVoting.ts +++ b/src/pages/api/startVoting.ts @@ -19,9 +19,9 @@ export default async function startVoting( req: NextApiRequest, res: NextApiResponse, ): Promise { - const { pollId } = req.body; - // TODO: Add session check to verify it is coordinator. Also additional security step of verifying coordinator's signature before updating poll? try { + const { pollId } = req.body; + // TODO: Add session check to verify it is coordinator. Also additional security step of verifying coordinator's signature before updating poll? const findPoll = await prisma.poll.findFirst({ where: { id: BigInt(pollId),