@@ -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 ) ;
0 commit comments