diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts index 9ed38a7e3..3e5575daa 100644 --- a/backend/src/server/routes/index.ts +++ b/backend/src/server/routes/index.ts @@ -895,7 +895,8 @@ export const registerRoutes = async ( certificateTemplateDAL, projectSlackConfigDAL, slackIntegrationDAL, - projectTemplateService + projectTemplateService, + groupProjectDAL }); const projectEnvService = projectEnvServiceFactory({ diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts index b5046be63..e1653d371 100644 --- a/backend/src/services/project/project-service.ts +++ b/backend/src/services/project/project-service.ts @@ -29,6 +29,7 @@ import { ActorType } from "../auth/auth-type"; import { TCertificateDALFactory } from "../certificate/certificate-dal"; import { TCertificateAuthorityDALFactory } from "../certificate-authority/certificate-authority-dal"; import { TCertificateTemplateDALFactory } from "../certificate-template/certificate-template-dal"; +import { TGroupProjectDALFactory } from "../group-project/group-project-dal"; import { TIdentityOrgDALFactory } from "../identity/identity-org-dal"; import { TIdentityProjectDALFactory } from "../identity-project/identity-project-dal"; import { TIdentityProjectMembershipRoleDALFactory } from "../identity-project/identity-project-membership-role-dal"; @@ -100,7 +101,8 @@ type TProjectServiceFactoryDep = { identityProjectDAL: TIdentityProjectDALFactory; identityProjectMembershipRoleDAL: Pick; projectKeyDAL: Pick; - projectMembershipDAL: Pick; + projectMembershipDAL: Pick; + groupProjectDAL: Pick; projectSlackConfigDAL: Pick; slackIntegrationDAL: Pick; projectUserMembershipRoleDAL: Pick; @@ -120,7 +122,7 @@ type TProjectServiceFactoryDep = { orgDAL: Pick; keyStore: Pick; projectBotDAL: Pick; - projectRoleDAL: Pick; + projectRoleDAL: Pick; kmsService: Pick< TKmsServiceFactory, | "updateProjectSecretManagerKmsKey" @@ -169,7 +171,8 @@ export const projectServiceFactory = ({ projectBotDAL, projectSlackConfigDAL, slackIntegrationDAL, - projectTemplateService + projectTemplateService, + groupProjectDAL }: TProjectServiceFactoryDep) => { /* * Create workspace. Make user the admin @@ -444,13 +447,32 @@ export const projectServiceFactory = ({ ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionActions.Delete, ProjectPermissionSub.Project); const deletedProject = await projectDAL.transaction(async (tx) => { + // delete these so that project custom roles can be deleted in cascade effect + // direct deletion of project without these will cause fk error + await projectMembershipDAL.delete({ projectId: project.id }, tx); + await groupProjectDAL.delete({ projectId: project.id }, tx); const delProject = await projectDAL.deleteById(project.id, tx); const projectGhostUser = await projectMembershipDAL.findProjectGhostUser(project.id, tx).catch(() => null); + // akhilmhdh: before removing those kms checking any other project uses it + // happened due to project split if (delProject.kmsCertificateKeyId) { - await kmsService.deleteInternalKms(delProject.kmsCertificateKeyId, delProject.orgId, tx); + const projectsLinkedToForiegnKey = await projectDAL.find( + { kmsCertificateKeyId: delProject.kmsCertificateKeyId }, + { tx } + ); + if (!projectsLinkedToForiegnKey.length) { + await kmsService.deleteInternalKms(delProject.kmsCertificateKeyId, delProject.orgId, tx); + } } + if (delProject.kmsSecretManagerKeyId) { - await kmsService.deleteInternalKms(delProject.kmsSecretManagerKeyId, delProject.orgId, tx); + const projectsLinkedToForiegnKey = await projectDAL.find( + { kmsSecretManagerKeyId: delProject.kmsSecretManagerKeyId }, + { tx } + ); + if (!projectsLinkedToForiegnKey.length) { + await kmsService.deleteInternalKms(delProject.kmsSecretManagerKeyId, delProject.orgId, tx); + } } // Delete the org membership for the ghost user if it's found. if (projectGhostUser) {