2016-08-10 15:29:31 -03:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2016-10-12 09:38:41 -03:00
|
|
|
|
angular.module('copayApp.controllers').controller('bitpayCardController', function($scope, $timeout, $log, $state, lodash, bitpayCardService, moment, popupService, gettextCatalog, $ionicHistory) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
|
|
|
|
|
var self = this;
|
2016-10-21 20:33:28 -03:00
|
|
|
|
var runningBalance;
|
2016-10-13 17:49:48 -03:00
|
|
|
|
$scope.dateRange = { value: 'last30Days'};
|
2016-10-11 14:50:35 -03:00
|
|
|
|
$scope.network = bitpayCardService.getEnvironment();
|
2016-09-28 21:09:41 -03:00
|
|
|
|
|
2016-10-17 10:01:51 -03:00
|
|
|
|
var updateHistoryFromCache = function(cb) {
|
2016-10-07 13:51:55 -03:00
|
|
|
|
bitpayCardService.getBitpayDebitCardsHistory($scope.cardId, function(err, data) {
|
2016-10-17 10:01:51 -03:00
|
|
|
|
if (err || lodash.isEmpty(data)) return cb();
|
2016-10-07 13:51:55 -03:00
|
|
|
|
$scope.historyCached = true;
|
|
|
|
|
|
self.bitpayCardTransactionHistory = data.transactions;
|
|
|
|
|
|
self.bitpayCardCurrentBalance = data.balance;
|
2016-10-17 10:01:51 -03:00
|
|
|
|
return cb();
|
2016-10-07 13:51:55 -03:00
|
|
|
|
});
|
|
|
|
|
|
};
|
2016-08-10 15:29:31 -03:00
|
|
|
|
|
|
|
|
|
|
var setDateRange = function(preset) {
|
|
|
|
|
|
var startDate, endDate;
|
|
|
|
|
|
preset = preset || 'last30Days';
|
|
|
|
|
|
switch(preset) {
|
|
|
|
|
|
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) {
|
|
|
|
|
|
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);
|
2016-10-16 21:22:54 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
this.update = function() {
|
2016-10-13 17:49:48 -03:00
|
|
|
|
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) {
|
|
|
|
|
|
$scope.loadingHistory = false;
|
2016-10-16 21:22:54 -03:00
|
|
|
|
|
2016-10-11 14:50:35 -03:00
|
|
|
|
if (err) {
|
|
|
|
|
|
$log.error(err);
|
2016-10-06 19:23:39 -03:00
|
|
|
|
$scope.error = gettextCatalog.getString('Could not get transactions');
|
2016-08-10 15:29:31 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-17 10:01:51 -03:00
|
|
|
|
setGetStarted(history, function(getStarted) {
|
|
|
|
|
|
self.getStarted = getStarted;
|
|
|
|
|
|
|
|
|
|
|
|
var txs = lodash.clone(history.txs);
|
2016-10-21 20:33:28 -03:00
|
|
|
|
runningBalance = parseFloat(history.endingBalance);
|
2016-10-17 10:01:51 -03:00
|
|
|
|
for (var i = 0; i < txs.length; i++) {
|
|
|
|
|
|
txs[i] = _getMerchantInfo(txs[i]);
|
|
|
|
|
|
txs[i].icon = _getIconName(txs[i]);
|
|
|
|
|
|
txs[i].desc = _processDescription(txs[i]);
|
2016-10-21 20:33:28 -03:00
|
|
|
|
txs[i].price = _price(txs[i]);
|
|
|
|
|
|
txs[i].runningBalance = runningBalance;
|
|
|
|
|
|
_runningBalance(txs[i]);
|
2016-10-17 10:01:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
self.bitpayCardTransactionHistory = txs;
|
|
|
|
|
|
self.bitpayCardCurrentBalance = history.currentCardBalance;
|
|
|
|
|
|
|
|
|
|
|
|
if ($scope.dateRange.value == 'last30Days') {
|
|
|
|
|
|
$log.debug('BitPay Card: store cache history');
|
|
|
|
|
|
var cacheHistory = {
|
|
|
|
|
|
balance: history.currentCardBalance,
|
|
|
|
|
|
transactions: history.txs
|
|
|
|
|
|
};
|
|
|
|
|
|
bitpayCardService.setBitpayDebitCardsHistory($scope.cardId, cacheHistory, {}, function(err) {
|
|
|
|
|
|
if (err) $log.error(err);
|
|
|
|
|
|
$scope.historyCached = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$apply();
|
2016-10-13 17:49:48 -03:00
|
|
|
|
});
|
2016-10-07 13:51:55 -03:00
|
|
|
|
});
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var _getMerchantInfo = function(tx) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var bpTranCodes = bitpayCardService.bpTranCodes;
|
|
|
|
|
|
lodash.keys(bpTranCodes).forEach(function(code) {
|
|
|
|
|
|
if (tx.type.indexOf(code) === 0) {
|
|
|
|
|
|
lodash.assign(tx, bpTranCodes[code]);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2016-10-13 17:49:48 -03:00
|
|
|
|
return tx;
|
2016-08-10 15:29:31 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var _getIconName = function(tx) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
var icon = tx.mcc || tx.category || null;
|
|
|
|
|
|
if (!icon) return 'default';
|
|
|
|
|
|
return bitpayCardService.iconMap[icon];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-13 17:49:48 -03:00
|
|
|
|
var _processDescription = function(tx) {
|
2016-08-10 15:29:31 -03:00
|
|
|
|
if (lodash.isArray(tx.description)) {
|
|
|
|
|
|
return tx.description[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
return tx.description;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-21 20:33:28 -03:00
|
|
|
|
var _price = function(tx) {
|
|
|
|
|
|
return parseFloat(tx.amount) + parseFloat(tx.fee)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var _runningBalance = function(tx) {
|
|
|
|
|
|
runningBalance -= parseFloat(tx.amount);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-10-06 19:23:39 -03:00
|
|
|
|
$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(null, msg);
|
|
|
|
|
|
} else {
|
2016-10-17 10:01:51 -03:00
|
|
|
|
updateHistoryFromCache(function() {
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
});
|
2016-10-06 19:23:39 -03:00
|
|
|
|
}
|
2016-09-28 11:08:08 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
2016-08-10 15:29:31 -03:00
|
|
|
|
});
|
|
|
|
|
|
|