Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: fix broken query #8264

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -28,15 +28,11 @@ export class PersonalDashboardReadModel implements IPersonalDashboardReadModel {
}>('projects')
.join('role_user', 'projects.id', 'role_user.project')
.join('roles', 'role_user.role_id', 'roles.id')
.leftJoin('group_user', (join) => {
.join('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(
.join('group_role', 'group_role.group_id', 'group_user.group_id')
.join(
'roles as group_roles',
'group_role.role_id',
'group_roles.id',
Expand Down Expand Up @@ -120,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
Loading