Fix exceptions and incorrect logic around chainquery

This commit is contained in:
Shawn 2018-10-10 11:28:53 -05:00
parent ea2ffc0a2b
commit bb81441357
4 changed files with 34 additions and 16 deletions

View file

@ -275,6 +275,7 @@ var ClaimModel = (sequelize, {
BOOLEAN, BOOLEAN,
DATE, DATE,
DECIMAL, DECIMAL,
ENUM,
INTEGER, INTEGER,
STRING, STRING,
TEXT, TEXT,
@ -378,12 +379,8 @@ var ClaimModel = (sequelize, {
type: STRING, type: STRING,
set() { }, set() { },
}, },
is_filtered: {
type: BOOLEAN,
set() { },
},
bid_state: { bid_state: {
type: STRING, type: ENUM('Active', 'Expired', 'Controlling', 'Spent', 'Accepted'),
set() { }, set() { },
}, },
created_at: { created_at: {
@ -836,7 +833,22 @@ const isShortClaimId = (claimId) => {
return (claimId && (claimId.length < 40)); return (claimId && (claimId.length < 40));
}; };
var claimQueries = (db, table) => ({ var claimQueries = (db, table, sequelize) => ({
getClaimChannelName: async (publisher_id) => {
return await table.findAll({
where : { claim_id: publisher_id },
attributes: ['name'],
}).then(result => {
if(result.length === 0) {
throw new Error(`no record found for ${claimId}`);
} else if(result.length !== 1) {
logger$1.warn(`more than one record matches ${claimId} in db.Claim`);
}
return result[0].name;
});
},
getShortClaimIdFromLongClaimId: async (claimId, claimName) => { getShortClaimIdFromLongClaimId: async (claimId, claimName) => {
logger$1.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); logger$1.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`);
@ -857,7 +869,6 @@ var claimQueries = (db, table) => ({
return await table.findAll({ return await table.findAll({
where: { publisher_id: channelClaimId }, where: { publisher_id: channelClaimId },
order: [['height', 'DESC']], order: [['height', 'DESC']],
raw : false, // returns an array of only data, not an array of instances
}) })
.then(channelClaimsArray => { .then(channelClaimsArray => {
if(channelClaimsArray.length === 0) { if(channelClaimsArray.length === 0) {
@ -1062,7 +1073,7 @@ if (!database || !username || !password) {
} }
// set sequelize options // set sequelize options
const sequelize$1 = new Sequelize(database, username, password, { const sequelize = new Sequelize(database, username, password, {
host : host$1, host : host$1,
import : port, import : port,
dialect : 'mysql', dialect : 'mysql',
@ -1086,8 +1097,8 @@ for(let i = 0; i < DATABASE_STRUCTURE_KEYS.length; i++) {
let dbKey = DATABASE_STRUCTURE_KEYS[i]; let dbKey = DATABASE_STRUCTURE_KEYS[i];
let currentData = DATABASE_STRUCTURE[dbKey]; let currentData = DATABASE_STRUCTURE[dbKey];
db[dbKey] = currentData.table.createModel(sequelize$1, Sequelize); db[dbKey] = currentData.table.createModel(sequelize, Sequelize);
db[dbKey].queries = currentData.queries(db, db[dbKey]); db[dbKey].queries = currentData.queries(db, db[dbKey], sequelize);
} }
// run model.association for each model in the db object that has an association // run model.association for each model in the db object that has an association
@ -1100,7 +1111,7 @@ DATABASE_STRUCTURE_KEYS.forEach(modelName => {
}); });
// establish mysql connection // establish mysql connection
sequelize$1 sequelize
.authenticate() .authenticate()
.then(() => { .then(() => {
logger$2.info('Sequelize has established mysql connection for chainquery successfully.'); logger$2.info('Sequelize has established mysql connection for chainquery successfully.');

View file

@ -7,7 +7,8 @@ const getChannelClaims = async (channelName, channelShortId, page) => {
const channelId = await chainquery.claim.queries.getLongClaimId(channelName, channelShortId); const channelId = await chainquery.claim.queries.getLongClaimId(channelName, channelShortId);
const channelClaims = await chainquery.claim.queries.getAllChannelClaims(channelId); const channelClaims = await chainquery.claim.queries.getAllChannelClaims(channelId);
const processedChannelClaims = await channelClaims.map((claim) => getClaimData(claim)); const processingChannelClaims = channelClaims ? channelClaims.map((claim) => getClaimData(claim)) : [];
const processedChannelClaims = await Promise.all(processingChannelClaims);
return returnPaginatedChannelClaims(channelName, channelId, processedChannelClaims, page); return returnPaginatedChannelClaims(channelName, channelId, processedChannelClaims, page);
}; };

View file

@ -75,8 +75,11 @@ module.exports = function (req, res) {
res.claimId = showState.assetList[assetKeys[0]].claimId; res.claimId = showState.assetList[assetKeys[0]].claimId;
} else { } else {
var channelKeys = Object.keys(showState.channelList); var channelKeys = Object.keys(showState.channelList);
if (channelKeys.length !== 0) {
res.claimId = showState.channelList[channelKeys[0]].longId; res.claimId = showState.channelList[channelKeys[0]].longId;
res.isChannel = true; res.isChannel = true;
}
} // render component to a string } // render component to a string

View file

@ -40,9 +40,12 @@ module.exports = (req, res) => {
res.claimId = showState.assetList[assetKeys[0]].claimId; res.claimId = showState.assetList[assetKeys[0]].claimId;
} else { } else {
const channelKeys = Object.keys(showState.channelList); const channelKeys = Object.keys(showState.channelList);
if(channelKeys.length !== 0) {
res.claimId = showState.channelList[channelKeys[0]].longId; res.claimId = showState.channelList[channelKeys[0]].longId;
res.isChannel = true; res.isChannel = true;
} }
}
// render component to a string // render component to a string
const html = renderToString( const html = renderToString(