diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-24 16:35:04 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-24 16:35:04 +0200 |
commit | a567551c8a6b03a956d21354a5504b9f9e1f40b1 (patch) | |
tree | 4f8242ecfa92c8a58e040a8dc705daafb2e68d6c /api/src/routes/invites | |
parent | :sparkles: typeorm entities (diff) | |
download | server-a567551c8a6b03a956d21354a5504b9f9e1f40b1.tar.xz |
:construction: api
Diffstat (limited to 'api/src/routes/invites')
-rw-r--r-- | api/src/routes/invites/index.ts | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index e7543dbb..e871af86 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { getPermission, GuildModel, InviteModel, toObject } from "@fosscord/util"; +import { getPermission, Guild, InviteModel, toObject } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { addMember } from "../../util/Member"; const router: Router = Router(); @@ -7,42 +7,40 @@ const router: Router = Router(); router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOne({ code }).exec(); + const invite = await Invite.findOneOrFail({ code }); if (!invite) throw new HTTPError("Unknown Invite", 404); - res.status(200).send(toObject(invite)); + res.status(200).send(invite); }); router.post("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }).exec(); + const invite = await Invite.findOneOrFailAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }); if (!invite) throw new HTTPError("Unknown Invite", 404); - if (invite.uses >= invite.max_uses) await InviteModel.deleteOne({ code }); + if (invite.uses >= invite.max_uses) await Invite.deleteOne({ code }); await addMember(req.user_id, invite.guild_id); - res.status(200).send(toObject(invite)); + res.status(200).send(invite); }); router.delete("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOne({ code }).exec(); + const invite = await Invite.findOneOrFail({ code }); const { guild_id, channel_id } = invite; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); const permission = await getPermission(req.user_id, guild_id, channel_id, { guild }); if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS")) throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401); - await InviteModel.deleteOne({ code }).exec(); + await Invite.deleteOne({ code }); - await GuildModel.updateOne({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } }) - .exec() - .catch((e) => {}); + await Guild.update({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } }).catch((e) => {}); - res.status(200).send({ invite: toObject(invite) }); + res.status(200).send({ invite: invite) }; }); export default router; |