# Welcome

![](https://3780118674-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-McimrO8ISoLqTJhv7zx%2F-Me6BS2zoKUIRikndunv%2F-Me6BjsPELAsRAqrPucJ%2Fblog-popup-banner_stech.png?alt=media\&token=0ef150cf-04ab-45d2-a076-b667be254a96)

* [1. Workflow Technical Documentation](#1-workflow-technical-documentation)
  * [1.1. Project Overview:](#11-project-overview) 2
  * [1.2. Technical Details](#12-technical-details)
    * [1.2.1. Main packages/lib](#121-main-packageslib)
    * [1.2.2. Rule Bodies for all types](#122-rule-bodies-for-all-types)
      * [1.2.2.1. Webhook](#1221-webhook)
      * [1.2.2.2. Kafka Event](#1222-kafka-event)
      * [1.2.2.3. Cron Jobs & Scheduled task](#1223-cron-jobs--scheduled-task)
    * [1.2.3. Actions](#123-actions)
      * [1.2.3.0.1. Example Rule Body with an action.](#12301-example-rule-body-with-an-action)
      * [1.2.3.0.2. What happens when this rule is triggered](#12302-what-happens-when-this-rule-is-triggered)
      * [1.2.3.0.3. Processed rule item looks like this](#12303-processed-rule-item-looks-like-this)
    * [1.2.4. Steps](#124-steps)
      * [1.2.4.1. Anatomy of the Rule](#1241-anatomy-of-the-rule)
  * [Filter Expression](#filter-expression)

## 1.1. Project Overview:

A workflow is an automation tool. Enable everyone to easily automate your business existing processes with a solution that works with the Workflow tools you use every day – improving time to market and employee efficiency.

## 1.2. Technical Details

Basically Workflow has 3 parts,

* Rule
* Actions & Pre-defined Actions (Http REST, Elastic Log etc.)
* Logical Processors and Operators (if, wait, etc.)&#x20;

Pseudo Object

```
Rule 
    { Rule Object Detail }
    { Triggers } 
    { Steps } 
       - { Action } 
       - { Logical Processors and Operators }
```

The rule object can be Webhook, Kafka Event Listener, and Scheduled Cronjobs.

### 1.2.1. Main packages/lib

```
    "axios": "^0.21.1", //for HTTP request
    "mustache": "^4.1.0", //for template engine
    "node-cron": "^3.0.0" // for cron & schedule
```

### 1.2.2. Rule Bodies for all types

#### 1.2.2.1. Webhook

A webhook in web development is a method of augmenting or altering the behavior of a web page or web application with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application.

```javascript
{
    "triggers": {
        "listen" : "Webhook",
        "name" : "__Mock_Webhook_Test",
        "url" : "/custom/webhook/url",
    },
    "status" : 1,
    "steps" : []
}
```

#### 1.2.2.2. Kafka Event

Apache Kafka is a framework implementation of a software bus using stream-processing. It is an open-source software platform developed by the Apache Software Foundation written in Scala and Java.

```javascript
{
    "listen" : "Kafka",
    "topic" : "LoginSucceeded",
    "name" : "__Mock_Kafka_Test",
    "status" : 1,
    "steps" : []
}
```

#### 1.2.2.3. Cron Jobs & Scheduled task

The software utility cron also known as cron job is a time-based job scheduler in Unix-like computer operating systems. Users who set up and maintain software environments use cron to schedule jobs to run periodically at fixed times, dates, or intervals. It typically automates system maintenance

```javascript
{
    "listen" : "Cron",
    "cron" : "* * * * * *", //every minute
    "name" : "__Mock_Cron_Test",
    "scheduled": false, 
    "initialData" : {
        "TestValue" : "This is a cron job"
    },
    "status" : 1,
    "steps" : []
}
```

If the `scheduled` value is `false`, it means the workflow will run every minute, otherwise it will run once. It may contain such initial data but is not mandatory. If you want to create scheduled cron you must use `true` as value for `scheduled` props. For example if `cron` value is `0 0 12 24 2 ? *` and `scheduled` is `true` it means it will run next 24th Februrary. When `scheduled` value is false it means it will run every year.

For detailed information visit cronmaker.com

```
1.    2022-02-24 Thu 12:00:00
2.    2023-02-24 Fri 12:00:00
3.    2024-02-24 Sat 12:00:00
4.    2025-02-24 Mon 12:00:00
5.    2026-02-24 Tue 12:00:00
...
```

### 1.2.3. Actions

The action is usually contain Http transactions. Example step object below

```javascript
{
    "name" : "Gamstop",
    "selector" : "gamstop_checker",
    "type" : "Http",
    "method" : "GET",
    "status" : 1,
    "url" : "https://api-k.spacecasino.co.uk/action/member/check_gamstop?token={{Token}}&memberId={{MemberId}}",
    "response" : "json",
    "headers" : [ 
        {
            "key" : "",
            "value" : ""
        }
    ],
    "keys" : [ 
        {
            "Token" : ""
        }, 
        {
            "MemberId" : ""
        }
    ]
}
```

For example, this Gamstop action that work with Query String parameters. Token and MemberId are dynamically populated. When someone uses this action, the rule must provide the values. So, if we had a rule or rule step with Webhook or HTTP, our first request would be Token and MemberId.

**1.2.3.0.1. Example Rule Body with an action.**

```javascript
{
    "listen" : "Webhook",
    "url" : "/test/webhook",
    "name": "Sample workflow",
    "status" : 1,
    "steps" : [ 
        {
            "type" : "Action",
            "name" : "Gamstop",
            "replace" : [ 
                {
                    "key" : "MemberId",
                    "value" : "{{Step1.memberId}}"
                }, 
                {
                    "key" : "Token",
                    "value" : "54fba61f-08e6b81653"
                }
            ]
        },
        {
            "type" : "function",
            "name" : "log",
            "text" : "Sample log {{Step2.result}}"
        }
    ]
}
```

**1.2.3.0.2. What happens when this rule is triggered**

Here is the development environment logs, result message is `Invalid token` because Gamstop API returns it when request has invalid token. So it means it is working correctly.

```
         √ Running rule Sample Workflow
         √ Step: Gamstop, type: Http
         √ Step: log, type: function
         √ Step result: log, type function Sample log Invalid token
```

**1.2.3.0.3. Processed rule item looks like this**

```javascript
{
    "_id" : ObjectId("605d3fb80a68153b64116411"),
    "listen" : "Webhook",
    "url" : "/test/webhook",
    "name" : "Sample Workflow",
    "status" : 5,
    "steps" : [ 
        {
            "_id" : ObjectId("603e640df50d3584b4a707bf"),
            "name" : "Gamstop",
            "selector" : "gamstop_checker",
            "type" : "Http",
            "method" : "GET",
            "status" : 1,
            "url" : "https://api-k.spacecasino.co.uk/action/member/check_gamstop?token=54fba61f-08e6b81653&memberId=1234b6a39fce38eb3220",
            "response" : "json",
            "headers" : [ 
                {
                    "key" : "",
                    "value" : ""
                }
            ],
            "keys" : [ 
                {
                    "Token" : ""
                }, 
                {
                    "MemberId" : ""
                }
            ]
        }, 
        {
            "type" : "function",
            "name" : "log",
            "text" : "Sample log {{Step2.message}}"
        }
    ],
    "data" : {
        "Step1" : {
            "memberId" : "bdca6f50452b4faeb6a39fce38eb3220"
        },
        "Defaults" : {
            "gamstop_checker" : {
                "api_token" : "",
                "name" : ""
            }
        },
        "Step2" : {
            "status" : 401,
            "message" : "Invalid token"
        }
    },
    "currentStep" : 1
}
```

![Image of Rule](https://github.com/WhiteOrg/workflow/blob/master/doc/images/sample_1.PNG)

### 1.2.4. Steps

Steps can contains Actions and Http Request types.

#### 1.2.4.1. Anatomy of the Rule

You can see a rule below.

When the `member registration` request rule is `triggered`> `wait` 5 seconds> take the `action` Gamstop> `if` the action result of step 3 is `equal` to `N` > `wait` 1 second> write to the `log` > if the action of step 3 is `Y`, rewrite the `log`. The last step will pass. Because the rule was configured incorrectly. Step3.result can have only one value at a time. N or Y, the rule manager must know this.

```javascript
{ 
    "listen" : "Webhook",
    "name" : "Full Metal Alchemist",
    "url" : "/member/registration",
    "status" : 1,
    "steps" : [ 
        {
            "type" : "function",
            "name" : "wait",
            "time" : 5000
        }, 
        {
            "name" : "Gamstop",
            "selector" : "gamstop_checker",
            "type" : "Http",
            "method" : "GET",
            "status" : 1,
            "url" : "...?token=54fba61f-aca03&memberId=bdc12345a",
            "response" : "json",
            "headers" : [ 
                {
                    "key" : "Authorization bearer",
                    "value" : "Static Variable"
                }
            ],
            "keys" : [ 
                {
                    "Token" : ""
                }, 
                {
                    "MemberId" : ""
                }
            ]
        }, 
        {
            "type" : "function",
            "name" : "if",
            "conditions" : [ 
                {
                    "operator" : "equal",
                    "defaultValue" : "N",
                    "value" : "{{Step3.result}}"
                }
            ]
        }, 
        {
            "type" : "function",
            "name" : "wait",
            "time" : 1000
        }, 
        {
            "type" : "function",
            "name" : "log",
            "text" : "Sample log {{Step3.result}}"
        }, 
        {
            "type" : "function",
            "name" : "if",
            "conditions" : [ 
                {
                    "operator" : "equal",
                    "defaultValue" : "Y",
                    "value" : "{{Step3.result}}"
                }
            ]
        }, 
        {
            "type" : "function",
            "name" : "log",
            "text" : "Another log {{Step1.memberId}} {{Step3.result}}"
        }
    ],
    "data" : {
        "Step1" : {
            "memberId" : "bdca6f50452b4faeb6a39fce38eb3220"
        },
        "Defaults" : {
            "gamstop_checker" : {
                "Token" : "",
                "MemberId" : ""
            }
        }
    },
    "currentStep" : 0
}
```

## Filter Expression

{ "$or": \[{ "data.Step1.TestName": "John"}, { "data.Step1.TestName": "Antonio"} ] }
