🐛 fix findOneAndUpdate

This commit is contained in:
Flam3rboy
2021-08-18 11:47:28 +02:00
parent 5e12d799e3
commit 4105df8dcb
17 changed files with 39 additions and 38 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response
const permission = await getPermission(req.user_id, undefined, channel_id);
permission.hasThrow("MANAGE_CHANNELS");
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec();
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload, { new: true }).exec();
const data = toObject(channel);
@@ -43,7 +43,7 @@ router.delete("/", async (req: Request, res: Response) => {
const permissions = await getPermission(req.user_id, undefined, channel_id);
permissions.hasThrow("MANAGE_MESSAGES");
await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec();
await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }, { new: true }).exec();
await emitEvent({
event: "MESSAGE_REACTION_REMOVE_ALL",
@@ -5,7 +5,8 @@ import {
emitEvent,
getPermission,
MemberModel,
RoleModel
RoleModel,
toObject
} from "@fosscord/util";
import { Router, Response, Request } from "express";
import { HTTPError } from "lambert-server";
@@ -47,12 +48,12 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number,
overwrite.deny = body.deny;
// @ts-ignore
channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel).exec();
channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel, { new: true }).exec();
await emitEvent({
event: "CHANNEL_UPDATE",
channel_id,
data: channel
data: toObject(channel)
} as ChannelUpdateEvent);
return res.sendStatus(204);
@@ -65,13 +66,17 @@ router.delete("/:overwrite_id", async (req: Request, res: Response) => {
const permissions = await getPermission(req.user_id, undefined, channel_id);
permissions.hasThrow("MANAGE_ROLES");
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, { $pull: { permission_overwrites: { id: overwrite_id } } });
const channel = await ChannelModel.findOneAndUpdate(
{ id: channel_id },
{ $pull: { permission_overwrites: { id: overwrite_id } } },
{ new: true }
);
if (!channel.guild_id) throw new HTTPError("Channel not found", 404);
await emitEvent({
event: "CHANNEL_UPDATE",
channel_id,
data: channel
data: toObject(channel)
} as ChannelUpdateEvent);
return res.sendStatus(204);
+1 -1
View File
@@ -57,7 +57,7 @@ router.delete("/:message_id", async (req: Request, res: Response) => {
permission.hasThrow("VIEW_CHANNEL");
if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES");
const message = toObject(await MessageModel.findOneAndUpdate({ id: message_id }, { pinned: false }).exec());
const message = toObject(await MessageModel.findOneAndUpdate({ id: message_id }, { pinned: false }, { new: true }).exec());
await emitEvent({
event: "MESSAGE_UPDATE",
+2 -13
View File
@@ -1,16 +1,5 @@
import { Router, Response, Request } from "express";
import {
ChannelCreateEvent,
ChannelModel,
ChannelType,
GuildModel,
Snowflake,
toObject,
ChannelUpdateEvent,
AnyChannel,
getPermission,
emitEvent
} from "@fosscord/util";
import { ChannelModel, toObject, ChannelUpdateEvent, getPermission, emitEvent } from "@fosscord/util";
import { HTTPError } from "lambert-server";
import { ChannelModifySchema } from "../../../schema/Channel";
@@ -63,7 +52,7 @@ router.patch(
}
}
const channel = await ChannelModel.findOneAndUpdate({ id: req.body, guild_id }, opts).exec();
const channel = await ChannelModel.findOneAndUpdate({ id: req.body, guild_id }, opts, { new: true }).exec();
await emitEvent({ event: "CHANNEL_UPDATE", data: toObject(channel), channel_id: body.id, guild_id } as ChannelUpdateEvent);
+1 -1
View File
@@ -48,7 +48,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
if (body.banner) body.banner = await handleFile(`/banners/${guild_id}`, body.banner);
if (body.splash) body.splash = await handleFile(`/splashes/${guild_id}`, body.splash);
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body, { new: true })
.populate({ path: "joined_at", match: { id: req.user_id } })
.exec();
@@ -36,7 +36,7 @@ router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response)
// TODO: check if user has permission to add role
}
const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body).exec();
const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body, { new: true }).exec();
await emitEvent({
event: "GUILD_MEMBER_UPDATE",
+2 -1
View File
@@ -108,7 +108,8 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res
guild_id: guild_id
},
// @ts-ignore
body
body,
{ new: true }
).exec();
await emitEvent({
+6 -2
View File
@@ -79,7 +79,7 @@ router.put("/:code", async (req: Request, res: Response) => {
const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_GUILD");
const template = await TemplateModel.findOneAndUpdate({ code }, { serialized_source_guild: guild }).exec();
const template = await TemplateModel.findOneAndUpdate({ code }, { serialized_source_guild: guild }, { new: true }).exec();
res.json(toObject(template)).send();
});
@@ -91,7 +91,11 @@ router.patch("/:code", check(TemplateModifySchema), async (req: Request, res: Re
const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_GUILD");
const template = await TemplateModel.findOneAndUpdate({ code }, { name: req.body.name, description: req.body.description }).exec();
const template = await TemplateModel.findOneAndUpdate(
{ code },
{ name: req.body.name, description: req.body.description },
{ new: true }
).exec();
res.json(toObject(template)).send();
});
+1 -1
View File
@@ -16,7 +16,7 @@ router.get("/:code", async (req: Request, res: Response) => {
router.post("/:code", async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }).exec();
const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }).exec();
if (!invite) throw new HTTPError("Unknown Invite", 404);
await addMember(req.user_id, invite.guild_id);
+1 -1
View File
@@ -38,7 +38,7 @@ router.patch("/", check(UserModifySchema), async (req: Request, res: Response) =
if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection }).exec();
const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection, new: true }).exec();
// TODO: dispatch user update event
res.json(toObject(user));