next/server/api/story/[id]/full.get.ts
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 1447960b1a
refactor(db/models): update populatedDoc types
- remove `& Document` qualifier
- cast `T | number` to T with the `as` keyword
2023-12-29 19:06:55 -05:00

28 lines
781 B
TypeScript

import { storyQuerier } from "@server/dbHelpers";
import { chapterTransformer } from "@server/dbHelpers";
import { isLoggedIn } from "@server/middlewareButNotReally";
import { messages } from "@server/constants";
import { IUser } from "@models/user";
export default eventHandler(async (ev) => {
isLoggedIn(ev);
const s = await storyQuerier(ev);
const hidden = s.chapters.some((a) => a.hidden);
if (
hidden &&
ev.context.currentUser?._id !== (s.author as IUser)._id &&
!ev.context.currentUser?.profile.isAdmin
) {
throw createError({
statusCode: 403,
message: messages[403],
});
}
const story = s.toObject();
for (let i = 0; i < story.chapters.length; i++) {
story.chapters[i] = (await chapterTransformer(s, ev, i)).currentChapter;
}
return story;
});