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

@ -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) {