feat(api): add endpoint for fetching full info for a user
This commit is contained in:
parent
e3f5918b2a
commit
79f5f2cc10
39
server/api/user/[id]/index.get.ts
Normal file
39
server/api/user/[id]/index.get.ts
Normal file
@ -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<IUser> = user.toObject();
|
||||||
|
if (!obj.profile!.showEmail) delete obj.email;
|
||||||
|
return obj;
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user