Skip to content

Commit

Permalink
Merge pull request #45 from ClearContracts/matt/catch-apis
Browse files Browse the repository at this point in the history
Modify try...catch of API routes
  • Loading branch information
mattlaux authored Nov 4, 2024
2 parents 4a6723b + 03f9113 commit 30b02e0
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/pages/api/endVoting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default async function endVoting(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
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),
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/getPoll/[pollId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default async function getPoll(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
const pollId = req.query.pollId;
try {
const pollId = req.query.pollId;
if (typeof pollId !== 'string') {
return res.status(400).json({
poll: null,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/getPollResults/[pollId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export default async function getPollResults(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
const pollId = req.query.pollId;
try {
const pollId = req.query.pollId;
if (typeof pollId !== 'string') {
return res.status(400).json({
votes: null,
Expand Down
28 changes: 14 additions & 14 deletions src/pages/api/getPollVote/[...params].ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ export default async function getPollVoteCount(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
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),
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/getPollVoteCount/[pollId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default async function getPollVoteCount(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
const pollId = req.query.pollId;
try {
const pollId = req.query.pollId;
if (typeof pollId !== 'string') {
return res.status(400).json({
count: 0,
Expand Down
13 changes: 0 additions & 13 deletions src/pages/api/hello.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/pages/api/newPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default async function newPoll(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
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) {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/api/newPollVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default async function newPollVote(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/startVoting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default async function startVoting(
req: NextApiRequest,
res: NextApiResponse<Data>,
): Promise<void> {
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),
Expand Down

0 comments on commit 30b02e0

Please sign in to comment.