1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00
unleash.unleash/src/lib/features/personal-dashboard/personal-dashboard-read-model.ts
Thomas Heartman 289324fd02
feat: add group project roles to project roles (#8245)
This PR adds project roles you get from groups to the list of roles
listed for each project.
2024-09-25 10:29:05 +02:00

124 lines
3.9 KiB
TypeScript

import type { Db } from '../../db/db';
import type {
IPersonalDashboardReadModel,
PersonalFeature,
PersonalProject,
} from './personal-dashboard-read-model-type';
type IntermediateProjectResult = Omit<PersonalProject, 'roles'> & {
roles: {
[id: number]: { id: number; name: string; type: string };
};
};
export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
private db: Db;
constructor(db: Db) {
this.db = db;
}
async getPersonalProjects(userId: number): Promise<PersonalProject[]> {
const result = await this.db<{
name: string;
id: string;
roleId: number;
roleName: string;
roleType: string;
}>('projects')
.join('role_user', 'projects.id', 'role_user.project')
.join('roles', 'role_user.role_id', 'roles.id')
.leftJoin('group_user', (join) => {
join.on('group_user.user_id', '=', this.db.raw('?', [userId]));
})
.leftJoin(
'group_role',
'group_role.group_id',
'group_user.group_id',
)
.leftJoin(
'roles as group_roles',
'group_role.role_id',
'group_roles.id',
)
.where('role_user.user_id', userId)
.orWhere('group_user.user_id', userId)
.whereNull('projects.archived_at')
.select(
'projects.name',
'projects.id',
'roles.id as roleId',
'roles.name as roleName',
'roles.type as roleType',
)
.limit(100);
const dict = result.reduce((acc, row) => {
if (acc[row.id]) {
acc[row.id].roles[row.roleId] = {
id: row.roleId,
name: row.roleName,
type: row.roleType,
};
} else {
acc[row.id] = {
id: row.id,
name: row.name,
roles: {
[row.roleId]: {
id: row.roleId,
name: row.roleName,
type: row.roleType,
},
},
};
}
return acc;
}, {});
const projectList: PersonalProject[] = Object.values(dict).map(
(project: IntermediateProjectResult) => {
const roles = Object.values(project.roles);
roles.sort((a, b) => a.id - b.id);
return {
...project,
roles,
} as PersonalProject;
},
);
projectList.sort((a, b) => a.name.localeCompare(b.name));
return projectList;
}
async getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
const result = await this.db<{
name: string;
type: string;
project: string;
}>('favorite_features')
.join('features', 'favorite_features.feature', 'features.name')
.where('favorite_features.user_id', userId)
.whereNull('features.archived_at')
.select(
'features.name as name',
'features.type',
'features.project',
'features.created_at',
)
.union(function () {
this.select('name', 'type', 'project', 'created_at')
.from('features')
.where('features.created_by_user_id', userId)
.whereNull('features.archived_at');
})
.orderBy('created_at', 'desc')
.limit(100);
return result.map((row) => ({
name: row.name,
type: row.type,
project: row.project,
}));
}
}