updated clear pb to new system

This commit is contained in:
Miodec 2021-07-12 23:34:55 +01:00
parent 509368b869
commit b5b619816b
4 changed files with 23 additions and 7 deletions

View file

@ -47,6 +47,16 @@ class UserController {
}
}
static async clearPb(req, res, next) {
try {
const { uid } = req.decodedToken;
await UsersDAO.clearPb(uid);
return res.sendStatus(200);
} catch (e) {
return next(e);
}
}
static async checkName(req, res, next) {
try {
const { name } = req.body;

View file

@ -4,6 +4,8 @@ const UserController = require("../controllers/user");
const router = Router();
router.get("/", authenticateRequest, UserController.getUser);
router.post("/signup", UserController.createNewUser);
router.post("/checkName", UserController.checkName);
@ -14,7 +16,7 @@ router.post("/updateName", authenticateRequest, UserController.updateName);
router.post("/updateEmail", authenticateRequest, UserController.updateEmail);
router.get("/", authenticateRequest, UserController.getUser);
router.post("/clearPb", authenticateRequest, UserController.clearPb);
router.post("/tags/add", authenticateRequest, UserController.addTag);

View file

@ -25,6 +25,12 @@ class UsersDAO {
.updateOne({ uid }, { $set: { name } });
}
static async clearPb(uid) {
return await mongoDB()
.collection("users")
.updateOne({ uid }, { $set: { personalBests: {} } });
}
static async isNameAvailable(name) {
const nameDoc = await mongoDB().collection("users").findOne({ name });
if (nameDoc) {

View file

@ -179,7 +179,7 @@ list.updateEmail = new SimplePopup(
.post("/user/updateEmail", {
uid: user.uid,
previousEmail: user.email,
newEmail: newEmail,
newEmail: email,
})
.then((data) => {
Loader.hide();
@ -333,7 +333,7 @@ list.resetPersonalBests = new SimplePopup(
let response;
try {
response = await axiosInstance.post("/user/resetPbs");
response = await axiosInstance.post("/user/clearPb");
} catch (e) {
Loader.hide();
let msg = e?.response?.data?.message ?? e.message;
@ -344,10 +344,8 @@ list.resetPersonalBests = new SimplePopup(
if (response.status !== 200) {
Notifications.add(response.data.message);
} else {
Notifications.add("Personal bests removed, refreshing the page...", 0);
setTimeout(() => {
location.reload();
}, 1500);
Notifications.add("Personal bests have been reset", 1);
DB.getSnapshot().personalBests = {};
}
} catch (e) {
Loader.hide();