Attaching Workflow on Custom remotes

To attach a workflow on custom remote add the remote and mappingName properties to the workflow mapping and give operation: custom as shown below.

{
  "modelName": "Order",
  "workflowBody": {
	"workflowDefinitionName": "ApprovalWorkflow"
  },
  "operation": "custom",
  "remote":{
    "path":"/special-order/:id",
    "method":"SpclOrderBYId",
    "verb":"put"
  },
  "mappingName":"splOrder",
  "wfDependent": true,
  "version": "v2"
}
property Description
path The path of the remote method on which workflow should be attached
method The remote method name
verb The remote method HTTP type
mappingName Name to uniquely identify the mapping

When we post this mapping to the the workflow-manager model ( POST /workflowManagers/workflows), a new custom maker-checker end point will be create on the attached model as shown below

POST orders/maker-checker/custom/{{mappingName}}

Example on how to attach workflow on remotes

Lets take a sample model Order which has one remote method defined and attach a workflow on that remote. Below is the model definition of order and its .js file.

Model: Order.json
{
  "name": "Order",
  "base": "BaseEntity",
  "properties": {
    "quantity": {
      "type": "number",
      "required": true
    },
    "buyer": {
      "type": "string",
      "required": true
    },
    "specialOrder": {
      "type": "boolean",
      "default": false
    }
  }
}
Model: Order.js
module.exports = function Order(Model) {
 
  Model.SpclOrderUpdate = function specialOrderUpdate(data, options, next) {
    if (typeof next === 'undefined') {
      next = options;
      options = {};
    }
    data.specialOrder = true;
    next(null, data);
  };

  Model.remoteMethod('SpclOrderBYId', {
    description: 'Remote method to add special order status',
    accessType: 'WRITE',
    accepts: [{
      arg: 'id',
      type: 'string',
      http: {
        source: 'path'
      },
      description: 'id of the model data'
    }, {
      arg: 'data',
      type: 'object',
      http: {
        source: 'body'
      },
      description: 'Model data to be posted'
    }, {
      arg: 'options',
      type: 'object',
      http: 'optionsFromRequest'
    }],
    http: {
      verb: 'put',
      path: '/special-order/:id'
    },
    returns: {
      arg: 'response',
      type: 'object',
      root: true
    }
  });

  Model.SpclOrderBYId = function specialOrderById(id, data, options, next) {
    if (typeof next === 'undefined') {
      next = options;
      options = {};
    }
    data.id = id;
    data.specialOrder = true;
    Model.create(data, options, next);
  };
};
Workflow mapping

POST /workflowManagers/workflws

{
  "modelName": "Order",
  "workflowBody": {
	"workflowDefinitionName": "ApprovalWorkflow"
  },
  "operation": "custom",
  "remote":{
    "path":"/special-order/:id",
    "method":"SpclOrderBYId",
    "verb":"put"
  },
  "mappingName":"splOrder",
  "wfDependent": true,
  "version": "v2"
}
Post to maker-checker end point

POST orders/maker-checker/custom/{{mappingName}} pass the below data

mappingName:

splOrder

data:

[
"Order12443",
{
  "quantity": 200,
  "buyer": "Tony"
},
"options"
]

Observations :

  1. The mapping name is the same name which you give in workflow-mapping record.
  2. data is an array with all the inputs to the custom remote method which is defined as part of remote.method in mapping.
  3. The order and type of the elements in data should be same as that of auguments we have defined in the method.
  4. options is the placeholder and it should be passed as string if your method accepts options as one of the parameter. The value of options will be in context with the call.