fix bitpay card

This commit is contained in:
Matias Alejo Garcia 2017-01-31 14:24:13 -03:00
commit 1501d74558
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
10 changed files with 135 additions and 81 deletions

View file

@ -9,20 +9,6 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
}; };
$scope.network = bitpayService.getEnvironment().network; $scope.network = bitpayService.getEnvironment().network;
var updateHistoryFromCache = function(cb) {
// TODO no cache for now
$log.warn ('TODO: cache');
return cb();
bitpayCardService.getHistory($scope.cardId, function(err, data) {
if (err ||  lodash.isEmpty(data)) return cb();
$scope.historyCached = true;
self.bitpayCardTransactionHistory = data.transactions;
self.bitpayCardCurrentBalance = data.balance;
return cb();
});
};
var setDateRange = function(preset) { var setDateRange = function(preset) {
var startDate, endDate; var startDate, endDate;
preset = preset ||  'last30Days'; preset = preset ||  'last30Days';
@ -49,13 +35,19 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
}; };
var setGetStarted = function(history, cb) { var setGetStarted = function(history, cb) {
if (lodash.isEmpty(history.transactionList)) {
var dateRange = setDateRange('all'); // Is the card new?
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) { if (!lodash.isEmpty(history.transactionList))
if (lodash.isEmpty(history.transactionList)) return cb(true); return cb();
return cb(false);
}); var dateRange = setDateRange('all');
} else return cb(false); bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
if (!err && lodash.isEmpty(history.transactionList))
self.getStated=true;
return cb();
});
}; };
this.update = function() { this.update = function() {
@ -63,18 +55,18 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.loadingHistory = true; $scope.loadingHistory = true;
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) { bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
$scope.loadingHistory = false; $scope.loadingHistory = false;
if (err) { if (err) {
$log.error(err); $log.error(err);
self.bitpayCardTransactionHistory = null; self.bitpayCardTransactionHistory = null;
self.bitpayCardCurrentBalance = null; self.balance = null;
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions')); popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions'));
return; return;
} }
setGetStarted(history, function(getStarted) { setGetStarted(history, function() {
self.getStarted = getStarted;
var txs = lodash.clone(history.txs); var txs = lodash.clone(history.txs);
runningBalance = parseFloat(history.endingBalance); runningBalance = parseFloat(history.endingBalance);
@ -87,13 +79,13 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
_runningBalance(txs[i]); _runningBalance(txs[i]);
} }
self.bitpayCardTransactionHistory = txs; self.bitpayCardTransactionHistory = txs;
self.bitpayCardCurrentBalance = history.currentCardBalance; self.balance = history.currentCardBalance;
self.updatedOn = null;
if ($scope.dateRange.value == 'last30Days') { if ($scope.dateRange.value == 'last30Days') {
// TODO CACHE // TODO?
// // $log.debug('BitPay Card: storing cache history');
// $log.debug('BitPay Card: store cache history');
// var cacheHistory = { // var cacheHistory = {
// balance: history.currentCardBalance, // balance: history.currentCardBalance,
// transactions: history.txs // transactions: history.txs
@ -143,24 +135,25 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.$on("$ionicView.beforeEnter", function(event, data) { $scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.cardId = data.stateParams.id; $scope.cardId = data.stateParams.id;
if (!$scope.cardId) { if (!$scope.cardId) {
var msg = gettextCatalog.getString('Bad param');
$ionicHistory.nextViewOptions({ $ionicHistory.nextViewOptions({
disableAnimate: true disableAnimate: true
}); });
$state.go('tabs.home'); $state.go('tabs.home');
popupService.showAlert(gettextCatalog.getString('Error'), msg);
} else {
updateHistoryFromCache(function() {
self.update();
});
bitpayCardService.getCards(function(err, cards) {
if (err) return;
$scope.card = lodash.find(cards, function(card) {
return card.eid == $scope.cardId;
});
});
} }
});
bitpayCardService.get({
cardId: $scope.cardId,
noRefresh: true,
}, function(err, cards) {
if (cards && cards[0]) {
self.lastFourDigits = cards[0].lastFourDigits;
self.balance = cards[0].balance;
self.updatedOn = cards[0].updatedOn;
}
self.update();
});
});
}); });

View file

@ -98,7 +98,7 @@ angular.module('copayApp.controllers').controller('tabHomeController',
$scope.buyAndSellItems = buyAndSellService.getLinked(); $scope.buyAndSellItems = buyAndSellService.getLinked();
$scope.homeIntegrations = homeIntegrationsService.get(); $scope.homeIntegrations = homeIntegrationsService.get();
bitpayCardService.get(function(err, cards) { bitpayCardService.get({}, function(err, cards) {
$scope.bitpayCardItems = cards; $scope.bitpayCardItems = cards;
}); });
@ -210,6 +210,9 @@ angular.module('copayApp.controllers').controller('tabHomeController',
} else { } else {
wallet.error = null; wallet.error = null;
wallet.status = status; wallet.status = status;
// TODO service refactor? not in profile service
profileService.setLastKnownBalance(wallet.id, wallet.status.totalBalanceStr, function() {});
} }
if (++j == i) { if (++j == i) {
updateTxps(); updateTxps();

View file

@ -25,6 +25,7 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct
isoCode: config.wallet.settings.alternativeIsoCode isoCode: config.wallet.settings.alternativeIsoCode
}; };
// TODO Move this to a generic service, like buyAndSell.
$scope.bitpayCardEnabled = config.bitpayCard.enabled; $scope.bitpayCardEnabled = config.bitpayCard.enabled;
if ($scope.bitpayCardEnabled) { if ($scope.bitpayCardEnabled) {

View file

@ -115,11 +115,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
transactions = data.data.data || {}; transactions = data.data.data || {};
transactions['txs'] = _processTransactions(invoices, transactions.transactionList); transactions['txs'] = _processTransactions(invoices, transactions.transactionList);
// TODO CACHE? root.setLastKnownBalance(cardId, transactions.currentCardBalance, function() {});
// update cache?
// if (lodash.isEmpty(opts)) {
// root.setHistoryCache(cardId, transactions, function() {});
// }
return cb(data.data.error, transactions); return cb(data.data.error, transactions);
}, function(data) { }, function(data) {
@ -179,15 +175,32 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, cb); storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, cb);
}; };
// TODO?? root.getLastKnownBalance = function(cardId, cb) {
// root.getHistoryCache = function(cardId, cb) { storageService.getBalanceCache(cardId, cb);
// storageService.getBitpayDebitCardHistory(cardId, cb); };
// };
// root.addLastKnownBalance = function(card, cb) {
// root.setHistoryCache = function(cardId, data, cb) { var now = Math.floor(Date.now()/1000);
// storageService.setBitpayDebitCardHistory(cardId, data, cb); var showRange = 600 ; // 10min;
// };
// root.getLastKnownBalance(card.eid, function(err, data){
if (data) {
data = JSON.parse(data);
card.balance = data.balance;
card.updatedOn = ( data.updatedOn < now - showRange) ? data.updatedOn : null;
}
return cb();
});
};
root.setLastKnownBalance = function(cardId, balance, cb) {
storageService.setBalanceCache(cardId, {
balance: balance,
updatedOn: Math.floor(Date.now()/1000),
}, cb);
};
root.remove = function(cardId, cb) { root.remove = function(cardId, cb) {
storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, cardId, function(err) { storageService.removeBitpayDebitCard(bitpayService.getEnvironment().network, cardId, function(err) {
@ -211,25 +224,33 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
}; };
root.get = function(cb) { root.get = function(opts, cb) {
root.getCards(function(err, cards) { root.getCards(function(err, cards) {
if (err) return; if (err) return;
if (lodash.isEmpty(cards)) { if (lodash.isEmpty(cards)) {
return cb(); return cb();
} }
// TODO
// bitpayCardService.getCardsHistoryCache(function(err, data) { // Async, no problem
// if (err) return; lodash.each(cards, function(x){
// if (lodash.isEmpty(data)) {
// $scope.cardsHistory = null; if (opts.cardId) {
// return; if (opts.cardId != x.eid) return;
// } }
// $scope.cardsHistory = data;
// }); root.addLastKnownBalance(x, function() {});
if (!opts.noRefresh) {
root.getHistory(x.id, {}, function(err, data) {
if (err) return;
root.addLastKnownBalance(x, function() {});
});
}
});
return cb(null, cards); return cb(null, cards);
}); });
}; };
/* /*
@ -1282,8 +1303,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
if (lodash.isEmpty(cards)) { if (lodash.isEmpty(cards)) {
nextStepsService.register(nextStepItem); nextStepsService.register(nextStepItem);
} else { } else {
nextStepsService.unregister(nextStepItem); nextStepsService.unregister(nextStepItem.name);
// homeIntegrationsService.register(homeItem);
} }
}); });
}; };

View file

@ -44,7 +44,7 @@ angular.module('copayApp.services')
body = gettextCatalog.getString('Insufficient funds'); body = gettextCatalog.getString('Insufficient funds');
break; break;
case 'CONNECTION_ERROR': case 'CONNECTION_ERROR':
body = gettextCatalog.getString('Network connection error'); body = gettextCatalog.getString('Network error');
break; break;
case 'NOT_FOUND': case 'NOT_FOUND':
body = gettextCatalog.getString('Wallet service not found'); body = gettextCatalog.getString('Wallet service not found');

View file

@ -747,6 +747,31 @@ angular.module('copayApp.services')
storageService.storeProfile(root.profile, cb); storageService.storeProfile(root.profile, cb);
}; };
root.getLastKnownBalance = function(wid, cb) {
storageService.getBalanceCache(wid, cb);
};
root.addLastKnownBalance = function(wallet, cb) {
var now = Math.floor(Date.now() / 1000);
var showRange = 600; // 10min;
root.getLastKnownBalance(wallet.id, function(err, data) {
if (data) {
data = JSON.parse(data);
wallet.cachedBalance = data.balance;
wallet.cachedBalanceUpdatedOn = (data.updatedOn < now - showRange) ? data.updatedOn : null;
}
return cb();
});
};
root.setLastKnownBalance = function(wid, balance, cb) {
storageService.setBalanceCache(wid, {
balance: balance,
updatedOn: Math.floor(Date.now() / 1000),
}, cb);
};
root.getWallets = function(opts) { root.getWallets = function(opts) {
if (opts && !lodash.isObject(opts)) if (opts && !lodash.isObject(opts))
@ -780,6 +805,12 @@ angular.module('copayApp.services')
}); });
} else {} } else {}
// Add cached balance async
lodash.each(ret, function(x) {
root.addLastKnownBalance(x, function() {});
});
return lodash.sortBy(ret, [ return lodash.sortBy(ret, [
function(x) { function(x) {
@ -796,7 +827,7 @@ angular.module('copayApp.services')
root.getNotifications = function(opts, cb) { root.getNotifications = function(opts, cb) {
opts = opts || {}; opts = opts || {};
var TIME_STAMP = 60 * 60 * 6; var TIME_STAMP = 60 * 60 * 6;
var MAX = 30; var MAX = 30;
var typeFilter = { var typeFilter = {

View file

@ -344,12 +344,12 @@ angular.module('copayApp.services')
storage.remove('coinbaseTxs-' + network, cb); storage.remove('coinbaseTxs-' + network, cb);
}; };
root.setBitpayDebitCardHistory = function(cardId, data, cb) { root.setBalanceCache = function(cardId, data, cb) {
storage.set('bitpayDebitCardHistory-' + cardId, data, cb); storage.set('bitpayDebitCardBalance-' + cardId, data, cb);
}; };
root.getBitpayDebitCardHistory = function(cardId, cb) { root.getBalanceCache = function(cardId, cb) {
storage.get('bitpayDebitCardHistory-' + cardId, cb); storage.get('bitpayDebitCardBalance-' + cardId, cb);
}; };
root.removeBitpayDebitCardHistory = function(cardId, cb) { root.removeBitpayDebitCardHistory = function(cardId, cb) {

View file

@ -2,7 +2,7 @@
<ion-nav-bar class="bar-royal"> <ion-nav-bar class="bar-royal">
<ion-nav-back-button> <ion-nav-back-button>
</ion-nav-back-button> </ion-nav-back-button>
<ion-nav-title>BitPay Visa<sup>&reg;</sup> Card ({{card.lastFourDigits}})</ion-nav-title> <ion-nav-title>BitPay Visa<sup>&reg;</sup> Card ({{bitpayCard.lastFourDigits}})</ion-nav-title>
</ion-nav-bar> </ion-nav-bar>
<ion-content> <ion-content>
@ -14,8 +14,13 @@
<div class="amount-wrapper" ng-show="!error"> <div class="amount-wrapper" ng-show="!error">
<div class="amount-bg"></div> <div class="amount-bg"></div>
<div class="amount"> <div class="amount">
<div ng-if="bitpayCard.bitpayCardCurrentBalance" ng-click="bitpayCard.update()"> <div ng-if="bitpayCard.balance" ng-click="bitpayCard.update()">
<div class="amount__balance">${{bitpayCard.bitpayCardCurrentBalance}}</div> <div class="amount__balance">${{bitpayCard.balance}}</div>
<div class="size-12 m5r" ng-if="bitpayCard.updatedOn">
{{bitpayCard.updatedOn * 1000 | amTimeAgo}}
</div>
<a class="button button-primary button-small m5t size-14" <a class="button button-primary button-small m5t size-14"
style="padding: 0.5em 1em;" style="padding: 0.5em 1em;"
ui-sref="tabs.bitpayCard.amount({'cardId': cardId, 'toName': 'BitPay Card'})"> ui-sref="tabs.bitpayCard.amount({'cardId': cardId, 'toName': 'BitPay Card'})">
@ -23,7 +28,7 @@
{{'Add Funds'|translate}} {{'Add Funds'|translate}}
</a> </a>
</div> </div>
<div ng-if="!bitpayCard.bitpayCardCurrentBalance" class="m10t"> <div ng-if="!bitpayCard.balance" class="m10t">
<strong class="size-36">...</strong> <strong class="size-36">...</strong>
</div> </div>
</div> </div>

View file

@ -12,7 +12,7 @@
<div class="bg icon-bitpay-card"></div> <div class="bg icon-bitpay-card"></div>
</i> </i>
<span>BitPay Visa&reg; Card ({{card.lastFourDigits}})</span> <span>BitPay Visa&reg; Card ({{card.lastFourDigits}})</span>
<p>{{card.balance ? '$' + card.balance : 'Add funds to get started'|translate}}</p> <p>{{card.balance ? '$' + card.balance : 'Add funds to get started'|translate}} {{card.updatedOn ? (' | ' + (card.updatedOn * 1000 | amTimeAgo)) : ''}}</p>
<i class="icon bp-arrow-right"></i> <i class="icon bp-arrow-right"></i>
</a> </a>
</div> </div>

View file

@ -81,7 +81,8 @@
Incomplete Incomplete
</span> </span>
<span ng-if="wallet.isComplete()"> <span ng-if="wallet.isComplete()">
<span ng-if="!wallet.balanceHidden">{{wallet.status.totalBalanceStr}}</span> <span ng-if="!wallet.balanceHidden"> {{wallet.status.totalBalanceStr ? wallet.status.totalBalanceStr : ( wallet.cachedBalance ? wallet.cachedBalance + ' | ' + (wallet.cachedBalanceUpdatedOn ? ( wallet.cachedBalanceUpdatedOn * 1000 | amTimeAgo) : '') : '' ) }} </span>
<span ng-if="wallet.balanceHidden" translate>[Balance Hidden]</span> <span ng-if="wallet.balanceHidden" translate>[Balance Hidden]</span>
<span class="tab-home__wallet__multisig-number" ng-if="wallet.n > 1"> <span class="tab-home__wallet__multisig-number" ng-if="wallet.n > 1">
{{wallet.m}}-of-{{wallet.n}} {{wallet.m}}-of-{{wallet.n}}