44 * API to upgrade projects
55 */
66import _ from 'lodash' ;
7+ import moment from 'moment' ;
78import validate from 'express-validation' ;
89import Joi from 'joi' ;
910import { middleware as tcMiddleware } from 'tc-core-library-js' ;
@@ -59,11 +60,11 @@ function applyTemplate(template, source, destination) {
5960 }
6061 // other sub sections which requires generic handling
6162 if ( subSection . fieldName ) { // if sub section contains field name, directly copy its value
62- console . log ( subSection . fieldName , _ . get ( source , subSection . fieldName ) ) ;
63+ // console.log(subSection.fieldName, _.get(source, subSection.fieldName));
6364 _ . set ( destination , subSection . fieldName , _ . get ( source , subSection . fieldName ) ) ;
6465 } else if ( subSection . type === 'questions' ) { // if questions typed subsection
6566 subSection . questions . forEach ( ( question ) => { // iterate throught each question to copy its value
66- console . log ( question . fieldName , _ . get ( source , question . fieldName ) ) ;
67+ // console.log(question.fieldName, _.get(source, question.fieldName));
6768 _ . set ( destination , question . fieldName , _ . get ( source , question . fieldName ) ) ;
6869 } ) ;
6970 }
@@ -90,6 +91,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
9091 const previousValue = _ . clone ( project . get ( { plain : true } ) ) ;
9192 await models . sequelize . transaction ( async ( transaction ) => {
9293 const products = project . details . products ;
94+
9395 const projectTemplate = await models . ProjectTemplate . find ( {
9496 where : { key : products [ 0 ] } ,
9597 attributes : [ 'id' , 'phases' ] ,
@@ -104,6 +106,11 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
104106 const endDate = projectCompleted
105107 ? ( await findCompletedProjectEndDate ( project . id , transaction ) ) || project . updatedAt
106108 : null ;
109+ // calculates the duration
110+ const projectDuration = endDate
111+ ? moment ( endDate ) . diff ( project . createdAt , 'days' )
112+ : moment ( ) . diff ( moment ( project . createdAt ) , 'days' ) ;
113+
107114 let phaseStatus = project . status ;
108115 // maps the in_review status to the draft status for the phase
109116 phaseStatus = phaseStatus === PROJECT_STATUS . IN_REVIEW ? PROJECT_STATUS . DRAFT : phaseStatus ;
@@ -116,6 +123,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
116123 name : phaseName || phaseObject . name || '' ,
117124 status : phaseStatus ,
118125 startDate : project . createdAt ,
126+ duration : projectDuration ,
119127 endDate,
120128 budget : project . details && project . details . appDefinition && project . details . appDefinition . budget ,
121129 progress : projectCompleted ? 100 : 0 ,
@@ -172,25 +180,16 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
172180 await project . update ( { version : 'v3' , templateId : projectTemplate . id } , { transaction } ) ;
173181 } ) ;
174182 newPhasesAndProducts . forEach ( ( { phase, products } ) => {
183+ const phaseJSON = phase . toJSON ( ) ;
184+ phaseJSON . products = products ;
175185 // Send events to buses (ProjectPhase)
176186 req . log . debug ( 'Sending event to RabbitMQ bus for project phase %d' , phase . id ) ;
177187 req . app . services . pubsub . publish ( EVENT . ROUTING_KEY . PROJECT_PHASE_ADDED ,
178- phase ,
188+ phaseJSON ,
179189 { correlationId : req . id } ,
180190 ) ;
181191 req . log . debug ( 'Sending event to Kafka bus for project phase %d' , phase . id ) ;
182- req . app . emit ( EVENT . ROUTING_KEY . PROJECT_PHASE_ADDED , { req, created : phase } ) ;
183-
184- products . forEach ( ( newPhaseProduct ) => {
185- // Send events to buses (PhaseProduct)
186- req . log . debug ( 'Sending event to RabbitMQ bus for phase product %d' , newPhaseProduct . id ) ;
187- req . app . services . pubsub . publish ( EVENT . ROUTING_KEY . PROJECT_PHASE_PRODUCT_ADDED ,
188- newPhaseProduct ,
189- { correlationId : req . id } ,
190- ) ;
191- req . log . debug ( 'Sending event to Kafka bus for phase product %d' , newPhaseProduct . id ) ;
192- req . app . emit ( EVENT . ROUTING_KEY . PROJECT_PHASE_PRODUCT_ADDED , { req, created : newPhaseProduct } ) ;
193- } ) ;
192+ req . app . emit ( EVENT . ROUTING_KEY . PROJECT_PHASE_ADDED , { req, created : phaseJSON } ) ;
194193 } ) ;
195194
196195 // Send events to buses (Project)
0 commit comments