From 55ada2a7cab8ea5a8bbd676be23149cf76df30f4 Mon Sep 17 00:00:00 2001 From: Gian Franco Zabarino Date: Mon, 30 Jul 2018 20:51:15 -0300 Subject: [PATCH] - Fixed issue where identical data were being treated as different and then triggering milestones' updates. - Do not trigger milestones' updates if the timeline endDate changed. - Added comments to milestones' updates when a timeline gets updated. --- src/routes/timelines/update.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/routes/timelines/update.js b/src/routes/timelines/update.js index 5cbfad5d..e91783e4 100644 --- a/src/routes/timelines/update.js +++ b/src/routes/timelines/update.js @@ -57,22 +57,31 @@ module.exports = [ // Omit deletedAt, deletedBy updated = _.omit(updatedTimeline.toJSON(), ['deletedAt', 'deletedBy']); - // Update milestones startDate and endDate if necessary - if (original.startDate !== updated.startDate || original.endDate !== updated.endDate) { + // Update milestones startDate and endDate if necessary, if the timeline startDate changed + if (original.startDate.getTime() !== updated.startDate.getTime()) { return updatedTimeline.getMilestones() .then((milestones) => { let startDate = updated.startDate; + + // Process milestones in order const updateMilestonePromises = _.chain(milestones).sortBy('order').map((_milestone) => { const milestone = _milestone; + + // Update if the iterating startDate is different than the saved one if (milestone.startDate.getTime() !== startDate.getTime()) { milestone.startDate = startDate; milestone.updatedBy = req.authUser.userId; } + + // Make sure the endDate is the correct, i.e. for duration = 1 it should be equal to the start date, + // for duration = 2 it should be equal to the next day and so on... const endDate = moment.utc(milestone.startDate).add(milestone.duration - 1, 'days').toDate(); if (!milestone.endDate || endDate.getTime() !== milestone.endDate.getTime()) { milestone.endDate = endDate; milestone.updatedBy = req.authUser.userId; } + + // Next iterated milestone should have as startDate this milestone's endDate plus one day startDate = moment.utc(milestone.endDate).add(1, 'days').toDate(); return milestone.save(); }).value();