next/server/api/user/me/favs.put.ts

37 lines
768 B
TypeScript
Raw Normal View History

import { FavPayload, SubPayload } from "@client/types/form/favSub";
import { isLoggedIn } from "@server/middlewareButNotReally";
import { User } from "@models/user";
export default eventHandler(async (ev) => {
isLoggedIn(ev);
const body = await readBody<FavPayload>(ev);
await User.findByIdAndUpdate(ev.context.currentUser!._id, {
$pull: {
"favs.authors": {
$in: body.pull!.authors,
},
"favs.stories": {
$in: body.pull!.stories,
},
},
});
const nu = await User.findByIdAndUpdate(
ev.context.currentUser!._id,
{
$addToSet: {
"favs.authors": {
$each: body.pull!.authors,
},
"favs.stories": {
$each: body.pull!.stories,
},
},
},
{ new: true },
);
return {
success: true,
data: nu?.favs,
};
});