2023-12-20 17:23:31 -05:00
|
|
|
import { FavPayload, SubPayload } from "@client/types/form/favSub";
|
2023-12-29 16:32:32 -05:00
|
|
|
import { isLoggedIn } from "@server/middlewareButNotReally";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { User } from "@models/user";
|
2023-10-11 16:43:32 -04:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
});
|