@@ -43,15 +43,31 @@ async function findCompletedProjectEndDate(projectId, transaction) {
4343 */
4444function applyTemplate ( template , source , destination ) {
4545 if ( ! template || typeof template !== 'object' ) { return ; }
46- Object . keys ( template ) . forEach ( ( key ) => {
47- const templateValue = template [ key ] ;
48- if ( typeof templateValue === 'object' ) {
49- // eslint-disable-next-line no-param-reassign
50- destination [ key ] = { } ;
51- applyTemplate ( templateValue , source [ key ] , destination [ key ] ) ;
52- } else if ( source && typeof source === 'object' ) {
53- // eslint-disable-next-line no-param-reassign
54- destination [ key ] = source [ key ] ;
46+ if ( ! template . questions || ! template . questions . length ) { return ; }
47+ // questions field is actually array of sections
48+ const templateQuestions = template . questions ;
49+ // loop through for every section
50+ templateQuestions . forEach ( ( section ) => {
51+ // find subsections
52+ if ( section . subSections && section . subSections . length ) {
53+ // loop through every sub section
54+ section . subSections . forEach ( ( subSection ) => {
55+ // screens type sub sections need separate handling
56+ if ( subSection . type === 'screens' ) {
57+ _ . set ( destination , subSection . fieldName , _ . get ( source , subSection . fieldName ) ) ;
58+ return ;
59+ }
60+ // other sub sections which requires generic handling
61+ if ( subSection . fieldName ) { // if sub section contains field name, directly copy its value
62+ console . log ( subSection . fieldName , _ . get ( source , subSection . fieldName ) ) ;
63+ _ . set ( destination , subSection . fieldName , _ . get ( source , subSection . fieldName ) ) ;
64+ } else if ( subSection . type === 'questions' ) { // if questions typed subsection
65+ subSection . questions . forEach ( ( question ) => { // iterate throught each question to copy its value
66+ console . log ( question . fieldName , _ . get ( source , question . fieldName ) ) ;
67+ _ . set ( destination , question . fieldName , _ . get ( source , question . fieldName ) ) ;
68+ } ) ;
69+ }
70+ } ) ;
5571 }
5672 } ) ;
5773}
@@ -131,7 +147,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
131147 let detailsObject ;
132148 if ( productTemplate . template ) {
133149 detailsObject = { } ;
134- applyTemplate ( productTemplate . template , project . details , detailsObject ) ;
150+ applyTemplate ( productTemplate . template , project , detailsObject ) ;
135151 }
136152 phaseAndProducts . products . push (
137153 await models . PhaseProduct . create ( {
@@ -144,7 +160,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
144160 type : productTemplate . productKey ,
145161 estimatedPrice : project . estimatedPrice ,
146162 actualPrice : project . actualPrice ,
147- details : detailsObject ,
163+ details : detailsObject . details ,
148164 createdBy : req . authUser . userId ,
149165 updatedBy : req . authUser . userId ,
150166 } , { transaction } ) ) ;
0 commit comments