{"size": 848, "ext": "js", "max_stars_repo_path": "app/core/orchestrator.js", "max_stars_repo_name": "thiagoabreudev/zeus-bot-spotfy", "max_stars_repo_stars_event_min_datetime": "2020-01-23T02:44:50.000Z", "max_stars_repo_stars_event_max_datetime": "2020-01-23T02:44:50.000Z", "max_issues_repo_path": "app/core/orchestrator.js", "max_issues_repo_name": "thiagoabreudev/zeus-bot-spotfy", "max_issues_repo_issues_event_min_datetime": "2019-07-21T16:08:31.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-10T02:56:57.000Z", "max_forks_repo_path": "app/core/orchestrator.js", "max_forks_repo_name": "thiagoabreudev/zeus-bot-spotfy", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 22.3157894737, "max_line_length": 68, "alphanum_fraction": 0.6450471698}
const _ = require('lodash');
const EventEmitter = require('events');

class Orchestrator extends EventEmitter {
  constructor(schemas) {
    super();
    this.schemas = schemas;
    this.on('message', this.onMessage);
  }

  onMessage(data) {
    const commandFound = this.findRobot(data)
    if (!_.isEmpty(commandFound)) {
      console.log('[orchestrator request handler]')
      commandFound.handler()
    }
  }

  findRobot(commandRequested) {
    for (let schema of this.schemas) {
      for (let command of Object.keys(schema.structure)) {
        if ((schema.structure[command]).regex == commandRequested) {
          return schema.structure[command]
        }
      }
    }
    return {}
  }

  handleRobot(schemaToExecute) {
    return schemaToExecute.handler()
  }
}


module.exports = (schemas) => {
  return new Orchestrator(schemas)
}