diff --git a/api/routes/links.ts b/api/routes/links.ts index f75cd8d..8683074 100644 --- a/api/routes/links.ts +++ b/api/routes/links.ts @@ -18,4 +18,17 @@ app.put("/", hasRank(Rank.TEAM_MEMBER), async (req: AuthenticatedRequest, res: R res.json({message: "Link successfully shortened", "shorten_url": link.shortenedId}); }); +app.delete("/:code", hasRank(Rank.TEAM_MEMBER), async (req: AuthenticatedRequest, res: Response) => { + if (!req.params.code) return res.status(400).json({message: "You need to provide the shorten id"}); + + const link = await ShortenedLink.findOne({shortenedId: req.params.code}); + if (link == null) return res.status(404).json({message: "The provided link does not exist"}); + + if (!(req.user.rank === Rank.TEAM_MEMBER && link.clientId === req.user.clientId || req.user.rank === Rank.ADMIN)) + return res.status(401).json({message: "You don't have the permission to delete this link"}); + + await link.delete(); + res.json({message: "The provided link got successfully deleted"}); +}); + module.exports = app; \ No newline at end of file