Skip to content

Commit 120bc6b

Browse files
author
Vikas Agarwal
committed
Fixed logic of applying product template
1 parent 02d7197 commit 120bc6b

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

src/routes/projectUpgrade/create.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,31 @@ async function findCompletedProjectEndDate(projectId, transaction) {
4343
*/
4444
function 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 }));

src/routes/projectUpgrade/create.spec.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ describe('Project upgrade', () => {
3636
name: 'a specific name',
3737
products: [productId],
3838
appDefinition: { budget: 10000 },
39-
sampleKey1: {
40-
sampleSubKey1: 'a specific value',
41-
},
42-
sampleKey2: {
43-
sampleSubKey2: 'a specific value',
44-
},
39+
testingNeeds: { hours: 10000 },
40+
appScreens: { screens: [{ name: 'a', desc: 'ad' }, { name: 'b', desc: 'bd' }] },
4541
},
4642
createdBy: 1,
4743
updatedBy: 1,
@@ -108,13 +104,24 @@ describe('Project upgrade', () => {
108104
alias2: [1, 2, 3],
109105
},
110106
template: {
111-
name: 'a template name',
112-
sampleKey1: {
113-
sampleSubKey1: 'a value',
114-
},
115-
sampleKey2: {
116-
sampleSubKey2: 'a value',
117-
},
107+
questions: [
108+
{
109+
subSections: [
110+
{ fieldName: 'details.name' },
111+
{ type: 'questions', questions: [{ fieldName: 'details.appDefinition.budget' }] },
112+
{ fieldName: 'details.testingNeeds.hours' },
113+
],
114+
},
115+
{
116+
subSections: [
117+
{
118+
fieldName: 'details.appScreens.screens',
119+
type: 'screens',
120+
questions: [{ fieldName: 'name' }, { fieldName: 'desc' }],
121+
},
122+
],
123+
},
124+
],
118125
},
119126
createdBy: 1,
120127
updatedBy: 2,
@@ -281,12 +288,9 @@ describe('Project upgrade', () => {
281288
expect(newPhaseProduct.actualPrice).to.equal(parseInt(project.actualPrice, 10));
282289
expect(newPhaseProduct.details).to.deep.equal({
283290
name: 'a specific name',
284-
sampleKey1: {
285-
sampleSubKey1: 'a specific value',
286-
},
287-
sampleKey2: {
288-
sampleSubKey2: 'a specific value',
289-
},
291+
appDefinition: { budget: 10000 },
292+
testingNeeds: { hours: 10000 },
293+
appScreens: { screens: [{ name: 'a', desc: 'ad' }, { name: 'b', desc: 'bd' }] },
290294
});
291295
}
292296
}

0 commit comments

Comments
 (0)