REF: get user invoices

This commit is contained in:
Overtorment 2020-03-27 21:03:38 +00:00
parent b58e7dac88
commit d7e91f51ec
2 changed files with 14 additions and 14 deletions

View File

@ -234,8 +234,11 @@ export class User {
return await this._redis.get('ispaid_' + payment_hash);
}
async getUserInvoices() {
async getUserInvoices(limit) {
let range = await this._redis.lrange('userinvoices_for_' + this._userid, 0, -1);
if (limit && !isNaN(parseInt(limit))) {
range = range.slice(parseInt(limit) * -1);
}
let result = [];
for (let invoice of range) {
invoice = JSON.parse(invoice);

View File

@ -269,13 +269,14 @@ router.get('/checkpayment/:payment_hash', async function(req, res) {
});
router.get('/balance', postLimiter, async function(req, res) {
try {
logger.log('/balance', [req.id]);
let u = new User(redis, bitcoinclient, lightning);
if (!(await u.loadByAuthorization(req.headers.authorization))) {
return errorBadAuth(res);
}
logger.log('/balance', [req.id, 'userid: ' + u.getUserId()]);
try {
if (!(await u.getAddress())) await u.generateAddress(); // onchain address needed further
await u.accountForPosibleTxids();
let balance = await u.getBalance();
@ -338,12 +339,8 @@ router.get('/getuserinvoices', postLimiter, async function(req, res) {
logger.log('/getuserinvoices', [req.id, 'userid: ' + u.getUserId()]);
try {
let invoices = await u.getUserInvoices();
if (req.query.limit && !isNaN(parseInt(req.query.limit))) {
res.send(invoices.slice(parseInt(req.query.limit) * -1));
} else {
let invoices = await u.getUserInvoices(req.query.limit);
res.send(invoices);
}
} catch (Err) {
logger.log('', [req.id, 'error getting user invoices:', Err.message, 'userid:', u.getUserId()]);
res.send([]);