mv CSV generation from index.js to preferencesHistory.js
This commit is contained in:
parent
1d5b8ced99
commit
9709d6b64a
5 changed files with 176 additions and 158 deletions
|
|
@ -125,12 +125,12 @@
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li class="p10 oh" ng-show="comment">
|
<li class="p10 oh" ng-show="btx.note">
|
||||||
<span class="text-gray" translate>Comment</span>
|
<span class="text-gray" translate>Comment</span>
|
||||||
<span class="right enable_text_select">{{comment}}</span><br>
|
<span class="right enable_text_select">{{btx.note.body}}</span><br>
|
||||||
<span class="right text-italic text-gray size-12 m10t">
|
<span class="right text-italic text-gray size-12 m10t">
|
||||||
<span>{{editedBy}}</span>
|
<span>{{btx.note.editedByName}}</span>
|
||||||
<time>{{createdOn * 1000 | amTimeAgo}}</time></span>
|
<time>{{btx.note.editedOn * 1000 | amTimeAgo}}</time></span>
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,22 @@
|
||||||
<div class="content preferences" ng-controller="preferencesHistory as history">
|
<div class="content preferences" ng-controller="preferencesHistory as history">
|
||||||
<h4></h4>
|
<h4></h4>
|
||||||
<ul class="no-bullet m0">
|
<ul class="no-bullet m0">
|
||||||
<li ng-if="!index.isCordova" ng-init="index.csvHistory()">
|
<li ng-if="!index.isCordova" ng-init="history.csvHistory()">
|
||||||
<a ng-style="{'color':index.backgroundColor}" ng-csv="index.csvContent"
|
<a style="color:#444"
|
||||||
csv-header="index.csvHeader"
|
ng-csv="history.csvContent"
|
||||||
filename="{{index.csvFilename }}" translate>
|
csv-header="history.csvHeader"
|
||||||
|
ng-show="history.csvReady"
|
||||||
|
filename="Copay-{{index.alias || index.walletName}}.csv" translate>
|
||||||
Export to file
|
Export to file
|
||||||
</a>
|
</a>
|
||||||
|
<a style="color:#777"
|
||||||
|
ng-show="!history.csvReady"
|
||||||
|
translate>
|
||||||
|
Export to file [preparing...]
|
||||||
|
</a>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
<li ng-style="{'color':index.backgroundColor}" ng-click="history.clearTransactionHistory();" translate>
|
<li ng-click="history.clearTransactionHistory();" translate>
|
||||||
Clear cache
|
Clear cache
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
||||||
|
|
@ -692,135 +692,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
self.csvHistory = function() {
|
|
||||||
|
|
||||||
function formatDate(date) {
|
|
||||||
var dateObj = new Date(date);
|
|
||||||
if (!dateObj) {
|
|
||||||
$log.debug('Error formating a date');
|
|
||||||
return 'DateError'
|
|
||||||
}
|
|
||||||
if (!dateObj.toJSON()) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return dateObj.toJSON();
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatString(str) {
|
|
||||||
if (!str) return '';
|
|
||||||
|
|
||||||
if (str.indexOf('"') !== -1) {
|
|
||||||
//replace all
|
|
||||||
str = str.replace(new RegExp('"', 'g'), '\'');
|
|
||||||
}
|
|
||||||
|
|
||||||
//escaping commas
|
|
||||||
str = '\"' + str + '\"';
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
var step = 6;
|
|
||||||
var unique = {};
|
|
||||||
|
|
||||||
function getHistory(cb) {
|
|
||||||
storageService.getTxHistory(c.walletId, function(err, txs) {
|
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
var txsFromLocal = [];
|
|
||||||
try {
|
|
||||||
txsFromLocal = JSON.parse(txs);
|
|
||||||
} catch (ex) {
|
|
||||||
$log.warn(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
allTxs.push(txsFromLocal);
|
|
||||||
return cb(null, lodash.flatten(allTxs));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isCordova) {
|
|
||||||
$log.info('CSV generation not available in mobile');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var fc = profileService.focusedClient;
|
|
||||||
var c = fc.credentials;
|
|
||||||
if (!fc.isComplete()) return;
|
|
||||||
var self = this;
|
|
||||||
var allTxs = [];
|
|
||||||
|
|
||||||
$log.debug('Generating CSV from History');
|
|
||||||
self.setOngoingProcess('generatingCSV', true);
|
|
||||||
|
|
||||||
getHistory(function(err, txs) {
|
|
||||||
self.setOngoingProcess('generatingCSV', false);
|
|
||||||
if (err) {
|
|
||||||
self.handleError(err);
|
|
||||||
} else {
|
|
||||||
$log.debug('Wallet Transaction History:', txs);
|
|
||||||
|
|
||||||
self.satToUnit = 1 / self.unitToSatoshi;
|
|
||||||
var data = txs;
|
|
||||||
var satToBtc = 1 / 100000000;
|
|
||||||
self.csvContent = [];
|
|
||||||
self.csvFilename = 'Copay-' + (self.alias || self.walletName) + '.csv';
|
|
||||||
self.csvHeader = ['Date', 'Destination', 'Description', 'Amount', 'Currency', 'Txid', 'Creator', 'Copayers'];
|
|
||||||
|
|
||||||
var _amount, _note, _copayers, _creator;
|
|
||||||
data.forEach(function(it, index) {
|
|
||||||
var amount = it.amount;
|
|
||||||
|
|
||||||
if (it.action == 'moved')
|
|
||||||
amount = 0;
|
|
||||||
|
|
||||||
_copayers = '';
|
|
||||||
_creator = '';
|
|
||||||
|
|
||||||
if (it.actions && it.actions.length > 1) {
|
|
||||||
for (var i = 0; i < it.actions.length; i++) {
|
|
||||||
_copayers += it.actions[i].copayerName + ':' + it.actions[i].type + ' - ';
|
|
||||||
}
|
|
||||||
_creator = (it.creatorName && it.creatorName != 'undefined') ? it.creatorName : '';
|
|
||||||
}
|
|
||||||
_copayers = formatString(_copayers);
|
|
||||||
_creator = formatString(_creator);
|
|
||||||
_amount = (it.action == 'sent' ? '-' : '') + (amount * satToBtc).toFixed(8);
|
|
||||||
_note = formatString((it.message ? it.message : ''));
|
|
||||||
|
|
||||||
if (it.action == 'moved')
|
|
||||||
_note += ' Moved:' + (it.amount * satToBtc).toFixed(8)
|
|
||||||
|
|
||||||
self.csvContent.push({
|
|
||||||
'Date': formatDate(it.time * 1000),
|
|
||||||
'Destination': formatString(it.addressTo),
|
|
||||||
'Description': _note,
|
|
||||||
'Amount': _amount,
|
|
||||||
'Currency': 'BTC',
|
|
||||||
'Txid': it.txid,
|
|
||||||
'Creator': _creator,
|
|
||||||
'Copayers': _copayers
|
|
||||||
});
|
|
||||||
|
|
||||||
if (it.fees && (it.action == 'moved' || it.action == 'sent')) {
|
|
||||||
var _fee = (it.fees * satToBtc).toFixed(8)
|
|
||||||
self.csvContent.push({
|
|
||||||
'Date': formatDate(it.time * 1000),
|
|
||||||
'Destination': 'Bitcoin Network Fees',
|
|
||||||
'Description': '',
|
|
||||||
'Amount': '-' + _fee,
|
|
||||||
'Currency': 'BTC',
|
|
||||||
'Txid': '',
|
|
||||||
'Creator': '',
|
|
||||||
'Copayers': ''
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
self.removeAndMarkSoftConfirmedTx = function(txs) {
|
self.removeAndMarkSoftConfirmedTx = function(txs) {
|
||||||
return lodash.filter(txs, function(tx) {
|
return lodash.filter(txs, function(tx) {
|
||||||
if (tx.confirmations >= SOFT_CONFIRMATION_LIMIT)
|
if (tx.confirmations >= SOFT_CONFIRMATION_LIMIT)
|
||||||
|
|
|
||||||
|
|
@ -13,20 +13,21 @@ angular.module('copayApp.controllers').controller('txDetailsController', functio
|
||||||
$scope.copayerId = fc.credentials.copayerId;
|
$scope.copayerId = fc.credentials.copayerId;
|
||||||
$scope.isShared = fc.credentials.n > 1;
|
$scope.isShared = fc.credentials.n > 1;
|
||||||
|
|
||||||
if ($scope.btx.txid) {
|
console.log('[txDetails.js.16:btx:]',$scope.btx); //TODO
|
||||||
fc.getTxNote({
|
// if ($scope.btx.txid) {
|
||||||
txid: $scope.btx.txid
|
// fc.getTxNote({
|
||||||
}, function(err, note) {
|
// txid: $scope.btx.txid
|
||||||
if (err || !note) {
|
// }, function(err, note) {
|
||||||
$log.debug(gettextCatalog.getString('Could not fetch transaction note'));
|
// if (err || !note) {
|
||||||
return;
|
// $log.debug(gettextCatalog.getString('Could not fetch transaction note'));
|
||||||
}
|
// return;
|
||||||
$scope.comment = note.body;
|
// }
|
||||||
$scope.editedBy = gettextCatalog.getString('Edited by') + ' ' + note.editedByName;
|
// $scope.comment = note.body;
|
||||||
$scope.createdOn = note.createdOn;
|
// $scope.editedBy = gettextCatalog.getString('Edited by') + ' ' + note.editedByName;
|
||||||
});
|
// $scope.createdOn = note.createdOn;
|
||||||
}
|
// });
|
||||||
|
// }
|
||||||
|
//
|
||||||
$scope.showCommentPopup = function() {
|
$scope.showCommentPopup = function() {
|
||||||
$scope.data = {
|
$scope.data = {
|
||||||
comment: ''
|
comment: ''
|
||||||
|
|
@ -45,11 +46,16 @@ angular.module('copayApp.controllers').controller('txDetailsController', functio
|
||||||
fc.editTxNote({
|
fc.editTxNote({
|
||||||
txid: $scope.btx.txid,
|
txid: $scope.btx.txid,
|
||||||
body: $scope.data.comment
|
body: $scope.data.comment
|
||||||
}, function() {});
|
}, function(err) {
|
||||||
$scope.comment = $scope.data.comment;
|
if (err) {
|
||||||
$scope.editedBy = gettextCatalog.getString('Edited by') + ' ' + fc.credentials.copayerName;
|
$log.debug('Could not save tx comment');
|
||||||
$scope.createdOn = Math.floor(Date.now() / 1000);
|
return;
|
||||||
commentPopup.close();
|
}
|
||||||
|
$scope.comment = $scope.data.comment;
|
||||||
|
$scope.editedBy = gettextCatalog.getString('Edited by') + ' ' + fc.credentials.copayerName;
|
||||||
|
$scope.createdOn = Math.floor(Date.now() / 1000);
|
||||||
|
commentPopup.close();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,142 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('preferencesHistory',
|
angular.module('copayApp.controllers').controller('preferencesHistory',
|
||||||
function($scope, $log, $timeout, storageService, go, profileService) {
|
function($scope, $log, $timeout, storageService, go, profileService, platformInfo, lodash) {
|
||||||
var fc = profileService.focusedClient;
|
var fc = profileService.focusedClient;
|
||||||
var c = fc.credentials;
|
var c = fc.credentials;
|
||||||
|
var isCordova = platformInfo.isCordova;
|
||||||
|
this.csvReady = false;
|
||||||
|
|
||||||
|
|
||||||
|
this.csvHistory = function() {
|
||||||
|
if (isCordova) return;
|
||||||
|
|
||||||
|
function formatDate(date) {
|
||||||
|
var dateObj = new Date(date);
|
||||||
|
if (!dateObj) {
|
||||||
|
$log.debug('Error formating a date');
|
||||||
|
return 'DateError'
|
||||||
|
}
|
||||||
|
if (!dateObj.toJSON()) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return dateObj.toJSON();
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatString(str) {
|
||||||
|
console.log('[index.js.710:str:]', str); //TODO
|
||||||
|
if (!str) return '';
|
||||||
|
|
||||||
|
if (str.indexOf('"') !== -1) {
|
||||||
|
//replace all
|
||||||
|
str = str.replace(new RegExp('"', 'g'), '\'');
|
||||||
|
}
|
||||||
|
|
||||||
|
//escaping commas
|
||||||
|
str = '\"' + str + '\"';
|
||||||
|
console.log('[index.js.720:str:]', str); //TODO
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
var step = 6;
|
||||||
|
var unique = {};
|
||||||
|
|
||||||
|
function getHistory(cb) {
|
||||||
|
storageService.getTxHistory(c.walletId, function(err, txs) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
var txsFromLocal = [];
|
||||||
|
try {
|
||||||
|
txsFromLocal = JSON.parse(txs);
|
||||||
|
} catch (ex) {
|
||||||
|
$log.warn(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
allTxs.push(txsFromLocal);
|
||||||
|
return cb(null, lodash.flatten(allTxs));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var fc = profileService.focusedClient;
|
||||||
|
var c = fc.credentials;
|
||||||
|
if (!fc.isComplete()) return;
|
||||||
|
var self = this;
|
||||||
|
var allTxs = [];
|
||||||
|
|
||||||
|
$log.debug('Generating CSV from History');
|
||||||
|
|
||||||
|
getHistory(function(err, txs) {
|
||||||
|
if (err || !txs || !txs[0]) {
|
||||||
|
$log.warn('Failed to generate CSV:', err);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
$log.debug('Wallet Transaction History Length:', txs.length);
|
||||||
|
|
||||||
|
self.satToUnit = 1 / self.unitToSatoshi;
|
||||||
|
var data = txs;
|
||||||
|
var satToBtc = 1 / 100000000;
|
||||||
|
self.csvContent = [];
|
||||||
|
self.csvFilename = 'Copay-' + (self.alias || self.walletName) + '.csv';
|
||||||
|
self.csvHeader = ['Date', 'Destination', 'Description', 'Amount', 'Currency', 'Txid', 'Creator', 'Copayers'];
|
||||||
|
|
||||||
|
var _amount, _note, _copayers, _creator, _comment;
|
||||||
|
data.forEach(function(it, index) {
|
||||||
|
var amount = it.amount;
|
||||||
|
|
||||||
|
if (it.action == 'moved')
|
||||||
|
amount = 0;
|
||||||
|
|
||||||
|
_copayers = '';
|
||||||
|
_creator = '';
|
||||||
|
|
||||||
|
if (it.actions && it.actions.length > 1) {
|
||||||
|
for (var i = 0; i < it.actions.length; i++) {
|
||||||
|
_copayers += it.actions[i].copayerName + ':' + it.actions[i].type + ' - ';
|
||||||
|
}
|
||||||
|
_creator = (it.creatorName && it.creatorName != 'undefined') ? it.creatorName : '';
|
||||||
|
}
|
||||||
|
_amount = (it.action == 'sent' ? '-' : '') + (amount * satToBtc).toFixed(8);
|
||||||
|
_note = it.message || '';
|
||||||
|
_comment = it.note ? it.note.body : '';
|
||||||
|
|
||||||
|
if (it.action == 'moved')
|
||||||
|
_note += ' Moved:' + (it.amount * satToBtc).toFixed(8)
|
||||||
|
|
||||||
|
self.csvContent.push({
|
||||||
|
'Date': formatDate(it.time * 1000),
|
||||||
|
'Destination': formatString(it.addressTo),
|
||||||
|
'Description': _note,
|
||||||
|
'Amount': _amount,
|
||||||
|
'Currency': 'BTC',
|
||||||
|
'Txid': it.txid,
|
||||||
|
'Creator': _creator,
|
||||||
|
'Copayers': _copayers,
|
||||||
|
'Comment': _comment
|
||||||
|
});
|
||||||
|
|
||||||
|
if (it.fees && (it.action == 'moved' || it.action == 'sent')) {
|
||||||
|
var _fee = (it.fees * satToBtc).toFixed(8)
|
||||||
|
self.csvContent.push({
|
||||||
|
'Date': formatDate(it.time * 1000),
|
||||||
|
'Destination': 'Bitcoin Network Fees',
|
||||||
|
'Description': '',
|
||||||
|
'Amount': '-' + _fee,
|
||||||
|
'Currency': 'BTC',
|
||||||
|
'Txid': '',
|
||||||
|
'Creator': '',
|
||||||
|
'Copayers': ''
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.csvReady = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
this.clearTransactionHistory = function() {
|
this.clearTransactionHistory = function() {
|
||||||
storageService.removeTxHistory(c.walletId, function(err) {
|
storageService.removeTxHistory(c.walletId, function(err) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue