{"size": 3361, "ext": "js", "max_stars_repo_path": "packages/app/lib/engine-api.js", "max_stars_repo_name": "nikku/camunda-playground", "max_stars_repo_stars_event_min_datetime": "2019-08-14T08:24:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-01T07:18:12.000Z", "max_issues_repo_path": "packages/app/lib/engine-api.js", "max_issues_repo_name": "nikku/camunda-playground", "max_issues_repo_issues_event_min_datetime": "2019-08-14T08:05:00.000Z", "max_issues_repo_issues_event_max_datetime": "2019-08-15T15:54:45.000Z", "max_forks_repo_path": "packages/app/lib/engine-api.js", "max_forks_repo_name": "nikku/camunda-playground", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 11, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 24.5328467153, "max_line_length": 115, "alphanum_fraction": 0.6334424278}
const FormData = require('form-data');

const fetch = require('node-fetch');
const path = require('path');


function EngineApi(camundaBaseUrl) {

  const baseUrl = `${camundaBaseUrl}/engine-rest`;


  async function deployDiagram(diagram) {

    const form = new FormData();

    form.append('deployment-name', 'camunda-playground-deployment');
    form.append('deployment-source', 'camunda-playground');

    const diagramName = diagram.path && path.basename(diagram.path) || diagram.name;

    form.append(diagramName, diagram.contents, {
      filename: diagramName,
      contentType: 'application/xml'
    });

    const response = await fetch(`${baseUrl}/deployment/create`, {
      method: 'POST',
      headers: {
        'accept': 'application/json'
      },
      body: form
    });

    if (response.ok) {

      const {
        id,
        deployedProcessDefinitions
      } = await response.json();

      return {
        id,
        deployedProcessDefinitions,
        deployedProcessDefinition: Object.values(deployedProcessDefinitions || {})[0]
      };
    }

    const details = await response.json();

    throw responseError('Deployment failed', response, details);
  }

  async function startProcessInstance(definition) {

    const response = await fetch(`${baseUrl}/process-definition/${definition.id}/start`, {
      method: 'POST',
      headers: {
        'accept': 'application/json',
        'content-type': 'application/json'
      },
      body: JSON.stringify({})
    });

    if (response.ok) {
      return response.json();
    }

    const details = await response.json();

    throw responseError('Starting process instance failed', response, details);
  }

  async function getProcessInstanceDetails(processInstance) {

    const {
      id
    } = processInstance;

    const [
      state,
      activityInstances
    ] = await Promise.all([

      // https://docs.camunda.org/manual/7.11/reference/rest/history/process-instance/get-process-instance/
      fetch(`${baseUrl}/history/process-instance/${id}`, {
        headers: {
          'accept': 'application/json',
          'content-type': 'application/json'
        }
      }),

      // https://docs.camunda.org/manual/7.11/reference/rest/history/activity-instance/get-activity-instance-query/
      fetch(`${baseUrl}/history/activity-instance?processInstanceId=${id}`, {
        headers: {
          'accept': 'application/json',
          'content-type': 'application/json'
        }
      })
    ].map(result => result.then(res => res.json())));

    return {
      id,
      state,
      activityInstances
    };
  }

  // api /////////////////

  this.deployDiagram = deployDiagram;
  this.getProcessInstanceDetails = getProcessInstanceDetails;
  this.startProcessInstance = startProcessInstance;

}


module.exports = EngineApi;


// helpers //////////////

const parseError = 'ENGINE-09005 Could not parse BPMN process. Errors: \n*';

function responseError(message, response, details) {
  const error = new Error(message);

  error.details = details;
  error.response = response;

  // fix engine not exposing details
  if (details && details.message.startsWith(parseError)) {
    details.problems = details.message.substring(parseError.length).split(/\s?\n\*\s?/g);
    details.message = 'ENGINE-09005 Could not parse BPMN process';
  }

  return error;
}