-
Notifications
You must be signed in to change notification settings - Fork 39
leetcode leaderboard page #1981
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
base: dev
Are you sure you want to change the base?
Changes from all commits
a2e92ab
52643a2
ca35deb
f884ee0
82e0ece
3703cd2
2d0cf4f
b5a0b3d
091d711
3a3aac8
8ca4c09
84c258a
5355c87
08e5b09
debec32
cf1c04c
8dcf67f
4691765
4d1710c
b05b753
5752ab4
8fd4582
9531133
b35f6a8
8201424
65c8ee4
85f7cb1
f75eeb2
4c6297b
a3a064d
21ab3d2
49cb24c
9fac8da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const { | ||
| OK, | ||
| SERVER_ERROR, | ||
| BAD_REQUEST | ||
| } = require('../../util/constants.js').STATUS_CODES; | ||
| const { decodeToken } = require('../util/token-functions.js'); | ||
| const logger = require('../../util/logger.js'); | ||
| const AuditLogActions = require('../util/auditLogActions.js'); | ||
| const AuditLog = require('../models/AuditLog.js'); | ||
| const membershipState = require('../../util/constants').MEMBERSHIP_STATE; | ||
| const { LEETCODE_LED_SIGN = {} } = require('../../config/config.json'); | ||
| const axios = require('axios'); | ||
| const LEETCODE_LED_SIGN_URL = process.env.LEETCODE_LED_SIGN_URL || 'http://localhost:12121'; | ||
|
|
||
| // middleware to abstract token decoding and enabled check | ||
| router.use(async (req, res, next) => { | ||
| const decoded = await decodeToken(req, membershipState.OFFICER); | ||
| if (decoded.status !== OK) { | ||
| return res.sendStatus(decoded.status); | ||
| } | ||
|
|
||
| if (!LEETCODE_LED_SIGN.ENABLED) { | ||
| logger.warn('LeetCode Leaderboard is disabled, returning 200 to mock the service'); | ||
| return res.sendStatus(OK); | ||
| } | ||
|
|
||
| res.locals.userId = decoded.token._id; | ||
| next(); | ||
| }); | ||
|
|
||
| // request handler for clark -> sign2 requests | ||
| const handleLeetCodeRequest = async (res, method, endpoint, data = null) => { | ||
| try { | ||
| const url = new URL(endpoint, LEETCODE_LED_SIGN_URL); | ||
| const response = await axios({ method, url: url.href, data }); | ||
|
|
||
| if (response.data && 'error' in response.data) { | ||
| throw new Error(response.data.error); | ||
| } | ||
|
|
||
| return response.data; | ||
| } catch (err) { | ||
| logger.error(`Error with LeetCode Leaderboard at ${endpoint}: `, err.message || err); | ||
| res.status(SERVER_ERROR).send('Error communicating with LeetCode Leaderboard'); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| router.get('/', async (req, res) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this code still, if we have the router.use does router.use take care of all this stuff now
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets do it it seems clean |
||
| const data = await handleLeetCodeRequest(res, 'GET', '/getAllUsers'); | ||
| if (data) { | ||
| return res.status(OK).json({ users: data.users }); | ||
| } | ||
| }); | ||
|
|
||
| router.post('/addUser', async (req, res) => { | ||
| const { username, firstName, lastName } = req.body; | ||
| const data = await handleLeetCodeRequest(res, 'POST', '/user/add', { username, firstName, lastName }); | ||
|
|
||
| if (data) { | ||
| AuditLog.create({ | ||
| userId: res.locals.userId, | ||
| action: AuditLogActions.ADD_LEETCODE_USER, | ||
| details: { username }, | ||
| }); | ||
| return res.sendStatus(OK); | ||
| } | ||
| }); | ||
|
|
||
| router.post('/deleteUser', async (req, res) => { | ||
| const { username } = req.body; | ||
| const data = await handleLeetCodeRequest(res, 'POST', '/user/remove', { username }); | ||
|
|
||
| if (data) { | ||
| AuditLog.create({ | ||
| userId: res.locals.userId, | ||
| action: AuditLogActions.DELETE_LEETCODE_USER, | ||
| details: { username }, | ||
| }); | ||
| return res.sendStatus(OK); | ||
| } | ||
| }); | ||
|
|
||
| module.exports = router; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ COPY ./package-lock.json /frontend/package-lock.json | |
| # and instead define them inline here. We do this as multi-stage | ||
| # builds and environment variables from docker-compose.yml weren't working. | ||
| ENV REACT_APP_BASE_API_URL https://sce.sjsu.edu | ||
| ENV REACT_APP_SIGN2_URL /sign2/ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we dont use this anywhere do we need it |
||
|
|
||
| RUN npm install | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { ApiResponse } from './ApiResponses'; | ||
| import { BASE_API_URL } from '../Enums'; | ||
|
|
||
| export async function getAllUsers(token) { | ||
| let status = new ApiResponse(); | ||
| try { | ||
| const url = new URL('/api/LeetCodeLeaderboard/', BASE_API_URL); | ||
| const res = await fetch(url.href, { | ||
| headers: { | ||
| 'Authorization': `Bearer ${token}`, | ||
| } | ||
| }); | ||
| if (res.ok) { | ||
| const result = await res.json(); | ||
| status.responseData = result; | ||
| } else { | ||
| status.error = true; | ||
| } | ||
| } catch (err) { | ||
| status.error = true; | ||
| status.responseData = err; | ||
| } | ||
| return status; | ||
| } | ||
|
|
||
| export async function addUser(userData, token) { | ||
| let status = new ApiResponse(); | ||
| try { | ||
| const url = new URL('/api/LeetCodeLeaderboard/addUser', BASE_API_URL); | ||
| const res = await fetch(url.href, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Authorization': `Bearer ${token}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(userData), | ||
| }); | ||
| if (res.ok) { | ||
| status.error = false; | ||
| } else { | ||
| status.error = true; | ||
| status.statusCode = res.status; | ||
| } | ||
| } catch (err) { | ||
| status.error = true; | ||
| status.responseData = err; | ||
| } | ||
| return status; | ||
| } | ||
|
|
||
| export async function deleteUser(username, token) { | ||
| let status = new ApiResponse(); | ||
| try { | ||
| const url = new URL('/api/LeetCodeLeaderboard/deleteUser', BASE_API_URL); | ||
| const res = await fetch(url.href, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Authorization': `Bearer ${token}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ username }), | ||
| }); | ||
| status.error = !res.ok; | ||
| } catch (err) { | ||
| status.error = true; | ||
| status.responseData = err; | ||
| } | ||
| return status; | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.