-
-
Notifications
You must be signed in to change notification settings - Fork 736
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
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2836606
project owners read model concept
Tymek 4ed31e0
feat: wrap enriching in flag
thomasheartman 3b6cf18
feat: add placeholders to project owners read model
thomasheartman cc23de9
feat: mock some changes
thomasheartman aa96471
Chore: stub out read model
thomasheartman f7c6984
feat: add more specific types for what we want
thomasheartman cbecd6c
feat: rewrite some tests
thomasheartman 4697b70
feat: stub out some tests
thomasheartman 426d268
Chore: add more test cases
thomasheartman 1108d6c
Chore: add first test
thomasheartman 046f9b2
Chore: update tests
thomasheartman 00f38b5
First stab at impl:
thomasheartman 724f643
Test: move test to unit
thomasheartman cdda3f6
Feat: first working impl
thomasheartman 6b8247c
fix project owners read mode test cleanup
Tymek 26a9581
project owners get all groups
Tymek 0232537
refactor: reduce complexity in project owners read model
Tymek d601a41
fix: comment plan
Tymek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/lib/features/project/project-owners-read-model.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
// 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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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