Skip to content

Commit

Permalink
refactor: move getProjectsByUser to read model (#8262)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Sep 26, 2024
1 parent aca0de4 commit 823f633
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 65 deletions.
3 changes: 3 additions & 0 deletions src/lib/features/project/fake-project-read-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ export class FakeProjectReadModel implements IProjectReadModel {
getProjectsForInsights(): Promise<ProjectForInsights[]> {
return Promise.resolve([]);
}
getProjectsByUser(): Promise<string[]> {
return Promise.resolve([]);
}
}
1 change: 1 addition & 0 deletions src/lib/features/project/project-read-model-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ export interface IProjectReadModel {
getFeatureProject(
featureName: string,
): Promise<{ project: string; createdAt: Date } | null>;
getProjectsByUser(userId: number): Promise<string[]>;
}
32 changes: 32 additions & 0 deletions src/lib/features/project/project-read-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,36 @@ export class ProjectReadModel implements IProjectReadModel {
memberTimer();
return members;
}

async getProjectsByUser(userId: number): Promise<string[]> {
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;
}
}
2 changes: 1 addition & 1 deletion src/lib/features/project/project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ export default class ProjectService {
}

async getProjectsByUser(userId: number): Promise<string[]> {
return this.projectStore.getProjectsByUser(userId);
return this.projectReadModel.getProjectsByUser(userId);
}

async getProjectRoleUsage(roleId: number): Promise<IProjectRoleUsage[]> {
Expand Down
2 changes: 0 additions & 2 deletions src/lib/features/project/project-store-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ export interface IProjectStore extends Store<IProject, string> {
date: string,
): Promise<number>;

getProjectsByUser(userId: number): Promise<string[]>;

count(): Promise<number>;

getAll(query?: IProjectQuery): Promise<IProject[]>;
Expand Down
57 changes: 0 additions & 57 deletions src/lib/features/project/project-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,63 +390,6 @@ class ProjectStore implements IProjectStore {
return rows.map(this.mapProjectEnvironmentRow);
}

private async getMembersCount(): Promise<IProjectMembersCount[]> {
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<string[]> {
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<number> {
const members = await this.db
.from((db) => {
Expand Down
5 changes: 0 additions & 5 deletions src/test/fixtures/fake-project-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
return Promise.resolve([]);
}

addEnvironmentToProjects(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
environment: string,
Expand Down

0 comments on commit 823f633

Please sign in to comment.