Repository navigation
Expand file tree
/
Copy pathProject.js
More file actions
245 lines (225 loc) · 7.35 KB
/
Copy pathProject.js
File metadata and controls
245 lines (225 loc) · 7.35 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
'use strict';
var _ = require('lodash');
var async = require('async');
/**
* /api/models/Project.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = _.merge(_.cloneDeep(require('../base/Model')), {
attributes: {
// Project title
title: {
type: 'string',
required: true,
minLength: 4
},
// Description of the project
description: {
type: 'text',
required: true,
defaultsTo: ''
},
// Project start date
dateStart: {
type: 'date',
required: true
},
// Project end date
dateEnd: {
type: 'date',
required: true
},
// Ignore weekends on this project, affects to burndown chart and phase duration calculations
ignoreWeekends: {
type: 'boolean',
defaultsTo: false
},
// Below is all specification for relations to another models
// ProjectUser objects that are related to project
projectUsers: {
collection: 'ProjectUser',
via: 'project'
},
// Phase objects that are related to project
phases: {
collection: 'Phase',
via: 'project'
},
// Task type objects that are related to project
taskTypes: {
collection: 'TaskType',
via: 'project'
},
// Sprint objects that are related to project
sprints: {
collection: 'Sprint',
via: 'project'
},
// Story objects that are related to project
stories: {
collection: 'Story',
via: 'project'
},
// Milestone objects that are related to project
milestones: {
collection: 'Milestone',
via: 'project'
},
// Epic objects that are related to project
epics: {
collection: 'Epic',
via: 'project'
},
// ExternalLink objects that are related to project
externalLinks: {
collection: 'ExternalLink',
via: 'project'
},
// PhaseDuration object that are related to project
phaseDurations: {
collection: 'PhaseDuration',
via: 'project'
},
// Dynamic data attributes
/**
* Sprint duration as in days.
*
* Note that this doesn't account possible sprint exclude days
*/
durationDays: function() {
var output = 0;
if (this.ignoreWeekends) {
var _start = this.dateStartObject().clone();
while (this.dateEndObject().diff(_start, 'days') >= 0) {
var weekDay = _start.isoWeekday();
if (weekDay !== 6 && weekDay !== 7) {
output = output + 1;
}
_start.add('days', 1);
}
} else {
output = this.dateEndObject().diff(this.dateStartObject(), 'days') + 1;
}
return output;
},
// Date start as moment object.
dateStartObject: function() {
return (this.dateStart && this.dateStart != '0000-00-00')
? sails.services['date'].convertDateObjectToUtc(this.dateStart) : null;
},
// Date end as moment object.
dateEndObject: function() {
return (this.dateEnd && this.dateEnd != '0000-00-00')
? sails.services['date'].convertDateObjectToUtc(this.dateEnd) : null;
}
},
// Lifecycle Callbacks
/**
* Before validation callback.
*
* @param {sails.model.project} values Values to create / update
* @param {Function} next Callback function
*/
beforeValidate: function(values, next) {
next();
},
/**
* Before create callback.
*
* @param {sails.model.project} values Values to create
* @param {Function} next Callback function
*/
beforeCreate: function(values, next) {
next();
},
/**
* Before update callback.
*
* @param {sails.model.project} values Values to update
* @param {Function} next Callback function
*/
beforeUpdate: function(values, next) {
next();
},
/**
* Before destroy callback.
*
* @param {{}} criteria Delete criteria
* @param {Function} next Callback function
*/
beforeDestroy: function(criteria, next) {
next();
},
/**
* After validation callback.
*
* @param {sails.model.project} values Values to create / update
* @param {Function} next Callback function
*/
afterValidate: function(values, next) {
next();
},
/**
* After create callback. This will create necessary project specified relation
* data so that users have a mock project at their hand after creation. Basically
* We will create following project related data automatic:
*
* 1) Attach create user as in 'Manager' role to project
* 2) Create default project phases
* 3) Create default project task types
*
* After creation of those, user can start work with created project right a way.
*
* @param {sails.model.project} record Newly inserted record
* @param {Function} next Callback function
*/
afterCreate: function(record, next) {
async.parallel(
{
history: function(callback) {
sails.services['history'].write('Project', record, 'Added new project', 0, callback);
},
users: function(callback) {
sails.services['project'].addDefaultProjectManager(record, callback);
},
phases: function(callback) {
sails.services['project'].addDefaultPhases(record, callback);
},
taskTypes: function(callback) {
sails.services['project'].addDefaultTaskTypes(record, callback);
}
},
function(error) {
next(error);
}
);
},
/**
* After update callback.
*
* @param {sails.model.project} record Updated record
* @param {Function} next Callback function
*/
afterUpdate: function(record, next) {
sails.services['history'].write('Project', record, 'Updated project data', 0, next);
},
/**
* After destroy callback.
*
* @param {sails.model.project[]} records Destroyed records
* @param {Function} next Callback function
*/
afterDestroy: function(records, next) {
async.each(
records,
function(record, callback) {
sails.services['history'].write('Project', record, 'Removed project', 0, callback);
},
function(error) {
next(error);
}
);
}
});