Skip to content

Commit

Permalink
fix stories.all, return all stories
Browse files Browse the repository at this point in the history
  • Loading branch information
gaboesquivel committed Oct 2, 2017
1 parent 1663a43 commit 2feee75
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
65 changes: 41 additions & 24 deletions lib/resources/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Source, Story resource structure:
https://www.pivotaltracker.com/help/api/rest/v5#story_resource
*/

var activity = require('./activity'),
var merge = require('lodash.merge'),
activity = require('./activity'),
comment = require('./comment'),
label = require('./label'),
task = require('./task'),
Expand Down Expand Up @@ -445,24 +445,56 @@ Service.prototype.task = function(taskId) {
};

Service.prototype.all = function(options, cb) { // cb(err, stories[])
var defaultOptions = {
limit: 500,
offset: 0
};
if (typeof options === 'function' && typeof cb === 'undefined') {
cb = options;
options = null; // options is an optional param
options = {}; // options is an optional param
}

if (!this.projectId) {
cb(new Error('Invalid project ID'), null);
}
else {
ptutil.api.get(
options = merge(defaultOptions, options);
var allStories = []

function getStories (allStories) {
var _this = this;

return ptutil.api.get(
this.config.trackerToken,
this.pathSegments(),
options, // query
this.config, // options
function(error, res) {
_callbackWithStories(error, res, cb);
if(error) {
return cb(error);
}

if (!error && res && Array.isArray(res.data) && res.data.length) {
res.data.forEach(function(ele) {
allStories.push(new Story(ele));
});
}

if(res.data.length <= 0) {
return cb(null, allStories);
}

// recursion in 500 batches, currently there's no way to tell how many piv stories in total
options.limit+=500;
options.offset+=500;
getStories.call(_this, allStories);
});
};


if (!this.projectId) {
cb(new Error('Invalid project ID'), null);
} else {
return getStories.call(this, allStories);
}


};

Service.prototype.get = function(cb) { // cb(err, story)
Expand Down Expand Up @@ -560,21 +592,6 @@ function _callbackWithStory(error, res, cb) {
cb(error, result);
}

function _callbackWithStories(error, res, cb) {

var arr = [];

if (!error && res && Array.isArray(res.data) && res.data.length) {

res.data.forEach(function(ele) {

arr.push(new Story(ele));
});
}

cb(error, arr);
}

function _serviceToString() {
var str = 'token: '+this.config.trackerToken+'\n';
str += 'projectId'+this.projectId+'\n';
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"test": "nodeunit ./tests/unit/run.js"
},
"dependencies": {
"request": "2.33.x",
"form-data": "~0.1.2"
"form-data": "~0.1.2",
"lodash.merge": "^4.6.0",
"request": "2.33.x"
},
"devDependencies": {
"nodeunit": "latest",
Expand Down

0 comments on commit 2feee75

Please sign in to comment.