Repository navigation
Expand file tree
/
Copy pathProjectController.js
More file actions
957 lines (839 loc) · 35.1 KB
/
Copy pathProjectController.js
File metadata and controls
957 lines (839 loc) · 35.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
/**
* ProjectController
*
* @module :: Controller
* @description :: A set of functions called `actions`.
*
* Actions contain code telling Sails how to respond to a certain type of request.
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
*
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
* and/or override them with custom routes (`config/routes.js`)
*
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
*
* @docs :: http://sailsjs.org/#!documentation/controllers
*
* @bugs :: api/controllers/ProjectController.js:636:62
*/
var async = require("async");
module.exports = {
/**
* Overrides for the settings in `config/controllers.js`
* (specific to ProjectController)
*/
_config: {},
/**
* Project add action. This will render a GUI for new project add.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
add: function(request, response) {
async.parallel(
{
// Fetch users
users: function(callback) {
DataService.getUsers({}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error
* @param {{users: sails.model.user[]}} data
*/
function(error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(data);
}
}
);
},
/**
* Project edit action. This will render a GUI for specified project edit.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
edit: function(request, response) {
var projectId = parseInt(request.param("id"), 10);
async.parallel(
{
// Fetch project data.
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch taskboard user data.
users: function(callback) {
DataService.getUsers({}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error
* @param {{
* project: sails.model.project
* users: sails.model.user[]
* }} data Object that contains 'project' and 'users' objects
*/
function(error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(data);
}
}
);
},
/**
* Project backlog action that will render a GUI that contains all stories
* that are not yet assigned to any sprint. These stories are in project backlog.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
backlog: function(request, response) {
var projectId = parseInt(request.param("id"), 10);
async.parallel(
{
// Fetch user role
role: function(callback) {
AuthService.hasProjectAccess(request.user, projectId, callback, true);
},
// Fetch project data.
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch project stories that are in backlog.
stories: function(callback) {
DataService.getStories({
projectId: projectId,
sprintId: 0
}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error
* @param {{
* role: sails.helper.role
* project: sails.model.project
* stories: sails.model.story[]
* }} data Data that contains 'role', 'project' and 'stories'
*/
function(error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(data);
}
}
);
},
/**
* Project milestones action, this will render a GUI that shows all milestones for specified
* project and some statistics data of those progress.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
milestones: function(request, response) {
var projectId = parseInt(request.param("id"), 10);
var data = {
role: 0,
milestones: false,
project: false,
progressMilestones: 0,
progressStories: 0,
progressTasks: 0,
cntMilestonesDone: 0,
cntMilestonesTotal: 0,
cntStoriesDone: 0,
cntStoriesTotal: 0,
cntTasksDone: 0,
cntTasksTotal: 0
};
async.parallel(
{
// Fetch user role
role: function(callback) {
AuthService.hasProjectAccess(request.user, projectId, callback, true);
},
// Fetch project data.
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch project milestone data.
milestones: function(callback) {
DataService.getMilestones({projectId: projectId}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error
* @param {{
* role: sails.helper.role
* project: sails.model.project
* milestones: sails.model.milestone[]
* }} results
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.role = results.role;
data.project = results.project;
data.milestones = results.milestones;
data.cntMilestonesTotal = data.milestones.length;
// Iterate each milestones and fetch stories of those
async.each(data.milestones, fetchStories, renderView);
}
}
);
/**
* Private function to fetch all stories that belongs to specified milestone.
*
* @param {sails.model.milestone} milestone Milestone object
* @param {Function} callback Callback function to call when processing is done
*/
function fetchStories(milestone, callback) {
milestone.stories = false;
// Fetch stories that belongs to specified milestone
DataService.getStories({milestoneId: milestone.id}, function(error, stories) {
if (error) {
callback(error);
} else {
milestone.doneStories = _.reduce(stories, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
data.cntStoriesTotal = data.cntStoriesTotal + stories.length;
data.cntStoriesDone = data.cntStoriesDone + milestone.doneStories;
if (milestone.doneStories > 0) {
if (stories.length === milestone.doneStories) {
data.cntMilestonesDone = data.cntMilestonesDone + 1;
}
milestone.progress = Math.round(milestone.doneStories / stories.length * 100);
} else {
milestone.progress = 0;
}
var storyIds = _.map(stories, function(story) {
return { storyId: story.id };
});
// Add stories to milestone data
milestone.stories = stories;
// We have some stories, so fetch tasks of those
if (storyIds.length > 0) {
fetchTasks(milestone, storyIds, callback);
} else {
// Add tasks to milestone data
milestone.tasks = [];
callback(null);
}
}
});
}
/**
* Private function to fetch all tasks that belongs to specified stories.
*
* @param {sails.model.milestone} milestone Milestone object
* @param {{storyId: {Number}}[]} storyIds Story ids as an array of objects
* @param {Function} callback Callback function to call whenever process is "done"
*/
function fetchTasks(milestone, storyIds, callback) {
// Fetch tasks
DataService.getTasks({or: storyIds}, function(error, tasks) {
if (error) {
callback(error);
} else {
data.cntTasksTotal = data.cntTasksTotal + tasks.length;
data.cntTasksDone = data.cntTasksDone + _.reduce(tasks, function(memo, task) {
return (task.isDone) ? memo + 1 : memo;
}, 0);
// Add tasks to milestone data
milestone.tasks = tasks;
callback(null);
}
});
}
/**
* Private function to render actual view. This is called whenever error occurs in
* sub-processes or all of those jobs are processed successfully.
*
* @param {null|Error} error Possible error
*/
function renderView(error) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
if (data.cntMilestonesDone > 0) {
data.progressMilestones = Math.round(data.cntMilestonesDone / data.cntMilestonesTotal * 100);
} else {
data.progressMilestones = 0;
}
if (data.cntStoriesDone > 0) {
data.progressStories = Math.round(data.cntStoriesDone / data.cntStoriesTotal * 100);
} else {
data.progressStories = 0;
}
if (data.cntTasksDone > 0) {
data.progressTasks = Math.round(data.cntTasksDone / data.cntTasksTotal * 100);
} else {
data.progressTasks = 0;
}
response.view(data);
}
}
},
/**
* Project planning action. This will render a simple GUI for project planning
* where user can drag&drop stories between sprints and project backlog.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
planning: function(request, response) {
var projectId = parseInt(request.param("id"), 10);
async.parallel(
{
// Fetch project data.
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch project story data
stories: function(callback) {
DataService.getStories({projectId: projectId}, callback);
},
// Fetch project sprint data
sprints: function(callback) {
DataService.getSprints({projectId: projectId}, callback);
},
// Fetch user role
role: function(callback) {
AuthService.hasProjectAccess(request.user, projectId, callback, true);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error
* @param {{
* project: sails.model.project,
* stories: sails.model.story[],
* sprints: sails.model.sprint[],
* role: sails.helper.role
* }} data Object that contains 'project', 'stories', 'sprints' and 'role'
*/
function(error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(data);
}
}
);
},
/**
* Project statistics action. This is really "heavy" action to process because basically we're
* fetching whole project data from database and make some calculations for that data.
*
* @todo should we just return basic data and handle drill downs by clicking on the gui?
* @todo should we store some statistic data to main objects, so calculations are not needed?
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
statistics: function(request, response) {
var projectId = parseInt(request.param("id"), 10);
// Specify template data to use
var data = {
project: {
data: false
},
milestones: {
data: false,
stories: [],
progress: 0,
cntDone: 0,
cntTotal: 0
},
sprints: {
data: false,
stories: [],
progress: 0,
cntDone: 0,
cntTotal: 0
},
stories: {
data: false,
progress: 0,
cntDone: 0,
cntTotal: 0
},
tasks: {
data: false,
progress: 0,
cntDone: 0,
cntTotal: 0
}
};
async.parallel(
{
// Fetch project data
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch project milestones
milestones: function(callback) {
DataService.getMilestones({projectId: projectId}, callback);
},
// Fetch project sprint data
sprints: function(callback) {
DataService.getSprints({projectId: projectId}, callback);
},
// Fetch project phases data
phases: function(callback) {
DataService.getPhases({projectId: projectId}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error
* @param {{
* project: sails.model.project,
* milestones: sails.model.milestone[],
* sprints: sails.model.sprint[],
* phases: sails.model.phase[]
* }} results Object that contains 'project', 'milestones', 'sprints' and 'phases'
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.project.data = results.project;
data.milestones.data = results.milestones;
data.milestones.cntTotal = results.milestones.length;
data.sprints.data = results.sprints;
data.sprints.cntTotal = results.sprints.length;
data.phases = results.phases;
// Fetch needed related data for view
fetchRelatedData();
}
}
);
/**
* Private function to fetch needed related data that are needed for GUI. Related data
* are following: sum of phase durations, stories, and tasks that are attached to these
* stories.
*
* Note that tasks are fetched in sub process in story fetching.
*/
function fetchRelatedData() {
async.parallel(
[
// Determine phase durations
function(callback) {
fetchPhaseDuration(callback);
},
// Fetch stories and task data of those
function(callback) {
fetchStories(callback);
}
],
/**
* Callback function which is called after all parallel jobs are done.
*
* @param {null|Error} error Possible error
*/
function(error) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
// Calculate statistics data
makeDetailedStatistics();
// Render view
response.view(data);
}
}
);
}
/**
* Private function to fetch project task phase duration times. This will sum tasks durations
* for each phase in this project.
*
* @param {Function} next Main callback function which must be called after all is done
*/
function fetchPhaseDuration(next) {
async.map(
data.phases,
/**
* Function to determine duration in specified phase.
*
* @param {sails.model.phase} phase
* @param {Function} callback
*/
function(phase, callback) {
PhaseDuration
.find({
sum: "duration"
})
.where({phaseId: phase.id})
.where({projectId: data.project.data.id})
.done(function(error, result) {
if (error) {
callback(error);
} else {
phase.duration = result[0].duration ? result[0].duration : 0;
callback(null);
}
});
},
/**
* Main callback function which is called after all phases are processed.
*
* @param {null|Error} error Possible error
*/
function(error) {
next(error)
}
);
}
/**
* Private function to fetch all stories that are attached to current project. Note that if stories
* are found in project function triggers task fetch process, which will eventually call the main
* callback function.
*
* @param {Function} next Main callback function which must be called after all is done
*/
function fetchStories(next) {
DataService.getStories({projectId: projectId}, function(error, stories) {
if (error) {
next(error)
} else {
data.stories.data = stories;
data.stories.cntTotal = stories.length;
data.stories.cntDone = _.reduce(stories, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
if (data.stories.cntDone > 0) {
data.stories.progress = Math.round(data.stories.cntDone / data.stories.cntTotal * 100);
}
if (stories.length > 0) {
fetchStoriesTasks(next);
} else {
next(null);
}
}
});
}
/**
* Private function to fetch all tasks that are attached to project stories. This is only
* called if project contains any stories.
*
* @param {Function} next Main callback function which must be called after all is done
*/
function fetchStoriesTasks(next) {
var storyIds = _.map(data.stories.data, function(story) {
return { storyId: story.id };
});
DataService.getTasks({or: storyIds}, function(error, tasks) {
if (error) {
next(error);
} else {
data.tasks.data = tasks;
data.tasks.cntTotal = tasks.length;
data.tasks.cntDone = _.reduce(tasks, function(memo, task) {
return (task.isDone) ? memo + 1 : memo;
}, 0);
if (data.tasks.cntDone > 0) {
data.tasks.progress = Math.round(data.tasks.cntDone / data.tasks.cntTotal * 100);
}
next(null);
}
});
}
/**
* Private function makes detailed statistics from fetched data. This function is
* called right before rendering the statistics GUI.
*
* Basically this is just a data "formatter" function that process fetched data and
* make some basic calculations about those.
*/
function makeDetailedStatistics() {
var totalTime = 0;
var totalTimeNoFirst = 0;
if (data.phases.length > 0) {
totalTime =_.pluck(data.phases, "duration").reduce(function(memo, i) {
return memo + i;
});
totalTimeNoFirst = _.pluck(_.reject(data.phases, function(phase) {
return phase.order === 0;
}), "duration").reduce(function(memo, i) {
return memo + i;
});
}
data.phaseDuration = {
totalTime: totalTime,
totalTimeNoFirst: totalTimeNoFirst
};
_.each(data.phases, function(phase) {
phase.durationPercentage = (phase.duration > 0 && phase.order !== 0)
? phase.duration / totalTimeNoFirst * 100 : 0;
phase.durationPercentageTotal = (phase.duration > 0) ? phase.duration / totalTime * 100 : 0;
});
_.each(data.stories.data, function(story) {
story.tasks = {
data: [],
cntTotal: 0,
cntDone: 0,
progress: 0
};
story.tasks.data = _.filter(data.tasks.data, function(task) {
return task.storyId === story.id;
});
story.tasks.cntTotal = story.tasks.data.length;
story.tasks.cntDone = _.reduce(story.tasks.data, function(memo, task) {
return (task.isDone) ? memo + 1 : memo;
}, 0);
if (story.tasks.cntDone > 0) {
story.tasks.progress = Math.round(story.tasks.cntDone / story.tasks.cntTotal * 100);
}
});
// Sort stories by tasks progress, story title and priority
data.stories.data.sort(HelperService.dynamicSortMultiple("!tasks.progress", "title", "priority"));
/**
* Iterate fetched sprints and add story data and stories statistics
* data to it
*/
_.each(data.sprints.data, function(sprint) {
sprint.stories = {
data: [],
cntTotal: 0,
cntDone: 0,
progress: 0
};
sprint.stories.data = _.filter(data.stories.data, function(story) {
return story.sprintId === sprint.id;
});
sprint.stories.cntTotal = sprint.stories.data.length;
sprint.stories.cntDone = _.reduce(sprint.stories.data, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
if (sprint.stories.cntDone > 0) {
sprint.stories.progress = Math.round(sprint.stories.cntDone / sprint.stories.cntTotal * 100);
if (sprint.stories.cntDone === sprint.stories.cntTotal) {
data.sprints.cntDone = data.sprints.cntDone + 1;
}
}
});
// Calculate sprints total progress if we have any done sprint
if (data.sprints.cntDone > 0) {
data.sprints.progress = Math.round(data.sprints.cntDone / data.sprints.cntTotal * 100);
}
_.each(data.milestones.data, function(milestone) {
milestone.stories = {
data: [],
cntTotal: 0,
cntDone: 0,
progress: 0
};
milestone.stories.data = _.filter(data.stories.data, function(story) {
return story.milestoneId === milestone.id;
});
milestone.stories.cntTotal = milestone.stories.data.length;
milestone.stories.cntDone = _.reduce(milestone.stories.data, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
if (milestone.stories.cntDone > 0) {
milestone.stories.progress = Math.round(milestone.stories.cntDone / milestone.stories.cntTotal * 100);
if (milestone.stories.cntDone === milestone.stories.cntTotal) {
data.milestones.cntDone = data.milestones.cntDone + 1;
}
}
});
if (data.milestones.cntDone > 0) {
data.milestones.progress = Math.round(data.milestones.cntDone / data.milestones.cntTotal * 100);
}
}
},
/**
* Project sprints action. This will render a GUI with project sprint progress and list of
* those. Within this list user can edit sprints.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
sprints: function(request, response) {
var projectId = parseInt(request.param("id"), 10);
var data = {
role: false,
project: false,
sprints: false,
stories: false,
tasks: false,
progressSprints: 0,
progressStories: 0,
progressTasks: 0,
cntSprintsDone: 0,
cntSprintsTotal: 0,
cntStoriesDone: 0,
cntStoriesTotal: 0,
cntTasksDone: 0,
cntTasksTotal: 0
};
async.parallel(
{
// Fetch user role
role: function(callback) {
AuthService.hasProjectAccess(request.user, projectId, callback, true);
},
// Fetch project data.
project: function(callback) {
DataService.getProject(projectId, callback);
},
// Fetch project sprint data
sprints: function(callback) {
DataService.getSprints({
projectId: projectId
}, callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {null|Error} error
* @param {{
* role: sails.helper.role,
* project: sails.model.project,
* sprints: sails.model.sprint[]
* }} results
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.role = results.role;
data.project = results.project;
data.sprints = results.sprints;
data.cntSprintsTotal = data.sprints.length;
// Fetch story data
fetchStoryData();
}
}
);
/**
* Private function to determine attached story data in current project sprints. This
* will map each sprints and fetch stories that are attached to them.
*
* If stories are found function will call private function to fetch task data of each
* sprint and determine current status of those.
*/
function fetchStoryData() {
async.map(
data.sprints,
/**
* Iterator function which is called to every sprint that project contains. This will
* fetch all attached stories of sprint and after that iterator will call private function
* to fetch tasks that are attached to sprint stories.
*
* @param {sails.model.sprint} sprint Sprint object
* @param {Function} callback Callback function to call after job is done
*/
function(sprint, callback) {
DataService.getStories({sprintId: sprint.id}, function(error, stories) {
if (error) {
callback(error, null);
} else {
// Add stories to sprint data
sprint.stories = stories;
// Calculate done stories count
sprint.doneStories = _.reduce(stories, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
// Add global story counter data
data.cntStoriesTotal = data.cntStoriesTotal + stories.length;
data.cntStoriesDone = data.cntStoriesDone + sprint.doneStories;
// Some of sprint stories are done, so calculate sprint progress
if (sprint.doneStories > 0) {
if (stories.length === sprint.doneStories) {
data.cntSprintsDone = data.cntSprintsDone + 1;
}
sprint.progress = Math.round(sprint.doneStories / stories.length * 100);
} else { // Otherwise sprint progress is zero
sprint.progress = 0;
}
// Fetch task data
fetchTaskData(sprint, callback);
}
});
},
/**
* Main callback function which will eventually render the GUI for user.
*
* @param {null|Error} error Possible error
* @param {sails.model.sprint[]} results Sprint data
*/
function(error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.sprints = results;
if (data.cntSprintsDone > 0) {
data.progressSprints = Math.round(data.cntSprintsDone / data.cntSprintsTotal * 100);
} else {
data.progressSprints = 0;
}
if (data.cntStoriesDone > 0) {
data.progressStories = Math.round(data.cntStoriesDone / data.cntStoriesTotal * 100);
} else {
data.progressStories = 0;
}
if (data.cntTasksDone > 0) {
data.progressTasks = Math.round(data.cntTasksDone / data.cntTasksTotal * 100);
} else {
data.progressTasks = 0;
}
response.view(data);
}
}
)
}
/**
* Private function to fetch task data which are attached to current sprint stories.
*
* @param {sails.model.sprint} sprint Sprint object
* @param {Function} next Callback function which must be called after this job
*/
function fetchTaskData(sprint, next) {
// Determine story id values for task data fetch
var storyIds = _.map(sprint.stories, function(story) {
return { storyId: story.id };
});
// We have stories so determine tasks of those stories
if (storyIds.length > 0) {
DataService.getTasks({or: storyIds}, function(error, tasks) {
if (error) {
next(error, null);
} else {
data.cntTasksTotal = data.cntTasksTotal + tasks.length;
data.cntTasksDone = data.cntTasksDone + _.reduce(tasks, function(memo, task) {
return (task.isDone) ? memo + 1 : memo;
}, 0);
// Add tasks to sprint data
sprint.tasks = tasks;
next(null, sprint);
}
});
} else { // Oh nou, no stories on this sprint
sprint.tasks = [];
next(null, sprint);
}
}
}
};