diff --git a/api/v1/controllers/samples.js b/api/v1/controllers/samples.js index e285299159..3bd9b7c714 100644 --- a/api/v1/controllers/samples.js +++ b/api/v1/controllers/samples.js @@ -23,6 +23,11 @@ const doPut = require('../helpers/verbs/doPut'); const u = require('../helpers/verbs/utils'); const httpStatus = require('../constants').httpStatus; const logAuditAPI = require('../../../utils/loggingUtil').logAuditAPI; +const sampleStore = require('../../../cache/sampleStore'); +const redisClient = require('../../../cache/redisCache').client.sampleStore; +const constants = sampleStore.constants; +const ZERO = 0; +const ONE = 1; module.exports = { @@ -49,7 +54,61 @@ module.exports = { * @param {Function} next - The next middleware function in the stack */ findSamples(req, res, next) { - doFind(req, res, next, helper); + if (featureToggles.isFeatureEnabled(constants.featureName)) { + const sampleCmds = []; + const aspectCmds = []; + const response = {}; + + // get all Samples + redisClient.smembersAsync(constants.indexKey.sample) + .then((allSampleKeys) => { + // add to commands to get sample + allSampleKeys.forEach((sampleName) => { + sampleCmds.push( + [constants.commands.hgetall, sampleName.toLowerCase()] + ); + }); + + // get all aspect names + return redisClient.smembersAsync(constants.indexKey.aspect); + }) + .then((allAspectKeys) => { + // add to commands to get aspect + allAspectKeys.forEach((aspectName) => { + aspectCmds.push( + [constants.commands.hgetall, aspectName.toLowerCase()] + ); + }); + + // get all samples and aspects + return Promise.all([ + redisClient.batch(sampleCmds).execAsync(), + redisClient.batch(aspectCmds).execAsync(), + ]); + }) + .then((sampleAndAspects) => { + const samples = sampleAndAspects[ZERO]; + const aspects = sampleAndAspects[ONE]; + + samples.forEach((sampleObj) => { + // attach aspect to sample + sampleObj.aspect = aspects.find((aspect) => + aspect.name === sampleObj.name.split('|')[ONE] + ); + + // add sample to response + response[sampleObj.name] = u.responsify( + sampleObj, helper, req.method + ); + }); + }) + .then(() => { + res.status(httpStatus.OK).json(response); + }) + .catch((err) => u.handleError(next, err, helper.modelName)); + } else { + doFind(req, res, next, helper); + } }, /** @@ -62,7 +121,51 @@ module.exports = { * @param {Function} next - The next middleware function in the stack */ getSample(req, res, next) { - doGet(req, res, next, helper); + // debugger; + // if (featureToggles.isFeatureEnabled(constants.featureName)) { + // const sampleCmds = []; + // const aspectCmds = []; + + // debugger; + // // get all Samples + // redisClient.smembersAsync(constants.indexKey.sample) + // .then((allSampleNames) => { + // debugger; + // // add to commands to get sample + // allSampleNames.forEach((sampleName) => { + // const sampleKey = toKey( + // constants.objectType.sample, + // sampleName.toLowerCase() + // ); + // sampleCmds.push([constants.commands.hgetall, sampleKey]); + // }); + + // // get all aspect names + // return redisClient.smembersAsync(constants.indexKey.aspect); + // }) + // .then((allAspectNames) => { + // debugger; + // // add to commands to get aspect + // allAspectNames.forEach((aspectName) => { + // const aspectKey = toKey( + // constants.objectType.aspect, + // aspectName.toLowerCase() + // ); + // aspectCmds.push([constants.commands.hgetall, aspectKey]); + // }); + + // // get all samples and aspects + // return Promise.all([ + // redisClient.batch(sampleCmds).execAsync(), + // redisClient.batch(aspectCmds).execAsync(), + // ]); + // }) + // .then((sampleAndAspects) => { + // debugger; + // }); + // } else { + doGet(req, res, next, helper); + // } }, /** diff --git a/api/v1/errorHandler.js b/api/v1/errorHandler.js index b8d6568590..c84ff384f3 100644 --- a/api/v1/errorHandler.js +++ b/api/v1/errorHandler.js @@ -116,18 +116,18 @@ module.exports = function errorHandler(err, req, res, next) { } } - // console.log('\n-----ERROR HANDLER------------------------------------'); - // console.log('ERROR STATUS: ', err.status); - // console.log('ERROR RESPONSE: ', errResponse); - // console.log('REQUEST BODY: ', req.body); - // console.log('STACK: ', err.stack); - // console.log('------------------------------------------------------\n'); + console.log('\n-----ERROR HANDLER------------------------------------'); + console.log('ERROR STATUS: ', err.status); + console.log('ERROR RESPONSE: ', errResponse); + console.log('REQUEST BODY: ', req.body); + console.log('STACK: ', err.stack); + console.log('------------------------------------------------------\n'); res.status(err.status).json(errResponse); } catch (err2) { - // console.log('\n-----ERROR HANDLER CATCH------------------------------'); - // console.log(err2); - // console.log('STACK: ', util.isError(err2) && err2.stack); - // console.log('------------------------------------------------------\n'); + console.log('\n-----ERROR HANDLER CATCH------------------------------'); + console.log(err2); + console.log('STACK: ', util.isError(err2) && err2.stack); + console.log('------------------------------------------------------\n'); return next; } }; diff --git a/cache/sampleStore.js b/cache/sampleStore.js index 3a9e98d702..7356ee6a58 100644 --- a/cache/sampleStore.js +++ b/cache/sampleStore.js @@ -38,6 +38,11 @@ const constants = { objectType: { aspect: 'aspect', sample: 'sample', subject: 'subject' }, prefix: PFX, separator: SEP, + commands: { + hgetall: 'hgetall', + hget: 'hget', + smembers: 'smembers', + }, }; /** @@ -81,12 +86,21 @@ function eradicate() { */ function removeNullsAndStringifyArrays(obj, arrayFields) { Object.keys(obj).forEach((key) => { - if (arrayFields.includes(key)) { - obj[key] = JSON.stringify(obj[key]); - } else if (obj[key] === null) { + // delete null fields and empty arrays, then stringify array fields + if (obj[key] === null || + (Array.isArray(obj[key]) && obj[key].length === 0)) { delete obj[key]; + } else if (arrayFields.includes(key)) { + obj[key] = JSON.stringify(obj[key]); } }); + // Object.keys(obj).forEach((key) => { + // if (arrayFields.includes(key)) { + // obj[key] = JSON.stringify(obj[key]); + // } else if (obj[key] === null) { + // delete obj[key]; + // } + // }); return obj; } // removeNullsAndStringifyArrays @@ -98,6 +112,7 @@ function removeNullsAndStringifyArrays(obj, arrayFields) { */ function cleanAspect(a) { let retval = a.get(); + retval = removeNullsAndStringifyArrays(retval, constants.fieldsToStringify.aspect); return retval; @@ -111,6 +126,7 @@ function cleanAspect(a) { */ function cleanSample(s) { let retval = s.get(); + delete retval.aspect; retval = removeNullsAndStringifyArrays(retval, constants.fieldsToStringify.sample);