next/server/api/review/[id]/index.delete.ts

34 lines
860 B
TypeScript

import { messages } from "@server/constants";
import { isLoggedIn } from "@server/middlewareButNotReally";
import { Story } from "@models/stories";
import { Review } from "@models/stories/review";
export default eventHandler(async (ev) => {
isLoggedIn(ev);
const revid = parseInt(getRouterParam(ev, "revid")!);
let c2d = await Review.findById(revid);
if (!c2d) {
throw createError({
statusCode: 404,
message: messages[404],
});
}
let s2v = await Story.findById(c2d!.leftOn);
if (!s2v)
throw createError({
statusCode: 400,
message: "bad parameter",
});
if (ev.context.currentUser!._id != s2v?.author && ev.context.currentUser!._id != c2d._id)
throw createError({
statusCode: 403,
message: messages[403],
});
s2v.reviews += 1;
await s2v!.save();
await Review.findByIdAndDelete(revid);
return {
success: true,
};
});