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

Project owners read model - db read #6916

Merged
merged 18 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
4 changes: 4 additions & 0 deletions src/lib/features/project/project-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ export default class ProjectController extends Controller {
user.id,
);

if (this.flagResolver.isEnabled('projectsListNewCards')) {
// const projectsWithOwners = projectOwnersReadModel(projects);
}

this.openApiService.respondWithValidation(
200,
res,
Expand Down
137 changes: 137 additions & 0 deletions src/lib/features/project/project-owners-read-model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import dbInit, { type ITestDb } from '../../../test/e2e/helpers/database-init';
import getLogger from '../../../test/fixtures/no-logger';
import { type IUser, RoleName } from '../../types';
import { randomId } from '../../util';
import { ProjectOwnersReadModel } from './project-owners-read-model';

describe('unit tests', () => {
test('maps owners to projects', () => {});
});

let db: ITestDb;
let readModel: ProjectOwnersReadModel;

let owner: IUser;
let member: IUser;

beforeAll(async () => {
db = await dbInit('project_owners_read_model_serial', getLogger);
readModel = new ProjectOwnersReadModel(db.rawDatabase);

const ownerData = {
name: 'owner',
username: 'owner',
email: '[email protected]',
imageUrl: 'image-url-1',
};
const memberData = {
name: 'member',
username: 'member',
email: '[email protected]',
imageUrl: 'image-url-2',
};

// create users
owner = await db.stores.userStore.insert(ownerData);
member = await db.stores.userStore.insert(memberData);
});

afterAll(async () => {
if (db) {
await db.destroy();
}
});

afterEach(async () => {
if (db) {
await db.stores.projectStore.deleteAll();
}
});

describe('integration tests', () => {
test('name takes precedence over username', () => {
// import { extractUsername } from '../../../util/extract-user';
});

test('gets project user owners', async () => {
const projectId = randomId();

await db.stores.projectStore.create({ id: projectId, name: projectId });

const ownerRole = await db.stores.roleStore.getRoleByName(
RoleName.OWNER,
);
await db.stores.accessStore.addUserToRole(
owner.id,
ownerRole.id,
projectId,
);

// fetch project owners
const owners = await readModel.getAllProjectOwners();

expect(owners).toMatchObject({
[projectId]: [{ name: 'owner' }],
});
});

test('does not get regular project members', async () => {
const projectId = randomId();

await db.stores.projectStore.create({ id: projectId, name: projectId });

const ownerRole = await db.stores.roleStore.getRoleByName(
RoleName.OWNER,
);

const memberRole = await db.stores.roleStore.getRoleByName(
RoleName.MEMBER,
);
await db.stores.accessStore.addUserToRole(
owner.id,
ownerRole.id,
projectId,
);

await db.stores.accessStore.addUserToRole(
member.id,
memberRole.id,
projectId,
);

// fetch project owners
const owners = await readModel.getAllProjectOwners();

expect(owners).toMatchObject({
[projectId]: [{ name: 'owner' }],
});
});

test('returns "system" when a project has no owners', async () => {
const projectId = randomId();

await db.stores.projectStore.create({ id: projectId, name: projectId });

// fetch project owners
const owners = await readModel.getAllProjectOwners();

expect(owners).toMatchObject({
[projectId]: [{ type: 'system' }],
});
});
test('gets project group owners', async () => {});
test('users are listed before groups', async () => {});
test('owners (users and groups) are sorted by when they were added; oldest first', async () => {});
test('returns the system owner for the default project', () => {});
test('returns an empty list if there are no projects', async () => {
const owners = await readModel.getAllProjectOwners();

expect(owners).toStrictEqual({});
});

test('enriches fully', async () => {
const owners = await readModel.enrichWithOwners([]);

expect(owners).toStrictEqual([]);
});
});
77 changes: 77 additions & 0 deletions src/lib/features/project/project-owners-read-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { Db } from '../../db/db';
import type { IProjectWithCount } from '../../types';

export type SystemOwner = { ownerType: 'system' };
export type NonSystemProjectOwner =
| {
ownerType: 'user';
name: string;
email?: string;
imageUrl?: string;
}
| {
ownerType: 'group';
name: string;
};

type ProjectOwners = [SystemOwner] | NonSystemProjectOwner[];

export type ProjectOwnersDictionary = Record<string, ProjectOwners>;

type IProjectWithCountAndOwners = IProjectWithCount & {
owners: ProjectOwners;
};

export class ProjectOwnersReadModel {
private db: Db;

constructor(db: Db) {
this.db = db;
}

addOwnerData(
projects: IProjectWithCount[],
owners: ProjectOwnersDictionary,
): IProjectWithCountAndOwners[] {
// const projectsWithOwners = projects.map((p) => ({
// ...p,
// owners: projectOwners[p.id] || [],
// }));
return [];
}
async getAllProjectOwners(): Promise<ProjectOwnersDictionary> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting it in 2 private methods makes the main one more readable to me, so that's what I did

// const ownerRole = await this.accessService.getRoleByName(
// RoleName.OWNER,
// );
// const ownerRoleId = ownerRole.id;

// async getAllProjectsUsersForRole(roleId: number): Promise<IUserWithProjectRoles[]> {
// const rows = await this.db
// .select(['user_id', 'ru.created_at', 'ru.project'])
// .from<IRole>(`${T.ROLE_USER} AS ru`)
// .join(`${T.ROLES} as r`, 'ru.role_id', 'id')
// .where('r.id', roleId);

// return rows.map((r) => ({
// id: r.user_id,
// addedAt: r.created_at,
// projectId: r.project,
// roleId,
// }));
// }

// async getAllProjectsGroupsForRole(roleId: number): Promise<any[]> {
// throw new Error('Method not implemented');
// }

return {};
}

async enrichWithOwners(
projects: IProjectWithCount[],
): Promise<IProjectWithCountAndOwners[]> {
const owners = await this.getAllProjectOwners();

return this.addOwnerData(projects, owners);
}
}
Loading