diff --git a/backend/api/controllers/result.js b/backend/api/controllers/result.js index 7fd1f1030..395745e64 100644 --- a/backend/api/controllers/result.js +++ b/backend/api/controllers/result.js @@ -18,6 +18,17 @@ class ResultController { } } + static async updateTags(req, res, next) { + try { + const { uid } = req.decodedToken; + const { tags, resultid } = req.body; + await ResultDAO.updateTags(uid, resultid, tags); + return res.sendStatus(200); + } catch (e) { + next(e); + } + } + static async addResult(req, res, next) { try { const { uid } = req.decodedToken; @@ -149,15 +160,13 @@ class ResultController { let addedResult = await ResultDAO.addResult(uid, result); - return res - .status(200) - .json({ - message: "Result saved", - isPb, - name: result.name, - tagPbs, - insertedId: addedResult.insertedId, - }); + return res.status(200).json({ + message: "Result saved", + isPb, + name: result.name, + tagPbs, + insertedId: addedResult.insertedId, + }); } catch (e) { next(e); } diff --git a/backend/api/routes/result.js b/backend/api/routes/result.js index 97fb844c3..65e351e5d 100644 --- a/backend/api/routes/result.js +++ b/backend/api/routes/result.js @@ -8,4 +8,6 @@ router.get("/", authenticateRequest, ResultController.getResults); router.post("/add", authenticateRequest, ResultController.addResult); +router.post("/updateTags", authenticateRequest, ResultController.updateTags); + module.exports = router; diff --git a/backend/dao/result.js b/backend/dao/result.js index 3bbd652c7..19c415806 100644 --- a/backend/dao/result.js +++ b/backend/dao/result.js @@ -1,3 +1,4 @@ +const { ObjectID } = require("mongodb"); const MonkeyError = require("../handlers/error"); const { mongoDB } = require("../init/mongodb"); const UserDAO = require("./user"); @@ -18,19 +19,22 @@ class ResultDAO { }; } - static async editResultTags(uid, id, tags) { - const result = await mongoDB().collection("results").findOne({ id, uid }); + static async updateTags(uid, resultid, tags) { + const result = await mongoDB() + .collection("results") + .findOne({ _id: ObjectID(resultid), uid }); if (!result) throw new MonkeyError(404, "Result not found"); const userTags = await UserDAO.getTags(uid); + const userTagIds = userTags.map((tag) => tag._id.toString()); let validTags = true; tags.forEach((tagId) => { - if (!userTags.includes(tagId)) validTags = false; + if (!userTagIds.includes(tagId)) validTags = false; }); if (!validTags) throw new MonkeyError(400, "One of the tag id's is not vaild"); return await mongoDB() .collection("results") - .updateOne({ id, uid }, { $set: { tags } }); + .updateOne({ _id: ObjectID(resultid), uid }, { $set: { tags } }); } static async getResult(uid, id) { @@ -45,7 +49,7 @@ class ResultDAO { const result = await mongoDB() .collection("results") .find({ uid }) - .sort({ timestamp: -1 }) + .sort({ timestamp: 1 }) .skip(start) .limit(end) .toArray(); // this needs to be changed to later take patreon into consideration diff --git a/src/js/account.js b/src/js/account.js index f7e7e69f0..3b9d591d4 100644 --- a/src/js/account.js +++ b/src/js/account.js @@ -248,13 +248,13 @@ function loadMoreLines(lineIndex) { restags = JSON.stringify(result.tags); } - let tagIcons = ``; + let tagIcons = ``; if (tagNames !== "") { if (result.tags !== undefined && result.tags.length > 1) { - tagIcons = ``; + tagIcons = ``; } else { - tagIcons = ``; + tagIcons = ``; } } diff --git a/src/js/db.js b/src/js/db.js index bcb4a4e49..7da08d785 100644 --- a/src/js/db.js +++ b/src/js/db.js @@ -126,6 +126,7 @@ export async function getUserResults() { try { let results = await axiosInstance.get("/results"); dbSnapshot.results = results.data; + await TodayTracker.addAllFromToday(); return true; } catch (e) { Notifications.add("Error getting results", -1); diff --git a/src/js/popups/result-tags-popup.js b/src/js/popups/result-tags-popup.js index 43daa948a..d67a490cb 100644 --- a/src/js/popups/result-tags-popup.js +++ b/src/js/popups/result-tags-popup.js @@ -86,13 +86,16 @@ $("#resultEditTagsPanel .confirmButton").click((e) => { Loader.show(); hide(); axiosInstance - .post("/updateResultTags", { + .post("/results/updateTags", { tags: newtags, resultid: resultid, }) - .then((r) => { + .then((response) => { Loader.hide(); - if (r.data.resultCode === 1) { + + if (response.status !== 200) { + Notifications.add(response.data.message); + } else { Notifications.add("Tags updated.", 1, 2); DB.getSnapshot().results.forEach((result) => { if (result.id === resultid) { @@ -143,8 +146,11 @@ $("#resultEditTagsPanel .confirmButton").click((e) => { "no tags" ); } - } else { - Notifications.add("Error updating tags: " + r.data.message, -1); } + }) + .catch((e) => { + Loader.hide(); + let msg = e?.response?.data?.message ?? e.message; + Notifications.add("Failed to update result tags: " + msg, -1); }); });