feat: Remove shared/

This commit is contained in:
Alexander Polynomdivision 2018-09-29 15:06:14 +02:00
parent 65070b1f5b
commit fdaa9cb8f3
8 changed files with 89 additions and 80 deletions

View File

@ -8,6 +8,12 @@
uid: number, uid: number,
showWelcome: boolean, showWelcome: boolean,
classId: string, classId: string,
score: number,
lastReview: {
correct: number,
wrong: number,
}
} }
` `

View File

@ -3,6 +3,10 @@ import * as bodyparser from "body-parser";
import { authRoute } from "../security/token"; import { authRoute } from "../security/token";
import { userFromSession } from "../utils/user";
import { IUser } from "../models/user";
const userRouter = express.Router(); const userRouter = express.Router();
userRouter.use(authRoute); userRouter.use(authRoute);
userRouter.use(bodyparser.json()); userRouter.use(bodyparser.json());
@ -16,12 +20,17 @@ userRouter.get("/me", async (req, res) => {
if (session !== null) { if (session !== null) {
const user = await db.collection("users").findOne({ username: session.username }); const user = await db.collection("users").findOne({ username: session.username });
// TODO: Strip salt, hash, _id // Copy and strip unneeded attributes
let copy = Object.assign({}, user, {
sessionToken: token,
});
delete copy._id;
delete copy.hash;
delete copy.salt;
res.send({ res.send({
error: "0", error: "0",
data: Object.assign({}, user, { data: copy,
sessionToken: token,
}),
}); });
} else { } else {
res.send({ res.send({
@ -127,22 +136,43 @@ userRouter.post("/level/:id", async (req, res) => {
// Get the data needed for the dashboard // Get the data needed for the dashboard
userRouter.get("/dashboard", async (req, res) => { userRouter.get("/dashboard", async (req, res) => {
console.log("STUB(post): /user/dashboard"); console.log("SEMI-STUB(post): /user/dashboard");
let users: any[] = []; //@ts-ignore
let nr = 10; const { db, token } = req;
for (let i = 0; i < 10; i++)
users = users.concat({ // Get the user
username: `Test User ${i}`, const user = await userFromSession(token, db);
score: 100 * i, // TODO: if (user)
level: Math.floor(Math.random() * Math.floor(10)),
nr: nr--, const { classId } = user;
// Fetch the top ten of the class
const rawTopTen = await db.collection("users").find({
classId,
}, {
sort: {
score: -1,
},
limit: 10,
}).toArray();
let nr = 1;
const topTen = rawTopTen.map((user: IUser) => {
return {
username: user.username,
score: user.score,
// TODO
level: 1,
nr: nr++,
};
}); });
// TODO: Stub // TODO: Stub
res.send({ res.send({
error: "200", error: "200",
data: { data: {
// TODO: Get this some way
nextLevel: { nextLevel: {
name: "Test level", name: "Test level",
desc: "Just a test", desc: "Just a test",
@ -150,11 +180,8 @@ userRouter.get("/dashboard", async (req, res) => {
done: false, done: false,
}, },
topTen: users, topTen,
lastReview: { lastReview: user.lastReview,
correct: 0,
wrong: 0,
},
}, },
}); });
}); });

View File

@ -0,0 +1,14 @@
export interface IUser {
username: string;
uid: string;
showWelcome: boolean;
score: number;
classId: string;
sessionToken: string;
lastReview: {
correct: number;
wrong: number;
};
};

View File

@ -2,7 +2,7 @@ import { pbkdf2Sync, randomBytes } from "crypto";
import { Db } from "mongodb"; import { Db } from "mongodb";
import { IUser } from "shared/user"; import { IUser } from "../models/user";
export async function isAuthenticated(token: string, db: Db): Promise<boolean> { export async function isAuthenticated(token: string, db: Db): Promise<boolean> {
// See if we can find a session with that token // See if we can find a session with that token
@ -34,6 +34,8 @@ export async function performLogin(username: string, password: string, db: Db):
//@ts-ignore //@ts-ignore
classId: user.classId, classId: user.classId,
lastReview: user.lastReview,
// TODO: Implement // TODO: Implement
score: 4, score: 4,
sessionToken, sessionToken,

18
backend/src/utils/user.ts Normal file
View File

@ -0,0 +1,18 @@
import { Db } from "mongodb";
import { IUser } from "../models/user";
export async function userFromSession(token: string, db: Db): Promise<IUser> {
// Get the username
const session = await db.collection("sessions").findOne({ token, });
if (session) {
const user = await db.collection("users").findOne({ username: session.username });
if (user) {
return user;
} else {
throw new Error("Failed to find user");
}
} else {
throw new Error("Failed to find session");
}
}

View File

@ -9,10 +9,6 @@
"outDir": "./dist/", "outDir": "./dist/",
"baseUrl": "./src/", "baseUrl": "./src/"
"paths": {
"shared/*": ["../../shared/*"]
}
} }
} }

View File

@ -19,10 +19,7 @@ module.exports = env => {
] ]
}, },
resolve: { resolve: {
extensions: [".ts", ".js"], extensions: [".ts", ".js"]
alias: {
shared: path.join(__dirname, "../shared/")
}
}, },
output: { output: {
filename: "bundle.js", filename: "bundle.js",

View File

@ -1,51 +0,0 @@
export interface IUser {
username: string;
uid: string;
showWelcome: boolean;
score: number;
sessionToken: string;
};
export interface IUserLevel {
// The numerical representation
level: number;
// The string representation
name: string;
// The user has this level until: score => levelCap
levelCap: number;
};
function levelFactory(): (name: string, levelCap: number) => IUserLevel {
let level = 1;
return (name: string, levelCap: number) => {
return {
level: level++,
name,
levelCap,
};
};
}
const l = levelFactory();
export const UserLevels: IUserLevel[] = [
l("Sklave", 35),
l("Farmer", 75),
l("Soldat", 120),
l("Gladiator", 170),
l("Zenturio", 220),
l("Prätor", 270),
l("Reiter", 320),
l("General", 370),
l("Konsul", 420),
l("Caesar", 470),
];
export function userScoreToLevel(userScore: number): IUserLevel {
// NOTE: The failure level should never be returned
return UserLevels.find((el) => userScore < el.levelCap) || {
level: 0,
name: "Failure",
levelCap: 10000000,
};
}