diff --git a/server/controllers/api/channel/claims/getChannelClaims.js b/server/controllers/api/channel/claims/getChannelClaims.js index aec6f03e..a36e559b 100644 --- a/server/controllers/api/channel/claims/getChannelClaims.js +++ b/server/controllers/api/channel/claims/getChannelClaims.js @@ -1,22 +1,20 @@ const db = require('../../../../models'); + const { returnPaginatedChannelClaims } = require('./channelPagination.js'); -const NO_CHANNEL = 'NO_CHANNEL'; const getChannelClaims = (channelName, channelClaimId, page) => { return new Promise((resolve, reject) => { + let longChannelClaimId; // 1. get the long channel Id (make sure channel exists) - db.Certificate.getLongChannelId(channelName, channelClaimId) - .then(longChannelClaimId => { - if (!longChannelClaimId) { - return [null, null, null]; - } - // 2. get the short ID and all claims for that channel - return Promise.all([longChannelClaimId, db.Claim.getAllChannelClaims(longChannelClaimId)]); + db.Certificate + .getLongChannelId(channelName, channelClaimId) + .then(result => { + longChannelClaimId = result; + return db + .Claim + .getAllChannelClaims(longChannelClaimId); }) - .then(([longChannelClaimId, channelClaimsArray]) => { - if (!longChannelClaimId) { - return resolve(NO_CHANNEL); - } + .then(channelClaimsArray => { // 3. format the data for the view, including pagination let paginatedChannelViewData = returnPaginatedChannelClaims(channelName, longChannelClaimId, channelClaimsArray, page); // 4. return all the channel information and contents diff --git a/server/controllers/api/channel/claims/index.js b/server/controllers/api/channel/claims/index.js index 4c7b6777..473394b2 100644 --- a/server/controllers/api/channel/claims/index.js +++ b/server/controllers/api/channel/claims/index.js @@ -16,12 +16,15 @@ const channelClaims = ({ ip, originalUrl, body, params }, res) => { const page = params.page; getChannelClaims(channelName, channelClaimId, page) .then(data => { - if (data === NO_CHANNEL) { - return res.status(404).json({success: false, message: 'No matching channel was found'}); - } res.status(200).json({success: true, data}); }) .catch(error => { + if (error === NO_CHANNEL) { + return res.status(404).json({ + success: false, + message: 'No matching channel was found', + }); + } handleErrorResponse(originalUrl, ip, error, res); }); }; diff --git a/server/controllers/api/channel/data/getChannelData.js b/server/controllers/api/channel/data/getChannelData.js index 98c3a8d5..a0db2374 100644 --- a/server/controllers/api/channel/data/getChannelData.js +++ b/server/controllers/api/channel/data/getChannelData.js @@ -1,22 +1,18 @@ const db = require('../../../../models'); -const NO_CHANNEL = 'NO_CHANNEL'; -const getChannelData = (channelName, channelClaimId, page) => { +const getChannelData = (channelName, channelClaimId) => { return new Promise((resolve, reject) => { + let longChannelClaimId; // 1. get the long channel Id (make sure channel exists) - db.Certificate.getLongChannelId(channelName, channelClaimId) - .then(longChannelClaimId => { - if (!longChannelClaimId) { - return [null, null, null]; - } - // 2. get the short ID and all claims for that channel - return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName)]); + db.Certificate + .getLongChannelId(channelName, channelClaimId) + .then(fullClaimId => { + longChannelClaimId = fullClaimId; + return db + .Certificate + .getShortChannelIdFromLongChannelId(fullClaimId, channelName); }) - .then(([longChannelClaimId, shortChannelClaimId]) => { - if (!longChannelClaimId) { - return resolve(NO_CHANNEL); - } - // 3. return all the channel information + .then(shortChannelClaimId => { resolve({ channelName, longChannelClaimId, diff --git a/server/controllers/api/channel/data/index.js b/server/controllers/api/channel/data/index.js index b52022f8..730549e5 100644 --- a/server/controllers/api/channel/data/index.js +++ b/server/controllers/api/channel/data/index.js @@ -14,14 +14,20 @@ const channelData = ({ ip, originalUrl, body, params }, res) => { const channelName = params.channelName; let channelClaimId = params.channelClaimId; if (channelClaimId === 'none') channelClaimId = null; - getChannelData(channelName, channelClaimId, 0) + getChannelData(channelName, channelClaimId) .then(data => { - if (data === NO_CHANNEL) { - return res.status(404).json({success: false, message: 'No matching channel was found'}); - } - res.status(200).json({success: true, data}); + res.status(200).json({ + success: true, + data + }); }) .catch(error => { + if (error === NO_CHANNEL) { + return res.status(404).json({ + success: false, + message: 'No matching channel was found', + }); + } handleErrorResponse(originalUrl, ip, error, res); }); }; diff --git a/server/models/certificate.js b/server/models/certificate.js index 85a5bc50..723b5065 100644 --- a/server/models/certificate.js +++ b/server/models/certificate.js @@ -120,7 +120,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { .then(result => { switch (result.length) { case 0: - throw new Error('No channel(s) found with that channel name'); + return reject(NO_CHANNEL); default: return resolve(returnShortId(result, longChannelId)); }