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

Fix/winner admin response #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions routes/winner.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import express from 'express';
import WinnerService from '../services/winner'
import config from '../config'
import { NotFoundError } from '../helpers';

const router = express.Router();

router.get("/", async (req, res) => {
router.get("/", async (req, res) => {
const { userEmail } = req.user;
const { admins } = config;
if (!admins.includes(userEmail)) {
res.status(401).json({message: 'Unauthorized'})
return res.status(401).json({ message: 'Unauthorized' })
}
try {
const winner = await WinnerService.getWinner();
return res.status(200).json(winner)
} catch (e) {
console.log(e)
if (e instanceof NotFoundError) {
return res.status(404).json({ message: e.message });
}
return res.status(500).json({ message: 'Server Error' })
}
})
Expand Down
7 changes: 4 additions & 3 deletions services/winner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getPool } from '../database';
import {sql} from 'slonik';
import {Proposal, ProposalState} from "../types/proposal";
import { sql } from 'slonik';
import { Proposal, ProposalState } from "../types/proposal";
import { NotFoundError } from '../helpers';

async function getWinner(): Promise<Proposal> {
const pool = await getPool();
Expand All @@ -27,7 +28,7 @@ async function getWinner(): Promise<Proposal> {
)
`);
if (proposals.length === 0) {
throw new Error('No open proposals found');
throw new NotFoundError('No open proposals found');
}
const randomIndex = Math.floor(Math.random() * proposals.length);
const chosenProposal = proposals[randomIndex];
Expand Down