getLongCLaimId updated
This commit is contained in:
parent
c6878eb0a3
commit
cbfce3a01e
3 changed files with 63 additions and 52 deletions
|
@ -112,7 +112,7 @@ module.exports = {
|
|||
getAssetByClaim (claimName, claimId) {
|
||||
logger.debug(`getAssetByClaim(${claimName}, ${claimId})`);
|
||||
return new Promise((resolve, reject) => {
|
||||
db.getLongClaimId(claimName, claimId) // 1. get the long claim id
|
||||
db.Claim.getLongClaimId(claimName, claimId) // 1. get the long claim id
|
||||
.then(result => { // 2. get the asset using the long claim id
|
||||
logger.debug('long claim id ===', result);
|
||||
if (result === NO_CLAIM) {
|
||||
|
|
|
@ -25,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 Claim = sequelize.define(
|
||||
'Claim',
|
||||
{
|
||||
|
@ -228,7 +228,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
|
|||
Claim.getClaimIdByLongChannelId = function (channelId, claimName) {
|
||||
logger.debug(`finding claim id for claim ${claimName} from channel ${channelId}`);
|
||||
return new Promise((resolve, reject) => {
|
||||
Claim
|
||||
this
|
||||
.findAll({
|
||||
where: { name: claimName, certificateId: channelId },
|
||||
order: [['id', 'ASC']],
|
||||
|
@ -250,5 +250,64 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
|
|||
});
|
||||
};
|
||||
|
||||
// sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||
Claim.getLongClaimIdFromShortClaimId = function (name, shortId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this
|
||||
.findAll({
|
||||
where: {
|
||||
name,
|
||||
claimId: {
|
||||
[Op.like]: `${shortId}%`,
|
||||
}},
|
||||
order: [['height', 'ASC']],
|
||||
})
|
||||
.then(result => {
|
||||
switch (result.length) {
|
||||
case 0:
|
||||
return resolve(NO_CLAIM);
|
||||
default: // note results must be sorted
|
||||
return resolve(result[0].claimId);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Claim.getTopFreeClaimIdByClaimName = function (name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this
|
||||
.findAll({
|
||||
where: { name },
|
||||
order: [['effectiveAmount', 'DESC'], ['height', 'ASC']], // note: maybe height and effective amount need to switch?
|
||||
})
|
||||
.then(result => {
|
||||
switch (result.length) {
|
||||
case 0:
|
||||
return resolve(NO_CLAIM);
|
||||
default:
|
||||
logger.debug('getTopFreeClaimIdByClaimName result:', result);
|
||||
return resolve(result[0].claimId);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Claim.getLongClaimId = function (claimName, claimId) {
|
||||
logger.debug(`getLongClaimId(${claimName}, ${claimId})`);
|
||||
if (claimId && (claimId.length === 40)) { // if a full claim id is provided
|
||||
return new Promise((resolve, reject) => resolve(claimId));
|
||||
} else if (claimId && claimId.length < 40) {
|
||||
return this.getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided
|
||||
} else {
|
||||
return this.getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided
|
||||
}
|
||||
};
|
||||
|
||||
return Claim;
|
||||
};
|
||||
|
|
|
@ -7,11 +7,11 @@ const db = {};
|
|||
const logger = require('winston');
|
||||
|
||||
const NO_CHANNEL = 'NO_CHANNEL';
|
||||
const NO_CLAIM = 'NO_CLAIM';
|
||||
|
||||
const database = config.get('Database.Database');
|
||||
const username = config.get('Database.Username');
|
||||
const password = config.get('Database.Password');
|
||||
|
||||
const sequelize = new Sequelize(database, username, password, {
|
||||
host : 'localhost',
|
||||
dialect: 'mysql',
|
||||
|
@ -24,43 +24,6 @@ const sequelize = new Sequelize(database, username, password, {
|
|||
},
|
||||
});
|
||||
|
||||
function getLongClaimIdFromShortClaimId (name, shortId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
db
|
||||
.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||
.then(result => {
|
||||
switch (result.length) {
|
||||
case 0:
|
||||
return resolve(NO_CLAIM);
|
||||
default: // note results must be sorted
|
||||
return resolve(result[0].claimId);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getTopFreeClaimIdByClaimName (name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
db
|
||||
.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1`, { type: db.sequelize.QueryTypes.SELECT })
|
||||
.then(result => {
|
||||
logger.debug('getTopFreeClaimIdByClaimName result:', result);
|
||||
switch (result.length) {
|
||||
case 0:
|
||||
return resolve(NO_CLAIM);
|
||||
default:
|
||||
return resolve(result[0].claimId);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function getLongChannelIdFromShortChannelId (channelName, channelId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
db
|
||||
|
@ -172,17 +135,6 @@ db['resolveClaim'] = (name, claimId) => {
|
|||
});
|
||||
};
|
||||
|
||||
db['getLongClaimId'] = (claimName, claimId) => {
|
||||
logger.debug(`getLongClaimId(${claimName}, ${claimId})`);
|
||||
if (claimId && (claimId.length === 40)) { // if a full claim id is provided
|
||||
return new Promise((resolve, reject) => resolve(claimId));
|
||||
} else if (claimId && claimId.length < 40) {
|
||||
return getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided
|
||||
} else {
|
||||
return getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided
|
||||
}
|
||||
};
|
||||
|
||||
db['getLongChannelId'] = (channelName, channelId) => {
|
||||
logger.debug(`getLongChannelId (${channelName}, ${channelId})`);
|
||||
if (channelId && (channelId.length === 40)) { // if a full channel id is provided
|
||||
|
|
Loading…
Reference in a new issue