Repository navigation
Expand file tree
/
Copy pathTaskboardInitService.js
More file actions
427 lines (400 loc) · 14.9 KB
/
Copy pathTaskboardInitService.js
File metadata and controls
427 lines (400 loc) · 14.9 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
/**
* /api/services/TaskboardInitService.js
*
* Taskboard init service. This is called only from config/bootstrap.js file on lifting sails.
*/
"use strict";
var async = require("async");
var moment = require("moment-timezone");
/**
* Main taskboard app initialize method. Basically this will do following jobs:
*
* - Create admin user, if he/she doesn't exists
* - Task types, if those doesn't exists already
* - Demo project, if no projects on database
*
* Each of these jobs are done in separate TaskboardInitService methods.
*
* @param {Function} next Callback function that must be called after all jobs.
*/
exports.init = function(next) {
// Call all necessary init jobs parallel
async.parallel(
{
// Create admin user account
admin: function(callback) {
TaskboardInitService.createAdmin(callback);
},
// Create default task types
types: function(callback) {
TaskboardInitService.createTypes(callback);
}
},
/**
* Callback function which is been called after all parallel jobs are processed.
*
* @param {Error} error
* @param {{}} results
*/
function(error, results) {
if (error) {
next(error, null);
} else {
// Create demo project if none exists
TaskboardInitService.initDemoProject(results, next);
}
}
);
};
/**
* This method creates admin user to database if it doesn't exists already.
*
* @param {Function} next Callback function to call after jobs
*/
exports.createAdmin = function(next) {
User
.findOne({username: "admin"})
.done(function(error, user) {
if (error) {
next(error, null);
} else if (!user) {
// Create admin user with default data
User
.create({
username: "admin",
firstName: "John",
lastName: "Doe",
email: "john.doe@localhost.com",
admin: true,
password: "taskboardisawesome"
})
.done(function(error, user) {
next(error, user);
});
} else {
next(null, user);
}
});
};
/**
* This method creates admin user to database if it doesn't exists already.
*
* @param {Function} next Callback function to call after jobs
*/
exports.createTypes = function(next) {
Type
.find()
.done(function(error, types) {
if (error) {
next(error, null);
} else if (_.size(types) === 0) {
// Specify "default" type data
var defaultTypes = [
{title: "Task", order: "1", class: "alert alert-warning", classText: "text-warning"},
{title: "Test", order: "2", class: "alert alert-success", classText: "text-success"},
{title: "Bug", order: "3", class: "alert alert-danger", classText: "text-danger"}
];
// Create default types and pass those to callback function
async.map(
defaultTypes,
function(type, callback) {
Type
.create(type)
.done(function(error, type) {
callback(error, type);
});
},
function(error, types) {
next(error, types)
}
)
} else {
next(null, types);
}
});
};
/**
* This method creates demo project to database. Note that this project is
* created only if any project doesn't exist in database yet.
*
* @param {{}} data Object that contains following data:
* - admin, user object
* - types, array of task type objects
* @param {Function} next Callback function that must be called after demo project init.
*/
exports.initDemoProject = function(data, next) {
async.waterfall(
[
// Create project
function(callback) {
TaskboardInitService.createProject(data, callback);
},
// Create phases for project
function (project, callback) {
if (!project) {
callback(null, null, null);
} else {
TaskboardInitService.createPhases(project, callback);
}
},
// Create sprints
function(project, phases, callback) {
if (!project) {
callback(null, null, null, null);
} else {
TaskboardInitService.createSprint(project, phases, callback);
}
},
// Create stories
function(project, phases, sprint, callback) {
if (!project) {
callback(null, null, null, null, null);
} else {
TaskboardInitService.createStories(data, project, phases, sprint, callback);
}
},
// Create tasks
function(project, phases, sprint, stories, callback) {
if (!project) {
callback(null, null, null, null, null, null);
} else {
TaskboardInitService.createTasks(data, project, phases, sprint, stories, callback);
}
}
],
/**
* Callback function which is been called after all specified jobs are processed.
*
* @param {Error|String} error
* @param {Null|sails.model.project} project Project object
* @param {Null|sails.model.phase[]} phases Phase objects as an array
* @param {Null|sails.model.sprint} sprint Sprint object
* @param {Null|sails.model.story[]} stories Story objects as an array
* @param {Null|sails.model.task[]} tasks Task objects as an array
*/
function(error, project, phases, sprint, stories, tasks) {
var output = {
project: project,
phases: phases,
sprint: sprint,
stories: stories,
tasks: tasks
};
next(error);
}
);
};
/**
* This method creates a demo project to taskboard if none exists in database.
*
* @param {{}} data Object that contains following data:
* - admin, user object
* - types, array of task type objects
* @param {Function} next Callback function to call after job is finished
*/
exports.createProject = function(data, next) {
Project
.find()
.done(function(error, projects) {
if (error) {
next(error, null);
} else if (_.size(projects) === 0) { // No project(s) found create demo project
// Specify demo project data
var projectData = {
managerId: data.admin.id,
title: "Demo project",
description: "This is a demo project, which is created automatic if any other projects doesn't exists yet.",
dateStart: moment().format("YYYY-MM-DD"),
dateEnd: moment().add("years", 1).format("YYYY-MM-DD")
};
// Create demo project
Project
.create(projectData)
.done(function(error, project) {
if (error) {
console.log(projectData);
console.log(error);
} else {
next(error, project);
}
});
} else { // Database contains projects so skip demo project init
next(null, null);
}
});
};
/**
* This method creates phases for specified project.
*
* @param {sails.model.project} project Project object
* @param {Function} next Callback function to call after job is finished
*/
exports.createPhases = function(project, next) {
// Specify project phase data
var phaseData = [
{projectId: project.id, title: "Tasks", order: 1, tasks: 0, isDone: false},
{projectId: project.id, title: "In process", order: 2, tasks: 3, isDone: false},
{projectId: project.id, title: "To review", order: 3, tasks: 3, isDone: false},
{projectId: project.id, title: "Reviewed", order: 4, tasks: 6, isDone: false},
{projectId: project.id, title: "Done", order: 5, tasks: 0, isDone: true}
];
// Create specified phases
async.map(
phaseData,
function(story, callback) {
Phase
.create(story)
.done(function(error, story) {
callback(error, story);
});
},
function(error, phases) {
next(error, project, phases);
}
);
};
/**
* This method creates sprints for specified demo project.
*
* @param {sails.model.project} project Project object
* @param {sails.model.phase[]} phases Array of phase objects
* @param {Function} next Callback function to call after job is finished
*/
exports.createSprint = function(project, phases, next) {
// Specify sprint data
var sprintData = {
projectId: project.id,
title: "First sprint",
description: "This is a first sprint in demo project.",
dateStart: moment(project.dateStart).format("YYYY-MM-DD"),
dateEnd: moment(project.dateStart).add("days", 30).format("YYYY-MM-DD")
};
// Create new sprint
Sprint
.create(sprintData)
.done(function(error, sprint) {
next(error, project, phases, sprint);
});
};
/**
* Method creates stories to default stories to demo project and assign those to
* specified demo sprint and project backlog.
*
* @param {{}} data Object that contains following data:
* - admin, user object
* - types, array of task type objects
* @param {sails.model.project} project Project object
* @param {sails.model.phase[]} phases Array of phase objects
* @param {sails.model.sprint} sprint Sprint object
* @param {Function} next Callback function to call after job is finished
*/
exports.createStories = function(data, project, phases, sprint, next) {
var typeTask = _.find(data.types, function(type) { return type.title === "Task"; });
// Specify story data
var storyData = [
{
projectId: project.id,
sprintId: sprint.id,
milestoneId: 0,
typeId: typeTask.id,
title: "My first user story",
description: "This is a basic user story where you can add new tasks.",
estimate: 5,
priority: 1
},
{
projectId: project.id,
sprintId: sprint.id,
milestoneId: 0,
typeId: typeTask.id,
title: "Another user story",
description: "You can add new user stories to current sprint by clicking '+' button on the board header row.",
estimate: -1,
priority: 2
},
{
projectId: project.id,
sprintId: 0,
milestoneId: 0,
typeId: typeTask.id,
title: "Planned user story",
description: "This user story has no relation to any existing sprint, so it is in project backlog, where you can prioritize it.",
estimate: 5,
priority: 1
}
];
// Create specified user stories
async.map(
storyData,
function(story, callback) {
Story
.create(story)
.done(function(error, story) {
callback(error, story);
});
},
function(error, stories) {
next(error, project, phases, sprint, stories);
}
);
};
/**
* Method creates some demo tasks to specified project sprint.
*
* @param {{}} data Object that contains following data:
* - admin, user object
* - types, array of task type objects
* @param {sails.model.project} project Project object
* @param {sails.model.phase[]} phases Array of phase objects
* @param {sails.model.sprint} sprint Sprint object
* @param {sails.model.story[]} stories Array of story objects
* @param {Function} next Callback function to call after job is finished
*/
exports.createTasks = function(data, project, phases, sprint, stories, next) {
var typeTask = _.find(data.types, function(type) { return type.title === "Task"; });
var typeTest = _.find(data.types, function(type) { return type.title === "Test"; });
var typeBug = _.find(data.types, function(type) { return type.title === "Bug"; });
var phaseFirst = _.find(phases, function(phase) { return phase.title === "Tasks"; });
var storyFirst = _.find(stories, function(story) { return story.title === "My first user story"; });
// Specify task data
var taskData = [
{
storyId: storyFirst.id,
userId: data.admin.id,
phaseId: phaseFirst.id,
typeId: typeTask.id,
title: "Normal task",
description: "This is a 'normal' task, which you can drag around the board."
},
{
storyId: storyFirst.id,
userId: data.admin.id,
phaseId: phaseFirst.id,
typeId: typeTest.id,
title: "Test task",
description: "This is a 'test' task, usually this means writing some kind of test to this user story."
},
{
storyId: storyFirst.id,
userId: data.admin.id,
phaseId: phaseFirst.id,
typeId: typeBug.id,
title: "Major bug in app",
description: "This is a 'bug' task, you can track bugs with these tasks in each user story."
}
];
// Create specified tasks
async.map(
taskData,
function(task, callback) {
Task
.create(task)
.done(function(error, task) {
callback(error, task);
});
},
function(error, tasks) {
next(error, project, phases, sprint, stories, tasks);
}
);
};