-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add winner to routes and services * chore(tests): made tests synchronous and added winner endpoint test * chore(tests): handle tie edge case * fix(winner): adjust query to sum votes * chore(winner): add admin list restriction * chore(winner): add results to response * fix: remove test user from default admin list
- Loading branch information
1 parent
51edb7d
commit 83f6654
Showing
7 changed files
with
176 additions
and
68 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
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,16 @@ | ||
import {TEST_USER} from "./helpers"; | ||
|
||
const admins = [ | ||
'zenlex@zenlex.dev', | ||
'alec@helmturner.dev', | ||
'niledixon475@gmail.com', | ||
'cryskayecarr@gmail.com' | ||
] | ||
|
||
if (process.env.NODE_ENV === 'test') { | ||
admins.push(TEST_USER.userEmail) | ||
} | ||
|
||
module.exports = { | ||
admins | ||
}; |
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,22 @@ | ||
import express from 'express'; | ||
import WinnerService from '../services/winner' | ||
import config from '../config' | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/", async (req, res) => { | ||
const { userEmail } = req.user; | ||
const { admins } = config; | ||
if (!admins.includes(userEmail)) { | ||
res.status(401).json({message: 'Unauthorized'}) | ||
} | ||
try { | ||
const winner = await WinnerService.getWinner(); | ||
return res.status(200).json(winner) | ||
} catch (e) { | ||
console.log(e) | ||
return res.status(500).json({ message: 'Server Error' }) | ||
} | ||
}) | ||
|
||
export default router; |
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,48 @@ | ||
import { getPool } from '../database'; | ||
import {sql} from 'slonik'; | ||
import {Proposal, ProposalState} from "../types/proposal"; | ||
|
||
async function getWinner(): Promise<Proposal> { | ||
const pool = await getPool(); | ||
return await pool.connect(async (connection) => { | ||
const proposals = await connection.any(sql.type(ProposalState)` | ||
SELECT | ||
p.*, | ||
json_agg( | ||
json_build_object('value', v.vote, 'comment', v.comment) | ||
) FILTER (WHERE v.vote IS NOT NULL) as results | ||
FROM proposals p | ||
JOIN votes v ON p.id = v.proposal_id | ||
WHERE p.status = 'open' | ||
GROUP BY p.id | ||
HAVING SUM(v.vote) = ( | ||
SELECT MAX(vote_sum) | ||
FROM ( | ||
SELECT SUM(v.vote) as vote_sum | ||
FROM proposals p | ||
JOIN votes v ON p.id = v.proposal_id | ||
WHERE p.status = 'open' | ||
GROUP BY p.id | ||
) as subquery | ||
) | ||
`); | ||
if (proposals.length === 0) { | ||
throw new Error('No open proposals found'); | ||
} | ||
const randomIndex = Math.floor(Math.random() * proposals.length); | ||
const chosenProposal = proposals[randomIndex]; | ||
const updatedProposal = await connection.one(sql.type(Proposal)` | ||
UPDATE proposals | ||
SET status = 'closed' | ||
WHERE id = ${chosenProposal.id} | ||
RETURNING * | ||
`); | ||
|
||
return { | ||
...updatedProposal, | ||
results: chosenProposal.results, | ||
}; | ||
}); | ||
} | ||
|
||
export default { getWinner }; |
This file was deleted.
Oops, something went wrong.
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
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