feat(mylogger): refs #6533 #6533 Add VnModels

This commit is contained in:
Javier Segarra 2024-04-25 08:19:58 +02:00
parent 3196510276
commit 409b98e01d
1 changed files with 19 additions and 10 deletions

View File

@ -1,6 +1,8 @@
const path = require('path');
const fs = require('fs');
const BACK_MODELS = `back/models`;
const FILE_EXTENSION = '.json';
const FILE_ENCODING = 'utf-8';
module.exports = Self => {
Self.remoteMethod('logInfo', {
description: 'Gets all models information',
@ -20,19 +22,26 @@ module.exports = Self => {
const modules = fs.readdirSync(modulesDir);
for (const mod of modules) {
const modelsDir = path.join(modulesDir, mod, `back/models`);
const modelsDir = path.join(modulesDir, mod, BACK_MODELS);
if (!fs.existsSync(modelsDir)) continue;
const models = (fs.readdirSync(modelsDir)).filter(fileName => fileName.endsWith('.json'));
for (const model of models) {
const modelFile = path.join(modelsDir, model);
const modelConfig = JSON.parse(fs.readFileSync(modelFile, {encoding: 'utf-8'}));
const {log} = modelConfig;
if (!log) continue;
modelsLocale.set(modelConfig.name, log);
}
handleModels(modelsDir);
}
const pathVn = path.join(process.cwd(), BACK_MODELS);
handleModels(pathVn);
Self.logInfo = async function() {
return Object.fromEntries(modelsLocale);
};
function handleModels(modelsDir) {
const models = (fs.readdirSync(modelsDir)).filter(fileName => fileName.endsWith(FILE_EXTENSION));
for (const model of models) {
const modelFile = path.join(modelsDir, model);
const modelConfig = JSON.parse(fs.readFileSync(modelFile, {encoding: FILE_ENCODING}));
const {log} = modelConfig;
if (!log?.mainTable) continue;
const key = modelConfig.name.startsWith('Vn') ? modelConfig.name.slice(2) : modelConfig.name;
modelsLocale.set(key.toLowerCase(), log);
}
}
};