From 823f6330b7834a684dd02b9431d06b2dfe9546c6 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Thu, 26 Sep 2024 09:45:02 +0200 Subject: [PATCH] refactor: move getProjectsByUser to read model (#8262) --- .../project/fake-project-read-model.ts | 3 + .../project/project-read-model-type.ts | 1 + .../features/project/project-read-model.ts | 32 +++++++++++ src/lib/features/project/project-service.ts | 2 +- .../features/project/project-store-type.ts | 2 - src/lib/features/project/project-store.ts | 57 ------------------- src/test/fixtures/fake-project-store.ts | 5 -- 7 files changed, 37 insertions(+), 65 deletions(-) diff --git a/src/lib/features/project/fake-project-read-model.ts b/src/lib/features/project/fake-project-read-model.ts index 1d9e40b8f7cd..3a861cd54047 100644 --- a/src/lib/features/project/fake-project-read-model.ts +++ b/src/lib/features/project/fake-project-read-model.ts @@ -14,4 +14,7 @@ export class FakeProjectReadModel implements IProjectReadModel { getProjectsForInsights(): Promise { return Promise.resolve([]); } + getProjectsByUser(): Promise { + return Promise.resolve([]); + } } diff --git a/src/lib/features/project/project-read-model-type.ts b/src/lib/features/project/project-read-model-type.ts index 0f131bf005e6..cd9b47028999 100644 --- a/src/lib/features/project/project-read-model-type.ts +++ b/src/lib/features/project/project-read-model-type.ts @@ -37,4 +37,5 @@ export interface IProjectReadModel { getFeatureProject( featureName: string, ): Promise<{ project: string; createdAt: Date } | null>; + getProjectsByUser(userId: number): Promise; } diff --git a/src/lib/features/project/project-read-model.ts b/src/lib/features/project/project-read-model.ts index b8b12d60cf87..66977d94e296 100644 --- a/src/lib/features/project/project-read-model.ts +++ b/src/lib/features/project/project-read-model.ts @@ -249,4 +249,36 @@ export class ProjectReadModel implements IProjectReadModel { memberTimer(); return members; } + + async getProjectsByUser(userId: number): Promise { + const projects = await this.db + .from((db) => { + db.select('role_user.project') + .from('role_user') + .leftJoin('roles', 'role_user.role_id', 'roles.id') + .leftJoin('projects', 'role_user.project', 'projects.id') + .where('user_id', userId) + .andWhere('projects.archived_at', null) + .union((queryBuilder) => { + queryBuilder + .select('group_role.project') + .from('group_role') + .leftJoin( + 'group_user', + 'group_user.group_id', + 'group_role.group_id', + ) + .leftJoin( + 'projects', + 'group_role.project', + 'projects.id', + ) + .where('group_user.user_id', userId) + .andWhere('projects.archived_at', null); + }) + .as('query'); + }) + .pluck('project'); + return projects; + } } diff --git a/src/lib/features/project/project-service.ts b/src/lib/features/project/project-service.ts index fbcec958bcba..ac5f16b38621 100644 --- a/src/lib/features/project/project-service.ts +++ b/src/lib/features/project/project-service.ts @@ -1344,7 +1344,7 @@ export default class ProjectService { } async getProjectsByUser(userId: number): Promise { - return this.projectStore.getProjectsByUser(userId); + return this.projectReadModel.getProjectsByUser(userId); } async getProjectRoleUsage(roleId: number): Promise { diff --git a/src/lib/features/project/project-store-type.ts b/src/lib/features/project/project-store-type.ts index c5076e86ca10..b4374f9b5cdf 100644 --- a/src/lib/features/project/project-store-type.ts +++ b/src/lib/features/project/project-store-type.ts @@ -96,8 +96,6 @@ export interface IProjectStore extends Store { date: string, ): Promise; - getProjectsByUser(userId: number): Promise; - count(): Promise; getAll(query?: IProjectQuery): Promise; diff --git a/src/lib/features/project/project-store.ts b/src/lib/features/project/project-store.ts index 7c0fc2188771..5df93193c63e 100644 --- a/src/lib/features/project/project-store.ts +++ b/src/lib/features/project/project-store.ts @@ -390,63 +390,6 @@ class ProjectStore implements IProjectStore { return rows.map(this.mapProjectEnvironmentRow); } - private async getMembersCount(): Promise { - const members = await this.db - .select('project') - .from((db) => { - db.select('user_id', 'project') - .from('role_user') - .leftJoin('roles', 'role_user.role_id', 'roles.id') - .where((builder) => builder.whereNot('type', 'root')) - .union((queryBuilder) => { - queryBuilder - .select('user_id', 'project') - .from('group_role') - .leftJoin( - 'group_user', - 'group_user.group_id', - 'group_role.group_id', - ); - }) - .as('query'); - }) - .groupBy('project') - .count('user_id'); - return members; - } - - async getProjectsByUser(userId: number): Promise { - const projects = await this.db - .from((db) => { - db.select('role_user.project') - .from('role_user') - .leftJoin('roles', 'role_user.role_id', 'roles.id') - .leftJoin('projects', 'role_user.project', 'projects.id') - .where('user_id', userId) - .andWhere('projects.archived_at', null) - .union((queryBuilder) => { - queryBuilder - .select('group_role.project') - .from('group_role') - .leftJoin( - 'group_user', - 'group_user.group_id', - 'group_role.group_id', - ) - .leftJoin( - 'projects', - 'group_role.project', - 'projects.id', - ) - .where('group_user.user_id', userId) - .andWhere('projects.archived_at', null); - }) - .as('query'); - }) - .pluck('project'); - return projects; - } - async getMembersCountByProject(projectId: string): Promise { const members = await this.db .from((db) => { diff --git a/src/test/fixtures/fake-project-store.ts b/src/test/fixtures/fake-project-store.ts index 04ee450db85b..7338bc6575bb 100644 --- a/src/test/fixtures/fake-project-store.ts +++ b/src/test/fixtures/fake-project-store.ts @@ -143,11 +143,6 @@ export default class FakeProjectStore implements IProjectStore { )!.health = healthUpdate.health; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - getProjectsByUser(userId: number): Promise { - return Promise.resolve([]); - } - addEnvironmentToProjects( // eslint-disable-next-line @typescript-eslint/no-unused-vars environment: string,