2022-11-15 10:41:57 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2019-01-25 16:05:53 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('modelInfo', {
|
|
|
|
description: 'Gets all models information',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'ctx',
|
|
|
|
type: 'Object',
|
|
|
|
http: {source: 'context'}
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
2019-01-25 16:05:53 +00:00
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/modelInfo`,
|
|
|
|
verb: 'GET'
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
2019-01-25 16:05:53 +00:00
|
|
|
});
|
2017-10-13 14:22:45 +00:00
|
|
|
|
2022-11-15 10:41:57 +00:00
|
|
|
const modelsLocale = new Map();
|
|
|
|
const modulesDir = path.resolve(`${__dirname}/../../../../modules`);
|
|
|
|
const modules = fs.readdirSync(modulesDir);
|
|
|
|
|
|
|
|
for (const mod of modules) {
|
|
|
|
const modelsDir = path.join(modulesDir, mod, `back/locale`);
|
|
|
|
if (!fs.existsSync(modelsDir)) continue;
|
|
|
|
const models = fs.readdirSync(modelsDir);
|
|
|
|
|
|
|
|
for (const model of models) {
|
|
|
|
const localeDir = path.join(modelsDir, model);
|
|
|
|
const localeFiles = fs.readdirSync(localeDir);
|
|
|
|
|
|
|
|
let modelName = model.charAt(0).toUpperCase() + model.substring(1);
|
|
|
|
modelName = modelName.replace(/-\w/g, match => {
|
|
|
|
return match.charAt(1).toUpperCase();
|
|
|
|
});
|
|
|
|
|
|
|
|
const modelLocale = new Map();
|
|
|
|
modelsLocale.set(modelName, modelLocale);
|
|
|
|
|
|
|
|
for (const localeFile of localeFiles) {
|
|
|
|
const localePath = path.join(localeDir, localeFile);
|
|
|
|
|
|
|
|
const match = localeFile.match(/^([a-z]+)\.yml$/);
|
|
|
|
if (!match) {
|
|
|
|
console.warn(`Skipping wrong model locale file: ${localeFile}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const translations = require(localePath);
|
|
|
|
modelLocale.set(match[1], translations);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 16:05:53 +00:00
|
|
|
Self.modelInfo = async function(ctx) {
|
2017-10-13 14:22:45 +00:00
|
|
|
let json = {};
|
2019-01-25 16:05:53 +00:00
|
|
|
let models = Self.app.models;
|
2017-10-13 14:22:45 +00:00
|
|
|
|
|
|
|
for (let modelName in models) {
|
|
|
|
let model = models[modelName];
|
|
|
|
let validations = model.validations;
|
|
|
|
let jsonValidations = {};
|
|
|
|
|
|
|
|
for (let fieldName in validations) {
|
|
|
|
let jsonField = [];
|
|
|
|
|
|
|
|
for (let validation of validations[fieldName]) {
|
|
|
|
let options = validation.options;
|
|
|
|
|
2018-01-11 10:41:14 +00:00
|
|
|
if ((options && options.async) ||
|
2017-10-13 14:22:45 +00:00
|
|
|
(validation.validation == 'custom' && !validation.isExportable))
|
|
|
|
continue;
|
|
|
|
|
2018-08-02 07:49:00 +00:00
|
|
|
let validationCp = Object.assign({}, validation);
|
|
|
|
|
|
|
|
if (validationCp.message)
|
2019-01-25 16:05:53 +00:00
|
|
|
validationCp.message = ctx.req.__(validationCp.message);
|
2018-08-02 07:49:00 +00:00
|
|
|
|
|
|
|
jsonField.push(toJson(validationCp));
|
2017-10-13 14:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonValidations[fieldName] = jsonField;
|
|
|
|
}
|
|
|
|
|
2022-11-15 10:41:57 +00:00
|
|
|
const modelLocale = modelsLocale.get(modelName);
|
|
|
|
const locale = modelLocale && modelLocale.get('es');
|
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
json[modelName] = {
|
|
|
|
properties: model.definition.rawProperties,
|
2022-11-15 10:41:57 +00:00
|
|
|
validations: jsonValidations,
|
|
|
|
locale
|
2017-10-13 14:22:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-25 16:05:53 +00:00
|
|
|
return json;
|
|
|
|
};
|
|
|
|
|
|
|
|
function toJson(object) {
|
|
|
|
let json = {};
|
|
|
|
|
|
|
|
for (let prop in object) {
|
|
|
|
let value = object[prop];
|
|
|
|
|
|
|
|
switch (typeof value) {
|
|
|
|
case 'object':
|
|
|
|
if (value instanceof RegExp)
|
|
|
|
json[prop] = value.source;
|
|
|
|
break;
|
|
|
|
case 'function':
|
|
|
|
json[prop] = value.toString();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
json[prop] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
2017-10-13 14:22:45 +00:00
|
|
|
};
|