next/server/api/user/[id]/index.get.ts

41 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import { messages } from "@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: "chapters.bands" },
{
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<IUser> = user.toObject();
if (!obj.profile!.showEmail) delete obj.email;
return obj;
});