-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e490f38
commit 558be1f
Showing
28 changed files
with
1,762 additions
and
121 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/back-end/src/api/environments/deleteEnvironment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { DeleteEnvironmentResponse } from "../../../types/openapi"; | ||
import { createApiRequestHandler } from "../../util/handler"; | ||
import { deleteEnvironmentValidator } from "../../validators/openapi"; | ||
import { updateOrganization } from "../../models/OrganizationModel"; | ||
import { OrganizationInterface } from "../../../types/organization"; | ||
import { auditDetailsDelete } from "../../services/audit"; | ||
|
||
export const deleteEnvironment = createApiRequestHandler( | ||
deleteEnvironmentValidator | ||
)( | ||
async (req): Promise<DeleteEnvironmentResponse> => { | ||
const id = req.params.id; | ||
const org = req.context.org; | ||
const environments = org.settings?.environments || []; | ||
|
||
const environment = environments.find((env) => env.id === id); | ||
if (!environment) { | ||
throw Error(`Environment ${id} does not exists!`); | ||
} | ||
|
||
if (!req.context.permissions.canDeleteEnvironment(environment)) | ||
req.context.permissions.throwPermissionError(); | ||
|
||
const updates: Partial<OrganizationInterface> = { | ||
settings: { | ||
...org.settings, | ||
environments: [...environments.filter((env) => env.id !== id)], | ||
}, | ||
}; | ||
|
||
await updateOrganization(org.id, updates); | ||
|
||
await req.audit({ | ||
event: "environment.delete", | ||
entity: { | ||
object: "environment", | ||
id: environment.id, | ||
}, | ||
details: auditDetailsDelete(environment), | ||
}); | ||
|
||
return { | ||
deletedId: id, | ||
}; | ||
} | ||
); |
14 changes: 14 additions & 0 deletions
14
packages/back-end/src/api/environments/environments.router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Router } from "express"; | ||
import { listEnvironments } from "./listEnvironments"; | ||
import { putEnvironment } from "./putEnvironment"; | ||
import { postEnvironment } from "./postEnvironment"; | ||
import { deleteEnvironment } from "./deleteEnvironment"; | ||
|
||
const router = Router(); | ||
|
||
router.get("/", listEnvironments); | ||
router.post("/", postEnvironment); | ||
router.put("/:id", putEnvironment); | ||
router.delete("/:id", deleteEnvironment); | ||
|
||
export default router; |
33 changes: 33 additions & 0 deletions
33
packages/back-end/src/api/environments/listEnvironments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ListEnvironmentsResponse } from "../../../types/openapi"; | ||
import { createApiRequestHandler } from "../../util/handler"; | ||
import { listEnvironmentsValidator } from "../../validators/openapi"; | ||
|
||
export const listEnvironments = createApiRequestHandler( | ||
listEnvironmentsValidator | ||
)( | ||
async (req): Promise<ListEnvironmentsResponse> => { | ||
const environments = ( | ||
req.context.org.settings?.environments || [] | ||
).filter((environment) => | ||
req.context.permissions.canReadMultiProjectResource(environment.projects) | ||
); | ||
|
||
return { | ||
environments: environments.map( | ||
({ | ||
id, | ||
description = "", | ||
toggleOnList = false, | ||
defaultState = false, | ||
projects = [], | ||
}) => ({ | ||
id, | ||
projects, | ||
description, | ||
defaultState, | ||
toggleOnList, | ||
}) | ||
), | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { PostEnvironmentResponse } from "../../../types/openapi"; | ||
import { createApiRequestHandler } from "../../util/handler"; | ||
import { postEnvironmentValidator } from "../../validators/openapi"; | ||
import { updateOrganization } from "../../models/OrganizationModel"; | ||
import { OrganizationInterface } from "../../../types/organization"; | ||
import { auditDetailsCreate } from "../../services/audit"; | ||
import { validatePayload } from "./validations"; | ||
|
||
export const postEnvironment = createApiRequestHandler( | ||
postEnvironmentValidator | ||
)( | ||
async (req): Promise<PostEnvironmentResponse> => { | ||
const environment = await validatePayload(req.context, req.body); | ||
|
||
const org = req.context.org; | ||
|
||
if (org.settings?.environments?.some((env) => env.id === environment.id)) { | ||
throw Error(`Environment ${environment.id} already exists!`); | ||
} | ||
|
||
if (!req.context.permissions.canCreateOrUpdateEnvironment(environment)) | ||
req.context.permissions.throwPermissionError(); | ||
|
||
const updates: Partial<OrganizationInterface> = { | ||
settings: { | ||
...org.settings, | ||
environments: [...(org.settings?.environments || []), environment], | ||
}, | ||
}; | ||
|
||
await updateOrganization(org.id, updates); | ||
|
||
await req.audit({ | ||
event: "environment.create", | ||
entity: { | ||
object: "environment", | ||
id: environment.id, | ||
}, | ||
details: auditDetailsCreate(environment), | ||
}); | ||
|
||
return { | ||
environment, | ||
}; | ||
} | ||
); |
Oops, something went wrong.
558be1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for docs ready!
✅ Preview
https://docs-gc0yq0oqc-growthbook.vercel.app
Built with commit 558be1f.
This pull request is being automatically deployed with vercel-action