2018-09-23 20:17:35 +00:00
|
|
|
import * as express from "express";
|
|
|
|
import * as bodyparser from "body-parser";
|
|
|
|
|
|
|
|
import { authRoute } from "../security/token";
|
|
|
|
|
|
|
|
const userRouter = express.Router();
|
|
|
|
userRouter.use(authRoute);
|
2018-09-29 12:23:09 +00:00
|
|
|
userRouter.use(bodyparser.json());
|
2018-09-23 20:17:35 +00:00
|
|
|
|
|
|
|
// Return the user object if the user is still authenticated
|
2018-09-29 12:23:09 +00:00
|
|
|
userRouter.get("/me", async (req, res) => {
|
|
|
|
//@ts-ignore
|
|
|
|
const { db, token } = req;
|
2018-09-23 20:17:35 +00:00
|
|
|
|
2018-09-29 12:23:09 +00:00
|
|
|
const session = await db.collection("sessions").findOne({ token, });
|
|
|
|
if (session !== null) {
|
|
|
|
const user = await db.collection("users").findOne({ username: session.username });
|
|
|
|
|
|
|
|
// TODO: Strip salt, hash, _id
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: Object.assign({}, user, {
|
|
|
|
sessionToken: token,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.send({
|
|
|
|
error: "404",
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
}
|
2018-09-23 20:17:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Removes the user's session
|
|
|
|
userRouter.get("/logout", async (req, res) => {
|
2018-09-29 12:23:09 +00:00
|
|
|
// Try to remove the session
|
|
|
|
//@ts-ignore
|
|
|
|
await req.db.collections("sessions").findOneAndDelete({ session: req.get("Token") });
|
2018-09-23 20:17:35 +00:00
|
|
|
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-09-24 11:53:20 +00:00
|
|
|
// TODO: This should be shared with the frontend, to remove code duplication
|
|
|
|
export enum VocabType {
|
|
|
|
NOMEN = 0,
|
|
|
|
VERB = 1,
|
|
|
|
ADJEKTIV = 2,
|
|
|
|
ADVERB = 3,
|
|
|
|
};
|
|
|
|
|
2018-09-23 20:17:35 +00:00
|
|
|
// Gets the user's review queue
|
|
|
|
userRouter.get("/queue", async (req, res) => {
|
|
|
|
console.log("STUB: /user/queue");
|
|
|
|
|
|
|
|
// TODO: Stub
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
2018-09-24 11:53:20 +00:00
|
|
|
data: {
|
|
|
|
queue: [{
|
|
|
|
german: ["Wein"],
|
|
|
|
hint: "Worte auf '-um' sind meistens NeutrUM",
|
|
|
|
type: VocabType.NOMEN,
|
|
|
|
latin: {
|
|
|
|
grundform: "Vinum",
|
|
|
|
genitiv: "Vini",
|
|
|
|
genus: "Neutrum"
|
|
|
|
},
|
|
|
|
id: 0
|
|
|
|
}]
|
|
|
|
},
|
2018-09-23 20:17:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get ot set the last review results
|
|
|
|
userRouter.get("/lastReview", async (req, res) => {
|
|
|
|
console.log("STUB(get): /user/lastReview");
|
|
|
|
|
|
|
|
// TODO: Stub
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
2018-09-24 11:36:42 +00:00
|
|
|
data: {
|
|
|
|
correct: 6,
|
|
|
|
wrong: 2,
|
|
|
|
},
|
2018-09-23 20:17:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
userRouter.post("/lastReview", async (req, res) => {
|
|
|
|
console.log("STUB(post): /user/lastReview");
|
|
|
|
|
|
|
|
// TODO: Stub
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the next level
|
|
|
|
userRouter.get("/nextLevel", async (req, res) => {
|
|
|
|
console.log("STUB: /user/nextLevel");
|
|
|
|
|
|
|
|
// TODO: Stub
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {
|
|
|
|
name: "Test level",
|
|
|
|
desc: "Just a test",
|
|
|
|
level: 3,
|
|
|
|
|
|
|
|
done: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark a level as done
|
|
|
|
userRouter.post("/level/:id", async (req, res) => {
|
|
|
|
console.log("STUB(post): /user/level/:id");
|
|
|
|
|
|
|
|
// TODO: Stub
|
|
|
|
res.send({
|
|
|
|
error: "0",
|
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Get the data needed for the dashboard
|
2018-09-24 16:29:29 +00:00
|
|
|
userRouter.get("/dashboard", async (req, res) => {
|
2018-09-23 20:17:35 +00:00
|
|
|
console.log("STUB(post): /user/dashboard");
|
|
|
|
|
2018-09-24 16:29:29 +00:00
|
|
|
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--,
|
|
|
|
});
|
|
|
|
|
2018-09-23 20:17:35 +00:00
|
|
|
// TODO: Stub
|
|
|
|
res.send({
|
2018-09-24 16:29:29 +00:00
|
|
|
error: "200",
|
2018-09-23 20:17:35 +00:00
|
|
|
data: {
|
2018-09-24 16:29:29 +00:00
|
|
|
nextLevel: {
|
|
|
|
name: "Test level",
|
|
|
|
desc: "Just a test",
|
|
|
|
level: 3,
|
|
|
|
|
|
|
|
done: false,
|
|
|
|
},
|
|
|
|
topTen: users,
|
|
|
|
lastReview: {
|
|
|
|
correct: 0,
|
|
|
|
wrong: 0,
|
|
|
|
},
|
2018-09-23 20:17:35 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
export default userRouter;
|