{"size": 1916, "ext": "js", "max_stars_repo_path": "components/script/index.js", "max_stars_repo_name": "milenkovyua/ut-service", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "components/script/index.js", "max_issues_repo_name": "milenkovyua/ut-service", "max_issues_repo_issues_event_min_datetime": "2020-07-17T08:11:03.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-17T09:00:54.000Z", "max_forks_repo_path": "components/script/index.js", "max_forks_repo_name": "milenkovyua/ut-service", "max_forks_repo_forks_event_min_datetime": "2020-07-16T10:38:21.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-25T18:10:34.000Z"}
{"max_stars_count": null, "max_issues_count": 4, "max_forks_count": 5, "avg_line_length": 36.1509433962, "max_line_length": 84, "alphanum_fraction": 0.4770354906}
const errorsFactory = require('./errors');
const methods = require('./methods');
const generateContextMethods = (port, context, method) => {
    port[method] = msg => context;
    if (typeof context === 'object' && !Array.isArray(context)) {
        Object.keys(context).forEach(key => {
            generateContextMethods(port, context[key], `${method}.${key}`);
        });
    };
};

const generateMetaMethods = (port, models = {}, service) => {
    port[`${service}.health`] = msg => ({});
    port[`${service}.models`] = msg => (Object.keys(models).reduce((all, key) => {
        all[key] = `/meta/models/${key}`;
        return all;
    }, {}));
    Object.keys(models).forEach(name => {
        const model = models[name].schema;
        if (model) {
            port[`${service}.models.${name}`] = msg => model;
        }
    });
};
module.exports = {
    ports: [
        function script(config = {}) {
            const port = {
                id: 'script',
                createPort: require('ut-port-script'),
                db: true,
                crypto: true,
                models: {},
                imports: [config.service],
                namespace: [config.service],
                start() {
                    Object.assign(this.errors, errorsFactory(this.bus));
                }
            };
            const {models = {}, db = true, context = {}} = config;
            Object.keys(models).forEach(type => {
                const handlers = methods({type, db});
                Object.keys(handlers).forEach(action => {
                    port[`${config.service}.${type}.${action}`] = handlers[action];
                });
            });
            generateContextMethods(port, context, `${config.service}.context`);
            generateMetaMethods(port, models, config.service);
            return port;
        }
    ]
};
