2023-10-11 16:43:05 -04:00
|
|
|
import { SubPayload } from "~/lib/client/types/form/favSub";
|
|
|
|
import isLoggedIn from "~/lib/server/middlewareButNotReally/isLoggedIn";
|
|
|
|
import { User } from "~/models/user";
|
|
|
|
|
|
|
|
export default eventHandler(async (ev) => {
|
|
|
|
isLoggedIn(ev);
|
|
|
|
// note: debounced batched update
|
|
|
|
const body = await readBody<SubPayload>(ev);
|
|
|
|
await User.findByIdAndUpdate(ev.context.currentUser!._id, {
|
|
|
|
$pull: {
|
|
|
|
"subscriptions.authors": {
|
2023-12-11 21:34:20 -05:00
|
|
|
$in: body.pull?.authors,
|
2023-10-11 16:43:05 -04:00
|
|
|
},
|
|
|
|
"subscriptions.bands": {
|
2023-12-11 21:34:20 -05:00
|
|
|
$in: body.pull?.bands ?? [],
|
2023-10-11 16:43:05 -04:00
|
|
|
},
|
|
|
|
"subscriptions.stories": {
|
2023-12-11 21:34:20 -05:00
|
|
|
$in: body.pull?.stories ?? [],
|
2023-10-11 16:43:05 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const nu = await User.findByIdAndUpdate(
|
|
|
|
ev.context.currentUser!._id,
|
|
|
|
{
|
|
|
|
$addToSet: {
|
|
|
|
"subscriptions.authors": {
|
2023-12-11 21:34:20 -05:00
|
|
|
$each: body.pull?.authors ?? [],
|
2023-10-11 16:43:05 -04:00
|
|
|
},
|
|
|
|
"subscriptions.bands": {
|
2023-12-11 21:34:20 -05:00
|
|
|
$each: body.pull?.bands ?? [],
|
2023-10-11 16:43:05 -04:00
|
|
|
},
|
|
|
|
"subscriptions.stories": {
|
2023-12-11 21:34:20 -05:00
|
|
|
$each: body.pull?.stories ?? [],
|
2023-10-11 16:43:05 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ new: true },
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
data: nu?.subscriptions,
|
|
|
|
};
|
|
|
|
});
|