diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-08-22 22:18:59 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-08-22 22:18:59 +1000 |
commit | 0cd9a46eea260c299db2e2983f7214ab8b119d29 (patch) | |
tree | 5fbb98e7adcfeab81594732089474afdde5893f9 /src/api/routes/guilds/#guild_id/templates.ts | |
parent | Merge branch 'master' into feat/captchaVerify (diff) | |
parent | Merge remote-tracking branch 'Puyodead1/patch/prettier-config' into staging (diff) | |
download | server-0cd9a46eea260c299db2e2983f7214ab8b119d29.tar.xz |
Merge remote-tracking branch 'upstream/staging' into feat/captchaVerify
Diffstat (limited to 'src/api/routes/guilds/#guild_id/templates.ts')
-rw-r--r-- | src/api/routes/guilds/#guild_id/templates.ts | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/api/routes/guilds/#guild_id/templates.ts b/src/api/routes/guilds/#guild_id/templates.ts new file mode 100644 index 00000000..9c79692d --- /dev/null +++ b/src/api/routes/guilds/#guild_id/templates.ts @@ -0,0 +1,83 @@ +import { Request, Response, Router } from "express"; +import { Guild, Template } from "@fosscord/util"; +import { HTTPError } from "@fosscord/util"; +import { route } from "@fosscord/api"; +import { generateCode } from "@fosscord/api"; +import { OrmUtils } from "@fosscord/util"; + +const router: Router = Router(); + +const TemplateGuildProjection: (keyof Guild)[] = [ + "name", + "description", + "region", + "verification_level", + "default_message_notifications", + "explicit_content_filter", + "preferred_locale", + "afk_timeout", + "roles", + // "channels", + "afk_channel_id", + "system_channel_id", + "system_channel_flags", + "icon" +]; + +router.get("/", route({}), async (req: Request, res: Response) => { + const { guild_id } = req.params; + + let templates = await Template.find({ where: { source_guild_id: guild_id } }); + + return res.json(templates); +}); + +router.post("/", route({ body: "TemplateCreateSchema", permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { + const { guild_id } = req.params; + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: TemplateGuildProjection }); + const exists = await Template.findOneOrFail({ where: { id: guild_id } }).catch((e) => {}); + if (exists) throw new HTTPError("Template already exists", 400); + + const template = await OrmUtils.mergeDeep(new Template(), { + ...req.body, + code: generateCode(), + creator_id: req.user_id, + created_at: new Date(), + updated_at: new Date(), + source_guild_id: guild_id, + serialized_source_guild: guild + }).save(); + + res.json(template); +}); + +router.delete("/:code", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { + const { code, guild_id } = req.params; + + const template = await Template.delete({ + code, + source_guild_id: guild_id + }); + + res.json(template); +}); + +router.put("/:code", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { + const { code, guild_id } = req.params; + const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: TemplateGuildProjection }); + + const template = await OrmUtils.mergeDeep(new Template(), { code, serialized_source_guild: guild }).save(); + + res.json(template); +}); + +router.patch("/:code", route({ body: "TemplateModifySchema", permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { + const { code, guild_id } = req.params; + const { name, description } = req.body; + + const template = await OrmUtils.mergeDeep(new Template(), { code, name: name, description: description, source_guild_id: guild_id }).save(); + + res.json(template); +}); + +export default router; |