Skip to content

GitHub topic#2609, When we edit phase titles, respective topic tiles are not updated in the sidebar #191

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 1 commit into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 57 additions & 6 deletions src/events/projectPhases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ const projectPhaseAddedHandler = Promise.coroutine(function* (logger, msg, chann
});

/**
* Handler for project phase updated event
* Indexes the project phase in the elastic search.
*
* @param {Object} logger logger to log along with trace id
* @param {Object} msg event payload
* @param {Object} channel channel to ack, nack
* @param {Object} data event payload
* @returns {undefined}
*/
const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, channel) { // eslint-disable-line func-names
const updateIndexProjectPhase = Promise.coroutine(function* (logger, data) { // eslint-disable-line func-names
try {
const data = JSON.parse(msg.content.toString());
const doc = yield eClient.get({ index: ES_PROJECT_INDEX, type: ES_PROJECT_TYPE, id: data.original.projectId });
const phases = _.map(data.allPhases, single => _.omit(single, ['deletedAt', 'deletedBy']));
const merged = _.assign(doc._source, { phases }); // eslint-disable-line no-underscore-dangle
Expand All @@ -127,7 +126,59 @@ const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, cha
doc: merged,
},
});
logger.debug('elasticsearch index updated, project phase updated successfully');
logger.debug('project phase updated to project document successfully');
} catch (error) {
logger.error('Error handling indexing the project phase', error);
// throw the error back to nack the bus
throw error;
}
});

/**
* Creates a new phase topic in message api.
*
* @param {Object} logger logger to log along with trace id
* @param {Object} msg event payload
* @returns {undefined}
*/
const updatePhaseTopic = Promise.coroutine(function* (logger, phase) { // eslint-disable-line func-names
try {
logger.debug('Updating topic for phase with phase', phase);
const topic = yield messageService.getPhaseTopic(phase.projectId, phase.id, logger);
logger.trace('Topic', topic);
const title = phase.name;
const titleChanged = topic && topic.title !== title;
logger.trace('titleChanged', titleChanged);
const contentPost = topic && topic.posts && topic.posts.length > 0 ? topic.posts[0] : null;
logger.trace('contentPost', contentPost);
const postId = _.get(contentPost, 'id');
const content = _.get(contentPost, 'body');
if (postId && content && titleChanged) {
const updatedTopic = yield messageService.updateTopic(topic.id, { title, postId, content }, logger);
logger.debug('topic for the phase updated successfully');
logger.trace('updated topic', updatedTopic);
}
} catch (error) {
logger.error('Error in updating topic for the project phase', error);
// don't throw the error back to nack the bus, because we don't want to get multiple topics per phase
// we can create topic for a phase manually, if somehow it fails
}
});

/**
* Handler for project phase updated event
* @param {Object} logger logger to log along with trace id
* @param {Object} msg event payload
* @param {Object} channel channel to ack, nack
* @returns {undefined}
*/
const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, channel) { // eslint-disable-line func-names
try {
const data = JSON.parse(msg.content.toString());
logger.debug('calling updateIndexProjectPhase', data);
yield updateIndexProjectPhase(logger, data, channel);
logger.debug('calling updatePhaseTopic', data.updated);
yield updatePhaseTopic(logger, data.updated);
channel.ack(msg);
} catch (error) {
logger.error('Error handling project.phase.updated event', error);
Expand Down
39 changes: 35 additions & 4 deletions src/services/messageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ function createTopic(topic, logger) {
});
}

/**
* Updates the given topic in message api
*
* @param {Number} topicId id of the topic to be updated
* @param {Object} topic the topic, should be a JSON object
* @param {Object} logger object
* @return {Promise} new topic promise
*/
function updateTopic(topicId, topic, logger) {
logger.debug(`updateTopic for topic: ${JSON.stringify(topic)}`);
return getClient(logger).then((msgClient) => {
logger.debug('calling message service');
return msgClient.post(`/topics/${topicId}/edit`, topic)
.then((resp) => {
logger.debug('Topic updated successfully');
logger.debug(`Topic updated successfully [status]: ${resp.status}`);
logger.debug(`Topic updated successfully [data]: ${resp.data}`);
return _.get(resp.data, 'result.content', {});
})
.catch((error) => {
logger.debug('Error updating topic');
logger.error(error);
// eslint-disable-line
});
}).catch((errMessage) => {
logger.debug(errMessage);
});
}

/**
* Deletes the given posts for the given topic.
*
Expand Down Expand Up @@ -121,12 +150,13 @@ function deletePosts(topicId, postIds, logger) {
* @return {Promise} topic promise
*/
function getPhaseTopic(projectId, phaseId, logger) {
logger.debug(`getPhaseTopic for phaseId: ${phaseId}`);
logger.debug(`getPhaseTopic for projectId: ${projectId} phaseId: ${phaseId}`);
return getClient(logger).then((msgClient) => {
logger.debug(`calling message service for fetching phaseId#${phaseId}`);
return msgClient.get('/topics/list', {
params: { filter: `reference=project&referenceId=${projectId}&tag=phase#${phaseId}` },
}).then((resp) => {
const encodedFilter = encodeURIComponent(`reference=project&referenceId=${projectId}&tag=phase#${phaseId}`);
return msgClient.get(`/topics/list/db?filter=${encodedFilter}`)
.then((resp) => {
logger.debug('Fetched phase topic', resp);
const topics = _.get(resp.data, 'result.content', []);
if (topics && topics.length > 0) {
return topics[0];
Expand All @@ -153,6 +183,7 @@ function deleteTopic(topicId, logger) {

module.exports = {
createTopic,
updateTopic,
deletePosts,
getPhaseTopic,
deleteTopic,
Expand Down