2021-06-07 14:40:51 +03:00
|
|
|
const { Op } = require('sequelize');
|
2021-10-06 15:17:31 +03:00
|
|
|
const Config = require('../../models/Config');
|
2021-06-09 11:58:45 +03:00
|
|
|
const { config } = require('./initialConfig.json');
|
2021-10-06 15:17:31 +03:00
|
|
|
|
|
|
|
const Logger = require('../Logger');
|
2021-06-22 15:49:00 +03:00
|
|
|
const logger = new Logger();
|
2021-06-07 14:40:51 +03:00
|
|
|
|
|
|
|
const initConfig = async () => {
|
|
|
|
// Get config values
|
|
|
|
const configPairs = await Config.findAll({
|
|
|
|
where: {
|
|
|
|
key: {
|
2021-09-06 13:24:01 +03:00
|
|
|
[Op.or]: config.map((pair) => pair.key),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-06-07 14:40:51 +03:00
|
|
|
|
|
|
|
// Get key from each pair
|
|
|
|
const configKeys = configPairs.map((pair) => pair.key);
|
|
|
|
|
|
|
|
// Create missing pairs
|
2021-09-06 13:24:01 +03:00
|
|
|
config.forEach(async ({ key, value }) => {
|
2021-06-07 14:40:51 +03:00
|
|
|
if (!configKeys.includes(key)) {
|
|
|
|
await Config.create({
|
|
|
|
key,
|
2021-06-09 11:58:45 +03:00
|
|
|
value,
|
2021-09-06 13:24:01 +03:00
|
|
|
valueType: typeof value,
|
|
|
|
});
|
2021-06-07 14:40:51 +03:00
|
|
|
}
|
2021-09-06 13:24:01 +03:00
|
|
|
});
|
2021-06-07 14:40:51 +03:00
|
|
|
|
2021-10-06 15:17:31 +03:00
|
|
|
if (process.env.NODE_ENV == 'development') {
|
|
|
|
logger.log('Initial config created');
|
|
|
|
}
|
|
|
|
|
2021-06-07 14:40:51 +03:00
|
|
|
return;
|
2021-09-06 13:24:01 +03:00
|
|
|
};
|
2021-06-07 14:40:51 +03:00
|
|
|
|
2021-09-06 13:24:01 +03:00
|
|
|
module.exports = initConfig;
|