diff --git a/libs/admin-console/src/common/collections/services/default-collection.service.spec.ts b/libs/admin-console/src/common/collections/services/default-collection.service.spec.ts index a0332cfb501..4fe2ac67863 100644 --- a/libs/admin-console/src/common/collections/services/default-collection.service.spec.ts +++ b/libs/admin-console/src/common/collections/services/default-collection.service.spec.ts @@ -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, diff --git a/libs/admin-console/src/common/collections/services/default-collection.service.ts b/libs/admin-console/src/common/collections/services/default-collection.service.ts index 39b3b491862..d3b4ebe1f34 100644 --- a/libs/admin-console/src/common/collections/services/default-collection.service.ts +++ b/libs/admin-console/src/common/collections/services/default-collection.service.ts @@ -213,15 +213,30 @@ export class DefaultCollectionService implements CollectionService { ); } + // Transforms the input CollectionViews into TreeNodes getAllNested(collections: CollectionView[]): TreeNode[] { - const nodes: TreeNode[] = []; - 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[] = []; + for (const group of groupedByOrg.values()) { + const nodes: TreeNode[] = []; + 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 { + const groupedByOrg = new Map(); + collections.map((c) => { + const key = c.organizationId; + (groupedByOrg.get(key) ?? groupedByOrg.set(key, []).get(key)!).push(c); }); - return nodes; + return groupedByOrg; } /**