From 9b781b781acdff78706baa30f8068cb22c4947ae Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Mon, 12 Aug 2024 13:29:38 +0200 Subject: [PATCH] feat: prevent move feature to archived project (#7839) --- .../project/project-service.e2e.test.ts | 29 +++++++++++++++++-- src/lib/features/project/project-service.ts | 17 ++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/lib/features/project/project-service.e2e.test.ts b/src/lib/features/project/project-service.e2e.test.ts index be318f0fe1..1388d26e27 100644 --- a/src/lib/features/project/project-service.e2e.test.ts +++ b/src/lib/features/project/project-service.e2e.test.ts @@ -962,7 +962,7 @@ test('should not change project if feature flag project does not match current p } }); -test('should return 404 if no project is found with the project id', async () => { +test('should return 404 if no active project is found with the project id', async () => { const project = { id: 'test-change-project-2', name: 'New project', @@ -985,7 +985,32 @@ test('should return 404 if no project is found with the project id', async () => auditUser, ); } catch (err) { - expect(err.message).toBe(`No project found`); + expect(err.message).toBe( + `Active project with id newProject does not exist`, + ); + } + + const newProject = { + id: 'newProject', + name: 'New project', + description: 'Blah', + mode: 'open' as const, + defaultStickiness: 'clientId', + }; + await projectService.createProject(newProject, user, auditUser); + await projectService.archiveProject(newProject.id, TEST_AUDIT_USER); + try { + await projectService.changeProject( + 'newProject', + flag.name, + user, + project.id, + auditUser, + ); + } catch (err) { + expect(err.message).toBe( + `Active project with id newProject does not exist`, + ); } }); diff --git a/src/lib/features/project/project-service.ts b/src/lib/features/project/project-service.ts index 8d2045029f..ec58fd372e 100644 --- a/src/lib/features/project/project-service.ts +++ b/src/lib/features/project/project-service.ts @@ -486,6 +486,18 @@ export default class ProjectService { await this.projectStore.addEnvironmentToProject(project, environment); } + private async validateActiveProject(projectId: string) { + if (this.flagResolver.isEnabled('archiveProjects')) { + const hasActiveProject = + await this.projectStore.hasActiveProject(projectId); + if (!hasActiveProject) { + throw new NotFoundError( + `Active project with id ${projectId} does not exist`, + ); + } + } + } + async changeProject( newProjectId: string, featureName: string, @@ -498,11 +510,8 @@ export default class ProjectService { if (feature.project !== currentProjectId) { throw new PermissionError(MOVE_FEATURE_TOGGLE); } - const project = await this.getProject(newProjectId); - if (!project) { - throw new NotFoundError(`Project ${newProjectId} not found`); - } + await this.validateActiveProject(newProjectId); const authorized = await this.accessService.hasPermission( user,