2023-12-20 17:23:31 -05:00
|
|
|
import { storyQuerier } from "@server/dbHelpers";
|
|
|
|
import { chapterTransformer } from "@server/dbHelpers";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { isLoggedIn } from "@server/middlewareButNotReally";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { messages } from "@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;
|
|
|
|
});
|