mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
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.
This commit is contained in:
parent
957ef12ca3
commit
289324fd02
@ -5,6 +5,7 @@ import {
|
|||||||
} from '../../../test/e2e/helpers/test-helper';
|
} from '../../../test/e2e/helpers/test-helper';
|
||||||
import getLogger from '../../../test/fixtures/no-logger';
|
import getLogger from '../../../test/fixtures/no-logger';
|
||||||
import type { IUser } from '../../types';
|
import type { IUser } from '../../types';
|
||||||
|
import { randomId } from '../../util';
|
||||||
|
|
||||||
let app: IUnleashTest;
|
let app: IUnleashTest;
|
||||||
let db: ITestDb;
|
let db: ITestDb;
|
||||||
@ -38,6 +39,7 @@ afterAll(async () => {
|
|||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await db.stores.featureToggleStore.deleteAll();
|
await db.stores.featureToggleStore.deleteAll();
|
||||||
|
await db.stores.userStore.deleteAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return personal dashboard with own flags and favorited flags', async () => {
|
test('should return personal dashboard with own flags and favorited flags', async () => {
|
||||||
@ -135,6 +137,67 @@ test('should return personal dashboard with membered projects', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return projects where users are part of a group', () => {
|
test('should return projects where users are part of a group', async () => {
|
||||||
// TODO
|
const { body: user1 } = await loginUser('user1@test.com');
|
||||||
|
const projectA = await createProject(`x${randomId()}`, user1);
|
||||||
|
|
||||||
|
const { body: user2 } = await loginUser('user2@test.com');
|
||||||
|
|
||||||
|
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',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -5,6 +5,12 @@ import type {
|
|||||||
PersonalProject,
|
PersonalProject,
|
||||||
} from './personal-dashboard-read-model-type';
|
} 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 {
|
export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
|
||||||
private db: Db;
|
private db: Db;
|
||||||
|
|
||||||
@ -22,7 +28,21 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
|
|||||||
}>('projects')
|
}>('projects')
|
||||||
.join('role_user', 'projects.id', 'role_user.project')
|
.join('role_user', 'projects.id', 'role_user.project')
|
||||||
.join('roles', 'role_user.role_id', 'roles.id')
|
.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)
|
.where('role_user.user_id', userId)
|
||||||
|
.orWhere('group_user.user_id', userId)
|
||||||
.whereNull('projects.archived_at')
|
.whereNull('projects.archived_at')
|
||||||
.select(
|
.select(
|
||||||
'projects.name',
|
'projects.name',
|
||||||
@ -35,28 +55,37 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
|
|||||||
|
|
||||||
const dict = result.reduce((acc, row) => {
|
const dict = result.reduce((acc, row) => {
|
||||||
if (acc[row.id]) {
|
if (acc[row.id]) {
|
||||||
acc[row.id].roles.push({
|
acc[row.id].roles[row.roleId] = {
|
||||||
id: row.roleId,
|
id: row.roleId,
|
||||||
name: row.roleName,
|
name: row.roleName,
|
||||||
type: row.roleType,
|
type: row.roleType,
|
||||||
});
|
};
|
||||||
} else {
|
} else {
|
||||||
acc[row.id] = {
|
acc[row.id] = {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
name: row.name,
|
name: row.name,
|
||||||
roles: [
|
roles: {
|
||||||
{
|
[row.roleId]: {
|
||||||
id: row.roleId,
|
id: row.roleId,
|
||||||
name: row.roleName,
|
name: row.roleName,
|
||||||
type: row.roleType,
|
type: row.roleType,
|
||||||
},
|
},
|
||||||
],
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return acc;
|
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));
|
projectList.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
return projectList;
|
return projectList;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user