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; +});