fix bitpay card

This commit is contained in:
Matias Alejo Garcia 2017-01-31 14:24:13 -03:00
parent 24c2d735cc
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;
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 startDate, endDate;
preset = preset ||  'last30Days';
@ -49,13 +35,19 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
};
var setGetStarted = function(history, cb) {
if (lodash.isEmpty(history.transactionList)) {
var dateRange = setDateRange('all');
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
if (lodash.isEmpty(history.transactionList)) return cb(true);
return cb(false);
});
} else return cb(false);
// Is the card new?
if (!lodash.isEmpty(history.transactionList))
return cb();
var dateRange = setDateRange('all');
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
if (!err && lodash.isEmpty(history.transactionList))
self.getStated=true;
return cb();
});
};
this.update = function() {
@ -63,18 +55,18 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.loadingHistory = true;
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
$scope.loadingHistory = false;
if (err) {
$log.error(err);
self.bitpayCardTransactionHistory = null;
self.bitpayCardCurrentBalance = null;
self.balance = null;
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions'));
return;
}
setGetStarted(history, function(getStarted) {
self.getStarted = getStarted;
setGetStarted(history, function() {
var txs = lodash.clone(history.txs);
runningBalance = parseFloat(history.endingBalance);
@ -87,13 +79,13 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
_runningBalance(txs[i]);
}
self.bitpayCardTransactionHistory = txs;
self.bitpayCardCurrentBalance = history.currentCardBalance;
self.balance = history.currentCardBalance;
self.updatedOn = null;
if ($scope.dateRange.value == 'last30Days') {
// TODO CACHE
//
// $log.debug('BitPay Card: store cache history');
// TODO?
// $log.debug('BitPay Card: storing cache history');
// var cacheHistory = {
// balance: history.currentCardBalance,
// transactions: history.txs
@ -143,24 +135,25 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.cardId = data.stateParams.id;
if (!$scope.cardId) {
var msg = gettextCatalog.getString('Bad param');
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$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.homeIntegrations = homeIntegrationsService.get();
bitpayCardService.get(function(err, cards) {
bitpayCardService.get({}, function(err, cards) {
$scope.bitpayCardItems = cards;
});
@ -210,6 +210,9 @@ angular.module('copayApp.controllers').controller('tabHomeController',
} else {
wallet.error = null;
wallet.status = status;
// TODO service refactor? not in profile service
profileService.setLastKnownBalance(wallet.id, wallet.status.totalBalanceStr, function() {});
}
if (++j == i) {
updateTxps();

View File

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

View File

@ -115,11 +115,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
transactions = data.data.data || {};
transactions['txs'] = _processTransactions(invoices, transactions.transactionList);
// TODO CACHE?
// update cache?
// if (lodash.isEmpty(opts)) {
// root.setHistoryCache(cardId, transactions, function() {});
// }
root.setLastKnownBalance(cardId, transactions.currentCardBalance, function() {});
return cb(data.data.error, transactions);
}, function(data) {
@ -179,15 +175,32 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
storageService.getBitpayDebitCards(bitpayService.getEnvironment().network, cb);
};
// TODO??
// root.getHistoryCache = function(cardId, cb) {
// storageService.getBitpayDebitCardHistory(cardId, cb);
// };
//
// root.setHistoryCache = function(cardId, data, cb) {
// storageService.setBitpayDebitCardHistory(cardId, data, cb);
// };
//
root.getLastKnownBalance = function(cardId, cb) {
storageService.getBalanceCache(cardId, cb);
};
root.addLastKnownBalance = function(card, cb) {
var now = Math.floor(Date.now()/1000);
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) {
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) {
if (err) return;
if (lodash.isEmpty(cards)) {
return cb();
}
// TODO
// bitpayCardService.getCardsHistoryCache(function(err, data) {
// if (err) return;
// if (lodash.isEmpty(data)) {
// $scope.cardsHistory = null;
// return;
// }
// $scope.cardsHistory = data;
// });
// Async, no problem
lodash.each(cards, function(x){
if (opts.cardId) {
if (opts.cardId != x.eid) return;
}
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);
});
};
/*
@ -1282,8 +1303,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($log,
if (lodash.isEmpty(cards)) {
nextStepsService.register(nextStepItem);
} else {
nextStepsService.unregister(nextStepItem);
// homeIntegrationsService.register(homeItem);
nextStepsService.unregister(nextStepItem.name);
}
});
};

View File

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

View File

@ -747,6 +747,31 @@ angular.module('copayApp.services')
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) {
if (opts && !lodash.isObject(opts))
@ -780,6 +805,12 @@ angular.module('copayApp.services')
});
} else {}
// Add cached balance async
lodash.each(ret, function(x) {
root.addLastKnownBalance(x, function() {});
});
return lodash.sortBy(ret, [
function(x) {
@ -796,7 +827,7 @@ angular.module('copayApp.services')
root.getNotifications = function(opts, cb) {
opts = opts || {};
var TIME_STAMP = 60 * 60 * 6;
var TIME_STAMP = 60 * 60 * 6;
var MAX = 30;
var typeFilter = {

View File

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

View File

@ -2,7 +2,7 @@
<ion-nav-bar class="bar-royal">
<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-content>
@ -14,8 +14,13 @@
<div class="amount-wrapper" ng-show="!error">
<div class="amount-bg"></div>
<div class="amount">
<div ng-if="bitpayCard.bitpayCardCurrentBalance" ng-click="bitpayCard.update()">
<div class="amount__balance">${{bitpayCard.bitpayCardCurrentBalance}}</div>
<div ng-if="bitpayCard.balance" ng-click="bitpayCard.update()">
<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"
style="padding: 0.5em 1em;"
ui-sref="tabs.bitpayCard.amount({'cardId': cardId, 'toName': 'BitPay Card'})">
@ -23,7 +28,7 @@
{{'Add Funds'|translate}}
</a>
</div>
<div ng-if="!bitpayCard.bitpayCardCurrentBalance" class="m10t">
<div ng-if="!bitpayCard.balance" class="m10t">
<strong class="size-36">...</strong>
</div>
</div>

View File

@ -12,7 +12,7 @@
<div class="bg icon-bitpay-card"></div>
</i>
<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>
</a>
</div>

View File

@ -81,7 +81,8 @@
Incomplete
</span>
<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 class="tab-home__wallet__multisig-number" ng-if="wallet.n > 1">
{{wallet.m}}-of-{{wallet.n}}