-
Notifications
You must be signed in to change notification settings - Fork 68
/
getAccount.js
77 lines (66 loc) · 1.68 KB
/
getAccount.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
'use strict';
var Logger = require('../../lib/logger');
var log = new Logger({scope: 'accounts'});
var smoment = require('../../lib/smoment');
var hbase = require('../../lib/hbase')
/**
* getAccount
*/
var getAccount = function(req, res, next) {
var 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 get accounts'
});
}
}
/**
* successResponse
* return a successful response
* @param {Object} resp
*/
function successResponse(resp) {
var result = {
result: 'success',
account_data: {
account: resp.rows[0].account,
parent: resp.rows[0].parent,
initial_balance: resp.rows[0].balance,
inception: smoment(resp.rows[0].executed_time).format(),
ledger_index: resp.rows[0].ledger_index,
tx_hash: resp.rows[0].tx_hash,
genesis_balance: resp.rows[0].genesis_balance,
genesis_index: resp.rows[0].genesis_index
}
};
res.json(result);
}
options = {
account: req.params.address,
limit: 1,
descending: false // query will be faster
};
hbase.getAccounts(options, function(err, resp) {
if (err) {
errorResponse(err);
} else if (!resp || !resp.rows || !resp.rows.length) {
errorResponse({error: 'Account not found', code: 404});
} else {
successResponse(resp);
}
});
};
module.exports = getAccount