Skip to content

Commit bf88813

Browse files
author
Maksym Mykhailenko
committed
fix: Kafka events when updating milestones
- don't raise status change event on every milestone update - removed/commented redundant code for raising events during cascading updates
1 parent 675a42b commit bf88813

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

src/events/busApi.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _ from 'lodash';
2-
import moment from 'moment';
32
import config from 'config';
43
import {
54
EVENT,
@@ -715,7 +714,6 @@ module.exports = (app, logger) => {
715714
req,
716715
resource,
717716
originalResource,
718-
cascadedUpdates,
719717
skipNotification,
720718
}) => { // eslint-disable-line no-unused-vars
721719
logger.debug(`receive MILESTONE_UPDATED event for milestone ${resource.id}`);
@@ -742,15 +740,9 @@ module.exports = (app, logger) => {
742740
timeline.progress = progress;
743741
sendMilestoneNotification(req, original, updated, project, timeline);
744742

745-
logger.debug('cascadedUpdates', cascadedUpdates);
746-
if (cascadedUpdates && cascadedUpdates.milestones && cascadedUpdates.milestones.length > 0) {
747-
_.each(cascadedUpdates.milestones, cascadedUpdate =>
748-
sendMilestoneNotification(req, cascadedUpdate.original, cascadedUpdate.updated, project, timeline),
749-
);
750-
}
751-
743+
// TODO raise this event again
752744
// if timeline is modified
753-
if (cascadedUpdates && cascadedUpdates.timeline) {
745+
/* if (cascadedUpdates && cascadedUpdates.timeline) {
754746
const cTimeline = cascadedUpdates.timeline;
755747
// if endDate of the timeline is modified, raise TIMELINE_ADJUSTED event
756748
if (!moment(cTimeline.original.endDate).isSame(cTimeline.updated.endDate)) {
@@ -766,7 +758,7 @@ module.exports = (app, logger) => {
766758
initiatorUserId: req.authUser.userId,
767759
}, logger);
768760
}
769-
}
761+
} */
770762
});
771763
}).catch(err => null); // eslint-disable-line no-unused-vars
772764
}

src/routes/milestones/bulkUpdate.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,20 @@ module.exports = [
9999
[
100100
[created, EVENT.ROUTING_KEY.MILESTONE_ADDED],
101101
[deleted, EVENT.ROUTING_KEY.MILESTONE_REMOVED],
102-
[updated, EVENT.ROUTING_KEY.MILESTONE_UPDATED],
103102
].forEach(([results, routingKey]) =>
104103
results.forEach(result => util.sendResourceToKafkaBus(req, routingKey, RESOURCES.MILESTONE, result)),
105104
);
106105

106+
updated.forEach(({ updated: updatedMilestone, original: originalMilestone }) => {
107+
util.sendResourceToKafkaBus(
108+
req,
109+
EVENT.ROUTING_KEY.MILESTONE_UPDATED,
110+
RESOURCES.MILESTONE,
111+
updatedMilestone,
112+
originalMilestone,
113+
);
114+
});
115+
107116
// return all the timeline milestones after all updates
108117
const milestones = await req.timeline.getMilestones()
109118
.map(milestone => _.omit(milestone.toJSON(), ['deletedAt', 'deletedBy']));

src/routes/milestones/commonHelper.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function deleteMilestone(authUser, timelineId, id, transaction, item) {
6565
* @param {Object} data The updated data
6666
* @param {Object} transaction The transaction to use
6767
* @param {Object} [item] The item to update
68-
* @returns {Object} The updated milestone
68+
* @returns {{updated: Object, original: Object}} The updated and original milestones
6969
* @throws {Error} If something went wrong
7070
*/
7171
async function updateMilestone(authUser, timelineId, data, transaction, item) {
@@ -87,6 +87,9 @@ async function updateMilestone(authUser, timelineId, data, transaction, item) {
8787
apiErr.status = 404;
8888
throw apiErr;
8989
}
90+
91+
const original = milestone.toJSON();
92+
9093
if (entityToUpdate.status === MILESTONE_STATUS.PAUSED && !validStatuses.includes(milestone.status)) {
9194
const validStatutesStr = validStatuses.join(', ');
9295
const apiErr = new Error(`Milestone can only be paused from the next statuses: ${validStatutesStr}`);
@@ -196,7 +199,10 @@ async function updateMilestone(authUser, timelineId, data, transaction, item) {
196199
*/
197200

198201
const result = await milestone.update(entityToUpdate, { comment: entityToUpdate.statusComment, transaction });
199-
return _.omit(result.toJSON(), ['deletedBy', 'deletedAt']);
202+
return {
203+
original: _.omit(original, ['deletedBy', 'deletedAt']),
204+
updated: _.omit(result.toJSON(), ['deletedBy', 'deletedAt']),
205+
};
200206
}
201207

202208

src/routes/milestones/update.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ module.exports = [
5959
req.params.timelineId,
6060
Object.assign({}, req.body, { id: req.params.milestoneId }),
6161
t))
62-
.then((result) => {
62+
.then(({ updated, original }) => {
6363
util.sendResourceToKafkaBus(
6464
req,
6565
EVENT.ROUTING_KEY.MILESTONE_UPDATED,
6666
RESOURCES.MILESTONE,
67-
result);
68-
res.json(result);
67+
updated,
68+
original,
69+
);
70+
res.json(updated);
6971
})
7072
.catch(next),
7173
];

src/routes/milestones/update.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ describe('UPDATE Milestone', () => {
12091209
done(err);
12101210
} else {
12111211
testUtil.wait(() => {
1212-
createEventSpy.callCount.should.be.eql(4);
1212+
createEventSpy.callCount.should.be.eql(3);
12131213

12141214
createEventSpy.calledWith(BUS_API_EVENT.MILESTONE_UPDATED, sinon.match({
12151215
resource: RESOURCES.MILESTONE,
@@ -1302,7 +1302,7 @@ describe('UPDATE Milestone', () => {
13021302
done(err);
13031303
} else {
13041304
testUtil.wait(() => {
1305-
createEventSpy.callCount.should.be.eql(3);
1305+
createEventSpy.callCount.should.be.eql(2);
13061306

13071307
createEventSpy.calledWith(BUS_API_EVENT.MILESTONE_UPDATED, sinon.match({
13081308
resource: RESOURCES.MILESTONE,
@@ -1329,7 +1329,7 @@ describe('UPDATE Milestone', () => {
13291329
done(err);
13301330
} else {
13311331
testUtil.wait(() => {
1332-
createEventSpy.callCount.should.be.eql(3);
1332+
createEventSpy.callCount.should.be.eql(2);
13331333

13341334
createEventSpy.calledWith(BUS_API_EVENT.MILESTONE_UPDATED, sinon.match({
13351335
resource: RESOURCES.MILESTONE,

0 commit comments

Comments
 (0)