-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathaccountBalanceChanges.js
153 lines (126 loc) · 3.49 KB
/
accountBalanceChanges.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
var Logger = require('../../lib/logger');
var log = new Logger({scope : 'account balance changes'});
var smoment = require('../../lib/smoment');
var utils = require('../../lib/utils');
var hbase = require('../../lib/hbase')
var types = [
'transaction_cost',
'exchange',
'intermediary',
'payment_source',
'payment_destination'
];
/**
* AccountBalanceChanges
*/
var AcccountBalanceChanges = function(req, res) {
var options = prepareOptions();
if (options.error) {
errorResponse(options);
return;
}
log.info(options.account);
hbase.getAccountBalanceChanges(options, function(err, changes) {
if (err) {
errorResponse(err);
} else {
changes.rows.forEach(function(ex) {
delete ex.rowkey;
delete ex.client;
delete ex.account;
ex.executed_time = smoment(ex.executed_time).format();
if (ex.change_type === 'fee' ||
ex.change_type === 'network fee') {
ex.change_type = 'transaction_cost';
}
});
successResponse(changes);
}
});
function prepareOptions() {
var options = {
account: req.params.address,
counterparty: req.query.counterparty || req.query.issuer,
currency: req.query.currency,
type: req.query.change_type || req.query.type,
limit: req.query.limit,
start: smoment(req.query.start || '2013-01-01'),
end: smoment(req.query.end),
marker: req.query.marker,
descending: (/true/i).test(req.query.descending) ? true : false,
format: (req.query.format || 'json').toLowerCase()
}
if (!options.start) {
return {error: 'invalid start date format', code: 400};
} else if (!options.end) {
return {error: 'invalid end date format', code: 400};
}
if (options.counterparty &&
options.currency &&
options.currency.toUpperCase() === 'XRP') {
return {
error: 'counterparty cannot be specified for XRP',
code: 400
};
}
if (options.type && options.type === 'transaction_cost') {
options.type = 'fee';
} else if (options.type && types.indexOf(options.type) === -1) {
return {
error: 'invalid change_type - use: ' + types.join(', '),
code: 400
};
}
if (isNaN(options.limit)) {
options.limit = 200;
} else if (options.limit > 1000) {
options.limit = 1000;
}
return options;
}
/**
* errorResponse
* return an error response
* @param {Object} err
*/
function errorResponse(err) {
log.error(err.error || err);
if (err.code && err.code.toString()[0] === '4') {
res.status(err.code).json({
result: 'error',
message: err.error
});
} else {
res.status(500).json({
result: 'error',
message: 'unable to retrieve balance changes'
});
}
}
/**
* successResponse
* return a successful response
* @param {Object} balance changes
*/
function successResponse(changes) {
var filename = options.account + ' - balance changes';
if (changes.marker) {
utils.addLinkHeader(req, res, changes.marker);
}
if (options.format === 'csv') {
if (options.currency) {
filename += ' ' + options.currency;
}
res.csv(changes.rows, filename + '.csv');
} else {
res.json({
result: 'success',
count: changes.rows.length,
marker: changes.marker,
balance_changes: changes.rows
});
}
}
};
module.exports = AcccountBalanceChanges