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 f213a1bd05..77de8984dc 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,19 @@ import { CollectionData, Collection, CollectionView } from "../models"; export abstract class CollectionvNextService { encryptedCollections$: (userId$: Observable) => Observable; decryptedCollections$: (userId$: Observable) => Observable; - + upsert: (collection: CollectionData | CollectionData[]) => Promise; + replace: (collections: { [id: string]: CollectionData }, userId: UserId) => Promise; + /** + * Clear decrypted state without affecting encrypted state. + * Used for locking the vault. + */ + clearDecryptedState: (userId: UserId) => Promise; + /** + * Clear decrypted and encrypted state. + * Used for logging out. + */ + clear: (userId?: string) => Promise; + delete: (id: string | string[]) => Promise; encrypt: (model: CollectionView) => Promise; /** * @deprecated This method will soon be made private, use `decryptedCollections$` instead. @@ -26,17 +38,4 @@ export abstract class CollectionvNextService { * Transforms the input CollectionViews into TreeNodes and then returns the Treenode with the specified id */ getNested: (collections: CollectionView[], id: string) => TreeNode; - upsert: (collection: CollectionData | CollectionData[]) => Promise; - replace: (collections: { [id: string]: CollectionData }, userId: UserId) => Promise; - /** - * Clear decrypted state without affecting encrypted state. - * Used for locking the vault. - */ - clearDecryptedState: (userId: UserId) => Promise; - /** - * Clear decrypted and encrypted state. - * Used for logging out. - */ - clear: (userId?: string) => Promise; - delete: (id: string | string[]) => Promise; } 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 d7f4285f2b..cddee799d3 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,6 +73,64 @@ export class DefaultCollectionvNextService implements CollectionvNextService { return userId$.pipe(switchMap((userId) => this.decryptedCollectionState(userId).state$)); } + async upsert(toUpdate: CollectionData | CollectionData[]): Promise { + if (toUpdate == null) { + return; + } + await this.activeUserEncryptedCollectionDataState.update((collections) => { + if (collections == null) { + collections = {}; + } + if (Array.isArray(toUpdate)) { + toUpdate.forEach((c) => { + collections[c.id] = c; + }); + } else { + collections[toUpdate.id] = toUpdate; + } + return collections; + }); + } + + async replace(collections: Record, userId: UserId): Promise { + await this.stateProvider + .getUser(userId, ENCRYPTED_COLLECTION_DATA_KEY) + .update(() => collections); + } + + async clearDecryptedState(userId: UserId): Promise { + if (userId == null) { + throw new Error("User ID is required."); + } + + await this.decryptedCollectionState(userId).forceValue(null); + } + + async clear(userId?: UserId): Promise { + if (userId == null) { + await this.activeUserEncryptedCollectionDataState.update(() => null); + await this.activeUserDecryptedCollectionDataState.forceValue(null); + } else { + await this.stateProvider.getUser(userId, ENCRYPTED_COLLECTION_DATA_KEY).update(() => null); + } + } + + async delete(id: CollectionId | CollectionId[]): Promise { + await this.activeUserEncryptedCollectionDataState.update((collections) => { + if (collections == null) { + collections = {}; + } + if (typeof id === "string") { + delete collections[id]; + } else { + (id as CollectionId[]).forEach((i) => { + delete collections[i]; + }); + } + return collections; + }); + } + async encrypt(model: CollectionView): Promise { if (model.organizationId == null) { throw new Error("Collection has no organization id."); @@ -139,64 +197,6 @@ export class DefaultCollectionvNextService implements CollectionvNextService { ) as TreeNode; } - async upsert(toUpdate: CollectionData | CollectionData[]): Promise { - if (toUpdate == null) { - return; - } - await this.activeUserEncryptedCollectionDataState.update((collections) => { - if (collections == null) { - collections = {}; - } - if (Array.isArray(toUpdate)) { - toUpdate.forEach((c) => { - collections[c.id] = c; - }); - } else { - collections[toUpdate.id] = toUpdate; - } - return collections; - }); - } - - async replace(collections: Record, userId: UserId): Promise { - await this.stateProvider - .getUser(userId, ENCRYPTED_COLLECTION_DATA_KEY) - .update(() => collections); - } - - async clearDecryptedState(userId: UserId): Promise { - if (userId == null) { - throw new Error("User ID is required."); - } - - await this.decryptedCollectionState(userId).forceValue(null); - } - - async clear(userId?: UserId): Promise { - if (userId == null) { - await this.activeUserEncryptedCollectionDataState.update(() => null); - await this.activeUserDecryptedCollectionDataState.forceValue(null); - } else { - await this.stateProvider.getUser(userId, ENCRYPTED_COLLECTION_DATA_KEY).update(() => null); - } - } - - async delete(id: CollectionId | CollectionId[]): Promise { - await this.activeUserEncryptedCollectionDataState.update((collections) => { - if (collections == null) { - collections = {}; - } - if (typeof id === "string") { - delete collections[id]; - } else { - (id as CollectionId[]).forEach((i) => { - delete collections[i]; - }); - } - return collections; - }); - } - /** * @returns a SingleUserState for encrypted collection data. */