From c0539a2f4b51f9bb5319121df6bb9dc8cc5889b8 Mon Sep 17 00:00:00 2001 From: Ian Goldstein Date: Thu, 29 Dec 2016 12:05:38 -0800 Subject: [PATCH 1/2] Clean up jscs in api (#160) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Clean up jscs errors in /api * Oops didn’t mean to check in package.xml --- api/v1/controllers/aspects.js | 2 +- api/v1/controllers/globalconfig.js | 4 +--- api/v1/controllers/lenses.js | 3 +-- api/v1/controllers/perspectives.js | 2 +- api/v1/controllers/ssoconfig.js | 12 +++++------- api/v1/controllers/subjects.js | 10 +++++++--- api/v1/controllers/tokens.js | 4 ++-- api/v1/controllers/userTokens.js | 8 +++----- api/v1/helpers/nouns/samples.js | 1 - api/v1/helpers/verbs/doFind.js | 1 + api/v1/helpers/verbs/doPut.js | 1 + api/v1/helpers/verbs/findUtils.js | 25 ++++++++++++++++--------- api/v1/helpers/verbs/utils.js | 18 +++++++----------- 13 files changed, 46 insertions(+), 45 deletions(-) diff --git a/api/v1/controllers/aspects.js b/api/v1/controllers/aspects.js index 88f759f1a7..dedfb97167 100644 --- a/api/v1/controllers/aspects.js +++ b/api/v1/controllers/aspects.js @@ -101,7 +101,7 @@ module.exports = { u.findAssociatedInstances(helper, params, helper.userModelAssociationName, options) .then((o) => { - // if the resolved object is an empty array, throw a ResourceNotFound error + // throw a ResourceNotFound error if resolved object is empty array u.throwErrorForEmptyArray(o, params.userNameOrId.value, userProps.modelName); const retval = u.responsify(o, helper, req.method); diff --git a/api/v1/controllers/globalconfig.js b/api/v1/controllers/globalconfig.js index 3c1399a944..f7bf47cc3b 100644 --- a/api/v1/controllers/globalconfig.js +++ b/api/v1/controllers/globalconfig.js @@ -28,9 +28,7 @@ function isAdmin(req) { if (req.headers.authorization) { // use the token jwtUtil.getUsernameFromToken(req) - .then((username) => { - return User.findOne({ where: { name: username } }); - }) + .then((username) => User.findOne({ where: { name: username } })) .then((user) => { resolve(Profile.isAdmin(user.profileId)); }) diff --git a/api/v1/controllers/lenses.js b/api/v1/controllers/lenses.js index 34af991c99..013b6c7652 100644 --- a/api/v1/controllers/lenses.js +++ b/api/v1/controllers/lenses.js @@ -112,7 +112,6 @@ function handleLensMetadata(requestObj, libraryParam, seqObj) { } } - /** * Prepares the object to be sent back in the response ("cleans" the object, * strips out nulls, adds API links). @@ -197,7 +196,7 @@ module.exports = { u.findAssociatedInstances(helper, params, helper.userModelAssociationName, options) .then((o) => { - // if the resolved object is an empty array, throw a ResourceNotFound error + // throw a ResourceNotFound error if resolved object is empty array u.throwErrorForEmptyArray(o, params.userNameOrId.value, userProps.modelName); const retval = u.responsify(o, helper, req.method); diff --git a/api/v1/controllers/perspectives.js b/api/v1/controllers/perspectives.js index 5a2a7d1fb4..3bae221345 100644 --- a/api/v1/controllers/perspectives.js +++ b/api/v1/controllers/perspectives.js @@ -101,7 +101,7 @@ module.exports = { u.findAssociatedInstances(helper, params, helper.userModelAssociationName, options) .then((o) => { - // if the resolved object is an empty array, throw a ResourceNotFound error + // throw ResourceNotFound error if resolved object is empty array u.throwErrorForEmptyArray(o, params.userNameOrId.value, userProps.modelName); const retval = u.responsify(o, helper, req.method); diff --git a/api/v1/controllers/ssoconfig.js b/api/v1/controllers/ssoconfig.js index ef9c6e109d..8e206edbc0 100644 --- a/api/v1/controllers/ssoconfig.js +++ b/api/v1/controllers/ssoconfig.js @@ -37,13 +37,11 @@ function getApiLinks(key, props, method) { } // Otherwise include all the methods specified for this resource - return Object.keys(props.apiLinks).map((i) => { - return { - href: props.baseUrl, - method: i, - rel: props.apiLinks[i], - }; - }); + return Object.keys(props.apiLinks).map((i) => ({ + href: props.baseUrl, + method: i, + rel: props.apiLinks[i], + })); } // getApiLinks /** diff --git a/api/v1/controllers/subjects.js b/api/v1/controllers/subjects.js index fdf9419a7e..fb5a37e896 100644 --- a/api/v1/controllers/subjects.js +++ b/api/v1/controllers/subjects.js @@ -36,17 +36,19 @@ const ONE = 1; */ function checkDuplicates(tagsArr) { const LEN = tagsArr.length - ONE; - // to store lowercase copies - const copyArr = []; + const copyArr = []; // store lowercase copies let toAdd; for (let i = LEN; i >= ZERO; i--) { toAdd = tagsArr[i].toLowerCase(); + // if duplicate found, return true if (copyArr.indexOf(toAdd) > -ONE) { return true; } + copyArr.push(toAdd); } + return false; } @@ -66,9 +68,11 @@ function validateRequest(requestBody, params) { // params.tags.value is a comma delimited string, not empty. tags = params.tags.value ? params.tags.value.split(',') : []; } + if (absolutePath) { throw new apiErrors.SubjectValidationError(); } + if (tags && tags.length) { if (checkDuplicates(tags)) { throw new apiErrors.DuplicateFieldError(); @@ -206,7 +210,7 @@ module.exports = { u.findAssociatedInstances(helper, params, helper.userModelAssociationName, options) .then((o) => { - // if the resolved object is an empty array, throw a ResourceNotFound error + // throw ResourceNotFound error if resolved object is empty array u.throwErrorForEmptyArray(o, params.userNameOrId.value, userProps.modelName); const retval = u.responsify(o, helper, req.method); diff --git a/api/v1/controllers/tokens.js b/api/v1/controllers/tokens.js index 5c942ed984..7cf0ea15d8 100644 --- a/api/v1/controllers/tokens.js +++ b/api/v1/controllers/tokens.js @@ -116,7 +116,7 @@ module.exports = { .catch((err) => u.handleError(next, err, helper.modelName)); }, - /** + /** * POST /tokens/{key}/revoke * * Revoke access for the specified token. @@ -141,4 +141,4 @@ module.exports = { }) .catch((err) => u.handleError(next, err, helper.modelName)); }, -}; // exports \ No newline at end of file +}; // exports diff --git a/api/v1/controllers/userTokens.js b/api/v1/controllers/userTokens.js index fecab74763..af8fc42fee 100644 --- a/api/v1/controllers/userTokens.js +++ b/api/v1/controllers/userTokens.js @@ -65,10 +65,8 @@ module.exports = { err.key = user + ', ' + tokenName; throw err; }) - .then((o) => { - return res.status(httpStatus.OK) - .json(u.responsify(o, helper, req.method)); - }) + .then((o) => res.status(httpStatus.OK) + .json(u.responsify(o, helper, req.method))) .catch((err) => u.handleError(next, err, helper.modelName)); }, @@ -170,4 +168,4 @@ module.exports = { }) .catch((err) => u.handleError(next, err, helper.modelName)); }, -}; // exports \ No newline at end of file +}; // exports diff --git a/api/v1/helpers/nouns/samples.js b/api/v1/helpers/nouns/samples.js index 172595999d..2c40f3ae78 100644 --- a/api/v1/helpers/nouns/samples.js +++ b/api/v1/helpers/nouns/samples.js @@ -22,7 +22,6 @@ const loggingEnabled = ( config.auditSamples === 'API' || config.auditSamples === 'ALL' ) || false; - module.exports = { apiLinks: { DELETE: `Delete this ${m}`, diff --git a/api/v1/helpers/verbs/doFind.js b/api/v1/helpers/verbs/doFind.js index 8fbe7c67de..fef6f52747 100644 --- a/api/v1/helpers/verbs/doFind.js +++ b/api/v1/helpers/verbs/doFind.js @@ -82,6 +82,7 @@ function doFindAll(reqResNext, props, opts) { if (tags && tags.value && tags.value.length) { retval = fu.filterArrFromArr(retval, tags.value); } + reqResNext.res.status(httpStatus.OK).json(retval); }) .catch((err) => u.handleError(reqResNext.next, err, props.modelName)); diff --git a/api/v1/helpers/verbs/doPut.js b/api/v1/helpers/verbs/doPut.js index 92f032d378..9ee39bdfdd 100644 --- a/api/v1/helpers/verbs/doPut.js +++ b/api/v1/helpers/verbs/doPut.js @@ -50,6 +50,7 @@ function doPut(req, res, next, props) { o.set(key, toPut[key]); } } + return o.save(); }) .then((o) => { diff --git a/api/v1/helpers/verbs/findUtils.js b/api/v1/helpers/verbs/findUtils.js index 15315515fa..d09b6e82ce 100644 --- a/api/v1/helpers/verbs/findUtils.js +++ b/api/v1/helpers/verbs/findUtils.js @@ -41,10 +41,8 @@ function escapePercentLiterals(val) { */ function toSequelizeWildcards(val) { const chars = val.split(constants.EMPTY_STRING); - const arr = chars.map((ch) => { - return ch === constants.QUERY_PARAM_WILDCARD ? - constants.SEQ_WILDCARD : ch; - }); + const arr = chars.map((ch) => + (ch === constants.QUERY_PARAM_WILDCARD ? constants.SEQ_WILDCARD : ch)); return arr.join(constants.EMPTY_STRING); } // toSequelizeWildcards @@ -116,6 +114,7 @@ function toSequelizeWhere(filter, props) { if (Array.isArray(props.fieldsWithEnum) && props.fieldsWithEnum.indexOf(key) > -ONE) { const enumArr = filter[key]; + // to use $in instead of $contains in toWhereClause props.isEnum = true; values.push(toWhereClause(enumArr, props)); @@ -239,22 +238,27 @@ function options(params, props) { function filterArrFromArr(sArr, tagsStr) { const tagsArr = tagsStr.split(','); const TAGLEN = tagsArr.length; - // assume TAGLEN has > 0 tags, since if ther's - // 0 tags express would've thrown an error + + // Assume TAGLEN has > 0 tags (otherwise express would throw error) const INCLUDE = tagsArr[ZERO].charAt(ZERO) !== '-'; - // if !INCLUDE, splice out the leading - in tags - // else throw exception if tag starts with - + + /* + * If !INCLUDE, splice out the leading "-" in tags. Otherwise throw an + * exception if tag starts with "-". + */ for (let i = TAGLEN - ONE; i >= ZERO; i--) { if (tagsArr[i].charAt(ZERO) === '-') { if (INCLUDE) { throw new Error('To specify EXCLUDE tags, ' + 'prepend each tag with -'); } + tagsArr[i] = tagsArr[i].slice(ONE); } } - let filteredArr = []; + const filteredArr = []; + // append iff subject's tags contains all tags in tagsArr if (INCLUDE) { for (let i = ZERO; i < sArr.length; i++) { @@ -265,6 +269,7 @@ function filterArrFromArr(sArr, tagsStr) { count++; } } + if (count === TAGLEN) { filteredArr.push(sArr[i]); } @@ -281,11 +286,13 @@ function filterArrFromArr(sArr, tagsStr) { break; } } + if (addToArr) { filteredArr.push(sArr[i]); } } } + return filteredArr; } diff --git a/api/v1/helpers/verbs/utils.js b/api/v1/helpers/verbs/utils.js index 6a1408e0b4..8f0cb2a1ff 100644 --- a/api/v1/helpers/verbs/utils.js +++ b/api/v1/helpers/verbs/utils.js @@ -59,9 +59,9 @@ function includeAssocToCreate(obj, props) { return includedAssoc; } // includeAssocToCreate - /** - * Function that capitalises the first letter of the string and returns it. + * Capitalize the first letter of the string and returns the modified string. + * * @param {String} str - String that has to have its first letter capitalized * @returns {String} str - String with the first letter capitalized */ @@ -167,15 +167,11 @@ function getApiLinks(key, props, method) { } // Otherwise include all the methods specified for this resource - return Object.keys(props.apiLinks).map((i) => { - return { - href: i === 'POST' ? - props.baseUrl : - props.baseUrl + constants.SLASH + key, - method: i, - rel: props.apiLinks[i], - }; - }); + return Object.keys(props.apiLinks).map((i) => ({ + href: i === 'POST' ? props.baseUrl : props.baseUrl + constants.SLASH + key, + method: i, + rel: props.apiLinks[i], + })); } // getApiLinks /** From ffb8d31e6714a0bcad6e3b105f8515b5634946f1 Mon Sep 17 00:00:00 2001 From: Ian Goldstein Date: Thu, 29 Dec 2016 12:12:04 -0800 Subject: [PATCH 2/2] Clean up jscs errors in /db (#161) --- db/helpers/common.js | 1 + db/model/aspect.js | 21 +++++++++------------ db/model/lens.js | 7 +++---- db/model/perspective.js | 7 +++---- db/model/sample.js | 7 +++---- db/model/subject.js | 31 +++++++++++++------------------ db/model/token.js | 15 +++++++-------- 7 files changed, 39 insertions(+), 50 deletions(-) diff --git a/db/helpers/common.js b/db/helpers/common.js index 4b8ea9f16c..426c84d9ec 100644 --- a/db/helpers/common.js +++ b/db/helpers/common.js @@ -49,6 +49,7 @@ function augmentSampleWithSubjectAspectInfo(seq, inst) { if (sub) { inst.dataValues.absolutePath = sub.absolutePath; } + inst.subject = sub; }).then(() => inst.getAspect()) .then((asp) => { diff --git a/db/model/aspect.js b/db/model/aspect.js index b116e55225..c5f99b560a 100644 --- a/db/model/aspect.js +++ b/db/model/aspect.js @@ -207,8 +207,8 @@ module.exports = function aspect(seq, dataTypes) { if (inst.changed('tags')) { return new seq.Promise((resolve, reject) => { inst.getSamples() - .each((samp) => { - return common.sampleAspectAndSubjectArePublished(seq, samp) + .each((samp) => + common.sampleAspectAndSubjectArePublished(seq, samp) .then((published) => { if (published) { return common.augmentSampleWithSubjectAspectInfo(seq, samp) @@ -218,8 +218,7 @@ module.exports = function aspect(seq, dataTypes) { } return seq.Promise.resolve(true); - }); - }) + })) .then(() => resolve(inst)) .catch(reject); }); @@ -255,8 +254,8 @@ module.exports = function aspect(seq, dataTypes) { if (inst.changed('tags')) { return new seq.Promise((resolve, reject) => { inst.getSamples() - .each((samp) => { - return common.sampleAspectAndSubjectArePublished(seq, samp) + .each((samp) => + common.sampleAspectAndSubjectArePublished(seq, samp) .then((published) => { if (published) { return common.augmentSampleWithSubjectAspectInfo(seq, samp) @@ -266,8 +265,7 @@ module.exports = function aspect(seq, dataTypes) { } return seq.Promise.resolve(true); - }); - }) + })) .then(() => resolve(inst)) .catch(reject); }); @@ -316,8 +314,8 @@ module.exports = function aspect(seq, dataTypes) { ], instanceMethods: { isWritableBy(who) { - return new seq.Promise((resolve, reject) => { - return this.getWriters() + return new seq.Promise((resolve /* , reject */) => + this.getWriters() .then((writers) => { if (!writers.length) { resolve(true); @@ -326,8 +324,7 @@ module.exports = function aspect(seq, dataTypes) { const found = writers.filter((w) => w.name === who || w.id === who); resolve(found.length === 1); - }); - }); + })); }, // isWritableBy }, paranoid: true, diff --git a/db/model/lens.js b/db/model/lens.js index 9b6600c1d0..c7a719a80c 100644 --- a/db/model/lens.js +++ b/db/model/lens.js @@ -168,8 +168,8 @@ module.exports = function lens(seq, dataTypes) { ], instanceMethods: { isWritableBy(who) { - return new seq.Promise((resolve, reject) => { - return this.getWriters() + return new seq.Promise((resolve /* , reject */) => + this.getWriters() .then((writers) => { if (!writers.length) { resolve(true); @@ -178,8 +178,7 @@ module.exports = function lens(seq, dataTypes) { const found = writers.filter((w) => w.name === who || w.id === who); resolve(found.length === 1); - }); - }); + })); }, // isWritableBy }, paranoid: true, diff --git a/db/model/perspective.js b/db/model/perspective.js index 0e03cc4c98..cb99ca350d 100644 --- a/db/model/perspective.js +++ b/db/model/perspective.js @@ -173,8 +173,8 @@ module.exports = function perspective(seq, dataTypes) { ], instanceMethods: { isWritableBy(who) { - return new seq.Promise((resolve, reject) => { - return this.getWriters() + return new seq.Promise((resolve /* , reject */) => + this.getWriters() .then((writers) => { if (!writers.length) { resolve(true); @@ -183,8 +183,7 @@ module.exports = function perspective(seq, dataTypes) { const found = writers.filter((w) => w.name === who || w.id === who); resolve(found.length === 1); - }); - }); + })); }, // isWritableBy }, paranoid: true, diff --git a/db/model/sample.js b/db/model/sample.js index dda66ee330..8e83aa204e 100644 --- a/db/model/sample.js +++ b/db/model/sample.js @@ -436,8 +436,8 @@ module.exports = function sample(seq, dataTypes) { }, // calculateStatus isWritableBy(who) { - return new seq.Promise((resolve, reject) => { - return this.getAspect() + return new seq.Promise((resolve /* , reject */) => + this.getAspect() .then((a) => a.getWriters()) .then((writers) => { if (!writers.length) { @@ -447,8 +447,7 @@ module.exports = function sample(seq, dataTypes) { const found = writers.filter((w) => w.name === who || w.id === who); resolve(found.length === 1); - }); - }); + })); }, // isWritableBy setStatusChangedAt() { diff --git a/db/model/subject.js b/db/model/subject.js index 04f3497da3..5fcd8f0f3f 100644 --- a/db/model/subject.js +++ b/db/model/subject.js @@ -161,20 +161,16 @@ module.exports = function subject(seq, dataTypes) { }, ], }); - Subject.addScope('id', (value) => { - return { - where: { - id: value, - }, - }; - }); - Subject.addScope('absolutePath', (value) => { - return { - where: { - absolutePath: value, - }, - }; - }); + Subject.addScope('id', (value) => ({ + where: { + id: value, + }, + })); + Subject.addScope('absolutePath', (value) => ({ + where: { + absolutePath: value, + }, + })); Subject.addScope('hierarchy', { where: { isPublished: true, @@ -621,8 +617,8 @@ module.exports = function subject(seq, dataTypes) { }, // instanceMethods.deleteHierarchy isWritableBy(who) { - return new seq.Promise((resolve, reject) => { - return this.getWriters() + return new seq.Promise((resolve /* , reject */) => + this.getWriters() .then((writers) => { if (!writers.length) { resolve(true); @@ -631,8 +627,7 @@ module.exports = function subject(seq, dataTypes) { const found = writers.filter((w) => w.name === who || w.id === who); resolve(found.length === 1); - }); - }); + })); }, // isWritableBy }, // instanceMethods paranoid: true, diff --git a/db/model/token.js b/db/model/token.js index 288df1860c..7002ea390b 100644 --- a/db/model/token.js +++ b/db/model/token.js @@ -87,18 +87,17 @@ module.exports = function token(seq, dataTypes) { ], instanceMethods: { restore() { - return new Promise((resolve, reject) => { - return this.update({ isRevoked: 0 }) + return new Promise((resolve, reject) => + this.update({ isRevoked: 0 }) .then(resolve) - .catch(reject); - }); + .catch(reject)); }, // restore + revoke() { - return new Promise((resolve, reject) => { - return this.update({ isRevoked: Date.now() }) + return new Promise((resolve, reject) => + this.update({ isRevoked: Date.now() }) .then(resolve) - .catch(reject); - }); + .catch(reject)); }, // revoke }, paranoid: true,