Skip to content

Commit

Permalink
Merge branch 'master' into clean-up-jscs-in-tests-api-v1-subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
annyhe committed Dec 29, 2016
2 parents 13757f4 + ffb8d31 commit 0492115
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 95 deletions.
2 changes: 1 addition & 1 deletion api/v1/controllers/aspects.js
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions api/v1/controllers/globalconfig.js
Expand Up @@ -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));
})
Expand Down
3 changes: 1 addition & 2 deletions api/v1/controllers/lenses.js
Expand Up @@ -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).
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion api/v1/controllers/perspectives.js
Expand Up @@ -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);
Expand Down
12 changes: 5 additions & 7 deletions api/v1/controllers/ssoconfig.js
Expand Up @@ -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

/**
Expand Down
10 changes: 7 additions & 3 deletions api/v1/controllers/subjects.js
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions api/v1/controllers/tokens.js
Expand Up @@ -116,7 +116,7 @@ module.exports = {
.catch((err) => u.handleError(next, err, helper.modelName));
},

/**
/**
* POST /tokens/{key}/revoke
*
* Revoke access for the specified token.
Expand All @@ -141,4 +141,4 @@ module.exports = {
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
}; // exports
}; // exports
8 changes: 3 additions & 5 deletions api/v1/controllers/userTokens.js
Expand Up @@ -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));
},

Expand Down Expand Up @@ -170,4 +168,4 @@ module.exports = {
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
}; // exports
}; // exports
1 change: 0 additions & 1 deletion api/v1/helpers/nouns/samples.js
Expand Up @@ -22,7 +22,6 @@ const loggingEnabled = (
config.auditSamples === 'API' || config.auditSamples === 'ALL'
) || false;


module.exports = {
apiLinks: {
DELETE: `Delete this ${m}`,
Expand Down
1 change: 1 addition & 0 deletions api/v1/helpers/verbs/doFind.js
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions api/v1/helpers/verbs/doPut.js
Expand Up @@ -50,6 +50,7 @@ function doPut(req, res, next, props) {
o.set(key, toPut[key]);
}
}

return o.save();
})
.then((o) => {
Expand Down
25 changes: 16 additions & 9 deletions api/v1/helpers/verbs/findUtils.js
Expand Up @@ -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

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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++) {
Expand All @@ -265,6 +269,7 @@ function filterArrFromArr(sArr, tagsStr) {
count++;
}
}

if (count === TAGLEN) {
filteredArr.push(sArr[i]);
}
Expand All @@ -281,11 +286,13 @@ function filterArrFromArr(sArr, tagsStr) {
break;
}
}

if (addToArr) {
filteredArr.push(sArr[i]);
}
}
}

return filteredArr;
}

Expand Down
18 changes: 7 additions & 11 deletions api/v1/helpers/verbs/utils.js
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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

/**
Expand Down
1 change: 1 addition & 0 deletions db/helpers/common.js
Expand Up @@ -49,6 +49,7 @@ function augmentSampleWithSubjectAspectInfo(seq, inst) {
if (sub) {
inst.dataValues.absolutePath = sub.absolutePath;
}

inst.subject = sub;
}).then(() => inst.getAspect())
.then((asp) => {
Expand Down
21 changes: 9 additions & 12 deletions db/model/aspect.js
Expand Up @@ -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)
Expand All @@ -218,8 +218,7 @@ module.exports = function aspect(seq, dataTypes) {
}

return seq.Promise.resolve(true);
});
})
}))
.then(() => resolve(inst))
.catch(reject);
});
Expand Down Expand Up @@ -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)
Expand All @@ -266,8 +265,7 @@ module.exports = function aspect(seq, dataTypes) {
}

return seq.Promise.resolve(true);
});
})
}))
.then(() => resolve(inst))
.catch(reject);
});
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions db/model/lens.js
Expand Up @@ -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);
Expand All @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions db/model/perspective.js
Expand Up @@ -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);
Expand All @@ -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,
Expand Down

0 comments on commit 0492115

Please sign in to comment.