2023-10-03 01:24:06 -04:00
|
|
|
import { Document } from "mongoose";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { IStory, Story } from "@models/stories";
|
|
|
|
import { FormStory } from "@client/types/form/story";
|
|
|
|
import { storyQuerier } 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 { canModify } from "@server/middlewareButNotReally/storyPrivileges";
|
2023-12-29 20:11:07 -05:00
|
|
|
import { bodyHandler, getBucket, modelFormChapter, replaceOrUploadContent } from "@server/storyHelpers";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { countWords } from "@functions";
|
|
|
|
import { messages } from "@server/constants";
|
2023-10-03 01:24:06 -04:00
|
|
|
|
|
|
|
export default eventHandler(async (ev) => {
|
2024-01-04 15:20:25 -05:00
|
|
|
let os: (Document<unknown, {}, IStory> & IStory) | null = await storyQuerier(ev);
|
2023-10-03 01:24:06 -04:00
|
|
|
isLoggedIn(ev);
|
|
|
|
if (!canModify(ev, os)) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 403,
|
|
|
|
message: messages[403],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const body: FormStory = await readBody<FormStory>(ev);
|
|
|
|
const update: Partial<IStory> = {
|
|
|
|
title: body.title,
|
|
|
|
completed: body.completed,
|
2023-12-28 18:14:38 -05:00
|
|
|
coAuthor: !!body.coAuthor ? body.coAuthor : null,
|
2023-10-03 01:24:06 -04:00
|
|
|
};
|
|
|
|
for (const oc of os.chapters) {
|
|
|
|
let filename = `/stories/${oc.id}.txt`;
|
|
|
|
const bucket = getBucket();
|
|
|
|
const curs = bucket.find({ filename }).limit(1);
|
|
|
|
for await (const d of curs) {
|
|
|
|
await bucket.delete(d._id);
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 20:03:18 -04:00
|
|
|
const cc = os.chapters;
|
|
|
|
os.chapters = [];
|
|
|
|
await os.save();
|
2023-10-03 01:24:06 -04:00
|
|
|
for (const c of body.chapters) {
|
2024-03-16 20:03:18 -04:00
|
|
|
let idx = cc.findIndex((k) => k.id === c.id);
|
2023-10-03 01:24:06 -04:00
|
|
|
const cont = await bodyHandler(c);
|
|
|
|
if (idx === -1) {
|
2024-03-16 20:03:18 -04:00
|
|
|
os.chapters!.push({
|
2023-10-03 01:24:06 -04:00
|
|
|
...modelFormChapter(c),
|
|
|
|
posted: new Date(Date.now()),
|
|
|
|
});
|
|
|
|
} else {
|
2024-03-16 20:03:18 -04:00
|
|
|
os.chapters!.push({
|
2023-10-03 01:24:06 -04:00
|
|
|
...modelFormChapter(c),
|
2024-03-16 20:03:18 -04:00
|
|
|
// id: os.chapters[idx].id,
|
|
|
|
words: countWords(cont),
|
|
|
|
posted: cc[idx].posted,
|
2023-10-03 01:24:06 -04:00
|
|
|
});
|
|
|
|
}
|
2024-03-16 20:03:18 -04:00
|
|
|
}
|
|
|
|
await os.save();
|
|
|
|
for (let i = 0; i < os.chapters.length; i++) {
|
|
|
|
const c = os.chapters[i];
|
|
|
|
const cont = await bodyHandler(body.chapters[i]);
|
|
|
|
await replaceOrUploadContent(c.id ?? c._id, cont);
|
2023-10-03 01:24:06 -04:00
|
|
|
}
|
|
|
|
os = await Story.findOneAndUpdate(
|
|
|
|
{
|
|
|
|
_id: os._id,
|
|
|
|
},
|
|
|
|
update,
|
|
|
|
{ new: true },
|
|
|
|
);
|
|
|
|
if (!os) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 500,
|
|
|
|
message: "Something went wrong.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
success: true,
|
2024-03-16 20:03:18 -04:00
|
|
|
story: os.toObject(),
|
2023-10-03 01:24:06 -04:00
|
|
|
};
|
|
|
|
});
|