From 2901a1f899445851cfe2ec53a66175406819f5ee Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Tue, 10 Jan 2023 01:36:29 +0100 Subject: [PATCH] chore: formattato il codice e rimosso un vecchio file --- README.md | 8 ++++++++ client/hooks.tsx | 2 +- server/auth.ts | 46 ++++++++++++++++++++++++---------------------- server/routes.ts | 10 +++++----- shared/model.ts | 4 ++-- shared/utils.ts | 7 +------ utils/util.js | 25 ------------------------- 7 files changed, 41 insertions(+), 61 deletions(-) delete mode 100644 utils/util.js diff --git a/README.md b/README.md index 3824ef9..32b23bd 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ $ npm run build $ npm run serve ``` +## Contribuire + +Plz esegui il seguente comando prima di fare commit così lo stile del codice è uniforme per tutti + +```bash shell +$ prettier -w '**/*.ts' '**/*.tsx' +``` + # TODO - [ ] Autenticazione vera magari con OAuth dell'ateneo o Google \ No newline at end of file diff --git a/client/hooks.tsx b/client/hooks.tsx index 2462864..60c3fc6 100644 --- a/client/hooks.tsx +++ b/client/hooks.tsx @@ -1,7 +1,7 @@ import { StateUpdater, useEffect, useState } from 'preact/hooks' import { createContext } from 'preact' -import { prependBaseUrl} from './api' +import { prependBaseUrl } from './api' type Metadata = { title?: string diff --git a/server/auth.ts b/server/auth.ts index 186d352..0049e5e 100644 --- a/server/auth.ts +++ b/server/auth.ts @@ -1,4 +1,4 @@ -import { Router } from 'express'; +import { Router } from 'express' import { AuthorizationCode } from 'simple-oauth2' import fetch from 'node-fetch' @@ -11,53 +11,55 @@ export function setupOauth(r: Router) { auth: { authorizePath: process.env.OAUTH_AUTH_URL ?? '', tokenHost: process.env.OAUTH_TOKEN_HOST ?? '', - tokenPath: process.env.OAUTH_TOKEN_PATH ?? '' + tokenPath: process.env.OAUTH_TOKEN_PATH ?? '', }, - }; + } const conf = { redirect_uri: process.env.OAUTH_REDIRECT_URL ?? '', scope: process.env.OAUTH_SCOPES ?? '', } - const client = new AuthorizationCode(config); + const client = new AuthorizationCode(config) const authorizationUri = client.authorizeURL({ redirect_uri: conf.redirect_uri, scope: conf.scope, - state: '' - }); + state: '', + }) r.get('/redirect', (req, res) => { - res.redirect(authorizationUri); - }); + res.redirect(authorizationUri) + }) // Callback service parsing the authorization token and asking for the access token r.get('/callback', async (req, res) => { - const code = req.query.code as string; + const code = req.query.code as string const options = { code, - redirect_uri: conf.redirect_uri - }; + redirect_uri: conf.redirect_uri, + } try { - const accessToken = await client.getToken(options); + const accessToken = await client.getToken(options) console.log(accessToken.token.access_token) - const userInfo = await (await fetch(process.env.OAUTH_USER_INFO_URL ?? '', { - method: 'GET', - headers: { - 'Authorization': "Bearer " + accessToken.token.access_token - } - })).json() + const userInfo = await ( + await fetch(process.env.OAUTH_USER_INFO_URL ?? '', { + method: 'GET', + headers: { + Authorization: 'Bearer ' + accessToken.token.access_token, + }, + }) + ).json() // TODO: call to db && login - return res.status(200).json(userInfo); + return res.status(200).json(userInfo) } catch (error) { - console.error('Access Token Error', error.message); - return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`); + console.error('Access Token Error', error.message) + return res.status(500).redirect(`/error?message=${encodeURIComponent('Autenticazione fallita')}`) } - }); + }) } diff --git a/server/routes.ts b/server/routes.ts index 8189431..0364a09 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -229,14 +229,14 @@ export async function createApiRouter() { solutions.forEach(s => { if (!stats[s.sentBy]) { stats[s.sentBy] = { - sentSolutionsCount: s.status !== "pending" ? 1 : 0, - correctSolutionsCount: s.status === "correct" ? 1 : 0 + sentSolutionsCount: s.status !== 'pending' ? 1 : 0, + correctSolutionsCount: s.status === 'correct' ? 1 : 0, } } else { - if (s.status !== "pending") { + if (s.status !== 'pending') { stats[s.sentBy].sentSolutionsCount += 1 } - if (s.status === "correct") { + if (s.status === 'correct') { stats[s.sentBy].correctSolutionsCount += 1 } } @@ -248,7 +248,7 @@ export async function createApiRouter() { r.get('/api/solutions', async (req, res) => { let queryUser = (req.query.user ?? null) as UserId | null let queryProblem = (req.query.problem ?? null) as ProblemId | null - let isPublic = (req.query.public === '') + let isPublic = req.query.public === '' const requestUser = await getRequestUser(req) diff --git a/shared/model.ts b/shared/model.ts index 933d8b8..a72c23c 100644 --- a/shared/model.ts +++ b/shared/model.ts @@ -57,8 +57,8 @@ export type SolutionStatus = 'pending' | 'correct' | 'wrong' export type SolutionId = Id export type SolutionStat = { - sentSolutionsCount: number, - correctSolutionsCount: number, + sentSolutionsCount: number + correctSolutionsCount: number } export type Solution = { diff --git a/shared/utils.ts b/shared/utils.ts index aaf83c0..cd97779 100644 --- a/shared/utils.ts +++ b/shared/utils.ts @@ -22,12 +22,7 @@ export function sortByNumericKey(items: T[], keyFn: KeyFn, ascendi return sortByKey(compareNumberKeys, items, keyFn, ascending) } -function sortByKey( - compareFn: (a: K, b: K) => number, - items: T[], - keyFn: (item: T) => K, - ascending: boolean -) { +function sortByKey(compareFn: (a: K, b: K) => number, items: T[], keyFn: (item: T) => K, ascending: boolean) { const sortedList = [...items] const order = ascending ? 1 : -1 sortedList.sort((a, b) => order * compareFn(keyFn(a), keyFn(b))) diff --git a/utils/util.js b/utils/util.js deleted file mode 100644 index 5e7ea9b..0000000 --- a/utils/util.js +++ /dev/null @@ -1,25 +0,0 @@ -export function toLocalISO(date) { - const tzo = -date.getTimezoneOffset() - const dif = tzo >= 0 ? '+' : '-' - const pad = function (num) { - return num.toString().padStart(2, '0') - } - - return ( - date.getFullYear() + - '-' + - pad(date.getMonth() + 1) + - '-' + - pad(date.getDate()) + - 'T' + - pad(date.getHours()) + - ':' + - pad(date.getMinutes()) + - ':' + - pad(date.getSeconds()) + - dif + - pad(Math.floor(Math.abs(tzo) / 60)) + - ':' + - pad(Math.abs(tzo) % 60) - ) -}