From 79f5f2cc10369f76acb3709073e176df3e2c7c90 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: Tue, 10 Oct 2023 21:53:53 -0400 Subject: [PATCH] feat(api): add endpoint for fetching full info for a user --- server/api/user/[id]/index.get.ts | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 server/api/user/[id]/index.get.ts diff --git a/server/api/user/[id]/index.get.ts b/server/api/user/[id]/index.get.ts new file mode 100644 index 0000000..78e8501 --- /dev/null +++ b/server/api/user/[id]/index.get.ts @@ -0,0 +1,39 @@ +import { messages } from "~/lib/server/constants"; +import { Ficmas } from "~/models/challenges/ficmas"; +import { Challenge } from "~/models/challenges/gen"; +import { IUser, User } from "~/models/user"; + +export default cachedEventHandler(async (ev) => { + const id = parseInt(getRouterParam(ev, "id")!); + const dontSelect = ["-password", "-auth"]; + + if (!ev.context.currentUser?.profile.isAdmin) { + dontSelect.push("-ipLog"); + } + + let user = await User.findOne({ _id: id }) + .select(dontSelect.join(" ")) + .populate({ path: "favs.authors", select: dontSelect.join(" ") }) + .populate({ + path: "favs.stories", + populate: [ + { path: "author", select: "username _id" }, + { + path: "ficmas", + model: Ficmas, + populate: { path: "wisher", select: "username _id" }, + }, + { path: "challenge", model: Challenge }, + ], + }) + .exec(); + if (!user) { + throw createError({ + statusCode: 404, + message: messages[404], + }); + } + let obj: Partial = user.toObject(); + if (!obj.profile!.showEmail) delete obj.email; + return obj; +});