diff --git a/backend/api/controllers/result.ts b/backend/api/controllers/result.ts index fca46bcf6..984a6c444 100644 --- a/backend/api/controllers/result.ts +++ b/backend/api/controllers/result.ts @@ -1,4 +1,4 @@ -import ResultDAO from "../../dao/result"; +import * as ResultDAL from "../../dao/result"; import { getUser, checkIfPb, @@ -44,7 +44,7 @@ export async function getResults( req: MonkeyTypes.Request ): Promise { const { uid } = req.ctx.decodedToken; - const results = await ResultDAO.getResults(uid); + const results = await ResultDAL.getResults(uid); return new MonkeyResponse("Result retrieved", results); } @@ -53,7 +53,7 @@ export async function deleteAll( ): Promise { const { uid } = req.ctx.decodedToken; - await ResultDAO.deleteAll(uid); + await ResultDAL.deleteAll(uid); Logger.logToDb("user_results_deleted", "", uid); return new MonkeyResponse("All results deleted"); } @@ -64,7 +64,7 @@ export async function updateTags( const { uid } = req.ctx.decodedToken; const { tagIds, resultId } = req.body; - await ResultDAO.updateTags(uid, resultId, tagIds); + await ResultDAL.updateTags(uid, resultId, tagIds); return new MonkeyResponse("Result tags updated"); } @@ -150,7 +150,7 @@ export async function addResult( //get latest result ordered by timestamp let lastResultTimestamp; try { - lastResultTimestamp = (await ResultDAO.getLastResult(uid)).timestamp; + lastResultTimestamp = (await ResultDAL.getLastResult(uid)).timestamp; } catch (e) { lastResultTimestamp = null; } @@ -293,7 +293,7 @@ export async function addResult( if (result.mode !== "custom") delete result.customText; - const addedResult = await ResultDAO.addResult(uid, result); + const addedResult = await ResultDAL.addResult(uid, result); if (isPb) { Logger.logToDb( diff --git a/backend/dao/result.js b/backend/dao/result.js deleted file mode 100644 index 338359424..000000000 --- a/backend/dao/result.js +++ /dev/null @@ -1,85 +0,0 @@ -import { ObjectId } from "mongodb"; -import MonkeyError from "../utils/error"; -import db from "../init/db"; - -import { getUser, getTags } from "./user"; - -class ResultDAO { - static async addResult(uid, result) { - let user; - try { - user = await getUser(uid); - } catch (e) { - user = null; - } - if (!user) throw new MonkeyError(404, "User not found", "add result"); - if (result.uid === undefined) result.uid = uid; - // result.ir = true; - let res = await db.collection("results").insertOne(result); - return { - insertedId: res.insertedId, - }; - } - - static async deleteAll(uid) { - return await db.collection("results").deleteMany({ uid }); - } - - static async updateTags(uid, resultid, tags) { - const result = await db - .collection("results") - .findOne({ _id: new ObjectId(resultid), uid }); - if (!result) throw new MonkeyError(404, "Result not found"); - const userTags = await getTags(uid); - const userTagIds = userTags.map((tag) => tag._id.toString()); - let validTags = true; - tags.forEach((tagId) => { - if (!userTagIds.includes(tagId)) validTags = false; - }); - if (!validTags) { - throw new MonkeyError(422, "One of the tag id's is not valid"); - } - return await db - .collection("results") - .updateOne({ _id: new ObjectId(resultid), uid }, { $set: { tags } }); - } - - static async getResult(uid, id) { - const result = await db - .collection("results") - .findOne({ _id: new ObjectId(id), uid }); - if (!result) throw new MonkeyError(404, "Result not found"); - return result; - } - - static async getLastResult(uid) { - const [lastResult] = await db - .collection("results") - .find({ uid }) - .sort({ timestamp: -1 }) - .limit(1) - .toArray(); - if (!lastResult) throw new MonkeyError(404, "No results found"); - return lastResult; - } - - static async getResultByTimestamp(uid, timestamp) { - return await db.collection("results").findOne({ uid, timestamp }); - } - - static async getResults(uid, start, end) { - start = start ?? 0; - end = end ?? 1000; - const result = await db - .collection("results") - .find({ uid }) - .sort({ timestamp: -1 }) - .skip(start) - .limit(end) - .toArray(); // this needs to be changed to later take patreon into consideration - if (!result) throw new MonkeyError(404, "Result not found"); - return result; - } -} - -export default ResultDAO; diff --git a/backend/dao/result.ts b/backend/dao/result.ts new file mode 100644 index 000000000..87935de2e --- /dev/null +++ b/backend/dao/result.ts @@ -0,0 +1,104 @@ +import { DeleteResult, ObjectId, UpdateResult } from "mongodb"; +import MonkeyError from "../utils/error"; +import db from "../init/db"; + +import { getUser, getTags } from "./user"; + +type MonkeyTypesResult = MonkeyTypes.Result; + +export async function addResult( + uid: string, + result: MonkeyTypesResult +): Promise<{ insertedId: ObjectId }> { + let user; + try { + user = await getUser(uid); + } catch (e) { + user = null; + } + if (!user) throw new MonkeyError(404, "User not found", "add result"); + if (result.uid === undefined) result.uid = uid; + // result.ir = true; + const res = await db + .collection("results") + .insertOne(result); + return { + insertedId: res.insertedId, + }; +} + +export async function deleteAll(uid: string): Promise { + return await db.collection("results").deleteMany({ uid }); +} + +export async function updateTags( + uid: string, + resultId: string, + tags: string[] +): Promise { + const result = await db + .collection("results") + .findOne({ _id: new ObjectId(resultId), uid }); + if (!result) throw new MonkeyError(404, "Result not found"); + const userTags = await getTags(uid); + const userTagIds = userTags.map((tag) => tag._id.toString()); + let validTags = true; + tags.forEach((tagId) => { + if (!userTagIds.includes(tagId)) validTags = false; + }); + if (!validTags) { + throw new MonkeyError(422, "One of the tag id's is not valid"); + } + return await db + .collection("results") + .updateOne({ _id: new ObjectId(resultId), uid }, { $set: { tags } }); +} + +export async function getResult( + uid: string, + id: string +): Promise { + const result = await db + .collection("results") + .findOne({ _id: new ObjectId(id), uid }); + if (!result) throw new MonkeyError(404, "Result not found"); + return result; +} + +export async function getLastResult(uid: string): Promise { + const [lastResult] = await db + .collection("results") + .find({ uid }) + .sort({ timestamp: -1 }) + .limit(1) + .toArray(); + if (!lastResult) throw new MonkeyError(404, "No results found"); + return lastResult; +} + +export async function getResultByTimestamp( + uid: string, + timestamp +): Promise { + return await db + .collection("results") + .findOne({ uid, timestamp }); +} + +export async function getResults( + uid: string, + start?: number, + end?: number +): Promise { + start = start ?? 0; + end = end ?? 1000; + const results = await db + .collection("results") + .find({ uid }) + .sort({ timestamp: -1 }) + .skip(start) + .limit(end) + .toArray(); // this needs to be changed to later take patreon into consideration + if (!results) throw new MonkeyError(404, "Result not found"); + return results; +} diff --git a/backend/types/types.d.ts b/backend/types/types.d.ts index 3dff04cee..2a9aef72e 100644 --- a/backend/types/types.d.ts +++ b/backend/types/types.d.ts @@ -155,7 +155,7 @@ declare namespace MonkeyTypes { } interface Result { - _id: string; + _id: ObjectId; wpm: number; rawWpm: number; charStats: number[];