-
Notifications
You must be signed in to change notification settings - Fork 37
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
Open
adarshm11
wants to merge
33
commits into
dev
Choose a base branch
from
sign2
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
c0ce851
sign2 has arrived
adarshm11 5e453fa
ts lint frying me
adarshm11 8a96dd1
sign2 api tests
adarshm11 62a4397
bug fix attempt 1
adarshm11 cb8934b
bug fix attempt 2
adarshm11 352614b
attempt 3
adarshm11 f6326a4
pmo
adarshm11 3421e8e
should fix failing test plz
adarshm11 aca3b13
please
adarshm11 75b6e34
PLZ
adarshm11 8a7a6b7
error msg color
adarshm11 e84e266
sign2 url for prod
adarshm11 3fbc842
test the 409 error thing before merge
adarshm11 667a72a
fix tests when 409 tested
adarshm11 84e2223
make sure to fix tests that fail because of sign2 disabled returning 200
adarshm11 7e2ee2c
sign2 returns 409 when adding existing user
adarshm11 d972b78
sign2 url from env
adarshm11 001bd4a
todo: add sign2 url to frontend env in docker compose
adarshm11 2ab660b
update sign2 url
adarshm11 a35c2d2
axios to fetch from sign2 server
adarshm11 7fe93e7
add nginx proxying for emulator
adarshm11 dc1ff80
add websocket support for emulator iframe
adarshm11 1dcbaa2
upgrade websocket connection
adarshm11 d27146e
fix tornado/nginx casing mismatch
adarshm11 14db141
static img instead of iframe and websocket
adarshm11 71bbf6f
remove unused member from config.json
adarshm11 e0fd01c
remove api call based unit tests
adarshm11 1bed3e0
simplify api
adarshm11 42589f4
lowkirk don't need unit tests rn
adarshm11 bf2b408
simplify api setup
adarshm11 1c160a8
fix lint
adarshm11 2c2743c
fix clark route to be /leetcode
adarshm11 58fb5a6
extend interval of re-querying emulator
adarshm11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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. |
||
| 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; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

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