Skip to content

Commit ba14b78

Browse files
author
vikasrohit
authored
Merge pull request #110 from architectt1/feature/newEvents
Added more bus api events
2 parents 66a3322 + cfba8d3 commit ba14b78

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/constants.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export const BUS_API_EVENT = {
7171
PROJECT_CANCELED: 'notifications.connect.project.canceled',
7272
PROJECT_ACTIVE: 'notifications.connect.project.active',
7373

74+
PROJECT_PHASE_TRANSITION_ACTIVE: 'notifications.connect.project.phase.transition.active',
75+
PROJECT_PHASE_TRANSITION_COMPLETED: 'notifications.connect.project.phase.transition.completed',
76+
PROJECT_PHASE_UPDATE_PAYMENT: 'notifications.connect.project.phase.update.payment',
77+
PROJECT_PHASE_UPDATE_PROGRESS: 'notifications.connect.project.phase.update.progress',
78+
PROJECT_PHASE_UPDATE_SCOPE: 'notifications.connect.project.phase.update.scope',
79+
7480
MEMBER_JOINED: 'notifications.connect.project.member.joined',
7581
MEMBER_LEFT: 'notifications.connect.project.member.left',
7682
MEMBER_REMOVED: 'notifications.connect.project.member.removed',
@@ -81,12 +87,15 @@ export const BUS_API_EVENT = {
8187
PROJECT_LINK_CREATED: 'notifications.connect.project.linkCreated',
8288
PROJECT_FILE_UPLOADED: 'notifications.connect.project.fileUploaded',
8389
PROJECT_SPECIFICATION_MODIFIED: 'notifications.connect.project.specificationModified',
90+
PROJECT_PROGRESS_MODIFIED: 'notifications.connect.project.progressModified',
8491

8592
// When phase is added/updated/deleted from the project,
8693
// When product is added/deleted from a phase
8794
// When product is updated on any field other than specification
8895
PROJECT_PLAN_MODIFIED: 'notifications.connect.project.planModified',
8996

97+
PROJECT_PLAN_READY: 'notifications.connect.project.planReady',
98+
9099
// When specification of a product is modified
91100
PROJECT_PRODUCT_SPECIFICATION_MODIFIED: 'notifications.connect.project.productSpecificationModified',
92101
};

src/events/busApi.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _ from 'lodash';
22
import config from 'config';
3-
import { EVENT, BUS_API_EVENT, PROJECT_STATUS, PROJECT_MEMBER_ROLE } from '../constants';
3+
import { EVENT, BUS_API_EVENT, PROJECT_STATUS, PROJECT_PHASE_STATUS, PROJECT_MEMBER_ROLE } from '../constants';
44
import { createEvent } from '../services/busApi';
55
import models from '../models';
66

@@ -207,6 +207,34 @@ module.exports = (app, logger) => {
207207
}).catch(err => null); // eslint-disable-line no-unused-vars
208208
});
209209

210+
/**
211+
* If the project is in draft status and the phase is in reviewed status, and it's the
212+
* only phase in the project with that status, then send the plan ready event.
213+
*
214+
* @param req the req
215+
* @param project the project
216+
* @param phase the phase that was created/updated
217+
* @returns {Promise<void>}
218+
*/
219+
async function sendPlanReadyEventIfNeeded(req, project, phase) {
220+
if (project.status === PROJECT_STATUS.DRAFT &&
221+
phase.status === PROJECT_PHASE_STATUS.REVIEWED) {
222+
await models.ProjectPhase.count({
223+
where: { projectId: project.id, status: PROJECT_PHASE_STATUS.REVIEWED },
224+
}).then(((count) => {
225+
// only send the plan ready event when this is the only reviewed phase in the project
226+
if (count !== 1) { return; }
227+
createEvent(BUS_API_EVENT.PROJECT_PLAN_READY, {
228+
projectId: project.id,
229+
phaseId: phase.id,
230+
projectName: project.name,
231+
userId: req.authUser.userId,
232+
initiatorUserId: req.authUser.userId,
233+
}, logger);
234+
}));
235+
}
236+
}
237+
210238
/**
211239
* PROJECT_PHASE_ADDED
212240
*/
@@ -226,6 +254,8 @@ module.exports = (app, logger) => {
226254
userId: req.authUser.userId,
227255
initiatorUserId: req.authUser.userId,
228256
}, logger);
257+
258+
return sendPlanReadyEventIfNeeded(req, project, created);
229259
}).catch(err => null); // eslint-disable-line no-unused-vars
230260
});
231261

@@ -258,6 +288,7 @@ module.exports = (app, logger) => {
258288
logger.debug('receive PROJECT_PHASE_UPDATED event');
259289

260290
const projectId = _.parseInt(req.params.projectId);
291+
const phaseId = _.parseInt(req.params.phaseId);
261292

262293
models.Project.findOne({
263294
where: { id: projectId },
@@ -270,6 +301,31 @@ module.exports = (app, logger) => {
270301
userId: req.authUser.userId,
271302
initiatorUserId: req.authUser.userId,
272303
}, logger);
304+
305+
[
306+
['spentBudget', BUS_API_EVENT.PROJECT_PHASE_UPDATE_PAYMENT],
307+
['progress', [BUS_API_EVENT.PROJECT_PHASE_UPDATE_PROGRESS, BUS_API_EVENT.PROJECT_PROGRESS_MODIFIED]],
308+
['details', BUS_API_EVENT.PROJECT_PHASE_UPDATE_SCOPE],
309+
['status', BUS_API_EVENT.PROJECT_PHASE_TRANSITION_ACTIVE, PROJECT_PHASE_STATUS.ACTIVE],
310+
['status', BUS_API_EVENT.PROJECT_PHASE_TRANSITION_COMPLETED, PROJECT_PHASE_STATUS.COMPLETED],
311+
].forEach(([key, events, sendIfEqual]) => {
312+
// eslint-disable-next-line no-param-reassign
313+
events = Array.isArray(events) ? events : [events];
314+
315+
// send event(s) only if the target field's value was updated, or when an update matches a "sendIfEqual" value
316+
if ((!sendIfEqual && !_.isEqual(original[key], updated[key])) ||
317+
(original[key] !== sendIfEqual && updated[key] === sendIfEqual)) {
318+
events.forEach(event => createEvent(event, {
319+
projectId,
320+
phaseId,
321+
projectName: project.name,
322+
userId: req.authUser.userId,
323+
initiatorUserId: req.authUser.userId,
324+
}, logger));
325+
}
326+
});
327+
328+
return sendPlanReadyEventIfNeeded(req, project, updated);
273329
}).catch(err => null); // eslint-disable-line no-unused-vars
274330
});
275331

0 commit comments

Comments
 (0)