From 2a754aeb33a85ce1cb0f2ec5631177ed9eb91d53 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 24 Sep 2024 14:37:31 +0200 Subject: [PATCH] feat: return projects --- .../personal-dashboard-controller.e2e.test.ts | 39 ++++++++++++------- .../personal-dashboard-read-model-type.ts | 6 ++- .../personal-dashboard-read-model.ts | 11 +++--- .../openapi/spec/personal-dashboard-schema.ts | 7 +++- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts b/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts index 73bcddc231a3..f38277cf3d9f 100644 --- a/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts +++ b/src/lib/features/personal-dashboard/personal-dashboard-controller.e2e.test.ts @@ -99,33 +99,42 @@ test('should return personal dashboard with membered projects', async () => { user1, ); - // Add user 1 as an owner of project C - await app.services.projectService.addAccess( - projectC.id, - [4], - [], - [user1.id], - user2, - ); - 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, - owners: [{ id: user1.id, name: user1.email, imageUrl: '' }], - roles: ['member'], + roles: [ + { + name: 'Member', + id: 5, + type: 'project', + }, + ], }, { name: projectC.name, id: projectC.id, - owners: [ - { id: user2.id, name: user2.email, imageUrl: '' }, - { id: user1.id, name: user1.email, imageUrl: '' }, + roles: [ + { + name: 'Owner', + id: 4, + type: 'project', + }, ], - roles: ['owner'], }, ], }); diff --git a/src/lib/features/personal-dashboard/personal-dashboard-read-model-type.ts b/src/lib/features/personal-dashboard/personal-dashboard-read-model-type.ts index 7d251d7c2da8..d928aabddd08 100644 --- a/src/lib/features/personal-dashboard/personal-dashboard-read-model-type.ts +++ b/src/lib/features/personal-dashboard/personal-dashboard-read-model-type.ts @@ -2,7 +2,11 @@ export type PersonalFeature = { name: string; type: string; project: string }; export type PersonalProject = { name: string; id: string; - roles: { name: string; id: number; type: 'custom' | 'project' }[]; + roles: { + name: string; + id: number; + type: 'custom' | 'project' | 'root' | 'custom-root'; + }[]; }; export interface IPersonalDashboardReadModel { diff --git a/src/lib/features/personal-dashboard/personal-dashboard-read-model.ts b/src/lib/features/personal-dashboard/personal-dashboard-read-model.ts index 7f239e7c5603..dd9a856355e8 100644 --- a/src/lib/features/personal-dashboard/personal-dashboard-read-model.ts +++ b/src/lib/features/personal-dashboard/personal-dashboard-read-model.ts @@ -23,7 +23,7 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel { .join('role_user', 'projects.id', 'role_user.project') .join('roles', 'role_user.role_id', 'roles.id') .where('role_user.user_id', userId) - .whereNull('project.archived_at') + .whereNull('projects.archived_at') .select( 'projects.name', 'projects.id', @@ -31,12 +31,9 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel { 'roles.name as roleName', 'roles.type as roleType', ) - .orderBy('projects.name', 'desc') .limit(100); - console.log(result); - - return result.reduce((acc, row) => { + const dict = result.reduce((acc, row) => { if (acc[row.id]) { acc[row.id].roles.push({ id: row.roleId, @@ -58,6 +55,10 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel { } return acc; }, {}); + + const projectList: PersonalProject[] = Object.values(dict); + projectList.sort((a, b) => a.name.localeCompare(b.name)); + return projectList; } async getPersonalFeatures(userId: number): Promise { diff --git a/src/lib/openapi/spec/personal-dashboard-schema.ts b/src/lib/openapi/spec/personal-dashboard-schema.ts index cc2d8e5c5542..0fd85c82480c 100644 --- a/src/lib/openapi/spec/personal-dashboard-schema.ts +++ b/src/lib/openapi/spec/personal-dashboard-schema.ts @@ -47,7 +47,12 @@ export const personalDashboardSchema = { }, type: { type: 'string', - enum: ['custom', 'project'], + enum: [ + 'custom', + 'project', + 'root', + 'custom-root', + ], example: 'project', description: 'The type of the role', },