Skip to content

Commit

Permalink
feat: include favorite projects in personal dashboard (#8309)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Sep 30, 2024
1 parent 6d16fc6 commit b037bfb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ const createProject = async (name: string, user: IUser) => {
return project;
};

const favoriteProject = async (projectName = 'default') => {
await app.request
.post(`/api/admin/projects/${projectName}/favorites`)
.set('Content-Type', 'application/json')
.expect(200);
};

test('should return personal dashboard with membered projects', async () => {
const { body: user1 } = await loginUser('[email protected]');
const projectA = await createProject('Project A', user1);
Expand Down Expand Up @@ -118,6 +125,36 @@ test('should return personal dashboard with membered projects', async () => {
{
name: projectC.name,
id: projectC.id,
health: 100,
memberCount: 1,
featureCount: 0,
},
],
});
});

test('should return personal dashboard with user favorited projects', async () => {
const { body: user1 } = await loginUser('[email protected]');
const projectA = await createProject('Project A', user1);

await loginUser('[email protected]');
await favoriteProject(projectA.id);

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

expect(body).toMatchObject({
projects: [
{
name: 'Default',
id: 'default',
health: 100,
memberCount: 0,
featureCount: 0,
},
{
name: projectA.name,
id: projectA.id,
health: 100,
memberCount: 1,
featureCount: 0,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ export class PersonalDashboardService {
}

async getPersonalProjects(userId: number): Promise<PersonalProject[]> {
// TODO: add favorite projects in addition to membership projects
const userProjectIds =
await this.projectReadModel.getProjectsByUser(userId);
const [userProjectIds, userFavoritedProjectIds] = await Promise.all([
this.projectReadModel.getProjectsByUser(userId),
this.projectReadModel.getProjectsFavoritedByUser(userId),
]);

const projects = await this.projectReadModel.getProjectsForAdminUi({
ids: userProjectIds,
ids: [...new Set([...userProjectIds, ...userFavoritedProjectIds])],
archived: false,
});

Expand Down
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 @@ -17,4 +17,7 @@ export class FakeProjectReadModel implements IProjectReadModel {
getProjectsByUser(): Promise<string[]> {
return Promise.resolve([]);
}
getProjectsFavoritedByUser(): 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 @@ -38,4 +38,5 @@ export interface IProjectReadModel {
featureName: string,
): Promise<{ project: string; createdAt: Date } | null>;
getProjectsByUser(userId: number): Promise<string[]>;
getProjectsFavoritedByUser(userId: number): Promise<string[]>;
}
12 changes: 12 additions & 0 deletions src/lib/features/project/project-read-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,16 @@ export class ProjectReadModel implements IProjectReadModel {
.pluck('project');
return projects;
}

async getProjectsFavoritedByUser(userId: number): Promise<string[]> {
const favoritedProjects = await this.db
.select('favorite_projects.project')
.from('favorite_projects')
.leftJoin('projects', 'favorite_projects.project', 'projects.id')
.where('favorite_projects.user_id', userId)
.andWhere('projects.archived_at', null)
.pluck('project');

return favoritedProjects;
}
}

0 comments on commit b037bfb

Please sign in to comment.