From b65b104099982bc4788540165f74ab107c051c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lud=C4=9Bk=20Nov=C3=BD?= <13610612+ludeknovy@users.noreply.github.com> Date: Wed, 6 Sep 2023 19:19:31 +0200 Subject: [PATCH] project exists middleware --- src/server/middleware/project-scenario-exists.ts | 14 ++++++++++++++ src/server/queries/projects.ts | 2 +- src/server/routes/item.ts | 15 +++++++++++++++ src/server/routes/label.ts | 4 ++++ src/server/routes/scenario.ts | 12 ++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/server/middleware/project-scenario-exists.ts diff --git a/src/server/middleware/project-scenario-exists.ts b/src/server/middleware/project-scenario-exists.ts new file mode 100644 index 00000000..4cffdce0 --- /dev/null +++ b/src/server/middleware/project-scenario-exists.ts @@ -0,0 +1,14 @@ +import { IGetUserAuthInfoRequest } from "./request.model" +import { NextFunction, Response } from "express" +import { db } from "../../db/db" +import * as boom from "boom" +import { findProjectId } from "../queries/projects" + +export const projectExistsMiddleware = async (req: IGetUserAuthInfoRequest, res: Response, next: NextFunction) => { + + const { projectName } = req.params + const project = await db.oneOrNone(findProjectId(projectName)) + if (!project) { + return next(boom.notFound("Project not found")) + } +} diff --git a/src/server/queries/projects.ts b/src/server/queries/projects.ts index a43bc527..054fe0f4 100644 --- a/src/server/queries/projects.ts +++ b/src/server/queries/projects.ts @@ -18,7 +18,7 @@ export const createNewProject = projectName => { export const findProjectId = projectName => { return { name: "find-project-id", - text: "SELECT id FROM jtl.projects WHERE project_name = $1 AND", + text: "SELECT id FROM jtl.projects WHERE project_name = $1", values: [projectName], } } diff --git a/src/server/routes/item.ts b/src/server/routes/item.ts index 90493f6d..d958a48e 100644 --- a/src/server/routes/item.ts +++ b/src/server/routes/item.ts @@ -33,6 +33,7 @@ import { getItemChartSettingsController } from "../controllers/item/get-item-cha import { AllowedRoles, authorizationMiddleware } from "../middleware/authorization-middleware" import { authenticationMiddleware } from "../middleware/authentication-middleware" import { getRequestStatsExportController } from "../controllers/item/get-request-stats-export-controller" +import { projectExistsMiddleware } from "../middleware/project-scenario-exists" export class ItemsRoutes { @@ -44,12 +45,14 @@ export class ItemsRoutes { authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(scenarioParamsSchema), queryParamsValidator(querySchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getItemsController(req, res))) .post( authenticationMiddleware, authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(newItemParamSchema), + projectExistsMiddleware, createItemController) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/start-async") @@ -58,6 +61,7 @@ export class ItemsRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), bodySchemaValidator(newAsyncItemStartBodySchema), paramsSchemaValidator(newItemParamSchema), + projectExistsMiddleware, createItemAsyncController) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId") @@ -66,6 +70,7 @@ export class ItemsRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getItemController(req, res))) .put( @@ -73,12 +78,14 @@ export class ItemsRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), bodySchemaValidator(updateItemBodySchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response, next: NextFunction) => updateItemController(req, res, next))) .delete( authenticationMiddleware, authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => deleteItemController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/request-stats-export") @@ -86,6 +93,7 @@ export class ItemsRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getRequestStatsExportController(req, res))) @@ -95,6 +103,7 @@ export class ItemsRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), bodySchemaValidator(stopItemAsyncBodySchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => stopItemAsyncController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/share-tokens") @@ -102,12 +111,14 @@ export class ItemsRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getItemLinksController(req, res))) .post( authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => createItemLinkController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/share-tokens/:tokenId") @@ -115,6 +126,7 @@ export class ItemsRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(shareTokenSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => deleteItemShareTokenController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/processing-items") @@ -123,6 +135,7 @@ export class ItemsRoutes { authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(scenarioParamsSchema), queryParamsValidator(environmentQuerySchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getProcessingItemsController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/custom-chart-settings") @@ -131,12 +144,14 @@ export class ItemsRoutes { authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), bodySchemaValidator(upsertUserItemChartSettings), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => upsertItemChartSettingsController(req, res)) ) .get( authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getItemChartSettingsController(req, res))) } } diff --git a/src/server/routes/label.ts b/src/server/routes/label.ts index 2a078ff8..4defc4ac 100644 --- a/src/server/routes/label.ts +++ b/src/server/routes/label.ts @@ -8,6 +8,7 @@ import { getLabelVirtualUsersController } from "../controllers/label/get-label-v import { getLabelErrorsController } from "../controllers/label/get-label-errors-controller" import { AllowedRoles, authorizationMiddleware } from "../middleware/authorization-middleware" import { authenticationMiddleware } from "../middleware/authentication-middleware" +import { projectExistsMiddleware } from "../middleware/project-scenario-exists" export class LabelRoutes { @@ -19,6 +20,7 @@ export class LabelRoutes { authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(labelParamSchema), queryParamsValidator(labelQuerySchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getLabelTrendController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/items/:itemId/label/:label/virtual-users") @@ -27,6 +29,7 @@ export class LabelRoutes { authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(labelParamSchema), queryParamsValidator(labelQuerySchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response, next: NextFunction) => getLabelVirtualUsersController(req, res, next))) @@ -35,6 +38,7 @@ export class LabelRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(labelParamSchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getLabelErrorsController(req, res))) } } diff --git a/src/server/routes/scenario.ts b/src/server/routes/scenario.ts index f6259a6a..82a9e6fa 100644 --- a/src/server/routes/scenario.ts +++ b/src/server/routes/scenario.ts @@ -28,6 +28,7 @@ import { AllowedRoles, authorizationMiddleware } from "../middleware/authorizati import { IGetUserAuthInfoRequest } from "../middleware/request.model" import { postScenarioTrendsSettings } from "../controllers/scenario/trends/update-scenario-trends-settings-controller" import { getScenarioEnvironmentController } from "../controllers/scenario/get-scenario-environment-controller" +import {projectExistsMiddleware} from "../middleware/project-scenario-exists"; export class ScenarioRoutes { @@ -38,6 +39,7 @@ export class ScenarioRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(projectNameParam), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getScenariosController(req, res))) .post( @@ -45,6 +47,7 @@ export class ScenarioRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(projectNameParam), bodySchemaValidator(scenarioSchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response, next: NextFunction) => createScenarioController(req, res, next))) @@ -53,6 +56,7 @@ export class ScenarioRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getScenarioController(req, res)) ) @@ -61,12 +65,14 @@ export class ScenarioRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), bodySchemaValidator(updateScenarioSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => updateScenarioController(req, res))) .delete( authenticationMiddleware, authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => deleteScenarioController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/notifications") @@ -74,6 +80,7 @@ export class ScenarioRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => getScenarioNotificationsController(req, res))) .post( @@ -81,6 +88,7 @@ export class ScenarioRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), bodySchemaValidator(scenarioNotificationBodySchema), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => createScenarioNotificationController(req, res))) @@ -89,6 +97,7 @@ export class ScenarioRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramSchemaNotification), + projectExistsMiddleware, wrapAsync((req: Request, res: Response) => deleteScenarioNotificationController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/trends") @@ -97,6 +106,7 @@ export class ScenarioRoutes { authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), queryParamsValidator(environmentQuerySchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getScenarioTrendsController(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/trends/settings") @@ -105,6 +115,7 @@ export class ScenarioRoutes { authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), bodySchemaValidator(scenarioTrendsSettings), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => postScenarioTrendsSettings(req, res))) app.route("/api/projects/:projectName/scenarios/:scenarioName/environment") @@ -112,6 +123,7 @@ export class ScenarioRoutes { authenticationMiddleware, authorizationMiddleware([AllowedRoles.Readonly, AllowedRoles.Operator, AllowedRoles.Admin]), paramsSchemaValidator(paramsSchema), + projectExistsMiddleware, wrapAsync((req: IGetUserAuthInfoRequest, res: Response) => getScenarioEnvironmentController(req, res))) } }