Skip to content

Commit 5f3e1d0

Browse files
committed
fix project data in DB
1 parent 1fd4d2a commit 5f3e1d0

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Admin endpoint to fix project in DB to be indexed in ES.
3+
*
4+
* Waits until the operation is completed and returns result.
5+
*/
6+
import { middleware as tcMiddleware } from 'tc-core-library-js';
7+
import fixProjectsForES from '../../utils/fixProjectsForES';
8+
9+
const permissions = tcMiddleware.permissions;
10+
11+
/**
12+
* Create a simple logger to log into an array.
13+
*
14+
* @param {Object} defaultLogger default logger which should be used apart from logging to array
15+
*
16+
* @returns {Object} logger
17+
*/
18+
const createArrayLogger = (defaultLogger) => {
19+
const loggerMethods = ['trace', 'debug', 'info', 'warn', 'error'];
20+
const log = [];
21+
const logger = {};
22+
23+
loggerMethods.forEach((method) => {
24+
logger[method] = (message) => {
25+
// log directly with the default logger first
26+
defaultLogger[method](message);
27+
// save the same message to the array
28+
log.push({
29+
level: method,
30+
message,
31+
});
32+
};
33+
});
34+
35+
logger.getLog = () => log;
36+
37+
return logger;
38+
};
39+
40+
module.exports = [
41+
permissions('project.admin'),
42+
(req, res, next) => {
43+
try {
44+
const logger = req.log;
45+
logger.debug('Entered Admin#fixProjectsForEs');
46+
47+
// this logger would use the default `logger` to log into console
48+
// while saving the same log messages to an array, so we can return it in response
49+
const arrayLogger = createArrayLogger(logger);
50+
51+
fixProjectsForES(arrayLogger)
52+
.then(() => {
53+
arrayLogger.info('Data has been successfully fixed in DB.');
54+
res.status(200).json(arrayLogger.getLog());
55+
})
56+
.catch(next);
57+
} catch (err) {
58+
next(err);
59+
}
60+
},
61+
];

src/routes/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ router.route('/v5/projects/admin/es/migrateFromDb')
101101
.patch(require('./admin/es-migrate-from-db'));
102102
router.route('/v5/projects/admin/es/fixMetadataForEs')
103103
.patch(require('./admin/es-fix-metadata-for-es'));
104+
router.route('/v5/projects/admin/es/fixProjectsForEs')
105+
.patch(require('./admin/es-fix-projects-for-es'));
104106
router.route('/v5/projects/admin/es/project/index')
105107
.post(require('./admin/project-index-create'));
106108
router.route('/v5/projects/admin/es/project/remove')

src/utils/fixProjectsForES.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-disable no-param-reassign, no-restricted-syntax, no-await-in-loop */
2+
/**
3+
* Temporary script to fix project in DB to be indexed in ES
4+
*
5+
* Update all records in the Project table.
6+
*/
7+
import _ from 'lodash';
8+
import models from '../models';
9+
10+
/**
11+
* Fix all projects.
12+
*
13+
* @param {Object} logger logger
14+
*
15+
* @returns {Promise} resolved when dene
16+
*/
17+
async function fixProjects(logger) {
18+
const path = 'taasDefinition.team.skills';
19+
const projects = await models.Project.findAll();
20+
for (const project of projects) {
21+
if (_.has(project, 'details')) {
22+
const details = JSON.parse(JSON.stringify(project.details));
23+
const skills = _.get(details, path);
24+
if (skills && !_.isArray(skills)) {
25+
_.set(details, path, []);
26+
project.details = details;
27+
await project.save();
28+
logger.info(`updated record of Project with id ${project.id}`);
29+
}
30+
}
31+
}
32+
}
33+
34+
/**
35+
* Fix project model.
36+
*
37+
* @param {Object} logger logger
38+
*
39+
* @returns {undefined}
40+
*/
41+
async function fixProjectsForES(logger) {
42+
await fixProjects(logger);
43+
}
44+
45+
module.exports = fixProjectsForES;

0 commit comments

Comments
 (0)