@@ -43,15 +43,31 @@ async function findCompletedProjectEndDate(projectId, transaction) {
43
43
*/
44
44
function applyTemplate ( template , source , destination ) {
45
45
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
+ } ) ;
55
71
}
56
72
} ) ;
57
73
}
@@ -131,7 +147,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
131
147
let detailsObject ;
132
148
if ( productTemplate . template ) {
133
149
detailsObject = { } ;
134
- applyTemplate ( productTemplate . template , project . details , detailsObject ) ;
150
+ applyTemplate ( productTemplate . template , project , detailsObject ) ;
135
151
}
136
152
phaseAndProducts . products . push (
137
153
await models . PhaseProduct . create ( {
@@ -144,7 +160,7 @@ async function migrateFromV2ToV3(req, project, defaultProductTemplateId, phaseNa
144
160
type : productTemplate . productKey ,
145
161
estimatedPrice : project . estimatedPrice ,
146
162
actualPrice : project . actualPrice ,
147
- details : detailsObject ,
163
+ details : detailsObject . details ,
148
164
createdBy : req . authUser . userId ,
149
165
updatedBy : req . authUser . userId ,
150
166
} , { transaction } ) ) ;
0 commit comments