Skip to content

Commit

Permalink
delete token
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy committed Nov 3, 2023
1 parent a41cf11 commit 239a86c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ export const deleteItemShareTokenController = async (req: IGetUserAuthInfoReques
await db.none(deleteShareToken(projectName, scenarioName, itemId, tokenId))
res.status(StatusCode.Ok).send()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IGetUserAuthInfoRequest } from "../../../middleware/request.model"
import { Response } from "express"
import { AllowedRoles } from "../../../middleware/authorization-middleware"
import { db } from "../../../../db/db"
import { StatusCode } from "../../../utils/status-code"
import { deleteMyScenarioShareToken, deleteScenarioShareToken } from "../../../queries/scenario"

export const deleteScenarioShareTokenController = async (req: IGetUserAuthInfoRequest, res: Response) => {
const { user } = req
const { projectName, scenarioName, shareTokenId } = req.params
if ([AllowedRoles.Readonly, AllowedRoles.Operator].includes(user.role)) {
await db.none(deleteMyScenarioShareToken(projectName, scenarioName, shareTokenId, user.userId))
res.status(StatusCode.Ok).send()

} else {
await db.none(deleteScenarioShareToken(projectName, scenarioName, shareTokenId))
res.status(StatusCode.Ok).send()
}
}
25 changes: 25 additions & 0 deletions src/server/queries/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,28 @@ export const createScenarioShareToken =
values: [projectName, scenarioName, token, note, userId],
}
}

export const deleteScenarioShareToken = (projectName, scenarioName, id) => {
return {
text: `DELETE FROM jtl.scenario_share_tokens as sst
USING jtl.scenario as sc
WHERE sst.id = $3
AND sc.name = $2
And sst.scenario_id = sc.id
AND sc.project_id = (SELECT id FROM jtl.projects WHERE project_name = $1)`,
values: [projectName, scenarioName, id],
}
}

export const deleteMyScenarioShareToken = (projectName, scenarioName, id, userId) => {
return {
text: `DELETE FROM jtl.scenario_share_tokens as sst
USING jtl.scenario as sc
WHERE sst.id = $3
AND sst.created_by = $3
AND sst.scenario_id = sc.id
AND sc.name = $2
AND sc.project_id = (SELECT id FROM jtl.projects WHERE project_name = $1)`,
values: [projectName, scenarioName, id, userId],
}
}
21 changes: 19 additions & 2 deletions src/server/routes/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import {
} from "../schema-validator/schema-validator-middleware"
import {
environmentQuerySchema,
paramSchemaNotification, paramsSchema,
scenarioNotificationBodySchema, scenarioShareToken, scenarioTrendsSettings, updateScenarioSchema,
paramSchemaNotification,
paramsSchema,
scenarioNotificationBodySchema,
scenarioShareToken,
scenarioShareTokenParamsSchema,
scenarioTrendsSettings,
updateScenarioSchema,
} from "../schema-validator/scenario-schema"
import { projectNameParam, scenarioSchema } from "../schema-validator/project-schema"
import { getScenariosController } from "../controllers/scenario/get-scenarios-controller"
Expand All @@ -32,6 +37,7 @@ import { projectExistsMiddleware } from "../middleware/project-exists-middleware
import { allowScenarioQueryTokenAuth } from "../middleware/allow-scenario-query-token-auth"
import { getScenarioShareTokenController } from "../controllers/scenario/share-token/get-scenario-share-token-controller"

Check failure on line 38 in src/server/routes/scenario.ts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 121. Maximum allowed is 120
import { createScenarioShareTokenController } from "../controllers/scenario/share-token/create-scenario-share-token-controller"

Check failure on line 39 in src/server/routes/scenario.ts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 127. Maximum allowed is 120
import { deleteScenarioShareTokenController } from "../controllers/scenario/share-token/delete-scenario-share-token-controller"

Check failure on line 40 in src/server/routes/scenario.ts

View workflow job for this annotation

GitHub Actions / build

This line has a length of 127. Maximum allowed is 120

export class ScenarioRoutes {

Expand Down Expand Up @@ -148,5 +154,16 @@ export class ScenarioRoutes {
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) =>
createScenarioShareTokenController(req, res)))


app.route("/api/projects/:projectName/scenarios/:scenarioName/share-token/:shareTokenId")
.delete(
authenticationMiddleware,
authorizationMiddleware([AllowedRoles.Operator, AllowedRoles.Admin]),
paramsSchemaValidator(scenarioShareTokenParamsSchema),
projectExistsMiddleware,
wrapAsync((req: IGetUserAuthInfoRequest, res: Response) =>
deleteScenarioShareTokenController(req, res)))


}
}
5 changes: 5 additions & 0 deletions src/server/schema-validator/scenario-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const paramsSchema = {
scenarioName: Joi.string().required(),
}

export const scenarioShareTokenParamsSchema = {
...paramsSchema,
shareTokenId: Joi.string().required(),
}

export const environmentQuerySchema = {
environment: Joi.string().max(ENVIRONMENT_MAX_LENGTH).allow(""),
token: Joi.string(),
Expand Down

0 comments on commit 239a86c

Please sign in to comment.