Skip to content

Commit

Permalink
wip: start messing around
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheartman committed Sep 26, 2024
1 parent 2a5b8e3 commit 4880782
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import type {
export class FakePersonalDashboardReadModel
implements IPersonalDashboardReadModel
{
enrichProjectIds(
userId: number,
projectIds: string[],
): Promise<PersonalProject[]> {
throw new Error('Method not implemented.');
}
async getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ export type PersonalProjectWithOwners = PersonalProject & {
export interface IPersonalDashboardReadModel {
getPersonalFeatures(userId: number): Promise<PersonalFeature[]>;
getPersonalProjects(userId: number): Promise<PersonalProject[]>;

enrichProjectIds(
userId: number,
projectIds: string[],
): Promise<PersonalProject[]>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,55 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
project: row.project,
}));
}

async enrichProjectIds(
userId: number,
projectIds: string[],
): Promise<PersonalProject[]> {
const T = {
ROLE_USER: 'role_user',
ROLES: 'roles',
GROUPS: 'groups',
GROUP_ROLE: 'group_role',
GROUP_USER: 'group_user',
ROLE_PERMISSION: 'role_permission',
PERMISSIONS: 'permissions',
PERMISSION_TYPES: 'permission_types',
CHANGE_REQUEST_SETTINGS: 'change_request_settings',
PERSONAL_ACCESS_TOKENS: 'personal_access_tokens',
PUBLIC_SIGNUP_TOKENS_USER: 'public_signup_tokens_user',
};

const roles = await this.db
.select(['id', 'name', 'type', 'project', 'description'])
.from(T.ROLES)
.innerJoin(`${T.ROLE_USER} as ru`, 'ru.role_id', 'id')
.where('ru.user_id', '=', userId)
.andWhere((builder) => {
builder
.whereIn('ru.project', projectIds)
.orWhere('type', '=', 'root');
})
.union([
this.db
.select(['id', 'name', 'type', 'project', 'description'])
.from(T.ROLES)
.innerJoin(`${T.GROUP_ROLE} as gr`, 'gr.role_id', 'id')
.innerJoin(
`${T.GROUP_USER} as gu`,
'gu.group_id',
'gr.group_id',
)
.where('gu.user_id', '=', userId)
.andWhere((builder) => {
builder
.whereIn('gr.project', projectIds)
.orWhere('type', '=', 'root');
}),
]);

console.log(roles);

return [];
}
}
14 changes: 14 additions & 0 deletions src/lib/features/personal-dashboard/personal-dashboard-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IProjectOwnersReadModel } from '../project/project-owners-read-model.type';
import type { IProjectReadModel } from '../project/project-read-model-type';
import type {
IPersonalDashboardReadModel,
PersonalFeature,
Expand All @@ -10,12 +11,16 @@ export class PersonalDashboardService {

private projectOwnersReadModel: IProjectOwnersReadModel;

private projectReadModel: IProjectReadModel;

constructor(
personalDashboardReadModel: IPersonalDashboardReadModel,
projectOwnersReadModel: IProjectOwnersReadModel,
projectReadModel: IProjectReadModel,
) {
this.personalDashboardReadModel = personalDashboardReadModel;
this.projectOwnersReadModel = projectOwnersReadModel;
this.projectReadModel = projectReadModel;
}

getPersonalFeatures(userId: number): Promise<PersonalFeature[]> {
Expand All @@ -25,6 +30,15 @@ export class PersonalDashboardService {
async getPersonalProjects(
userId: number,
): Promise<PersonalProjectWithOwners[]> {
const projectIds =
await this.projectReadModel.getProjectsByUser(userId);

console.log('got these', projectIds.length, 'projects', projectIds);
const enriched = await this.personalDashboardReadModel.enrichProjectIds(
userId,
projectIds,
);
// console.log('got these', enriched.length, 'projects', projectIds);
const projects =
await this.personalDashboardReadModel.getPersonalProjects(userId);

Expand Down
5 changes: 5 additions & 0 deletions src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ import { PersonalDashboardReadModel } from '../features/personal-dashboard/perso
import { FakePersonalDashboardReadModel } from '../features/personal-dashboard/fake-personal-dashboard-read-model';
import { ProjectOwnersReadModel } from '../features/project/project-owners-read-model';
import { FakeProjectOwnersReadModel } from '../features/project/fake-project-owners-read-model';
import { ProjectReadModel } from '../features/project/project-read-model';
import { FakeProjectReadModel } from '../features/project/fake-project-read-model';

export const createServices = (
stores: IUnleashStores,
Expand Down Expand Up @@ -412,6 +414,9 @@ export const createServices = (
: new FakePersonalDashboardReadModel(),

db ? new ProjectOwnersReadModel(db) : new FakeProjectOwnersReadModel(),
db
? new ProjectReadModel(db, config.eventBus, config.flagResolver)
: new FakeProjectReadModel(),
);

return {
Expand Down

0 comments on commit 4880782

Please sign in to comment.