1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-06 00:13:28 +00:00

[PM-24633] - group same parent name collections by org (#16404)

* key nested collections by orgId

* move groupByOrganization to separate function
This commit is contained in:
Jordan Aasen
2025-09-17 09:25:59 -07:00
committed by GitHub
parent 0e23d6d9f1
commit 814305a778
2 changed files with 42 additions and 6 deletions

View File

@@ -369,6 +369,27 @@ describe("DefaultCollectionService", () => {
});
});
describe("groupByOrganization", () => {
it("groups collections by organization", () => {
const org1 = { organizationId: "org1" } as CollectionView;
org1.name = "Collection 1";
const org2 = { organizationId: "org1" } as CollectionView;
org2.name = "Collection 2";
const org3 = { organizationId: "org2" } as CollectionView;
org3.name = "Collection 3";
const collections = [org1, org2, org3];
const result = collectionService.groupByOrganization(collections);
expect(result.size).toBe(2);
expect(result.get(org1.organizationId)?.length).toBe(2);
expect(result.get(org1.organizationId)).toContainPartialObjects([org1, org2]);
expect(result.get(org3.organizationId)?.length).toBe(1);
expect(result.get(org3.organizationId)).toContainPartialObjects([org3]);
});
});
const setEncryptedState = (collectionData: CollectionData[] | null) =>
stateProvider.setUserState(
ENCRYPTED_COLLECTION_DATA_KEY,

View File

@@ -213,15 +213,30 @@ export class DefaultCollectionService implements CollectionService {
);
}
// Transforms the input CollectionViews into TreeNodes
getAllNested(collections: CollectionView[]): TreeNode<CollectionView>[] {
const nodes: TreeNode<CollectionView>[] = [];
collections.forEach((c) => {
const collectionCopy = Object.assign(new CollectionView({ ...c, name: c.name }), c);
const groupedByOrg = this.groupByOrganization(collections);
const parts = c.name != null ? c.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter) : [];
ServiceUtils.nestedTraverse(nodes, 0, parts, collectionCopy, undefined, NestingDelimiter);
const all: TreeNode<CollectionView>[] = [];
for (const group of groupedByOrg.values()) {
const nodes: TreeNode<CollectionView>[] = [];
for (const c of group) {
const collectionCopy = Object.assign(new CollectionView({ ...c, name: c.name }), c);
const parts = c.name ? c.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter) : [];
ServiceUtils.nestedTraverse(nodes, 0, parts, collectionCopy, undefined, NestingDelimiter);
}
all.push(...nodes);
}
return all;
}
groupByOrganization(collections: CollectionView[]): Map<string, CollectionView[]> {
const groupedByOrg = new Map<string, CollectionView[]>();
collections.map((c) => {
const key = c.organizationId;
(groupedByOrg.get(key) ?? groupedByOrg.set(key, []).get(key)!).push(c);
});
return nodes;
return groupedByOrg;
}
/**