From eac716ce45bc28e590de885f15b369602ee90705 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Thu, 3 Aug 2023 23:41:16 +0200 Subject: [PATCH] Created the license.ts routes --- src/routes/v1/license.ts | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/routes/v1/license.ts diff --git a/src/routes/v1/license.ts b/src/routes/v1/license.ts new file mode 100644 index 0000000..3626975 --- /dev/null +++ b/src/routes/v1/license.ts @@ -0,0 +1,53 @@ +import { Request, Response, Router } from "express"; +import { createLicense, deleteLicense, getLicense, listLicensesPaginated, updateLicense } from "@controller/license"; +import { sendError, validateSchema } from "@utils/error"; +import { licenseCreationValidation } from "./validations/license"; + +const app: Router = Router(); + +app.get("/:projectId/list", async (req: Request, res: Response) => { + const page = Number(req.query.page) || 0; + const limit = Number(req.query.limit) || 100; + + const licenses = await listLicensesPaginated(String(req.user?._id), req.params.projectId, page, limit); + if ("code" in licenses) return res.json(licenses); + + res.json(licenses); +}); + +app.get("/:projectId/:licenseKey", async (req: Request, res: Response) => { + const license = await getLicense(String(req.user?._id), req.params.projectId, req.params.licenseKey); + if ("code" in license) return res.json(license); + + res.json(license); +}); + +app.put("/:projectId", async (req: Request, res: Response) => { + if (validateSchema(res, licenseCreationValidation, req.body)) return; + + const license = await createLicense(String(req.user?._id), req.params.projectId, req.body); + if ("code" in license) return res.json(license); + + res.json({ key: license.key, message: "License created successfully" }); +}); + +app.patch("/:projectId/:licenseKey", async (req: Request, res: Response) => { + if (validateSchema(res, licenseCreationValidation, req.body)) return; + + if (Object.keys(req.body).length === 0) + return sendError(res, 400, 4, "You need to provide at least one field to update"); + + const license = await updateLicense(String(req.user?._id), req.params.projectId, req.params.licenseKey, req.body); + if ("code" in license) return res.json(license); + + res.json({ message: "License updated successfully" }); +}); + +app.delete("/:projectId/:licenseKey", async (req: Request, res: Response) => { + const license = await deleteLicense(String(req.user?._id), req.params.projectId, req.params.licenseKey); + if ("code" in license) return res.json(license); + + res.json({ message: "License deleted successfully" }); +}); + +export default app; \ No newline at end of file