From da0b811428bb95d816d84efd3b1804adca4374f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Wed, 11 Oct 2023 16:45:11 -0400 Subject: [PATCH] feat(api): create endpoint to retrieve all reviews left on the current user's story --- server/api/user/me/reviews.get.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server/api/user/me/reviews.get.ts diff --git a/server/api/user/me/reviews.get.ts b/server/api/user/me/reviews.get.ts new file mode 100644 index 0000000..ab779f9 --- /dev/null +++ b/server/api/user/me/reviews.get.ts @@ -0,0 +1,24 @@ +import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn"; +import { Story } from "~/models/stories"; +import { Review } from "~/models/stories/review"; + +export default eventHandler(async (ev) => { + isLoggedIn(ev); + let stories = await Story.find({ + author: ev.context.currentUser!._id, + }).exec(); + let idArr = stories.map((a) => a._id); + let ar = await Review.find({ + leftOn: { + $in: idArr, + }, + replyingTo: null, + }) + .populate("story") + .exec(); + return ar + .map((a) => a.toObject()) + .sort( + (a, b) => b.datePosted.getMilliseconds() - a.datePosted.getMilliseconds(), + ); +});