2023-10-11 16:39:11 -04:00
|
|
|
import storyQuerier from "~/lib/server/dbHelpers/storyQuerier";
|
|
|
|
import chapterTransformer from "~/lib/server/dbHelpers/chapterTransformer";
|
|
|
|
import storyCheck from "~/lib/server/middlewareButNotReally/storyCheck";
|
|
|
|
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
|
2023-12-09 16:56:57 -05:00
|
|
|
import { messages } from "~/lib/server/constants";
|
2023-10-11 16:39:11 -04:00
|
|
|
|
|
|
|
export default eventHandler(async (ev) => {
|
|
|
|
isLoggedIn(ev);
|
|
|
|
const s = await storyQuerier(ev);
|
2023-12-09 16:56:57 -05:00
|
|
|
const hidden = s.chapters.some((a) => a.hidden);
|
2023-12-09 16:58:35 -05:00
|
|
|
if (
|
|
|
|
hidden &&
|
|
|
|
ev.context.currentUser?._id !== s.author._id &&
|
|
|
|
!ev.context.currentUser?.profile.isAdmin
|
|
|
|
) {
|
2023-12-09 16:56:57 -05:00
|
|
|
throw createError({
|
|
|
|
statusCode: 403,
|
|
|
|
message: messages[403],
|
|
|
|
});
|
|
|
|
}
|
2023-10-11 16:39:11 -04:00
|
|
|
const story = s.toObject();
|
2023-12-09 16:56:57 -05:00
|
|
|
|
2023-10-11 16:39:11 -04:00
|
|
|
for (let i = 0; i < story.chapters.length; i++) {
|
2023-12-09 16:56:57 -05:00
|
|
|
story.chapters[i] = (await chapterTransformer(s, ev, i)).currentChapter;
|
2023-10-11 16:39:11 -04:00
|
|
|
}
|
|
|
|
return story;
|
|
|
|
});
|