Migrated to the new link controller & updated the permissions of the routes
This commit is contained in:
parent
f018392067
commit
a5498d3409
@ -3,20 +3,23 @@ import {AuthenticatedRequest, hasRank} from "../middlewares/authenticate";
|
||||
import {Rank} from "../../models/User";
|
||||
import {validateSchema} from "../../util/validate";
|
||||
import {shortUrl} from "../validations/linkValidation";
|
||||
import {ShortenedLink} from "../../models/ShortenedLink";
|
||||
import {getLinkById, getLinkObjectById, listAllLinks, listLinksByUser, shortLink} from "../../controller/links";
|
||||
|
||||
const app = Router();
|
||||
|
||||
app.get("/", hasRank(Rank.TEAM_MEMBER), async (req: AuthenticatedRequest, res: Response) => {
|
||||
res.json(await ShortenedLink.find({}, {_id: 0, __v: 0}));
|
||||
res.json(await (req.user.rank === Rank.TEAM_MEMBER ? listLinksByUser(req.user.clientId) : listAllLinks()));
|
||||
});
|
||||
|
||||
app.get("/: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}, {_id: 0, __v: 0});
|
||||
const link = await getLinkById(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 show information about this link"});
|
||||
|
||||
res.json(link);
|
||||
});
|
||||
|
||||
@ -24,17 +27,17 @@ app.put("/", hasRank(Rank.TEAM_MEMBER), async (req: AuthenticatedRequest, res: R
|
||||
const validationError = validateSchema(shortUrl, req.body);
|
||||
if (validationError) return res.status(400).json({message: validationError});
|
||||
|
||||
if (await ShortenedLink.findOne({shortenedId: req.body.shortenedId}) != null)
|
||||
if (await getLinkById(req.body.shortenedId) != null)
|
||||
return res.status(409).json({message: "The provided id has already been taken"});
|
||||
|
||||
const link = await ShortenedLink.create({originalUrl: req.body.originalUrl, shortenedId: req.body.shortenedId, clientId: req.user.clientId});
|
||||
const link = await shortLink(req.body.originalUrl, req.body.shortenedId, req.user.clientId);
|
||||
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});
|
||||
const link = await getLinkObjectById(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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user