made fixes based on JKs feedback
This commit is contained in:
parent
1391fcceff
commit
821dd6be41
4 changed files with 31 additions and 53 deletions
25
helpers/sequelizeHelpers.js
Normal file
25
helpers/sequelizeHelpers.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
module.exports = {
|
||||||
|
returnShortId: function (result, longId) {
|
||||||
|
let claimIndex;
|
||||||
|
let shortId = longId.substring(0, 1); // default sort id is the first letter
|
||||||
|
let shortIdLength = 0;
|
||||||
|
// find the index of this claim id
|
||||||
|
claimIndex = result.findIndex(element => {
|
||||||
|
return element.claimId === longId;
|
||||||
|
});
|
||||||
|
if (claimIndex < 0) {
|
||||||
|
throw new Error('claim id not found in claims list');
|
||||||
|
}
|
||||||
|
// get an array of all claims with lower height
|
||||||
|
let possibleMatches = result.slice(0, claimIndex);
|
||||||
|
// remove certificates with the same prefixes until none are left.
|
||||||
|
while (possibleMatches.length > 0) {
|
||||||
|
shortIdLength += 1;
|
||||||
|
shortId = longId.substring(0, shortIdLength);
|
||||||
|
possibleMatches = possibleMatches.filter(element => {
|
||||||
|
return (element.claimId.substring(0, shortIdLength) === shortId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return shortId;
|
||||||
|
},
|
||||||
|
};
|
|
@ -1,31 +1,8 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
const { returnShortId } = require('../helpers/sequelizeHelpers.js');
|
||||||
const NO_CHANNEL = 'NO_CHANNEL';
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
function sortResult (result, longId) {
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT }) => {
|
||||||
let claimIndex;
|
|
||||||
let shortId = longId.substring(0, 1); // default sort id is the first letter
|
|
||||||
let shortIdLength = 0;
|
|
||||||
// find the index of this certificate
|
|
||||||
claimIndex = result.findIndex(element => {
|
|
||||||
return element.claimId === longId;
|
|
||||||
});
|
|
||||||
if (claimIndex < 0) { throw new Error('channelId not found in possible sorted list') }
|
|
||||||
// get an array of all certificates with lower height
|
|
||||||
let possibleMatches = result.slice(0, claimIndex);
|
|
||||||
// remove certificates with the same prefixes until none are left.
|
|
||||||
while (possibleMatches.length > 0) {
|
|
||||||
shortIdLength += 1;
|
|
||||||
shortId = longId.substring(0, shortIdLength);
|
|
||||||
possibleMatches = possibleMatches.filter(element => {
|
|
||||||
return (element.claimId.substring(0, shortIdLength) === shortId);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// return the short Id
|
|
||||||
logger.debug('short channel id ===', shortId);
|
|
||||||
return shortId;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => {
|
|
||||||
const Certificate = sequelize.define(
|
const Certificate = sequelize.define(
|
||||||
'Certificate',
|
'Certificate',
|
||||||
{
|
{
|
||||||
|
@ -137,7 +114,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
|
||||||
case 0:
|
case 0:
|
||||||
throw new Error('That is an invalid channel name');
|
throw new Error('That is an invalid channel name');
|
||||||
default:
|
default:
|
||||||
return resolve(sortResult(result, longChannelId));
|
return resolve(returnShortId(result, longChannelId));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
|
@ -1,30 +1,7 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
const { returnShortId } = require('../helpers/sequelizeHelpers.js');
|
||||||
const NO_CLAIM = 'NO_CLAIM';
|
const NO_CLAIM = 'NO_CLAIM';
|
||||||
|
|
||||||
function sortResult (result, longId) {
|
|
||||||
let claimIndex;
|
|
||||||
let shortId = longId.substring(0, 1); // default sort id is the first letter
|
|
||||||
let shortIdLength = 0;
|
|
||||||
// find the index of this certificate
|
|
||||||
claimIndex = result.findIndex(element => {
|
|
||||||
return element.claimId === longId;
|
|
||||||
});
|
|
||||||
if (claimIndex < 0) { throw new Error('claimid not found in possible sorted list') }
|
|
||||||
// get an array of all certificates with lower height
|
|
||||||
let possibleMatches = result.slice(0, claimIndex);
|
|
||||||
// remove certificates with the same prefixes until none are left.
|
|
||||||
while (possibleMatches.length > 0) {
|
|
||||||
shortIdLength += 1;
|
|
||||||
shortId = longId.substring(0, shortIdLength);
|
|
||||||
possibleMatches = possibleMatches.filter(element => {
|
|
||||||
return (element.claimId.substring(0, shortIdLength) === shortId);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// return the short Id
|
|
||||||
logger.debug('short claim id ===', shortId);
|
|
||||||
return shortId;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => {
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE, Op }) => {
|
||||||
const Claim = sequelize.define(
|
const Claim = sequelize.define(
|
||||||
'Claim',
|
'Claim',
|
||||||
|
@ -194,7 +171,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
|
||||||
case 0:
|
case 0:
|
||||||
throw new Error('That is an invalid claim name');
|
throw new Error('That is an invalid claim name');
|
||||||
default:
|
default:
|
||||||
resolve(sortResult(result, claimId));
|
resolve(returnShortId(result, claimId));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
|
@ -147,8 +147,7 @@ module.exports = (app) => {
|
||||||
// route to get a short claim id from long claim Id
|
// route to get a short claim id from long claim Id
|
||||||
app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => {
|
app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => {
|
||||||
// serve content
|
// serve content
|
||||||
db.Claim
|
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
||||||
.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
|
||||||
.then(shortId => {
|
.then(shortId => {
|
||||||
res.status(200).json(shortId);
|
res.status(200).json(shortId);
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue