From 162d84e2d2b912c0106531432fc18af8193853ea Mon Sep 17 00:00:00 2001 From: Andy Phillipson Date: Tue, 7 Feb 2017 15:49:42 -0500 Subject: [PATCH] WIP, rebasing. --- src/js/controllers/bitpayCardIntro.js | 2 +- src/js/controllers/tab-settings.js | 4 +-- src/js/services/bitpayAccountService.js | 15 +++----- src/js/services/bitpayCardService.js | 2 +- src/js/services/storageService.js | 48 +++++++++++++------------ 5 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/js/controllers/bitpayCardIntro.js b/src/js/controllers/bitpayCardIntro.js index 8abe4dfeb..ca712a22b 100644 --- a/src/js/controllers/bitpayCardIntro.js +++ b/src/js/controllers/bitpayCardIntro.js @@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('bitpayCardIntroController', f return; } if (paired) { - bitpayCardService.sync(apiContext, function(err, cards) { + bitpayCardService.sync(apiContext, function(err, data) { if (err) { popupService.showAlert(gettextCatalog.getString('Error updating Debit Cards'), err); return; diff --git a/src/js/controllers/tab-settings.js b/src/js/controllers/tab-settings.js index ab42a38de..d032e8b72 100644 --- a/src/js/controllers/tab-settings.js +++ b/src/js/controllers/tab-settings.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabSettingsController', function($scope, appConfigService, $ionicModal, $log, lodash, uxLanguage, platformInfo, profileService, feeService, configService, externalLinkService, bitpayAccountService, bitpayCardService, storageService, glideraService, gettextCatalog) { +angular.module('copayApp.controllers').controller('tabSettingsController', function($scope, appConfigService, $ionicModal, $log, lodash, uxLanguage, platformInfo, profileService, feeService, configService, externalLinkService, bitpayAccountService, bitpayCardService, storageService, glideraService, gettextCatalog, buyAndSellService) { var updateConfig = function() { var isCordova = platformInfo.isCordova; @@ -34,7 +34,7 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct }); if ($scope.bitpayCardEnabled) { - bitpayCardService.getBitpayDebitCards(function(err, cards) { + bitpayCardService.getCards(function(err, cards) { if (err) $log.error(err); $scope.bitpayCards = cards && cards.length > 0; }); diff --git a/src/js/services/bitpayAccountService.js b/src/js/services/bitpayAccountService.js index 232bd17d8..5ddefb9f5 100644 --- a/src/js/services/bitpayAccountService.js +++ b/src/js/services/bitpayAccountService.js @@ -143,22 +143,18 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo var accountsArray = []; lodash.forEach(Object.keys(accounts), function(key) { - accounts[key].bitpayDebitCards = accounts[key]['bitpayDebitCards-' + bitpayService.getEnvironment().network]; + accounts[key].bitpayDebitCards = accounts[key].cards; accounts[key].email = key; - accounts[key].firstName = accounts[key]['basicInfo-' + bitpayService.getEnvironment().network].givenName; - accounts[key].lastName = accounts[key]['basicInfo-' + bitpayService.getEnvironment().network].familyName; + accounts[key].firstName = accounts[key].givenName || ''; + accounts[key].lastName = accounts[key].familyName || ''; accounts[key].apiContext = { - token: accounts[key]['bitpayApi-' + bitpayService.getEnvironment().network].token, + token: accounts[key].token, pairData: { email: key }, appIdentity: appIdentity }; - // Remove environment keyed attributes. - delete accounts[key]['bitpayApi-' + bitpayService.getEnvironment().network]; - delete accounts[key]['bitpayDebitCards-' + bitpayService.getEnvironment().network]; - accountsArray.push(accounts[key]); }); return cb(null, accountsArray); @@ -167,8 +163,7 @@ angular.module('copayApp.services').factory('bitpayAccountService', function($lo }; var setBitpayAccount = function(account, cb) { - var data = JSON.stringify(account); - storageService.setBitpayAccount(bitpayService.getEnvironment().network, data, function(err) { + storageService.setBitpayAccount(bitpayService.getEnvironment().network, account, function(err) { if (err) { return cb(err); } diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index f8b8b207c..0144ee0b3 100644 --- a/src/js/services/bitpayCardService.js +++ b/src/js/services/bitpayCardService.js @@ -52,7 +52,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log, cards: data.data.data, email: apiContext.pairData.email } - root.setBitpayDebitCards(cardData, function(err) { + storageService.setBitpayDebitCards(bitpayService.getEnvironment().network, cardData, function(err) { return cb(err, {token: apiContext.token, cards: data.data.data, email: apiContext.pairData.email}); }); }, function(data) { diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 8bbb57e15..fbcb99e50 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -356,6 +356,7 @@ angular.module('copayApp.services') storage.remove('balanceCache-' + cardId, cb); }; + // data: { // cards: [ // eid: card id // id: card id @@ -371,16 +372,17 @@ angular.module('copayApp.services') } data = data || {}; if (lodash.isEmpty(data) || !data.email) return cb('Cannot set cards: no account to set'); - storage.get('bitpayAccounts-v3-' + network, function(err, bitpayAccounts) { + + root.getBitpayAccounts(network, function(err, allAccounts) { if (err) return cb(err); - if (!allAccounts[email]) { - return cb('Cannot set cards for unknown account ' + email); + allAccounts = allAccounts || {}; + if (!allAccounts[data.email]) { + return cb('Cannot set cards for unknown account ' + data.email); } - bitpayAccounts = bitpayAccounts || {}; - bitpayAccounts[data.email] = bitpayAccounts[data.email] || {}; - bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data.cards; - storage.set('bitpayAccounts-v3-' + network, JSON.stringify(bitpayAccounts), cb); + allAccounts[data.email] = allAccounts[data.email] || {}; + allAccounts[data.email]['bitpayDebitCards-' + network] = data.cards; + storage.set('bitpayAccounts-v2-' + network, JSON.stringify(allAccounts), cb); }); }; @@ -400,13 +402,15 @@ angular.module('copayApp.services') lodash.each(allAccounts, function(account, email) { - // Add account's email to card list, for convenience - var cards = lodash.clone(account.cards); - lodash.each(cards, function(x) { - x.email = email; - }); + if (account.cards) { + // Add account's email to card list, for convenience + var cards = lodash.clone(account.cards); + lodash.each(cards, function(x) { + x.email = email; + }); - allCards = allCards.concat(cards); + allCards = allCards.concat(cards); + } }); return cb(null, allCards); @@ -494,25 +498,25 @@ angular.module('copayApp.services') // data: { // email: account email // token: account token + // familyName: account family (last) name + // givenName: account given (first) name // } root.setBitpayAccount = function(network, data, cb) { - if (!lodash.isObject(data) || !data.email || !data.token) return cb('No account to set'); - var email = data.email; - var token = data.token; - - root.getBitpayAccounts(network, function(err, allAccounts) { if (err) return cb(err); - var account = allAccounts[email] || {}; - account.token = token; + allAccounts = allAccounts || {}; + var account = allAccounts[data.email] || {}; + account.token = data.token; + account.familyName = data.familyName; + account.givenName = data.givenName; - allAccounts[email] = account; + allAccounts[data.email] = account; - $log.info('Storing BitPay accounts with new account:' + email); + $log.info('Storing BitPay accounts with new account:' + data.email); storage.set('bitpayAccounts-v2-' + network, allAccounts, cb); }); };