From 1ce4b5443bd840070b9416bb1e6f254ef6ff04c2 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 26 Apr 2017 11:59:31 -0400 Subject: [PATCH] bug fixes around fetching keys for org sync --- src/services/syncService.js | 60 ++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/services/syncService.js b/src/services/syncService.js index 4a98c8aeae..a3566652de 100644 --- a/src/services/syncService.js +++ b/src/services/syncService.js @@ -84,35 +84,41 @@ function initSyncService() { var deferred = Q.defer(); var self = this; + function setKeys(hasPrivateKey, response, d) { + if (response.organizations && response.organizations.length && !hasPrivateKey) { + self.apiService.getKeys(function (keysResponse) { + if (keysResponse.privateKey) { + self.cryptoService.setEncPrivateKey(keysResponse.privateKey).then(function () { + return self.cryptoService.setOrgKeys(response.organizations); + }, function () { + d.reject(); + }).then(function () { + d.resolve(); + }, function () { + d.reject(); + }); + } + else { + d.resolve(); + } + }, function () { + d.reject(); + }); + } + else { + self.cryptoService.setOrgKeys(response.organizations).then(function () { + d.resolve(); + }, function () { + d.reject(); + }); + } + } + self.apiService.getProfile(function (response) { self.cryptoService.getPrivateKey().then(function (privateKey) { - if (response.organizations && !privateKey) { - self.apiService.getKeys(function (keysResponse) { - if (keysResponse.privateKey) { - self.cryptoService.setEncPrivateKey(keysResponse.privateKey).then(function () { - return self.cryptoService.setOrgKeys(response.organizations); - }, function () { - deferred.reject(); - }).then(function () { - deferred.resolve(); - }, function () { - deferred.reject(); - }); - } - else { - deferred.resolve(); - } - }, function () { - deferred.reject(); - }); - } - else { - self.cryptoService.setOrgKeys(response.organizations).then(function () { - deferred.resolve(); - }, function () { - deferred.reject(); - }); - } + setKeys(!!privateKey, response, deferred); + }, function () { + setKeys(false, response, deferred); }); }, function () { deferred.reject();