Link channels from Transaction page.

This commit is contained in:
softsimon
2022-05-07 11:32:15 +04:00
parent 31d280f729
commit 67eab93129
7 changed files with 86 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ class ChannelsApi {
const [rows]: any = await DB.query(query);
return rows;
} catch (e) {
logger.err('$getChannel error: ' + (e instanceof Error ? e.message : e));
logger.err('$getAllChannels error: ' + (e instanceof Error ? e.message : e));
throw e;
}
}
@@ -19,7 +19,7 @@ class ChannelsApi {
const [rows]: any = await DB.query(query, [status]);
return rows;
} catch (e) {
logger.err('$getChannel error: ' + (e instanceof Error ? e.message : e));
logger.err('$getChannelsByStatus error: ' + (e instanceof Error ? e.message : e));
throw e;
}
}
@@ -46,6 +46,17 @@ class ChannelsApi {
}
}
public async $getChannelByTransactionId(transactionId: string): Promise<any> {
try {
const query = `SELECT n1.alias AS alias_left, n2.alias AS alias_right, channels.* FROM channels LEFT JOIN nodes AS n1 ON n1.public_key = channels.node1_public_key LEFT JOIN nodes AS n2 ON n2.public_key = channels.node2_public_key WHERE channels.transaction_id = ?`;
const [rows]: any = await DB.query(query, [transactionId]);
return rows[0];
} catch (e) {
logger.err('$getChannelByTransactionId error: ' + (e instanceof Error ? e.message : e));
throw e;
}
}
public async $getChannelsForNode(public_key: string): Promise<any> {
try {
const query = `SELECT n1.alias AS alias_left, n2.alias AS alias_right, channels.* FROM channels LEFT JOIN nodes AS n1 ON n1.public_key = channels.node1_public_key LEFT JOIN nodes AS n2 ON n2.public_key = channels.node2_public_key WHERE node1_public_key = ? OR node2_public_key = ?`;

View File

@@ -7,6 +7,7 @@ class ChannelsRoutes {
public initRoutes(app: Express) {
app
.get(config.MEMPOOL.API_URL_PREFIX + 'channels/txids', this.$getChannelsByTransactionIds)
.get(config.MEMPOOL.API_URL_PREFIX + 'channels/:short_id', this.$getChannel)
.get(config.MEMPOOL.API_URL_PREFIX + 'channels', this.$getChannels)
;
@@ -38,6 +39,29 @@ class ChannelsRoutes {
}
}
private async $getChannelsByTransactionIds(req: Request, res: Response) {
try {
if (!Array.isArray(req.query.txId)) {
res.status(500).send('Not an array');
return;
}
const txIds: string[] = [];
for (const _txId in req.query.txId) {
if (typeof req.query.txId[_txId] === 'string') {
txIds.push(req.query.txId[_txId].toString());
}
}
const channels: any[] = [];
for (const txId of txIds) {
const channel = await channelsApi.$getChannelByTransactionId(txId);
channels.push(channel);
}
res.json(channels);
} catch (e) {
res.status(500).send(e instanceof Error ? e.message : e);
}
}
}
export default new ChannelsRoutes();