Wallet/src/js/controllers/bitpayCard.js

154 lines
4.7 KiB
JavaScript
Raw Normal View History

2016-08-10 15:29:31 -03:00
'use strict';
2017-06-07 18:55:43 -04:00
angular.module('copayApp.controllers').controller('bitpayCardController', function($scope, $timeout, $log, $state, lodash, bitpayCardService, moment, popupService, gettextCatalog, $ionicHistory, bitpayService, externalLinkService, timeService) {
2016-08-10 15:29:31 -03:00
var self = this;
2016-10-26 17:10:21 -03:00
$scope.dateRange = {
value: 'last30Days'
};
$scope.network = bitpayService.getEnvironment().network;
2016-09-28 21:09:41 -03:00
2016-08-10 15:29:31 -03:00
var setDateRange = function(preset) {
var startDate, endDate;
2016-10-26 17:10:21 -03:00
preset = preset ||  'last30Days';
switch (preset) {
2016-08-10 15:29:31 -03:00
case 'last30Days':
startDate = moment().subtract(30, 'days').toISOString();
endDate = moment().toISOString();
break;
case 'lastMonth':
startDate = moment().startOf('month').subtract(1, 'month').toISOString();
endDate = moment().startOf('month').toISOString();
break;
case 'all':
startDate = null;
endDate = null;
break;
default:
return;
}
return {
startDate: startDate,
endDate: endDate
};
};
2016-10-17 10:01:51 -03:00
var setGetStarted = function(history, cb) {
2017-01-31 14:24:13 -03:00
// Is the card new?
if (!lodash.isEmpty(history.transactionList))
2017-01-31 14:24:13 -03:00
return cb();
var dateRange = setDateRange('all');
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
if (!err && lodash.isEmpty(history.transactionList))
self.getStarted = true;
2017-01-31 14:24:13 -03:00
return cb();
});
};
2016-08-10 15:29:31 -03:00
this.update = function() {
var dateRange = setDateRange($scope.dateRange.value);
2016-08-10 15:29:31 -03:00
2016-10-06 19:23:39 -03:00
$scope.loadingHistory = true;
bitpayCardService.getHistory($scope.cardId, dateRange, function(err, history) {
2017-01-31 14:24:13 -03:00
2016-10-06 19:23:39 -03:00
$scope.loadingHistory = false;
2016-10-11 14:50:35 -03:00
if (err) {
$log.error(err);
self.bitpayCardTransactionHistoryCompleted = null;
self.bitpayCardTransactionHistoryConfirming = null;
self.bitpayCardTransactionHistoryPreAuth = null;
2017-01-31 14:24:13 -03:00
self.balance = null;
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions'));
2016-08-10 15:29:31 -03:00
return;
}
2017-01-31 14:24:13 -03:00
setGetStarted(history, function() {
2016-10-17 10:01:51 -03:00
var txs = lodash.clone(history.txs);
self.bitpayCardTransactionHistoryConfirming = bitpayCardService.filterTransactions('confirming', txs);
self.bitpayCardTransactionHistoryCompleted = bitpayCardService.filterTransactions('completed', txs);
self.bitpayCardTransactionHistoryPreAuth = bitpayCardService.filterTransactions('preAuth', txs);
2017-01-31 14:24:13 -03:00
self.balance = history.currentCardBalance;
self.updatedOn = null;
2016-10-17 10:01:51 -03:00
if ($scope.dateRange.value == 'last30Days') {
2017-01-27 17:54:41 -03:00
2017-01-31 14:24:13 -03:00
// TODO?
// $log.debug('BitPay Card: storing cache history');
2017-01-27 17:54:41 -03:00
// var cacheHistory = {
// balance: history.currentCardBalance,
// transactions: history.txs
// };
// bitpayCardService.setHistory($scope.cardId, cacheHistory, {}, function(err) {
// if (err) $log.error(err);
// $scope.historyCached = true;
// });
2016-10-17 10:01:51 -03:00
}
$timeout(function() {
$scope.$apply();
});
2016-10-07 13:51:55 -03:00
});
2016-08-10 15:29:31 -03:00
});
};
$scope.createdWithinPastDay = function(tx) {
2017-06-07 18:55:43 -04:00
var result = false;
if (tx.date) {
result = timeService.withinPastDay(tx.date);
2017-06-07 18:55:43 -04:00
}
return result;
};
this.openExternalLink = function(url) {
var optIn = true;
var title = null;
var message = gettextCatalog.getString('Help and support information is available at the website.');
var okText = gettextCatalog.getString('Open');
var cancelText = gettextCatalog.getString('Go Back');
externalLinkService.open(url, optIn, title, message, okText, cancelText);
};
this.viewOnBlockchain = function(transactionId) {
var url = 'https://insight.bitpay.com/tx/' + transactionId;
var optIn = true;
var title = null;
var message = gettextCatalog.getString('View Transaction on Insight');
var okText = gettextCatalog.getString('Open Insight');
var cancelText = gettextCatalog.getString('Go Back');
externalLinkService.open(url, optIn, title, message, okText, cancelText);
};
2016-10-06 19:23:39 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.cardId = data.stateParams.id;
2017-01-31 14:24:13 -03:00
2016-10-06 19:23:39 -03:00
if (!$scope.cardId) {
$ionicHistory.nextViewOptions({
disableAnimate: true
});
$state.go('tabs.home');
}
2016-09-28 11:08:08 -03:00
2017-01-31 14:24:13 -03:00
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.currencySymbol = cards[0].currencySymbol;
2017-01-31 14:24:13 -03:00
self.updatedOn = cards[0].updatedOn;
2017-06-05 12:09:48 -03:00
self.currency = cards[0].currency;
2017-01-31 14:24:13 -03:00
}
self.update();
});
});
2016-08-10 15:29:31 -03:00
});