@@ -9,19 +9,20 @@ import Sequelize from 'sequelize';
9
9
import { middleware as tcMiddleware } from 'tc-core-library-js' ;
10
10
import util from '../../util' ;
11
11
import validateTimeline from '../../middlewares/validateTimeline' ;
12
- import { EVENT } from '../../constants' ;
12
+ import { EVENT , MILESTONE_STATUS } from '../../constants' ;
13
13
import models from '../../models' ;
14
14
15
15
const permissions = tcMiddleware . permissions ;
16
16
17
17
/**
18
18
* Cascades endDate/completionDate changes to all milestones with a greater order than the given one.
19
+ * @param {Object } originalMilestone the original milestone that was updated
19
20
* @param {Object } updatedMilestone the milestone that was updated
20
21
* @returns {Promise<void> } a promise that resolves to the last found milestone. If no milestone exists with an
21
22
* order greater than the passed <b>updatedMilestone</b>, the promise will resolve to the passed
22
23
* <b>updatedMilestone</b>
23
24
*/
24
- function updateComingMilestones ( updatedMilestone ) {
25
+ function updateComingMilestones ( originalMilestone , updatedMilestone ) {
25
26
return models . Milestone . findAll ( {
26
27
where : {
27
28
timelineId : updatedMilestone . timelineId ,
@@ -32,7 +33,7 @@ function updateComingMilestones(updatedMilestone) {
32
33
let startDate = moment . utc ( updatedMilestone . completionDate
33
34
? updatedMilestone . completionDate
34
35
: updatedMilestone . endDate ) . add ( 1 , 'days' ) . toDate ( ) ;
35
- const promises = _ . map ( comingMilestones , ( _milestone ) => {
36
+ const promises = _ . map ( comingMilestones , ( _milestone , idx ) => {
36
37
const milestone = _milestone ;
37
38
38
39
// Update the milestone startDate if different than the iterated startDate
@@ -48,6 +49,12 @@ function updateComingMilestones(updatedMilestone) {
48
49
milestone . updatedBy = updatedMilestone . updatedBy ;
49
50
}
50
51
52
+ // if completionDate is alerted, update status of the next milestone to the current one
53
+ if ( ! _ . isEqual ( originalMilestone . completionDate , updatedMilestone . completionDate ) && idx === 0 ) {
54
+ // activate next milestone
55
+ milestone . status = MILESTONE_STATUS . ACTIVE ;
56
+ }
57
+
51
58
// Set the next startDate value to the next day after completionDate if present or the endDate
52
59
startDate = moment . utc ( milestone . completionDate
53
60
? milestone . completionDate
@@ -132,14 +139,33 @@ module.exports = [
132
139
}
133
140
134
141
original = _ . omit ( milestone . toJSON ( ) , [ 'deletedAt' , 'deletedBy' ] ) ;
142
+ const durationChanged = entityToUpdate . duration && entityToUpdate . duration !== milestone . duration ;
143
+ const statusChanged = entityToUpdate . status && entityToUpdate . status !== milestone . status ;
144
+ const completionDateChanged = entityToUpdate . completionDate
145
+ && ! _ . isEqual ( milestone . completionDate , entityToUpdate . completionDate ) ;
135
146
136
147
// Merge JSON fields
137
148
entityToUpdate . details = util . mergeJsonObjects ( milestone . details , entityToUpdate . details ) ;
138
149
139
- if ( entityToUpdate . duration && entityToUpdate . duration !== milestone . duration ) {
150
+ if ( durationChanged ) {
140
151
entityToUpdate . endDate = moment . utc ( milestone . startDate ) . add ( entityToUpdate . duration - 1 , 'days' ) . toDate ( ) ;
141
152
}
142
153
154
+ // if status has changed
155
+ if ( statusChanged ) {
156
+ // if status has changed to be completed, set the compeltionDate if not provided
157
+ if ( entityToUpdate . status === MILESTONE_STATUS . COMPLETED ) {
158
+ const today = moment . utc ( ) . hours ( 0 ) . minutes ( 0 ) . seconds ( 0 )
159
+ . milliseconds ( 0 ) ;
160
+ entityToUpdate . completionDate = entityToUpdate . completionDate ? entityToUpdate . completionDate : today ;
161
+ }
162
+ }
163
+
164
+ // if completionDate has changed
165
+ if ( ! statusChanged && completionDateChanged ) {
166
+ entityToUpdate . status = MILESTONE_STATUS . COMPLETED ;
167
+ }
168
+
143
169
// Update
144
170
return milestone . update ( entityToUpdate ) ;
145
171
} )
@@ -190,7 +216,7 @@ module.exports = [
190
216
. then ( ( ) => {
191
217
// Update dates of the other milestones only if the completionDate or the duration changed
192
218
if ( ! _ . isEqual ( original . completionDate , updated . completionDate ) || original . duration !== updated . duration ) {
193
- return updateComingMilestones ( updated )
219
+ return updateComingMilestones ( original , updated )
194
220
. then ( ( lastTimelineMilestone ) => {
195
221
if ( ! _ . isEqual ( lastTimelineMilestone . endDate , timeline . endDate ) ) {
196
222
timeline . endDate = lastTimelineMilestone . endDate ;
0 commit comments