Skip to content

Commit

Permalink
feat: do some impl
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Sep 25, 2024
1 parent c8ea16e commit d5cb78a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ test('should return personal dashboard with membered projects', async () => {
type: 'root',
},
],
owners: [],
},
{
name: projectA.name,
Expand All @@ -121,6 +122,7 @@ test('should return personal dashboard with membered projects', async () => {
type: 'project',
},
],
owners: [],
},
{
name: projectC.name,
Expand All @@ -132,6 +134,7 @@ test('should return personal dashboard with membered projects', async () => {
type: 'project',
},
],
owners: [],
},
],
});
Expand Down Expand Up @@ -181,6 +184,7 @@ test('should return projects where users are part of a group', async () => {
type: 'root',
},
],
owners: [],
},
{
name: projectA.name,
Expand All @@ -197,6 +201,7 @@ test('should return projects where users are part of a group', async () => {
type: 'project',
},
],
owners: [],
},
],
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Db } from '../../db/db';
import type { IProjectOwnersReadModel } from '../project/project-owners-read-model.type';
import type {
IPersonalDashboardReadModel,
PersonalFeature,
Expand All @@ -14,8 +15,11 @@ type IntermediateProjectResult = Omit<PersonalProject, 'roles'> & {
export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
private db: Db;

constructor(db: Db) {
private projectOwnersReadModel: IProjectOwnersReadModel;

constructor(db: Db, ProjectOwnersReadModel: IProjectOwnersReadModel) {
this.db = db;
this.projectOwnersReadModel = ProjectOwnersReadModel;
}

async getPersonalProjects(userId: number): Promise<PersonalProject[]> {
Expand Down Expand Up @@ -80,14 +84,18 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
(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;
const withOwners =
await this.projectOwnersReadModel.addOwners(projectList);

return withOwners;
}

async getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
Expand Down
9 changes: 4 additions & 5 deletions src/lib/features/project/fake-project-owners-read-model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type {
IProjectOwnersReadModel,
IProjectForUiWithOwners,
WithProjectOwners,
} from './project-owners-read-model.type';
import type { ProjectForUi } from './project-read-model-type';

export class FakeProjectOwnersReadModel implements IProjectOwnersReadModel {
async addOwners(
projects: ProjectForUi[],
): Promise<IProjectForUiWithOwners[]> {
async addOwners<T extends { id: string }>(
projects: T[],
): Promise<WithProjectOwners<T>> {
return projects.map((project) => ({
...project,
owners: [{ ownerType: 'system' }],
Expand Down
6 changes: 1 addition & 5 deletions src/lib/features/project/project-owners-read-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
IProjectOwnersReadModel,
ProjectOwnersDictionary,
UserProjectOwner,
ProjectOwners,
WithProjectOwners,
} from './project-owners-read-model.type';

const T = {
Expand All @@ -16,10 +16,6 @@ const T = {
USERS: 'users',
};

type WithProjectOwners<T extends { id: string }> = (T & {
owners: ProjectOwners;
})[];

export class ProjectOwnersReadModel implements IProjectOwnersReadModel {
private db: Db;

Expand Down
10 changes: 7 additions & 3 deletions src/lib/features/project/project-owners-read-model.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ export type IProjectForUiWithOwners = ProjectForUi & {
owners: ProjectOwners;
};

export type WithProjectOwners<T extends { id: string }> = (T & {
owners: ProjectOwners;
})[];

export interface IProjectOwnersReadModel {
addOwners(
projects: ProjectForUi[],
addOwners<T extends { id: string }>(
projects: T[],
anonymizeProjectOwners?: boolean,
): Promise<IProjectForUiWithOwners[]>;
): Promise<WithProjectOwners<T>>;
}
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/personal-dashboard-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FromSchema } from 'json-schema-to-ts';
import { projectSchema } from './project-schema';

export const personalDashboardSchema = {
$id: '#/components/schemas/personalDashboardSchema',
Expand All @@ -24,6 +25,7 @@ export const personalDashboardSchema = {
example: 'My Project',
description: 'The name of the project',
},
owners: projectSchema.properties.owners,
roles: {
type: 'array',
description:
Expand Down
3 changes: 2 additions & 1 deletion src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ import { OnboardingService } from '../features/onboarding/onboarding-service';
import { PersonalDashboardService } from '../features/personal-dashboard/personal-dashboard-service';
import { PersonalDashboardReadModel } from '../features/personal-dashboard/personal-dashboard-read-model';
import { FakePersonalDashboardReadModel } from '../features/personal-dashboard/fake-personal-dashboard-read-model';
import { ProjectOwnersReadModel } from '../features/project/project-owners-read-model';

export const createServices = (
stores: IUnleashStores,
Expand Down Expand Up @@ -406,7 +407,7 @@ export const createServices = (

const personalDashboardService = new PersonalDashboardService(
db
? new PersonalDashboardReadModel(db)
? new PersonalDashboardReadModel(db, new ProjectOwnersReadModel(db))
: new FakePersonalDashboardReadModel(),
);

Expand Down

0 comments on commit d5cb78a

Please sign in to comment.