Skip to content

Commit ee5771e

Browse files
committed
fix: fixMetadataForES script to be deployable
1 parent 2b3cf21 commit ee5771e

File tree

3 files changed

+172
-167
lines changed

3 files changed

+172
-167
lines changed

migrations/fixMetadataForES.js

Lines changed: 10 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
/* eslint-disable no-console */
2-
/* eslint-disable no-param-reassign */
3-
/* eslint-disable no-restricted-syntax */
4-
/* eslint-disable no-await-in-loop */
1+
/* eslint-disable no-console */
52
/**
63
* Update all records in the ProjectTemplate table.
74
* - inside “scope” field update “buildingBlocks.<KEY>.price” (for any <KEY>) to be a string if it’s not a string.
@@ -10,166 +7,13 @@
107
* Update all records in the ProductTemplate table.
118
* - inside "template" field update all "required" properties which is not of "boolean" type to boolean.
129
*/
13-
import _ from 'lodash';
14-
import models from '../src/models';
10+
import fixMetadataForES from '../src/utils/fixMetadataForES';
1511

16-
/**
17-
* Update the wizard property of an object.
18-
*
19-
* @param {Object} data any object
20-
* @returns {undefined}
21-
*/
22-
function updateWizardProperty(data) {
23-
if (typeof data.wizard === 'boolean') {
24-
data.wizard = { enabled: data.wizard };
25-
}
26-
}
27-
28-
/**
29-
* Update the scope property of a projectTemplate.
30-
*
31-
* @param {Object} scope the scope property
32-
* @returns {Object} the updated scope
33-
*/
34-
function updateScope(scope) {
35-
// update price properties
36-
if (scope.buildingBlocks) {
37-
for (const key of Object.keys(scope.buildingBlocks)) {
38-
const price = scope.buildingBlocks[key].price;
39-
if (price !== undefined) {
40-
if (typeof price !== 'string') {
41-
scope.buildingBlocks[key].price = price.toString();
42-
}
43-
}
44-
}
45-
}
46-
// update wizard properties
47-
updateWizardProperty(scope);
48-
if (scope.sections) {
49-
for (const section of scope.sections) {
50-
updateWizardProperty(section);
51-
if (section.subSections) {
52-
for (const subSection of section.subSections) {
53-
updateWizardProperty(subSection);
54-
}
55-
}
56-
}
57-
}
58-
return scope;
59-
}
60-
61-
/**
62-
* Fix all projectTemplates.
63-
*
64-
* @param {Object} logger logger
65-
*
66-
* @returns {Promise} resolved when dene
67-
*/
68-
async function fixProjectTemplates(logger) {
69-
const projectTemplates = await models.ProjectTemplate.findAll();
70-
for (const projectTemplate of projectTemplates) {
71-
if (projectTemplate.scope) {
72-
const updatedScope = updateScope(JSON.parse(JSON.stringify(projectTemplate.scope)));
73-
if (!_.isEqual(updatedScope, projectTemplate.scope)) {
74-
projectTemplate.scope = updatedScope;
75-
await projectTemplate.save();
76-
logger.info(`updated record of ProjectTemplate with id ${projectTemplate.id}`);
77-
}
78-
}
79-
}
80-
}
81-
82-
/**
83-
* Update the required property of an object.
84-
*
85-
* @param {Object} data any object
86-
*
87-
* @returns {undefined}
88-
*/
89-
function updateRequiredProperty(data) {
90-
if (typeof data.required !== 'undefined' && typeof data.required !== 'boolean') {
91-
if (data.required === 'false') {
92-
data.required = false;
93-
} else if (data.required === 'true') {
94-
data.required = true;
95-
} else {
96-
throw new Error(`"required" value ${data.required} cannot be converted to boolean.`);
97-
}
98-
}
99-
}
100-
101-
/**
102-
* Update the template property of a productTemplate.
103-
*
104-
* @param {Object} template the template property
105-
* @returns {Object} the updated template
106-
*/
107-
function updateTemplate(template) {
108-
// update wizard properties
109-
updateRequiredProperty(template);
110-
if (template.sections) {
111-
for (const section of template.sections) {
112-
updateRequiredProperty(section);
113-
if (section.subSections) {
114-
for (const subSection of section.subSections) {
115-
updateRequiredProperty(subSection);
116-
if (subSection.questions) {
117-
for (const question of subSection.questions) {
118-
updateRequiredProperty(question);
119-
}
120-
}
121-
}
122-
}
123-
}
124-
}
125-
return template;
126-
}
127-
128-
/**
129-
* Fix all productTemplates.
130-
*
131-
* @param {Object} logger logger
132-
*
133-
* @returns {Promise} resolved when dene
134-
*/
135-
async function fixProductTemplates(logger) {
136-
const productTemplates = await models.ProductTemplate.findAll();
137-
138-
for (const productTemplate of productTemplates) {
139-
if (productTemplate.template) {
140-
const updatedTemplate = updateTemplate(JSON.parse(JSON.stringify(productTemplate.template)));
141-
if (!_.isEqual(updatedTemplate, productTemplate.template)) {
142-
productTemplate.template = updatedTemplate;
143-
await productTemplate.save();
144-
logger.info(`updated record of ProductTemplate with id ${productTemplate.id}`);
145-
}
146-
}
147-
}
148-
}
149-
150-
/**
151-
* Fix all metadata models.
152-
*
153-
* @param {Object} logger logger
154-
*
155-
* @returns {undefined}
156-
*/
157-
async function fixMetadataForES(logger) {
158-
await fixProjectTemplates(logger);
159-
await fixProductTemplates(logger);
160-
}
161-
162-
if (!module.parent) {
163-
fixMetadataForES(console)
164-
.then(() => {
165-
console.log('done!');
166-
process.exit();
167-
}).catch((err) => {
168-
console.error('Error syncing database', err);
169-
process.exit(1);
170-
});
171-
}
172-
173-
module.exports = {
174-
fixMetadataForES,
175-
};
12+
fixMetadataForES(console)
13+
.then(() => {
14+
console.log('done!');
15+
process.exit();
16+
}).catch((err) => {
17+
console.error('Error syncing database', err);
18+
process.exit(1);
19+
});

src/routes/admin/es-fix-metadata-for-es.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Waits until the operation is completed and returns result.
55
*/
66
import { middleware as tcMiddleware } from 'tc-core-library-js';
7-
import { fixMetadataForES } from '../../../migrations/fixMetadataForES';
7+
import fixMetadataForES from '../../utils/fixMetadataForES';
88

99
const permissions = tcMiddleware.permissions;
1010

src/utils/fixMetadataForES.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/* eslint-disable no-param-reassign, no-restricted-syntax, no-await-in-loop */
2+
/**
3+
* Temporary script to fix metadata in DB to be indexed in ES
4+
*
5+
* Update all records in the ProjectTemplate table.
6+
* - inside “scope” field update “buildingBlocks.<KEY>.price” (for any <KEY>) to be a string if it’s not a string.
7+
* - inside “scope” field replace all the ‘“wizard”: true’ with ‘“wizard”: {“enabled”: true}’,
8+
* and ‘“wizard”: false’ replace with ‘“wizard”: {“enabled”: false}’.
9+
* Update all records in the ProductTemplate table.
10+
* - inside "template" field update all "required" properties which is not of "boolean" type to boolean.
11+
*/
12+
import _ from 'lodash';
13+
import models from '../models';
14+
15+
/**
16+
* Update the wizard property of an object.
17+
*
18+
* @param {Object} data any object
19+
* @returns {undefined}
20+
*/
21+
function updateWizardProperty(data) {
22+
if (typeof data.wizard === 'boolean') {
23+
data.wizard = { enabled: data.wizard };
24+
}
25+
}
26+
27+
/**
28+
* Update the scope property of a projectTemplate.
29+
*
30+
* @param {Object} scope the scope property
31+
* @returns {Object} the updated scope
32+
*/
33+
function updateScope(scope) {
34+
// update price properties
35+
if (scope.buildingBlocks) {
36+
for (const key of Object.keys(scope.buildingBlocks)) {
37+
const price = scope.buildingBlocks[key].price;
38+
if (price !== undefined) {
39+
if (typeof price !== 'string') {
40+
scope.buildingBlocks[key].price = price.toString();
41+
}
42+
}
43+
}
44+
}
45+
// update wizard properties
46+
updateWizardProperty(scope);
47+
if (scope.sections) {
48+
for (const section of scope.sections) {
49+
updateWizardProperty(section);
50+
if (section.subSections) {
51+
for (const subSection of section.subSections) {
52+
updateWizardProperty(subSection);
53+
}
54+
}
55+
}
56+
}
57+
return scope;
58+
}
59+
60+
/**
61+
* Fix all projectTemplates.
62+
*
63+
* @param {Object} logger logger
64+
*
65+
* @returns {Promise} resolved when dene
66+
*/
67+
async function fixProjectTemplates(logger) {
68+
const projectTemplates = await models.ProjectTemplate.findAll();
69+
for (const projectTemplate of projectTemplates) {
70+
if (projectTemplate.scope) {
71+
const updatedScope = updateScope(JSON.parse(JSON.stringify(projectTemplate.scope)));
72+
if (!_.isEqual(updatedScope, projectTemplate.scope)) {
73+
projectTemplate.scope = updatedScope;
74+
await projectTemplate.save();
75+
logger.info(`updated record of ProjectTemplate with id ${projectTemplate.id}`);
76+
}
77+
}
78+
}
79+
}
80+
81+
/**
82+
* Update the required property of an object.
83+
*
84+
* @param {Object} data any object
85+
*
86+
* @returns {undefined}
87+
*/
88+
function updateRequiredProperty(data) {
89+
if (typeof data.required !== 'undefined' && typeof data.required !== 'boolean') {
90+
if (data.required === 'false') {
91+
data.required = false;
92+
} else if (data.required === 'true') {
93+
data.required = true;
94+
} else {
95+
throw new Error(`"required" value ${data.required} cannot be converted to boolean.`);
96+
}
97+
}
98+
}
99+
100+
/**
101+
* Update the template property of a productTemplate.
102+
*
103+
* @param {Object} template the template property
104+
* @returns {Object} the updated template
105+
*/
106+
function updateTemplate(template) {
107+
// update wizard properties
108+
updateRequiredProperty(template);
109+
if (template.sections) {
110+
for (const section of template.sections) {
111+
updateRequiredProperty(section);
112+
if (section.subSections) {
113+
for (const subSection of section.subSections) {
114+
updateRequiredProperty(subSection);
115+
if (subSection.questions) {
116+
for (const question of subSection.questions) {
117+
updateRequiredProperty(question);
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
124+
return template;
125+
}
126+
127+
/**
128+
* Fix all productTemplates.
129+
*
130+
* @param {Object} logger logger
131+
*
132+
* @returns {Promise} resolved when dene
133+
*/
134+
async function fixProductTemplates(logger) {
135+
const productTemplates = await models.ProductTemplate.findAll();
136+
137+
for (const productTemplate of productTemplates) {
138+
if (productTemplate.template) {
139+
const updatedTemplate = updateTemplate(JSON.parse(JSON.stringify(productTemplate.template)));
140+
if (!_.isEqual(updatedTemplate, productTemplate.template)) {
141+
productTemplate.template = updatedTemplate;
142+
await productTemplate.save();
143+
logger.info(`updated record of ProductTemplate with id ${productTemplate.id}`);
144+
}
145+
}
146+
}
147+
}
148+
149+
/**
150+
* Fix all metadata models.
151+
*
152+
* @param {Object} logger logger
153+
*
154+
* @returns {undefined}
155+
*/
156+
async function fixMetadataForES(logger) {
157+
await fixProjectTemplates(logger);
158+
await fixProductTemplates(logger);
159+
}
160+
161+
module.exports = fixMetadataForES;

0 commit comments

Comments
 (0)