npm package: @honeybpm/hbpm
Description: A simple business process management tool written for NodeJS
Author: Daniel R. Hayes
Latest version: 0.6.0

![HoneyBPM Logo](https://i.imgur.com/fgrlvFQ.png)

A BPM (Business Process Management) framework to run business processes in a consistent and reusable way.


[![npm (scoped)](https://img.shields.io/npm/v/@honeybpm/hbpm.svg)](https://www.npmjs.com/package/@honeybpm/hbpm)
[![Build Status](https://travis-ci.org/DanielRHayes/honeybpm.svg?branch=master)](https://travis-ci.org/DanielRHayes/honeybpm)
[![Coverage Status](https://coveralls.io/repos/github/DanielRHayes/honeybpm/badge.svg?branch=master)](https://coveralls.io/github/DanielRHayes/honeybpm?branch=master)
[![license](https://img.shields.io/github/license/DanielRHayes/honeybpm.svg)](https://github.com/DanielRHayes/honeybpm/tree/master/packages/honeybpm)


# Installation

```
npm install @honeybpm/hbpm
```


HoneyBPM is a simple process runner to allow developers to create business process which are built with reusable components.  They do this by defining a [ProcessMap](#process-map) made up of various [Steps](#steps).  Each Step runs a small piece of code called a [Vertex](#vertices).


# Getting Started

The goal of HoneyBPM is to allow developers to make business processes in a component-based and standardized way. They do this by defining a [ProcessMap](#process-map) made up of various [Steps](#steps). Each Step runs a piece of code called a [Vertex](#vertices).

## Your First Process Map

Let's make our first process. One that calculates tax rate for a value.

```javascript

// todo: Add a process map code

```



# Vertex

A Vertex is the smallest piece of a HoneyBPM process. Vertices should be reusable across many processes.  There are two types of vertices, [VertexSync](#vertex-sync) and [VertexAsync](#vertex-async).

## Vertex Sync

VertexSync is a vertex which will run to completion, as opposed to [VertexAsync](#vertex-async) which allows you escape the vertex in the midst of a process and return to it later.


### Creation
To create a ``VertexSync`` pass a name, and run middleware into the constructor.

```javascript
let SomeSyncVertex = new VertexSync({
    name: "VertexA",
    description: "This vertex doesn't do anything useful",
    run: function(data, context,next) {
        console.log("Ran SyncVertex");
        return next();
    }
});
```

### Using in a Step

To use in a step, pass the vertex into the step constructor as the ``vertex``.

```javascript

let SomeStep = new Step({
    name: "SomeStepName",
    description: "Some description of this step",
    vertex: SomeSyncVertex
});

```

## Vertex Async

A VertexAsync is a vertex which you are able to leave and then return to at some point in a process map.


### Creation
To create a ``VertexSync`` pass a name, and run middleware into the constructor.

```javascript
let SomeAsyncVertex = new VertexAsync({
    name: "VertexA",
    description: "This vertex doesn't do anything useful",
    runAsync: function(data, context, next) {
        console.log("The vertex runs this function when you are running a process, after next() is called the process will stop");
        return next();
    },
    continueAsync: function(data, context,next) {
        console.log("Then when the process is continued this function will run");
        return next();     
    }
});
```

### Uses

VertexAsync is useful for in processes which required user intervention.  For example, you could have a ``VertexAsync`` create a task for a user to complete and then continue the process after they complete it.

```javascript
let TaskVertex = new VertexAsync({
    name: "TaskVertex", 
    description: "A vertex for a task",
    runAsync: function(data, context, next) {
        
        // some code to write a task to a database...
        
        // write the taskId to the processes data so you can use it when you continue the process
        data.taskId = task.id;
        
        return next();
    },
    continueAsync: function(data, context,next) {
      
        let taskId = data.taskId;
        
        // code to find and complete the task using the data passed into the process
        
        return next();        
    }
    
});

```


### Using in a Step

To use in a step, pass the vertex into the step constructor as the ``vertex``.

```javascript

let SomeStep = new Step({
    name: "SomeStepName",
    description: "Some description of this step",
    vertex: SomeAsyncVertex
});

```



# Steps

## Creating

```javascript
'use strict';
const hbpm = require('@honeybpm/hbpm');

const logStep = new hbpm.Step({
    name: "LogStep",
    inputMapping: function (processData, done) {
        let inputData = {};
        inputData.title = "DDD";
        inputData.user = "Test";
        return done(null, inputData);
    },
    outputMapping: function (processData, stepData, done) {
        processData.a = "A";
        return done();
    },
    vertex: LogVertex
});

```

### inputMapping

``inputMapping: function(processData, done)``
inputMapping is a function which has processData and a done callback.  You pass the data you want the vertex to the done callback as the second parameter (the first is an error).

### outputMapping

``outputMapping: function(processData, stepData, done)``

outputMapping is a function which has processData, stepData, and a done callback.  You don't need to pass anything to done.

# Process Map

A process map is the structure of a business process and made up of various steps linked together with a ``to`` function which declares which step to run next.

## Defining a Process Map