From 740d8a9f2cebc55c00c16a5b0ef6040132764756 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: Mon, 25 Sep 2023 19:31:30 -0400 Subject: [PATCH] refactor(api): create story query helper this is for retrieving + populating stories from a database with a specified query --- lib/server/listQuerier.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/server/listQuerier.ts diff --git a/lib/server/listQuerier.ts b/lib/server/listQuerier.ts new file mode 100644 index 0000000..44f0259 --- /dev/null +++ b/lib/server/listQuerier.ts @@ -0,0 +1,23 @@ +import { Band } from "~/models/band" +import { Challenge } from "~/models/challenges/gen" +import { Story } from "~/models/stories" + +export default async function(query, context, limit?, sort?) { + query["chapters.hidden"] = false + if(context.currentUser) { + if(!query.author) query.author = {} + if(!query["chapters.bands"]) query["chapters.bands"] = {} + query["chapters.bands"]["$nin"] = context.currentUser.hidden_bands + query["author"]["$nin"] = context.currentUser.hidden_authors + } + query["ficmas"] = { + "$nin": context.ficmasarray_raw.map(a => a._id) + }; + console.log(query) + let stories = await Story.find(query, null).collation({locale: "en"}).sort(sort ? sort : {"chapters.posted": -1}) + .populate({path: 'ficmas', populate: {path: 'wisher', model: 'User', select: 'username _id'}}).populate("chapters.bands") + .populate({path: "challenge", model: Challenge}).populate('author', 'username _id') + .limit(limit || Infinity) + .exec(); + return stories +} \ No newline at end of file