removed constants from model layer

This commit is contained in:
bill bittner 2017-12-05 17:08:34 -08:00
parent 54bc80b08d
commit fa15b87e5e
3 changed files with 36 additions and 39 deletions

View file

@ -3,6 +3,7 @@ const logger = require('winston');
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png'; const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
const NO_FILE = 'NO_FILE'; const NO_FILE = 'NO_FILE';
module.exports = { module.exports = {
@ -16,9 +17,12 @@ module.exports = {
getClaimIdByClaim (claimName, claimId) { getClaimIdByClaim (claimName, claimId) {
logger.debug(`getClaimIdByClaim(${claimName}, ${claimId})`); logger.debug(`getClaimIdByClaim(${claimName}, ${claimId})`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db.Claim.getLongClaimId(claimName, claimId) // get the long claim id db.Claim.getLongClaimId(claimName, claimId)
.then(result => { .then(longClaimId => {
resolve(result); // resolves with NO_CLAIM or valid claim id if (!longClaimId) {
resolve(NO_CLAIM);
}
resolve(longClaimId);
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
@ -29,15 +33,20 @@ module.exports = {
logger.debug(`getClaimIdByChannel(${channelName}, ${channelClaimId}, ${claimName})`); logger.debug(`getClaimIdByChannel(${channelName}, ${channelClaimId}, ${claimName})`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel id db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel id
.then(result => { .then(longChannelId => {
if (result === NO_CHANNEL) { if (!longChannelId) {
resolve(result); // resolves NO_CHANNEL return [null, null];
return;
} }
return db.Claim.getClaimIdByLongChannelId(result, claimName); // 2. get the long claim id return Promise.all([longChannelId, db.Claim.getClaimIdByLongChannelId(longChannelId, claimName)]); // 2. get the long claim id
}) })
.then(result => { .then(([longChannelId, longClaimId]) => {
resolve(result); // resolves with NO_CLAIM or valid claim id if (!longChannelId) {
return resolve(NO_CHANNEL);
}
if (!longClaimId) {
return resolve(NO_CLAIM);
}
resolve(longClaimId);
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
@ -46,30 +55,19 @@ module.exports = {
}, },
getChannelContents (channelName, channelClaimId) { getChannelContents (channelName, channelClaimId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let longChannelClaimId;
let shortChannelClaimId;
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel Id db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel Id
.then(result => { // 2. get all claims for that channel .then(longChannelClaimId => { // 2. get all claims for that channel
if (result === NO_CHANNEL) { if (!longChannelClaimId) {
return NO_CHANNEL; return [null, null, null];
} }
longChannelClaimId = result; return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName), db.Claim.getAllChannelClaims(longChannelClaimId)]);
return db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName);
}) })
.then(result => { // 3. get all Claim records for this channel .then(([longChannelClaimId, shortChannelClaimId, channelClaimsArray]) => { // 4. add extra data not available from Claim table
if (result === NO_CHANNEL) { if (!longChannelClaimId) {
return NO_CHANNEL; return resolve(NO_CHANNEL);
} }
shortChannelClaimId = result; if (channelClaimsArray) {
return db.Claim.getAllChannelClaims(longChannelClaimId); channelClaimsArray.forEach(element => {
})
.then(result => { // 4. add extra data not available from Claim table
if (result === NO_CHANNEL) {
resolve(result);
return;
}
if (result) {
result.forEach(element => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1); const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
element['showUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}`; element['showUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}`;
element['directUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}.${fileExtenstion}`; element['directUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}.${fileExtenstion}`;
@ -82,7 +80,7 @@ module.exports = {
channelName, channelName,
longChannelClaimId, longChannelClaimId,
shortChannelClaimId, shortChannelClaimId,
claims: result, claims: channelClaimsArray,
}); });
}) })
.catch(error => { .catch(error => {

View file

@ -1,6 +1,5 @@
const logger = require('winston'); const logger = require('winston');
const { returnShortId } = require('../helpers/sequelizeHelpers.js'); const { returnShortId } = require('../helpers/sequelizeHelpers.js');
const NO_CHANNEL = 'NO_CHANNEL';
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
const Certificate = sequelize.define( const Certificate = sequelize.define(
@ -138,7 +137,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
.then(result => { .then(result => {
switch (result.length) { switch (result.length) {
case 0: case 0:
return resolve(NO_CHANNEL); return resolve(null);
default: // note results must be sorted default: // note results must be sorted
return resolve(result[0].claimId); return resolve(result[0].claimId);
} }
@ -160,7 +159,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
.then(result => { .then(result => {
switch (result.length) { switch (result.length) {
case 0: case 0:
return resolve(NO_CHANNEL); return resolve(null);
default: default:
return resolve(result[0].claimId); return resolve(result[0].claimId);
} }
@ -178,7 +177,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
}) })
.then(result => { .then(result => {
if (!result) { if (!result) {
return resolve(NO_CHANNEL); return resolve(null);
}; };
resolve(claimId); resolve(claimId);
}) })

View file

@ -213,7 +213,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
.then(result => { .then(result => {
switch (result.length) { switch (result.length) {
case 0: case 0:
return resolve(NO_CLAIM); return resolve(null);
case 1: case 1:
return resolve(result[0].claimId); return resolve(result[0].claimId);
default: default:
@ -241,7 +241,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
.then(result => { .then(result => {
switch (result.length) { switch (result.length) {
case 0: case 0:
return resolve(NO_CLAIM); return resolve(null);
default: // note results must be sorted default: // note results must be sorted
return resolve(result[0].claimId); return resolve(result[0].claimId);
} }
@ -263,7 +263,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
logger.debug('length of result', result.length); logger.debug('length of result', result.length);
switch (result.length) { switch (result.length) {
case 0: case 0:
return resolve(NO_CLAIM); return resolve(null);
default: default:
return resolve(result[0].dataValues.claimId); return resolve(result[0].dataValues.claimId);
} }
@ -281,7 +281,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
}) })
.then(result => { .then(result => {
if (!result) { if (!result) {
return resolve(NO_CLAIM); return resolve(null);
}; };
resolve(claimId); resolve(claimId);
}) })