mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-09 13:44:29 +08:00
rewrote config to the new standard
This commit is contained in:
parent
c09f9723f8
commit
218ccb3d53
4 changed files with 42 additions and 82 deletions
18
backend/api/controllers/config.js
Normal file
18
backend/api/controllers/config.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import ConfigDAO from "../../dao/configDAO";
|
||||
import { validateConfig } from "../../handlers/validation";
|
||||
|
||||
class ConfigController {
|
||||
static async saveConfig(req, res, next) {
|
||||
try {
|
||||
const { config } = req.body;
|
||||
const { uid } = req.decodedToken;
|
||||
validateConfig(config);
|
||||
await ConfigDAO.saveConfig(uid, config);
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConfigController;
|
||||
10
backend/api/routes/config.js
Normal file
10
backend/api/routes/config.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { authenticateRequest } from "../../middlewares/auth";
|
||||
|
||||
const { Router } = require("express");
|
||||
import ConfigController from "../controllers/auth";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/config/save", authenticateRequest, ConfigController.saveConfig);
|
||||
|
||||
module.exports = router;
|
||||
14
backend/dao/configDAO.js
Normal file
14
backend/dao/configDAO.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
const MonkeyError = require("../handlers/error");
|
||||
const { mongoDB } = require("../init/mongodb");
|
||||
|
||||
class ConfigDAO {
|
||||
|
||||
static async saveConfig(uid, config) {
|
||||
return await mongoDB()
|
||||
.collection("configs")
|
||||
.updateOne({ uid }, { $set: { config } }, { upsert: true });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = ConfigDAO;
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
|
||||
|
||||
app.post("/saveConfig", authenticateToken, (req, res) => {
|
||||
try {
|
||||
if (req.uid === undefined || req.body.obj === undefined) {
|
||||
console.error(`error saving config for ${req.uid} - missing input`);
|
||||
res.send({
|
||||
resultCode: -1,
|
||||
message: "Missing input",
|
||||
});
|
||||
}
|
||||
|
||||
let obj = req.body.obj;
|
||||
let errorMessage = "";
|
||||
let err = false;
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if (err) return;
|
||||
if (!isConfigKeyValid(key)) {
|
||||
err = true;
|
||||
console.error(`${key} failed regex check`);
|
||||
errorMessage = `${key} failed regex check`;
|
||||
}
|
||||
if (err) return;
|
||||
if (key === "resultFilters") return;
|
||||
if (key === "customBackground") return;
|
||||
let val = obj[key];
|
||||
if (Array.isArray(val)) {
|
||||
val.forEach((valarr) => {
|
||||
if (!isConfigKeyValid(valarr)) {
|
||||
err = true;
|
||||
console.error(`${key}: ${valarr} failed regex check`);
|
||||
errorMessage = `${key}: ${valarr} failed regex check`;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (!isConfigKeyValid(val)) {
|
||||
err = true;
|
||||
console.error(`${key}: ${val} failed regex check`);
|
||||
errorMessage = `${key}: ${val} failed regex check`;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (err) {
|
||||
console.error(
|
||||
`error saving config for ${req.uid} - bad input - ${JSON.stringify(
|
||||
request.obj
|
||||
)}`
|
||||
);
|
||||
res.send({
|
||||
resultCode: -1,
|
||||
message: "Bad input. " + errorMessage,
|
||||
});
|
||||
}
|
||||
|
||||
User.findOne({ uid: req.uid }, (err, user) => {
|
||||
if (err) res.status(500).send({ error: err });
|
||||
user.config = obj;
|
||||
user.save();
|
||||
})
|
||||
.then(() => {
|
||||
res.send({
|
||||
resultCode: 1,
|
||||
message: "Saved",
|
||||
});
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(
|
||||
`error saving config to DB for ${req.uid} - ${e.message}`
|
||||
);
|
||||
res.send({
|
||||
resultCode: -1,
|
||||
message: e.message,
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(`error saving config for ${req.uid} - ${e}`);
|
||||
res.send({
|
||||
resultCode: -999,
|
||||
message: e,
|
||||
});
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue