Skip to content

Commit 63abc6e

Browse files
author
Vikas Agarwal
committed
Fixed logic to trigger cascade updates when a milestone is marked as active earlier or later than its scheduled start
1 parent 14dc571 commit 63abc6e

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/routes/milestones/update.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ module.exports = [
246246
});
247247
})
248248
.then(() => {
249-
// Update dates of the other milestones only if the completionDate or the duration changed
250-
if (!_.isEqual(original.completionDate, updated.completionDate) || original.duration !== updated.duration) {
249+
// we need to recalculate change in fields because we update some fields before making actual update
250+
const needToCascade = !_.isEqual(original.completionDate, updated.completionDate) // completion date changed
251+
|| original.duration !== updated.duration // duration changed
252+
|| original.actualStartDate !== updated.actualStartDate; // actual start date updated
253+
// Update dates of the other milestones only if cascade updates needed
254+
if (needToCascade) {
251255
return updateComingMilestones(original, updated)
252256
.then(({ originalMilestones, updatedMilestones }) => {
253257
// finds the last milestone updated

src/routes/milestones/update.spec.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,66 @@ describe('UPDATE Milestone', () => {
841841
});
842842
});
843843

844+
it('should return 200 for admin - marking milestone active later will cascade changes to coming ' +
845+
// eslint-disable-next-line func-names
846+
'milestones', function (done) {
847+
this.timeout(10000);
848+
const today = moment.utc().hours(0).minutes(0).seconds(0)
849+
.milliseconds(0);
850+
851+
request(server)
852+
.patch('/v4/timelines/1/milestones/2')
853+
.set({
854+
Authorization: `Bearer ${testUtil.jwts.admin}`,
855+
})
856+
.send({ param: { status: MILESTONE_STATUS.ACTIVE } })
857+
.expect(200)
858+
.end(() => {
859+
// Milestone 2: startDate: '2018-05-14T00:00:00.000Z' to '2018-05-14T00:00:00.000Z'
860+
// actualStartDate: null to today
861+
// endDate: null to today + 2 (2 = duration - 1)
862+
// Milestone 3: startDate: '2018-05-14T00:00:00.000Z' to today + 3
863+
// endDate: null to today + 5 (5 = 3 + duration - 1)
864+
// Milestone 4: startDate: '2018-05-14T00:00:00.000Z' to today + 6
865+
// endDate: null to today + 8 (2 = 6 + duration - 1)
866+
models.Milestone.findById(2)
867+
.then((milestone) => {
868+
should.exist(milestone.actualStartDate);
869+
moment.utc(milestone.actualStartDate).diff(today, 'days').should.be.eql(0);
870+
// start date of the updated milestone should not change
871+
milestone.startDate.should.be.eql(new Date('2018-05-14T00:00:00.000Z'));
872+
today.add('days', milestone.duration - 1);
873+
// end date of the updated milestone should change, as delayed start caused scheduled to be delayed
874+
moment.utc(milestone.endDate).diff(today, 'days').should.be.eql(0);
875+
milestone.status.should.be.eql(MILESTONE_STATUS.ACTIVE);
876+
return models.Milestone.findById(3);
877+
})
878+
.then((milestone) => {
879+
today.add('days', 1); // should have start date next to previous one's end date
880+
moment.utc(milestone.startDate).diff(today, 'days').should.be.eql(0);
881+
should.not.exist(milestone.actualStartDate);
882+
today.add('days', milestone.duration - 1);
883+
moment.utc(milestone.endDate).diff(today, 'days').should.be.eql(0);
884+
return models.Milestone.findById(4);
885+
})
886+
.then((milestone) => {
887+
today.add('days', 1); // should have start date next to previous one's end date
888+
moment.utc(milestone.startDate).diff(today, 'days').should.be.eql(0);
889+
should.not.exist(milestone.actualStartDate);
890+
today.add('days', milestone.duration - 1);
891+
moment.utc(milestone.endDate).diff(today, 'days').should.be.eql(0);
892+
done();
893+
})
894+
.catch(done);
895+
});
896+
});
897+
844898
it('should return 200 for admin - changing completionDate will cascade changes to coming ' +
845899
// eslint-disable-next-line func-names
846900
'milestones', function (done) {
847901
this.timeout(10000);
902+
const today = moment.utc().hours(0).minutes(0).seconds(0)
903+
.milliseconds(0);
848904

849905
request(server)
850906
.patch('/v4/timelines/1/milestones/2')
@@ -863,7 +919,6 @@ describe('UPDATE Milestone', () => {
863919
models.Milestone.findById(3)
864920
.then((milestone) => {
865921
milestone.startDate.should.be.eql(new Date('2018-05-19T00:00:00.000Z'));
866-
const today = moment.utc();
867922
should.exist(milestone.actualStartDate);
868923
moment().utc(milestone.actualStartDate).diff(today, 'days').should.be.eql(0);
869924
// milestone.actualStartDate.should.be.eql(today);

0 commit comments

Comments
 (0)