From 9b9e87ceb3346758dfbb4be37d49a0f88fe9e97e Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 24 Apr 2024 14:16:39 +0200 Subject: [PATCH] feat(salix): refs #6533 #6533 feat logInfo method --- loopback/common/methods/schema/log-info.js | 31 ++++++++++++++++------ loopback/common/models/schema.js | 1 + loopback/common/models/schema.json | 8 +++++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/loopback/common/methods/schema/log-info.js b/loopback/common/methods/schema/log-info.js index 193471841..50b31d867 100644 --- a/loopback/common/methods/schema/log-info.js +++ b/loopback/common/methods/schema/log-info.js @@ -1,13 +1,10 @@ +const path = require('path'); +const fs = require('fs'); + module.exports = Self => { Self.remoteMethod('logInfo', { description: 'Gets all models information', - accepts: [ - { - arg: 'ctx', - type: 'Object', - http: {source: 'context'} - } - ], + accepts: [], returns: { type: 'Object', root: true @@ -18,6 +15,24 @@ module.exports = Self => { } }); - Self.logInfo = async function(ctx) { + const modelsLocale = new Map(); + const modulesDir = path.resolve(`${process.cwd()}/modules`); + const modules = fs.readdirSync(modulesDir); + + for (const mod of modules) { + 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); + } + } + + Self.logInfo = async function() { + return Object.fromEntries(modelsLocale); }; }; diff --git a/loopback/common/models/schema.js b/loopback/common/models/schema.js index f88cc895c..4734a40a0 100644 --- a/loopback/common/models/schema.js +++ b/loopback/common/models/schema.js @@ -1,4 +1,5 @@ module.exports = function(Self) { require('../methods/schema/model-info')(Self); + require('../methods/schema/log-info')(Self); }; diff --git a/loopback/common/models/schema.json b/loopback/common/models/schema.json index 1bdf0fcf7..cfdacdf3f 100644 --- a/loopback/common/models/schema.json +++ b/loopback/common/models/schema.json @@ -7,6 +7,12 @@ "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW" + }, + { + "property": "logInfo", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" } ] -} \ No newline at end of file +}