@@ -177,6 +177,34 @@ module.exports = (app, logger) => {
177
177
} ) . catch ( err => null ) ; // eslint-disable-line no-unused-vars
178
178
} ) ;
179
179
180
+ /**
181
+ * If the project is in draft status and the phase is in reviewed status, and it's the
182
+ * only phase in the project with that status, then send the plan ready event.
183
+ *
184
+ * @param req
185
+ * @param project
186
+ * @param phase
187
+ * @returns {Promise<void> }
188
+ */
189
+ async function sendPlanReadyEventIfNeeded ( req , project , phase ) {
190
+ if ( project . status === PROJECT_STATUS . DRAFT &&
191
+ phase . status === PROJECT_PHASE_STATUS . REVIEWED ) {
192
+ models . ProjectPhase . count ( {
193
+ where : { projectId : project . id , status : PROJECT_PHASE_STATUS . REVIEWED }
194
+ } ) . then ( ( count => {
195
+ // only send the plan ready event when this is the only reviewed phase in the project
196
+ if ( count !== 1 ) { return ; }
197
+ createEvent ( BUS_API_EVENT . PROJECT_PLAN_READY , {
198
+ projectId : project . id ,
199
+ phaseId : phase . id ,
200
+ projectName : project . name ,
201
+ userId : req . authUser . userId ,
202
+ initiatorUserId : req . authUser . userId ,
203
+ } , logger )
204
+ } ) ) ;
205
+ }
206
+ }
207
+
180
208
/**
181
209
* PROJECT_PHASE_ADDED
182
210
*/
@@ -195,6 +223,8 @@ module.exports = (app, logger) => {
195
223
userId : req . authUser . userId ,
196
224
initiatorUserId : req . authUser . userId ,
197
225
} , logger ) ;
226
+
227
+ return sendPlanReadyEventIfNeeded ( req , project , created ) ;
198
228
} ) . catch ( err => null ) ; // eslint-disable-line no-unused-vars
199
229
} ) ;
200
230
@@ -261,6 +291,8 @@ module.exports = (app, logger) => {
261
291
} , logger ) ) ;
262
292
}
263
293
} ) ;
294
+
295
+ return sendPlanReadyEventIfNeeded ( req , project , updated ) ;
264
296
} ) . catch ( err => null ) ; // eslint-disable-line no-unused-vars
265
297
} ) ;
266
298
0 commit comments