Skip to content

Added more bus api events #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export const BUS_API_EVENT = {
PROJECT_CANCELED: 'notifications.connect.project.canceled',
PROJECT_ACTIVE: 'notifications.connect.project.active',

PROJECT_PHASE_TRANSITION_ACTIVE: 'project.phase.transition.active',
Copy link

@vikasrohit vikasrohit Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@architectt1 @gondzo I think we need to keep notifications.connect as prefix for all new events as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I'll push the change shortly

PROJECT_PHASE_TRANSITION_COMPLETED: 'project.phase.transition.completed',
PROJECT_PHASE_UPDATE_PAYMENT: 'project.phase.update.payment',
PROJECT_PHASE_UPDATE_PROGRESS: 'project.phase.update.progress',
PROJECT_PHASE_UPDATE_SCOPE: 'project.phase.update.scope',

MEMBER_JOINED: 'notifications.connect.project.member.joined',
MEMBER_LEFT: 'notifications.connect.project.member.left',
MEMBER_REMOVED: 'notifications.connect.project.member.removed',
Expand All @@ -71,12 +77,15 @@ export const BUS_API_EVENT = {
PROJECT_LINK_CREATED: 'notifications.connect.project.linkCreated',
PROJECT_FILE_UPLOADED: 'notifications.connect.project.fileUploaded',
PROJECT_SPECIFICATION_MODIFIED: 'notifications.connect.project.specificationModified',
PROJECT_PROGRESS_MODIFIED: 'notifications.connect.project.progressModified',

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

PROJECT_PLAN_READY: 'notifications.connect.project.planReady',

// When specification of a product is modified
PROJECT_PRODUCT_SPECIFICATION_MODIFIED: 'notifications.connect.project.productSpecificationModified',
};
Expand Down
58 changes: 57 additions & 1 deletion src/events/busApi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import 'config';
import { EVENT, BUS_API_EVENT, PROJECT_STATUS, PROJECT_MEMBER_ROLE } from '../constants';
import { EVENT, BUS_API_EVENT, PROJECT_STATUS, PROJECT_PHASE_STATUS, PROJECT_MEMBER_ROLE } from '../constants';
import { createEvent } from '../services/busApi';
import models from '../models';

Expand Down Expand Up @@ -177,6 +177,34 @@ module.exports = (app, logger) => {
}).catch(err => null); // eslint-disable-line no-unused-vars
});

/**
* If the project is in draft status and the phase is in reviewed status, and it's the
* only phase in the project with that status, then send the plan ready event.
*
* @param req the req
* @param project the project
* @param phase the phase that was created/updated
* @returns {Promise<void>}
*/
async function sendPlanReadyEventIfNeeded(req, project, phase) {
if (project.status === PROJECT_STATUS.DRAFT &&
phase.status === PROJECT_PHASE_STATUS.REVIEWED) {
await models.ProjectPhase.count({
where: { projectId: project.id, status: PROJECT_PHASE_STATUS.REVIEWED },
}).then(((count) => {
// only send the plan ready event when this is the only reviewed phase in the project
if (count !== 1) { return; }
createEvent(BUS_API_EVENT.PROJECT_PLAN_READY, {
projectId: project.id,
phaseId: phase.id,
projectName: project.name,
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
}, logger);
}));
}
}

/**
* PROJECT_PHASE_ADDED
*/
Expand All @@ -195,6 +223,8 @@ module.exports = (app, logger) => {
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
}, logger);

return sendPlanReadyEventIfNeeded(req, project, created);
}).catch(err => null); // eslint-disable-line no-unused-vars
});

Expand Down Expand Up @@ -226,6 +256,7 @@ module.exports = (app, logger) => {
logger.debug('receive PROJECT_PHASE_UPDATED event');

const projectId = _.parseInt(req.params.projectId);
const phaseId = _.parseInt(req.params.phaseId);

models.Project.findOne({
where: { id: projectId },
Expand All @@ -237,6 +268,31 @@ module.exports = (app, logger) => {
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
}, logger);

[
['spentBudget', BUS_API_EVENT.PROJECT_PHASE_UPDATE_PAYMENT],
['progress', [BUS_API_EVENT.PROJECT_PHASE_UPDATE_PROGRESS, BUS_API_EVENT.PROJECT_PROGRESS_MODIFIED]],
['details', BUS_API_EVENT.PROJECT_PHASE_UPDATE_SCOPE],
['status', BUS_API_EVENT.PROJECT_PHASE_TRANSITION_ACTIVE, PROJECT_PHASE_STATUS.ACTIVE],
['status', BUS_API_EVENT.PROJECT_PHASE_TRANSITION_COMPLETED, PROJECT_PHASE_STATUS.COMPLETED],
].forEach(([key, events, sendIfEqual]) => {
// eslint-disable-next-line no-param-reassign
events = Array.isArray(events) ? events : [events];

// send event(s) only if the target field's value was updated, or when an update matches a "sendIfEqual" value
if ((!sendIfEqual && !_.isEqual(original[key], updated[key])) ||
(original[key] !== sendIfEqual && updated[key] === sendIfEqual)) {
events.forEach(event => createEvent(event, {
projectId,
phaseId,
projectName: project.name,
userId: req.authUser.userId,
initiatorUserId: req.authUser.userId,
}, logger));
}
});

return sendPlanReadyEventIfNeeded(req, project, updated);
}).catch(err => null); // eslint-disable-line no-unused-vars
});

Expand Down