feat: Remove shared/
This commit is contained in:
parent
65070b1f5b
commit
fdaa9cb8f3
@ -8,6 +8,12 @@
|
||||
uid: number,
|
||||
showWelcome: boolean,
|
||||
classId: string,
|
||||
score: number,
|
||||
|
||||
lastReview: {
|
||||
correct: number,
|
||||
wrong: number,
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
|
@ -3,6 +3,10 @@ import * as bodyparser from "body-parser";
|
||||
|
||||
import { authRoute } from "../security/token";
|
||||
|
||||
import { userFromSession } from "../utils/user";
|
||||
|
||||
import { IUser } from "../models/user";
|
||||
|
||||
const userRouter = express.Router();
|
||||
userRouter.use(authRoute);
|
||||
userRouter.use(bodyparser.json());
|
||||
@ -16,12 +20,17 @@ userRouter.get("/me", async (req, res) => {
|
||||
if (session !== null) {
|
||||
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({
|
||||
error: "0",
|
||||
data: Object.assign({}, user, {
|
||||
sessionToken: token,
|
||||
}),
|
||||
data: copy,
|
||||
});
|
||||
} else {
|
||||
res.send({
|
||||
@ -127,22 +136,43 @@ userRouter.post("/level/:id", async (req, res) => {
|
||||
|
||||
// Get the data needed for the dashboard
|
||||
userRouter.get("/dashboard", async (req, res) => {
|
||||
console.log("STUB(post): /user/dashboard");
|
||||
console.log("SEMI-STUB(post): /user/dashboard");
|
||||
|
||||
let users: any[] = [];
|
||||
let nr = 10;
|
||||
for (let i = 0; i < 10; i++)
|
||||
users = users.concat({
|
||||
username: `Test User ${i}`,
|
||||
score: 100 * i,
|
||||
level: Math.floor(Math.random() * Math.floor(10)),
|
||||
nr: nr--,
|
||||
//@ts-ignore
|
||||
const { db, token } = req;
|
||||
|
||||
// Get the user
|
||||
const user = await userFromSession(token, db);
|
||||
// TODO: if (user)
|
||||
|
||||
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
|
||||
res.send({
|
||||
error: "200",
|
||||
data: {
|
||||
// TODO: Get this some way
|
||||
nextLevel: {
|
||||
name: "Test level",
|
||||
desc: "Just a test",
|
||||
@ -150,11 +180,8 @@ userRouter.get("/dashboard", async (req, res) => {
|
||||
|
||||
done: false,
|
||||
},
|
||||
topTen: users,
|
||||
lastReview: {
|
||||
correct: 0,
|
||||
wrong: 0,
|
||||
},
|
||||
topTen,
|
||||
lastReview: user.lastReview,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
14
backend/src/models/user.ts
Normal file
14
backend/src/models/user.ts
Normal 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;
|
||||
};
|
||||
};
|
@ -2,7 +2,7 @@ import { pbkdf2Sync, randomBytes } from "crypto";
|
||||
|
||||
import { Db } from "mongodb";
|
||||
|
||||
import { IUser } from "shared/user";
|
||||
import { IUser } from "../models/user";
|
||||
|
||||
export async function isAuthenticated(token: string, db: Db): Promise<boolean> {
|
||||
// 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
|
||||
classId: user.classId,
|
||||
|
||||
lastReview: user.lastReview,
|
||||
|
||||
// TODO: Implement
|
||||
score: 4,
|
||||
sessionToken,
|
||||
|
18
backend/src/utils/user.ts
Normal file
18
backend/src/utils/user.ts
Normal 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");
|
||||
}
|
||||
}
|
@ -9,10 +9,6 @@
|
||||
|
||||
"outDir": "./dist/",
|
||||
|
||||
"baseUrl": "./src/",
|
||||
|
||||
"paths": {
|
||||
"shared/*": ["../../shared/*"]
|
||||
}
|
||||
"baseUrl": "./src/"
|
||||
}
|
||||
}
|
||||
|
@ -19,10 +19,7 @@ module.exports = env => {
|
||||
]
|
||||
},
|
||||
resolve: {
|
||||
extensions: [".ts", ".js"],
|
||||
alias: {
|
||||
shared: path.join(__dirname, "../shared/")
|
||||
}
|
||||
extensions: [".ts", ".js"]
|
||||
},
|
||||
output: {
|
||||
filename: "bundle.js",
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user