Skip to content

Commit

Permalink
feat: add group project roles to project roles (#8245)
Browse files Browse the repository at this point in the history
This PR adds project roles you get from groups to the list of roles
listed for each project.
  • Loading branch information
thomasheartman authored Sep 25, 2024
1 parent 957ef12 commit 289324f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '../../../test/e2e/helpers/test-helper';
import getLogger from '../../../test/fixtures/no-logger';
import type { IUser } from '../../types';
import { randomId } from '../../util';

let app: IUnleashTest;
let db: ITestDb;
Expand Down Expand Up @@ -38,6 +39,7 @@ afterAll(async () => {

beforeEach(async () => {
await db.stores.featureToggleStore.deleteAll();
await db.stores.userStore.deleteAll();
});

test('should return personal dashboard with own flags and favorited flags', async () => {
Expand Down Expand Up @@ -135,6 +137,67 @@ test('should return personal dashboard with membered projects', async () => {
});
});

test('should return projects where users are part of a group', () => {
// TODO
test('should return projects where users are part of a group', async () => {
const { body: user1 } = await loginUser('[email protected]');
const projectA = await createProject(`x${randomId()}`, user1);

const { body: user2 } = await loginUser('[email protected]');

const group = await app.services.groupService.createGroup(
{
name: 'groupA',
users: [{ user: user2 }],
},
user1,
);

await app.services.projectService.addAccess(
projectA.id,
[5], // member role
[],
[user2.id],
user1,
);

await app.services.projectService.addAccess(
projectA.id,
[4], // owner role
[group.id],
[],
user1,
);

const { body } = await app.request.get(`/api/admin/personal-dashboard`);

expect(body).toMatchObject({
projects: [
{
name: 'Default',
id: 'default',
roles: [
{
name: 'Editor',
id: 2,
type: 'root',
},
],
},
{
name: projectA.name,
id: projectA.id,
roles: [
{
name: 'Owner',
id: 4,
type: 'project',
},
{
name: 'Member',
id: 5,
type: 'project',
},
],
},
],
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import type {
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;

Expand All @@ -22,7 +28,21 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
}>('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',
Expand All @@ -35,28 +55,37 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {

const dict = result.reduce((acc, row) => {
if (acc[row.id]) {
acc[row.id].roles.push({
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: [
{
roles: {
[row.roleId]: {
id: row.roleId,
name: row.roleName,
type: row.roleType,
},
],
},
};
}
return acc;
}, {});

const projectList: PersonalProject[] = Object.values(dict);
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;
}
Expand Down

0 comments on commit 289324f

Please sign in to comment.