import type { RegisteredClient } from '@wireapp/api-client/src/client/' ; import type { CRUDEngine } from '@wireapp/store-engine' ; import { CryptographyService } from '../cryptography/' ; import type { MetaClient } from './ClientService' ; export enum DatabaseStores { CLIENTS = 'clients' } export class ClientDatabaseRepository { public static readonly STORES = DatabaseStores ; public static KEYS = { LOCAL_IDENTITY : 'local_identity' , } ; constructor ( private readonly storeEngine ) { } public getLocalClient ( ) { return this . getClient ( ClientDatabaseRepository . KEYS . LOCAL_IDENTITY ) ; } public getClient ( sessionId ) { return this . storeEngine . read < MetaClient > ( ClientDatabaseRepository . STORES . CLIENTS , sessionId ) ; } public deleteLocalClient ( ) { return this . storeEngine . delete ( ClientDatabaseRepository . STORES . CLIENTS , ClientDatabaseRepository . KEYS . LOCAL_IDENTITY ) ; } public deleteClient ( sessionId ) { return this . storeEngine . delete ( ClientDatabaseRepository . STORES . CLIENTS , sessionId ) ; } public createClientList ( userId , clientList , domain ) { const createClientTasks = [ ] ; for ( const client of clientList ) { createClientTasks . push ( this . createClient ( userId , client , domain ) ) ; } return Promise . all ( createClientTasks ) ; } public async createLocalClient ( client , domain ) { const transformedClient = this . transformLocalClient ( client , domain ) ; await this . storeEngine . create ( ClientDatabaseRepository . STORES . CLIENTS , ClientDatabaseRepository . KEYS . LOCAL_IDENTITY , transformedClient ) ; return transformedClient ; } public async updateLocalClient ( client , domain ) { const transformedClient = this . transformLocalClient ( client , domain ) ; await this . storeEngine . update ( ClientDatabaseRepository . STORES . CLIENTS , ClientDatabaseRepository . KEYS . LOCAL_IDENTITY , transformedClient ) ; return transformedClient ; } public async updateClient ( userId , client , domain ) { const transformedClient = this . transformClient ( userId , client , false , domain ) ; await this . storeEngine . update ( ClientDatabaseRepository . STORES . CLIENTS , CryptographyService . constructSessionId ( userId , client . id , domain ) , transformedClient ) ; return transformedClient ; } public async createClient ( userId , client , domain ) { const transformedClient = this . transformClient ( userId , client , false , domain ) ; await this . storeEngine . create ( ClientDatabaseRepository . STORES . CLIENTS , CryptographyService . constructSessionId ( userId , client . id , domain ) , transformedClient ) ; return transformedClient ; } private transformClient ( userId , client , verified , domain ) { return { ... client , domain , meta : { is_verified : verified , primary_key : CryptographyService . constructSessionId ( userId , client . id , domain ) } , } ; } private transformLocalClient ( client , domain ) { return { ... client , domain , meta : { is_verified : true , primary_key : ClientDatabaseRepository . KEYS . LOCAL_IDENTITY } , } ; } }