1
1
import _ from 'lodash' ;
2
2
import config from 'config' ;
3
- import { EVENT , BUS_API_EVENT , PROJECT_STATUS , PROJECT_MEMBER_ROLE } from '../constants' ;
3
+ import { EVENT , BUS_API_EVENT , PROJECT_STATUS , PROJECT_PHASE_STATUS , PROJECT_MEMBER_ROLE } from '../constants' ;
4
4
import { createEvent } from '../services/busApi' ;
5
5
import models from '../models' ;
6
6
@@ -207,6 +207,34 @@ module.exports = (app, logger) => {
207
207
} ) . catch ( err => null ) ; // eslint-disable-line no-unused-vars
208
208
} ) ;
209
209
210
+ /**
211
+ * If the project is in draft status and the phase is in reviewed status, and it's the
212
+ * only phase in the project with that status, then send the plan ready event.
213
+ *
214
+ * @param req the req
215
+ * @param project the project
216
+ * @param phase the phase that was created/updated
217
+ * @returns {Promise<void> }
218
+ */
219
+ async function sendPlanReadyEventIfNeeded ( req , project , phase ) {
220
+ if ( project . status === PROJECT_STATUS . DRAFT &&
221
+ phase . status === PROJECT_PHASE_STATUS . REVIEWED ) {
222
+ await models . ProjectPhase . count ( {
223
+ where : { projectId : project . id , status : PROJECT_PHASE_STATUS . REVIEWED } ,
224
+ } ) . then ( ( ( count ) => {
225
+ // only send the plan ready event when this is the only reviewed phase in the project
226
+ if ( count !== 1 ) { return ; }
227
+ createEvent ( BUS_API_EVENT . PROJECT_PLAN_READY , {
228
+ projectId : project . id ,
229
+ phaseId : phase . id ,
230
+ projectName : project . name ,
231
+ userId : req . authUser . userId ,
232
+ initiatorUserId : req . authUser . userId ,
233
+ } , logger ) ;
234
+ } ) ) ;
235
+ }
236
+ }
237
+
210
238
/**
211
239
* PROJECT_PHASE_ADDED
212
240
*/
@@ -226,6 +254,8 @@ module.exports = (app, logger) => {
226
254
userId : req . authUser . userId ,
227
255
initiatorUserId : req . authUser . userId ,
228
256
} , logger ) ;
257
+
258
+ return sendPlanReadyEventIfNeeded ( req , project , created ) ;
229
259
} ) . catch ( err => null ) ; // eslint-disable-line no-unused-vars
230
260
} ) ;
231
261
@@ -258,6 +288,7 @@ module.exports = (app, logger) => {
258
288
logger . debug ( 'receive PROJECT_PHASE_UPDATED event' ) ;
259
289
260
290
const projectId = _ . parseInt ( req . params . projectId ) ;
291
+ const phaseId = _ . parseInt ( req . params . phaseId ) ;
261
292
262
293
models . Project . findOne ( {
263
294
where : { id : projectId } ,
@@ -270,6 +301,31 @@ module.exports = (app, logger) => {
270
301
userId : req . authUser . userId ,
271
302
initiatorUserId : req . authUser . userId ,
272
303
} , logger ) ;
304
+
305
+ [
306
+ [ 'spentBudget' , BUS_API_EVENT . PROJECT_PHASE_UPDATE_PAYMENT ] ,
307
+ [ 'progress' , [ BUS_API_EVENT . PROJECT_PHASE_UPDATE_PROGRESS , BUS_API_EVENT . PROJECT_PROGRESS_MODIFIED ] ] ,
308
+ [ 'details' , BUS_API_EVENT . PROJECT_PHASE_UPDATE_SCOPE ] ,
309
+ [ 'status' , BUS_API_EVENT . PROJECT_PHASE_TRANSITION_ACTIVE , PROJECT_PHASE_STATUS . ACTIVE ] ,
310
+ [ 'status' , BUS_API_EVENT . PROJECT_PHASE_TRANSITION_COMPLETED , PROJECT_PHASE_STATUS . COMPLETED ] ,
311
+ ] . forEach ( ( [ key , events , sendIfEqual ] ) => {
312
+ // eslint-disable-next-line no-param-reassign
313
+ events = Array . isArray ( events ) ? events : [ events ] ;
314
+
315
+ // send event(s) only if the target field's value was updated, or when an update matches a "sendIfEqual" value
316
+ if ( ( ! sendIfEqual && ! _ . isEqual ( original [ key ] , updated [ key ] ) ) ||
317
+ ( original [ key ] !== sendIfEqual && updated [ key ] === sendIfEqual ) ) {
318
+ events . forEach ( event => createEvent ( event , {
319
+ projectId,
320
+ phaseId,
321
+ projectName : project . name ,
322
+ userId : req . authUser . userId ,
323
+ initiatorUserId : req . authUser . userId ,
324
+ } , logger ) ) ;
325
+ }
326
+ } ) ;
327
+
328
+ return sendPlanReadyEventIfNeeded ( req , project , updated ) ;
273
329
} ) . catch ( err => null ) ; // eslint-disable-line no-unused-vars
274
330
} ) ;
275
331
0 commit comments