@@ -7,20 +7,21 @@ import config from 'config';
7
7
import _ from 'lodash' ;
8
8
import Promise from 'bluebird' ;
9
9
import util from '../../util' ;
10
+ import messageService from '../../services/messageService' ;
10
11
11
12
const ES_PROJECT_INDEX = config . get ( 'elasticsearchConfig.indexName' ) ;
12
13
const ES_PROJECT_TYPE = config . get ( 'elasticsearchConfig.docType' ) ;
13
14
14
15
const eClient = util . getElasticSearchClient ( ) ;
15
16
16
17
/**
17
- * Handler for project phase creation event
18
+ * Indexes the project phase in the elastic search.
19
+ *
18
20
* @param {Object } logger logger to log along with trace id
19
21
* @param {Object } msg event payload
20
- * @param {Object } channel channel to ack, nack
21
22
* @returns {undefined }
22
23
*/
23
- const projectPhaseAddedHandler = Promise . coroutine ( function * ( logger , msg , channel ) { // eslint-disable-line func-names
24
+ const indexProjectPhase = Promise . coroutine ( function * ( logger , msg ) { // eslint-disable-line func-names
24
25
try {
25
26
const data = JSON . parse ( msg . content . toString ( ) ) ;
26
27
const doc = yield eClient . get ( { index : ES_PROJECT_INDEX , type : ES_PROJECT_TYPE , id : data . projectId } ) ;
@@ -29,6 +30,50 @@ const projectPhaseAddedHandler = Promise.coroutine(function* (logger, msg, chann
29
30
const merged = _ . assign ( doc . _source , { phases } ) ; // eslint-disable-line no-underscore-dangle
30
31
yield eClient . update ( { index : ES_PROJECT_INDEX , type : ES_PROJECT_TYPE , id : data . projectId , body : { doc : merged } } ) ;
31
32
logger . debug ( 'project phase added to project document successfully' ) ;
33
+ } catch ( error ) {
34
+ logger . error ( 'Error handling indexing the project phase' , error ) ;
35
+ // throw the error back to nack the bus
36
+ throw error ;
37
+ }
38
+ } ) ;
39
+
40
+ /**
41
+ * Creates a new phase topic in message api.
42
+ *
43
+ * @param {Object } logger logger to log along with trace id
44
+ * @param {Object } msg event payload
45
+ * @returns {undefined }
46
+ */
47
+ const createPhaseTopic = Promise . coroutine ( function * ( logger , msg ) { // eslint-disable-line func-names
48
+ try {
49
+ const phase = JSON . parse ( msg . content . toString ( ) ) ;
50
+ const topic = yield messageService . createTopic ( {
51
+ reference : 'project' ,
52
+ referenceId : `${ phase . projectId } ` ,
53
+ tag : `phase#${ phase . id } ` ,
54
+ title : phase . name ,
55
+ body : 'Welcome!!! Please use this channel for communication around the phase.' ,
56
+ } , logger ) ;
57
+ logger . debug ( 'topic for the phase created successfully' ) ;
58
+ logger . debug ( topic ) ;
59
+ } catch ( error ) {
60
+ logger . error ( 'Error in creating topic for the project phase' , error ) ;
61
+ // don't throw the error back to nack the bus, because we don't want to get multiple topics per phase
62
+ // we can create topic for a phase manually, if somehow it fails
63
+ }
64
+ } ) ;
65
+
66
+ /**
67
+ * Handler for project phase creation event
68
+ * @param {Object } logger logger to log along with trace id
69
+ * @param {Object } msg event payload
70
+ * @param {Object } channel channel to ack, nack
71
+ * @returns {undefined }
72
+ */
73
+ const projectPhaseAddedHandler = Promise . coroutine ( function * ( logger , msg , channel ) { // eslint-disable-line func-names
74
+ try {
75
+ yield indexProjectPhase ( logger , msg , channel ) ;
76
+ yield createPhaseTopic ( logger , msg ) ;
32
77
channel . ack ( msg ) ;
33
78
} catch ( error ) {
34
79
logger . error ( 'Error handling project.phase.added event' , error ) ;
@@ -73,13 +118,13 @@ const projectPhaseUpdatedHandler = Promise.coroutine(function* (logger, msg, cha
73
118
} ) ;
74
119
75
120
/**
76
- * Handler for project phase deleted event
121
+ * Removes the project phase from the elastic search.
122
+ *
77
123
* @param {Object } logger logger to log along with trace id
78
124
* @param {Object } msg event payload
79
- * @param {Object } channel channel to ack, nack
80
125
* @returns {undefined }
81
126
*/
82
- const projectPhaseRemovedHandler = Promise . coroutine ( function * ( logger , msg , channel ) { // eslint-disable-line func-names
127
+ const removePhaseFromIndex = Promise . coroutine ( function * ( logger , msg ) { // eslint-disable-line func-names
83
128
try {
84
129
const data = JSON . parse ( msg . content . toString ( ) ) ;
85
130
const doc = yield eClient . get ( { index : ES_PROJECT_INDEX , type : ES_PROJECT_TYPE , id : data . projectId } ) ;
@@ -94,6 +139,45 @@ const projectPhaseRemovedHandler = Promise.coroutine(function* (logger, msg, cha
94
139
} ,
95
140
} ) ;
96
141
logger . debug ( 'project phase removed from project document successfully' ) ;
142
+ } catch ( error ) {
143
+ logger . error ( 'Error in removing project phase from index' , error ) ;
144
+ // throw the error back to nack the bus
145
+ throw error ;
146
+ }
147
+ } ) ;
148
+
149
+ /**
150
+ * Removes the phase topic from the message api.
151
+ *
152
+ * @param {Object } logger logger to log along with trace id
153
+ * @param {Object } msg event payload
154
+ * @returns {undefined }
155
+ */
156
+ const removePhaseTopic = Promise . coroutine ( function * ( logger , msg ) { // eslint-disable-line func-names
157
+ try {
158
+ const phase = JSON . parse ( msg . content . toString ( ) ) ;
159
+ const phaseTopic = yield messageService . getPhaseTopic ( phase . projectId , phase . id , logger ) ;
160
+ yield messageService . deletePosts ( phaseTopic . id , phaseTopic . postIds , logger ) ;
161
+ yield messageService . deleteTopic ( phaseTopic . id , logger ) ;
162
+ logger . debug ( 'topic for the phase removed successfully' ) ;
163
+ } catch ( error ) {
164
+ logger . error ( 'Error in removing topic for the project phase' , error ) ;
165
+ // don't throw the error back to nack the bus
166
+ // we can delete topic for a phase manually, if somehow it fails
167
+ }
168
+ } ) ;
169
+
170
+ /**
171
+ * Handler for project phase deleted event
172
+ * @param {Object } logger logger to log along with trace id
173
+ * @param {Object } msg event payload
174
+ * @param {Object } channel channel to ack, nack
175
+ * @returns {undefined }
176
+ */
177
+ const projectPhaseRemovedHandler = Promise . coroutine ( function * ( logger , msg , channel ) { // eslint-disable-line func-names
178
+ try {
179
+ yield removePhaseFromIndex ( logger , msg , channel ) ;
180
+ yield removePhaseTopic ( logger , msg ) ;
97
181
channel . ack ( msg ) ;
98
182
} catch ( error ) {
99
183
logger . error ( 'Error fetching project document from elasticsearch' , error ) ;
0 commit comments