Skip to content

Commit 6f5a9a6

Browse files
author
vikasrohit
authored
Merge pull request #191 from topcoder-platform/feature/phase-topic-title-mismatch-squashed
GitHub topic#2609, When we edit phase titles, respective topic tiles are not updated in the sidebar
2 parents 22d12e3 + 99b624c commit 6f5a9a6

File tree

2 files changed

+92
-10
lines changed

2 files changed

+92
-10
lines changed

src/events/projectPhases/index.js

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,14 @@ const projectPhaseAddedHandler = Promise.coroutine(function* (logger, msg, chann
107107
});
108108

109109
/**
110-
* Handler for project phase updated event
110+
* Indexes the project phase in the elastic search.
111+
*
111112
* @param {Object} logger logger to log along with trace id
112-
* @param {Object} msg event payload
113-
* @param {Object} channel channel to ack, nack
113+
* @param {Object} data event payload
114114
* @returns {undefined}
115115
*/
116-
const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, channel) { // eslint-disable-line func-names
116+
const updateIndexProjectPhase = Promise.coroutine(function* (logger, data) { // eslint-disable-line func-names
117117
try {
118-
const data = JSON.parse(msg.content.toString());
119118
const doc = yield eClient.get({ index: ES_PROJECT_INDEX, type: ES_PROJECT_TYPE, id: data.original.projectId });
120119
const phases = _.map(data.allPhases, single => _.omit(single, ['deletedAt', 'deletedBy']));
121120
const merged = _.assign(doc._source, { phases }); // eslint-disable-line no-underscore-dangle
@@ -127,7 +126,59 @@ const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, cha
127126
doc: merged,
128127
},
129128
});
130-
logger.debug('elasticsearch index updated, project phase updated successfully');
129+
logger.debug('project phase updated to project document successfully');
130+
} catch (error) {
131+
logger.error('Error handling indexing the project phase', error);
132+
// throw the error back to nack the bus
133+
throw error;
134+
}
135+
});
136+
137+
/**
138+
* Creates a new phase topic in message api.
139+
*
140+
* @param {Object} logger logger to log along with trace id
141+
* @param {Object} msg event payload
142+
* @returns {undefined}
143+
*/
144+
const updatePhaseTopic = Promise.coroutine(function* (logger, phase) { // eslint-disable-line func-names
145+
try {
146+
logger.debug('Updating topic for phase with phase', phase);
147+
const topic = yield messageService.getPhaseTopic(phase.projectId, phase.id, logger);
148+
logger.trace('Topic', topic);
149+
const title = phase.name;
150+
const titleChanged = topic && topic.title !== title;
151+
logger.trace('titleChanged', titleChanged);
152+
const contentPost = topic && topic.posts && topic.posts.length > 0 ? topic.posts[0] : null;
153+
logger.trace('contentPost', contentPost);
154+
const postId = _.get(contentPost, 'id');
155+
const content = _.get(contentPost, 'body');
156+
if (postId && content && titleChanged) {
157+
const updatedTopic = yield messageService.updateTopic(topic.id, { title, postId, content }, logger);
158+
logger.debug('topic for the phase updated successfully');
159+
logger.trace('updated topic', updatedTopic);
160+
}
161+
} catch (error) {
162+
logger.error('Error in updating topic for the project phase', error);
163+
// don't throw the error back to nack the bus, because we don't want to get multiple topics per phase
164+
// we can create topic for a phase manually, if somehow it fails
165+
}
166+
});
167+
168+
/**
169+
* Handler for project phase updated event
170+
* @param {Object} logger logger to log along with trace id
171+
* @param {Object} msg event payload
172+
* @param {Object} channel channel to ack, nack
173+
* @returns {undefined}
174+
*/
175+
const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, channel) { // eslint-disable-line func-names
176+
try {
177+
const data = JSON.parse(msg.content.toString());
178+
logger.debug('calling updateIndexProjectPhase', data);
179+
yield updateIndexProjectPhase(logger, data, channel);
180+
logger.debug('calling updatePhaseTopic', data.updated);
181+
yield updatePhaseTopic(logger, data.updated);
131182
channel.ack(msg);
132183
} catch (error) {
133184
logger.error('Error handling project.phase.updated event', error);

src/services/messageService.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ function createTopic(topic, logger) {
8787
});
8888
}
8989

90+
/**
91+
* Updates the given topic in message api
92+
*
93+
* @param {Number} topicId id of the topic to be updated
94+
* @param {Object} topic the topic, should be a JSON object
95+
* @param {Object} logger object
96+
* @return {Promise} new topic promise
97+
*/
98+
function updateTopic(topicId, topic, logger) {
99+
logger.debug(`updateTopic for topic: ${JSON.stringify(topic)}`);
100+
return getClient(logger).then((msgClient) => {
101+
logger.debug('calling message service');
102+
return msgClient.post(`/topics/${topicId}/edit`, topic)
103+
.then((resp) => {
104+
logger.debug('Topic updated successfully');
105+
logger.debug(`Topic updated successfully [status]: ${resp.status}`);
106+
logger.debug(`Topic updated successfully [data]: ${resp.data}`);
107+
return _.get(resp.data, 'result.content', {});
108+
})
109+
.catch((error) => {
110+
logger.debug('Error updating topic');
111+
logger.error(error);
112+
// eslint-disable-line
113+
});
114+
}).catch((errMessage) => {
115+
logger.debug(errMessage);
116+
});
117+
}
118+
90119
/**
91120
* Deletes the given posts for the given topic.
92121
*
@@ -121,12 +150,13 @@ function deletePosts(topicId, postIds, logger) {
121150
* @return {Promise} topic promise
122151
*/
123152
function getPhaseTopic(projectId, phaseId, logger) {
124-
logger.debug(`getPhaseTopic for phaseId: ${phaseId}`);
153+
logger.debug(`getPhaseTopic for projectId: ${projectId} phaseId: ${phaseId}`);
125154
return getClient(logger).then((msgClient) => {
126155
logger.debug(`calling message service for fetching phaseId#${phaseId}`);
127-
return msgClient.get('/topics/list', {
128-
params: { filter: `reference=project&referenceId=${projectId}&tag=phase#${phaseId}` },
129-
}).then((resp) => {
156+
const encodedFilter = encodeURIComponent(`reference=project&referenceId=${projectId}&tag=phase#${phaseId}`);
157+
return msgClient.get(`/topics/list/db?filter=${encodedFilter}`)
158+
.then((resp) => {
159+
logger.debug('Fetched phase topic', resp);
130160
const topics = _.get(resp.data, 'result.content', []);
131161
if (topics && topics.length > 0) {
132162
return topics[0];
@@ -153,6 +183,7 @@ function deleteTopic(topicId, logger) {
153183

154184
module.exports = {
155185
createTopic,
186+
updateTopic,
156187
deletePosts,
157188
getPhaseTopic,
158189
deleteTopic,

0 commit comments

Comments
 (0)