2023-12-20 17:23:31 -05:00
|
|
|
import { messages } from "@server/constants";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { isLoggedIn } from "@server/middlewareButNotReally";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { Story } from "@models/stories";
|
|
|
|
import { Review } from "@models/stories/review";
|
2023-10-03 00:21:14 -04:00
|
|
|
|
|
|
|
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",
|
|
|
|
});
|
2023-12-29 20:53:29 -05:00
|
|
|
if (ev.context.currentUser!._id != s2v?.author && ev.context.currentUser!._id != c2d._id)
|
2023-10-03 00:21:14 -04:00
|
|
|
throw createError({
|
|
|
|
statusCode: 403,
|
|
|
|
message: messages[403],
|
|
|
|
});
|
|
|
|
s2v.reviews += 1;
|
|
|
|
await s2v!.save();
|
2023-12-29 16:32:32 -05:00
|
|
|
await Review.findByIdAndDelete(revid);
|
2023-10-03 00:21:14 -04:00
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
});
|