next/server/api/user/me/subscriptions.put.ts
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 3a8fd82dae
fix(api): fix new subscriptions not being saved to database
for some reason i forgot to set the field referenced by $addToSet to `body.push` lmao #justcopyandpastethings
2023-12-30 16:02:27 -05:00

45 lines
1013 B
TypeScript

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