diff --git a/libs/admin-console/src/common/collections/abstractions/collection-vNext.service.ts b/libs/admin-console/src/common/collections/abstractions/collection-vNext.service.ts index 77de8984dc..e4636a3564 100644 --- a/libs/admin-console/src/common/collections/abstractions/collection-vNext.service.ts +++ b/libs/admin-console/src/common/collections/abstractions/collection-vNext.service.ts @@ -9,7 +9,7 @@ import { CollectionData, Collection, CollectionView } from "../models"; export abstract class CollectionvNextService { encryptedCollections$: (userId$: Observable) => Observable; decryptedCollections$: (userId$: Observable) => Observable; - upsert: (collection: CollectionData | CollectionData[]) => Promise; + upsert: (collection: CollectionData | CollectionData[], userId: UserId) => Promise; replace: (collections: { [id: string]: CollectionData }, userId: UserId) => Promise; /** * Clear decrypted state without affecting encrypted state. diff --git a/libs/admin-console/src/common/collections/services/default-collection-vNext.service.spec.ts b/libs/admin-console/src/common/collections/services/default-collection-vNext.service.spec.ts index 5325b58c07..2505d8cc07 100644 --- a/libs/admin-console/src/common/collections/services/default-collection-vNext.service.spec.ts +++ b/libs/admin-console/src/common/collections/services/default-collection-vNext.service.spec.ts @@ -167,14 +167,74 @@ describe("DefaultCollectionService", () => { it("handles null collection state", async () => { await stateProvider.setUserState(ENCRYPTED_COLLECTION_DATA_KEY, null, userId); - cryptoKeys.next({}); const decryptedCollections = await firstValueFrom( - collectionService.decryptedCollections$(of(userId)), + collectionService.encryptedCollections$(of(userId)), ); expect(decryptedCollections.length).toBe(0); }); }); + + describe("upsert", () => { + it("upserts to existing collections", async () => { + const org1 = Utils.newGuid() as OrganizationId; + const collection1 = collectionDataFactory(org1); + + const org2 = Utils.newGuid() as OrganizationId; + const collection2 = collectionDataFactory(org2); + + await stateProvider.setUserState( + ENCRYPTED_COLLECTION_DATA_KEY, + { + [collection1.id]: collection1, + [collection2.id]: collection2, + }, + userId, + ); + + const updatedCollection1 = Object.assign(new CollectionData({} as any), collection1, { + name: makeEncString("UPDATED_ENC_NAME_" + collection1.id).encryptedString, + }); + const newCollection3 = collectionDataFactory(org2); + + await collectionService.upsert([updatedCollection1, newCollection3], userId); + + const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId))); + expect(result.length).toBe(3); + expect(result).toIncludeAllPartialMembers([ + { + id: collection1.id, + name: makeEncString("UPDATED_ENC_NAME_" + collection1.id), + }, + { + id: collection2.id, + name: makeEncString("ENC_NAME_" + collection2.id), + }, + { + id: newCollection3.id, + name: makeEncString("ENC_NAME_" + newCollection3.id), + }, + ]); + }); + + it("upserts to a null state", async () => { + const org1 = Utils.newGuid() as OrganizationId; + const collection1 = collectionDataFactory(org1); + + await stateProvider.setUserState(ENCRYPTED_COLLECTION_DATA_KEY, null, userId); + + await collectionService.upsert(collection1, userId); + + const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId))); + expect(result.length).toBe(1); + expect(result).toIncludeAllPartialMembers([ + { + id: collection1.id, + name: makeEncString("ENC_NAME_" + collection1.id), + }, + ]); + }); + }); }); const collectionDataFactory = (orgId: OrganizationId) => { diff --git a/libs/admin-console/src/common/collections/services/default-collection-vNext.service.ts b/libs/admin-console/src/common/collections/services/default-collection-vNext.service.ts index cddee799d3..0ec2a386ba 100644 --- a/libs/admin-console/src/common/collections/services/default-collection-vNext.service.ts +++ b/libs/admin-console/src/common/collections/services/default-collection-vNext.service.ts @@ -73,11 +73,11 @@ export class DefaultCollectionvNextService implements CollectionvNextService { return userId$.pipe(switchMap((userId) => this.decryptedCollectionState(userId).state$)); } - async upsert(toUpdate: CollectionData | CollectionData[]): Promise { + async upsert(toUpdate: CollectionData | CollectionData[], userId: UserId): Promise { if (toUpdate == null) { return; } - await this.activeUserEncryptedCollectionDataState.update((collections) => { + await this.encryptedCollectionState(userId).update((collections) => { if (collections == null) { collections = {}; }