diff --git a/frontend/src/app/models/queue/source-sync-message.ts b/frontend/src/app/models/queue/source-sync-message.ts index 7372c026..4e22e512 100644 --- a/frontend/src/app/models/queue/source-sync-message.ts +++ b/frontend/src/app/models/queue/source-sync-message.ts @@ -3,4 +3,5 @@ import {Source} from '../../../lib/models/database/source'; export class SourceSyncMessage { source: Source userIdentifier: string + encryptionKey?: string } diff --git a/frontend/src/app/pages/dashboard/dashboard.component.ts b/frontend/src/app/pages/dashboard/dashboard.component.ts index ff50a0c5..d481de79 100644 --- a/frontend/src/app/pages/dashboard/dashboard.component.ts +++ b/frontend/src/app/pages/dashboard/dashboard.component.ts @@ -54,7 +54,7 @@ export class DashboardComponent implements OnInit { let metadataSource = results[1] as { [name: string]: MetadataSource } //process - console.log(summary); + console.log("SUMMARY RESPONSE",summary); this.sources = summary.sources this.metadataSource = metadataSource this.metadataSource["manual"] = { diff --git a/frontend/src/app/services/fasten-db.service.ts b/frontend/src/app/services/fasten-db.service.ts index 1e242a9e..b6f1bdd6 100644 --- a/frontend/src/app/services/fasten-db.service.ts +++ b/frontend/src/app/services/fasten-db.service.ts @@ -30,7 +30,7 @@ export class FastenDbService extends PouchdbRepository { remotePouchEndpoint = "http://localhost:5984" constructor(private _httpClient: HttpClient) { const userIdentifier = localStorage.getItem("current_user") - super(userIdentifier); + super(userIdentifier, "my-secret-encryption-key"); if(userIdentifier){ this.enableSync(userIdentifier) } @@ -110,12 +110,16 @@ export class FastenDbService extends PouchdbRepository { .then((paginatedResp) => paginatedResp.rows) // summary.patients = [] - summary.patients = await this.GetDB().find({ - selector: { - doc_type: DocType.ResourceFhir, - source_resource_type: "Patient", - } - }).then((results) => results.docs) + summary.patients = await this.GetDB() + .then((db) => { + return db.find({ + selector: { + doc_type: DocType.ResourceFhir, + source_resource_type: "Patient", + } + }) + }) + .then((results) => results.docs) summary.resource_type_counts = await this.findDocumentByPrefix(`${DocType.ResourceFhir}`, false) .then((paginatedResp) => { diff --git a/frontend/src/app/workers/queue.service.ts b/frontend/src/app/workers/queue.service.ts index ffa32ee1..550e9c6c 100644 --- a/frontend/src/app/workers/queue.service.ts +++ b/frontend/src/app/workers/queue.service.ts @@ -16,6 +16,7 @@ export class QueueService { const sourceSync = new SourceSyncMessage() sourceSync.source = source sourceSync.userIdentifier = localStorage.getItem("current_user") + sourceSync.encryptionKey = "my-secret-encryption-key" const input$: Observable = of(JSON.stringify(sourceSync)); return fromWorker(() => new Worker(new URL('./source-sync.worker', import.meta.url), {type: 'module'}), input$) // .subscribe(message => { diff --git a/frontend/src/app/workers/source-sync.worker.ts b/frontend/src/app/workers/source-sync.worker.ts index bc689bff..4e9e03a3 100644 --- a/frontend/src/app/workers/source-sync.worker.ts +++ b/frontend/src/app/workers/source-sync.worker.ts @@ -17,7 +17,7 @@ export class SourceSyncWorker implements DoWork { console.log(msg); // outputs 'Hello from main thread' const sourceSyncMessage = JSON.parse(msg) as SourceSyncMessage - const db = NewRepositiory(sourceSyncMessage.userIdentifier) + const db = NewRepositiory(sourceSyncMessage.userIdentifier, sourceSyncMessage.encryptionKey) const client = NewClient(sourceSyncMessage.source.source_type, sourceSyncMessage.source) console.log("!!!!!!!!!!!!!!STARTING WORKER SYNC!!!!!!!!!", sourceSyncMessage) return client.SyncAll(db) diff --git a/frontend/src/lib/database/pouchdb_repository.ts b/frontend/src/lib/database/pouchdb_repository.ts index 48292ba1..2cea2947 100644 --- a/frontend/src/lib/database/pouchdb_repository.ts +++ b/frontend/src/lib/database/pouchdb_repository.ts @@ -27,21 +27,22 @@ PouchDB.plugin(PouchCrypto); * Eventually this method should dyanmically dtermine the version of the repo to return from the env. * @constructor */ -export function NewRepositiory(userIdentifier?: string): IDatabaseRepository { - return new PouchdbRepository(userIdentifier) +export function NewRepositiory(userIdentifier?: string, encryptionKey?: string): IDatabaseRepository { + return new PouchdbRepository(userIdentifier, encryptionKey) } export class PouchdbRepository implements IDatabaseRepository { - + encryptionKey: string localPouchDb: PouchDB.Database - constructor(userIdentifier?: string) { + constructor(userIdentifier?: string, encryptionKey?: string) { //setup PouchDB Plugins //https://pouchdb.com/guides/mango-queries.html this.localPouchDb = null if(userIdentifier){ this.localPouchDb = new PouchDB(userIdentifier); + this.encryptionKey = encryptionKey } } @@ -175,11 +176,22 @@ export class PouchdbRepository implements IDatabaseRepository { // Get the active PouchDB instance. Throws an error if no PouchDB instance is // available (ie, user has not yet been configured with call to .configureForUser()). - public GetDB(): any { + public async GetDB(): Promise { if(!this.localPouchDb) { throw(new Error( "Database is not available - please configure an instance." )); } - return this.localPouchDb; + // if(this.encryptionKey){ + // return this.localPouchDb.crypto(this.encryptionKey, {ignore:[ + // 'doc_type', + // 'source_id', + // 'source_resource_type', + // 'source_resource_id', + // ]}).then(() => { + // return this.localPouchDb + // }) + // } else { + return this.localPouchDb; + // } } // create a new document. Returns a promise of the generated id. @@ -189,8 +201,9 @@ export class PouchdbRepository implements IDatabaseRepository { // NOTE: All friends are given the key-prefix of "friend:". This way, when we go // to query for friends, we can limit the scope to keys with in this key-space. + return this.GetDB() - .put(doc) + .then((db) => db.put(doc)) .then(( result ): string => { return( result.id ); } @@ -200,7 +213,9 @@ export class PouchdbRepository implements IDatabaseRepository { // create multiple documents, returns a list of generated ids protected createBulk(docs: IDatabaseDocument[]): Promise { return this.GetDB() - .bulkDocs(docs.map((doc) => { doc.populateId(); return doc })) + .then((db) => { + return db.bulkDocs(docs.map((doc) => { doc.populateId(); return doc })) + }) .then((results): string[] => { return results.map((result) => result.id) }) @@ -208,7 +223,7 @@ export class PouchdbRepository implements IDatabaseRepository { protected getDocument(id: string): Promise { return this.GetDB() - .get(id) + .then((db) => db.get(id)) } @@ -217,17 +232,20 @@ export class PouchdbRepository implements IDatabaseRepository { } protected findDocumentByPrefix(prefix: string, includeDocs: boolean = true): Promise { return this.GetDB() - .allDocs({ - include_docs: includeDocs, - startkey: `${prefix}:`, - endkey: `${prefix}:\uffff` + .then((db) => { + return db.allDocs({ + include_docs: includeDocs, + startkey: `${prefix}:`, + endkey: `${prefix}:\uffff` + }) }) + } protected async deleteDocument(id: string): Promise { const docToDelete = await this.getDocument(id) return this.GetDB() - .remove(docToDelete) + .then((db) => db.remove(docToDelete)) .then((result) => { return result.ok })