Repository navigation
Expand file tree
/
Copy pathMilestoneController.js
More file actions
249 lines (221 loc) · 8.97 KB
/
Copy pathMilestoneController.js
File metadata and controls
249 lines (221 loc) · 8.97 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
/**
* MilestoneController
*
* @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
*/
"use strict";
var async = require("async");
module.exports = {
/**
* Overrides for the settings in `config/controllers.js`
* (specific to MilestoneController)
*/
_config: {},
/**
* Milestone add action. This will render GUI for new milestone add. This GUI is shown
* in bootstrap modal in board.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
add: function(request, response) {
var projectId = parseInt(request.param("projectId"), 10);
// Fetch user role and project data
async.parallel(
{
// Determine user role in this project
role: function(callback) {
AuthService.hasProjectAccess(request.user, projectId, callback, true);
},
// Fetch project data
project: function(callback) {
DataService.getProject(projectId, callback);
}
},
/**
* Main callback function which is called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error object
* @param {null|{}} data Data object that contains 'role' and 'project' data
*/
function(error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.projectId = projectId;
response.view(data);
}
}
);
},
/**
* Milestone edit action. This will render GUI for specified milestone object edit.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
edit: function(request, response) {
var milestoneId = parseInt(request.param("id"), 10);
// Fetch role and milestone data
async.parallel(
{
// Determine user role in this milestone
role: function(callback) {
AuthService.hasMilestoneAccess(request.user, milestoneId, callback, true);
},
// Fetch milestone data
milestone: function(callback) {
DataService.getMilestone(milestoneId, callback)
}
},
/**
* Callback function which is called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error object
* @param {null|{}} data Data object that contains 'role' and 'milestone' data
*/
function (error, data) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
response.view(data);
}
}
);
},
/**
* Milestone stories action.
*
* @param {Request} request Request object
* @param {Response} response Response object
*/
stories: function(request, response) {
var milestoneId = parseInt(request.param("id"), 10);
var data = {
role: 0,
milestone: {
data: false,
progressStory: 0,
progressTask: 0,
cntStoryDone: 0,
cntStoryTotal: 0,
cntTaskDone: 0,
cntTaskTotal: 0
},
stories: false
};
// Fetch role, milestone and stories data
async.parallel(
{
// Determine user role in this milestone
role: function(callback) {
AuthService.hasMilestoneAccess(request.user, milestoneId, callback, true);
},
// Fetch milestone data
milestone: function(callback) {
DataService.getMilestone(milestoneId, callback)
},
// Fetch stories that are attached to this milestone
stories: function(callback) {
DataService.getStories({milestoneId: milestoneId}, callback);
}
},
/**
* Callback function which is called after all parallel jobs are processed.
*
* @param {null|Error} error Possible error object
* @param {{}} results Data object that contains 'role', 'milestone' and 'stories' data
*/
function (error, results) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
data.role = results.role;
data.milestone.data = results.milestone;
data.stories = results.stories;
fetchTasks();
}
}
);
/**
* Private function to fetch tasks of stories in current milestone.
*
* This will make also required calculations for milestone total and story
* specified task statistics data.
*/
function fetchTasks() {
data.milestone.cntStoryTotal = data.stories.length;
data.milestone.cntStoryDone = _.reduce(data.stories, function(memo, story) {
return (story.isDone) ? memo + 1 : memo;
}, 0);
if (data.milestone.cntStoryDone > 0) {
data.milestone.progressStory = Math.round(data.milestone.cntStoryDone / data.milestone.cntStoryTotal * 100);
}
// Map each stories and fetch task data for those
async.map(
data.stories,
/**
* Iterator function to process single story object and fetch task data for that.
*
* @param {sails.model.story} story Story object
* @param {Function} callback Callback function to call after story is processed
*/
function(story, callback) {
Task
.find()
.where({
storyId: story.id
})
.sort("title ASC")
.done(function(error, tasks) {
if (error) {
callback(error, null);
} else {
// Add tasks to story data
story.tasks = tasks;
story.doneTasks = _.reduce(tasks, function (memo, task) {
return (task.isDone) ? memo + 1 : memo;
}, 0);
// Add milestone task counter
data.milestone.cntTaskTotal = data.milestone.cntTaskTotal + story.tasks.length;
data.milestone.cntTaskDone = data.milestone.cntTaskDone + story.doneTasks;
if (story.doneTasks > 0) {
story.progress = Math.round(story.doneTasks / tasks.length * 100);
} else {
story.progress = 0;
}
callback(null, true);
}
});
},
/**
* Callback function which is call after are stories are processed.
*
* @param {null|Error} error Possible error object
* @param {null|Boolean} result Result data
*/
function(error, result) {
if (error) {
ResponseService.makeError(error, request, response);
} else {
if (data.milestone.cntTaskDone > 0) {
data.milestone.progressTask = Math.round(data.milestone.cntTaskDone / data.milestone.cntTaskTotal * 100);
}
response.view(data);
}
}
);
}
}
};