updated getLongChannelId
This commit is contained in:
parent
cbfce3a01e
commit
913c24c3a9
2 changed files with 63 additions and 51 deletions
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue