diff --git a/frontend/src/app/pages/encryption-manager/encryption-manager.component.ts b/frontend/src/app/pages/encryption-manager/encryption-manager.component.ts index 77149ebc..f9aed249 100644 --- a/frontend/src/app/pages/encryption-manager/encryption-manager.component.ts +++ b/frontend/src/app/pages/encryption-manager/encryption-manager.component.ts @@ -109,7 +109,19 @@ export class EncryptionManagerComponent implements OnInit { //redirect user to dashboard return this.router.navigate(['/dashboard']); }) - .catch(console.error) + .catch((err) => { + // delete invalid encryption key + this.currentStep = 1 + return PouchdbCrypto.DeleteCryptConfig(this.fastenDbService.current_user) + .then(() => { + //an error occurred while importing credential + const toastNotification = new ToastNotification() + toastNotification.type = ToastType.Error + toastNotification.message = "Provided encryption key does not match. Generating new encryption key, please store it and try again." + toastNotification.autohide = false + this.toastService.show(toastNotification) + }) + }) } ///////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/frontend/src/app/services/fasten-db.service.ts b/frontend/src/app/services/fasten-db.service.ts index 214f653c..bb2cc738 100644 --- a/frontend/src/app/services/fasten-db.service.ts +++ b/frontend/src/app/services/fasten-db.service.ts @@ -149,14 +149,17 @@ export class FastenDbService extends PouchdbRepository { // summary.patients = [] summary.patients = await this.GetDB() .then((db) => { + return db.find({ selector: { doc_type: DocType.ResourceFhir, source_resource_type: "Patient", } - }) + }).then((results) => { + return Promise.all((results.docs || []).map((doc) => PouchdbCrypto.decryptDocument(db, doc))) + }) }) - .then((results) => results.docs) + summary.resource_type_counts = await this.findDocumentByPrefix(`${DocType.ResourceFhir}`, false) .then((paginatedResp) => { diff --git a/frontend/src/lib/database/plugins/crypto.ts b/frontend/src/lib/database/plugins/crypto.ts index 813c881c..b081c471 100644 --- a/frontend/src/lib/database/plugins/crypto.ts +++ b/frontend/src/lib/database/plugins/crypto.ts @@ -136,19 +136,23 @@ export class PouchdbCrypto { return encrypted }, outgoing: async (doc) => { - // if no crypt, ex: after .removeCrypto(), just return the doc - if (!db._crypt) { return doc } - let decryptedString = await db._crypt.decrypt(doc.payload) - let decrypted = JSON.parse(decryptedString) - for (let key of db._ignore) { - // patch decrypted doc with ignored fields - if (key in doc) decrypted[key] = doc[key] - } - return decrypted + return await this.decryptDocument(db, doc) } }) return db } + public static async decryptDocument(db, doc):Promise{ + // if no crypt, ex: after .removeCrypto(), just return the doc + if (!db._crypt) { return doc } + let decryptedString = await db._crypt.decrypt(doc.payload) + let decrypted = JSON.parse(decryptedString) + for (let key of db._ignore) { + // patch decrypted doc with ignored fields + if (key in doc) decrypted[key] = doc[key] + } + return decrypted + } + public static removeCrypto(db) { delete db._crypt }