|
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 */ |
5 | 2 | /**
|
6 | 3 | * Update all records in the ProjectTemplate table.
|
7 | 4 | * - inside “scope” field update “buildingBlocks.<KEY>.price” (for any <KEY>) to be a string if it’s not a string.
|
|
10 | 7 | * Update all records in the ProductTemplate table.
|
11 | 8 | * - inside "template" field update all "required" properties which is not of "boolean" type to boolean.
|
12 | 9 | */
|
13 |
| -import _ from 'lodash'; |
14 |
| -import models from '../src/models'; |
| 10 | +import fixMetadataForES from '../src/utils/fixMetadataForES'; |
15 | 11 |
|
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 | + }); |
0 commit comments