From 913c24c3a90d483370da95fa64044f3a2b12e453 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Oct 2017 16:23:11 -0700 Subject: [PATCH] updated getLongChannelId --- models/certificate.js | 64 ++++++++++++++++++++++++++++++++++++++++++- models/index.js | 50 --------------------------------- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/models/certificate.js b/models/certificate.js index 89fa2323..e21f71b7 100644 --- a/models/certificate.js +++ b/models/certificate.js @@ -1,4 +1,5 @@ const logger = require('winston'); +const NO_CHANNEL = 'NO_CHANNEL'; function sortResult (result, longId) { let claimIndex; @@ -24,7 +25,7 @@ function sortResult (result, longId) { return shortId; } -module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { +module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => { const Certificate = sequelize.define( 'Certificate', { @@ -145,5 +146,66 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D }); }; + // sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) + Certificate.getLongChannelIdFromShortChannelId = function (channelName, channelId) { + return new Promise((resolve, reject) => { + this + .findAll({ + where: { + name : channelName, + claimId: { + [Op.like]: `${channelId}%`, + }, + order: [['height', 'ASC']], + }, + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CHANNEL); + default: // note results must be sorted + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + + // sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) + Certificate.getLongChannelIdFromChannelName = function (channelName) { + logger.debug(`getLongChannelIdFromChannelName(${channelName})`); + return new Promise((resolve, reject) => { + this + .findAll({ + where: { name: channelName }, + order: [['effectiveAmount', 'DESC'], ['height', 'ASC']], + }) + .then(result => { + switch (result.length) { + case 0: + return resolve(NO_CHANNEL); + default: + return resolve(result[0].claimId); + } + }) + .catch(error => { + reject(error); + }); + }); + }; + + Certificate.getLongChannelId = function (channelName, channelId) { + logger.debug(`getLongChannelId(${channelName}, ${channelId})`); + if (channelId && (channelId.length === 40)) { // if a full channel id is provided + return new Promise((resolve, reject) => resolve(channelId)); + } else if (channelId && channelId.length < 40) { // if a short channel id is provided + return this.getLongChannelIdFromShortChannelId(channelName, channelId); + } else { + return this.getLongChannelIdFromChannelName(channelName); // if no channel id provided + } + }; + return Certificate; }; diff --git a/models/index.js b/models/index.js index ea227777..3d187a24 100644 --- a/models/index.js +++ b/models/index.js @@ -6,8 +6,6 @@ const config = require('config'); const db = {}; const logger = require('winston'); -const NO_CHANNEL = 'NO_CHANNEL'; - const database = config.get('Database.Database'); const username = config.get('Database.Username'); const password = config.get('Database.Password'); @@ -24,43 +22,6 @@ const sequelize = new Sequelize(database, username, password, { }, }); -function getLongChannelIdFromShortChannelId (channelName, channelId) { - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(NO_CHANNEL); - default: // note results must be sorted - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -} - -function getLongChannelIdFromChannelName (channelName) { - logger.debug(`getLongChannelIdFromChannelName(${channelName})`); - return new Promise((resolve, reject) => { - db - .sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT }) - .then(result => { - switch (result.length) { - case 0: - return resolve(NO_CHANNEL); - default: - return resolve(result[0].claimId); - } - }) - .catch(error => { - reject(error); - }); - }); -} - sequelize .authenticate() .then(() => { @@ -135,15 +96,4 @@ db['resolveClaim'] = (name, claimId) => { }); }; -db['getLongChannelId'] = (channelName, channelId) => { - logger.debug(`getLongChannelId (${channelName}, ${channelId})`); - if (channelId && (channelId.length === 40)) { // if a full channel id is provided - return new Promise((resolve, reject) => resolve(channelId)); - } else if (channelId && channelId.length < 40) { // if a short channel id is provided - return getLongChannelIdFromShortChannelId(channelName, channelId); - } else { - return getLongChannelIdFromChannelName(channelName); // if no channel id provided - } -}; - module.exports = db;