Skip to content

Commit

Permalink
--no-fat-arrow
Browse files Browse the repository at this point in the history
CodeClimate didn't like 'em...
  • Loading branch information
danieljsummers committed Aug 30, 2017
1 parent 3f45e26 commit a57f3c1
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,26 @@ module.exports = function(ctx) {

Post.method('setCategories', function(cats) {
// Remove empty categories, preserving hierarchies
cats = cats.filter(cat =>
Array.isArray(cat) ? true : cat != null && cat !== ''
).map(cat =>
Array.isArray(cat) ? removeEmptyTag(cat) : cat + ''
);
cats = cats.filter(function(cat) {
return Array.isArray(cat) ? true : cat != null && cat !== '';
}).map(function(cat) {
return Array.isArray(cat) ? removeEmptyTag(cat) : cat + '';
});

var PostCategory = ctx.model('PostCategory');
var Category = ctx.model('Category');
var id = this._id;
var allIds = [];
var existed = PostCategory.find({post_id: id}, {lean: true}).map(pickID);
var hasHierarchy = cats.filter(cat => Array.isArray(cat)).length > 0;
var hasHierarchy = cats.filter(Array.isArray).length > 0;

// Add a hierarchy of categories
var addHierarchy = catHierarchy => {
var addHierarchy = function(catHierarchy) {
var parentIds = [];
if (!Array.isArray(catHierarchy)) catHierarchy = [catHierarchy];
// Don't use "Promise.map". It doesn't run in series.
// MUST USE "Promise.each".
return Promise.each(catHierarchy, (cat, i) => {
return Promise.each(catHierarchy, function(cat, i) {
// Find the category by name
var data = Category.findOne({
name: cat,
Expand All @@ -170,7 +170,7 @@ module.exports = function(ctx) {
var obj = {name: cat};
if (i) obj.parent = parentIds[i - 1];

return Category.insert(obj).catch(err => {
return Category.insert(obj).catch(function(err) {
// Try to find the category again. Throw the error if not found
var data = Category.findOne({
name: cat,
Expand All @@ -179,18 +179,18 @@ module.exports = function(ctx) {

if (data) return data;
throw err;
}).then(data => {
}).then(function(data) {
allIds.push(data._id);
parentIds.push(data._id);
return data;
});
});
};

return (hasHierarchy
? Promise.each(cats, addHierarchy)
: Promise.resolve(addHierarchy(cats))
).then(() => allIds).map(catId => {
return (hasHierarchy ? Promise.each(cats, addHierarchy) : Promise.resolve(addHierarchy(cats))
).then(function() {
return allIds;
}).map(function(catId) {
// Find the reference
var ref = PostCategory.findOne({post_id: id, category_id: catId}, {lean: true});
if (ref) return ref;
Expand All @@ -200,10 +200,12 @@ module.exports = function(ctx) {
post_id: id,
category_id: catId
});
}).then(postCats =>
}).then(function(postCats) {
// Remove old categories
_.difference(existed, postCats.map(pickID))
).map(cat => PostCategory.removeById(cat));
return _.difference(existed, postCats.map(pickID));
}).map(function(cat) {
return PostCategory.removeById(cat);
});
});

// Remove PostTag references
Expand Down

0 comments on commit a57f3c1

Please sign in to comment.