-
Notifications
You must be signed in to change notification settings - Fork 68
/
accountPayments.js
142 lines (118 loc) · 3.36 KB
/
accountPayments.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
'use strict';
var Logger = require('../../lib/logger');
var log = new Logger({scope : 'account payments'});
var smoment = require('../../lib/smoment');
var utils = require('../../lib/utils');
var hbase = require('../../lib/hbase')
var types = ['sent', 'received'];
/**
* AccountPayments
*/
var AccountPayments = function (req, res, next) {
var options = prepareOptions();
if (options.error) {
errorResponse(options);
return;
} else {
log.info("get: " + options.account);
hbase.getAccountPayments(options, function(err, payments) {
if (err) {
errorResponse(err);
} else {
payments.rows.forEach(function(p) {
p.executed_time = smoment(p.executed_time).format();
p.transaction_cost = p.fee;
delete p.fee;
delete p.rowkey;
delete p.client;
});
successResponse(payments);
}
});
}
/**
* prepareOptions
*/
function prepareOptions() {
var options = {
account: req.params.address,
start: smoment(req.query.start || '2013-01-01'),
end: smoment(req.query.end),
type: req.query.type ? req.query.type.toLowerCase() : undefined,
destination_tag: req.query.destination_tag,
source_tag: req.query.source_tag,
currency: req.query.currency ? req.query.currency.toUpperCase() : undefined,
marker: req.query.marker,
descending: (/true/i).test(req.query.descending) ? true : false,
limit: Number(req.query.limit) || 200,
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.account) {
return {error: 'Account is required', code: 400};
} else if (options.type && types.indexOf(options.type) === -1) {
return {error: 'invalid 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 payments'
});
}
}
/**
* successResponse
* return a successful response
* @param {Object} payments
*/
function successResponse(payments) {
var filename = options.account + ' - payments';
var results = [ ];
if (payments.marker) {
utils.addLinkHeader(req, res, payments.marker);
}
if (options.format === 'csv') {
if (options.type) {
filename += ' ' + options.type;
}
if (options.currency) {
filename += ' ' + options.currency;
}
payments.rows.forEach(function(r) {
results.push(utils.flattenJSON(r));
});
res.csv(results, filename + '.csv');
} else {
res.json({
result: 'success',
count: payments.rows.length,
marker: payments.marker,
payments: payments.rows
});
}
}
};
module.exports = AccountPayments;