Official Plugins (Kuzzle v2.x)
Workflows v1.x
2

This plugin is part of the Kuzzle Enterprise Plan. If you are interested, please contact us.

WorkflowContext #

Copied to clipboard!
import _ from 'lodash';
import {
  KuzzleRequest,
  DocumentNotification,
  JSONObject,
} from 'kuzzle';
import { Workflow } from '../models';
export class WorkflowContext {
  /**
    * Tenant index where the workflow document is present
    */
  tenantIndex: string;
  /**
    * Workflow currently executed
    */
    workflow: Workflow;
    /**
      * Original request who triggered the workflow.
      *
      * (`trigger.type=event`)
      */
    request?: KuzzleRequest;
    /**
      * Original notification who triggered the workflow.
      *
      * (`trigger.type=notification`)
      */
    notification?: DocumentNotification;
    /**
      * Extracted payload
      */
    payload: JSONObject;
    /**
      * Additional information passed between actions
      */
    props: JSONObject;
  constructor ({ tenantIndex, workflow, request, notification, props }: {
    tenantIndex: string,
    workflow: Workflow,
    request?: KuzzleRequest,
    notification?: DocumentNotification,
    props?: JSONObject,
  }) {
    this.tenantIndex = tenantIndex;
    this.workflow = workflow;
    this.request = request;
    this.notification = notification;
    this.props = props || {};
    this.payload = this.extractPayload();
  }
  extractPayload () {
    const item = this.workflow._source.trigger.type === 'event'
      ? this.request
      : this.notification;
    return this.workflow._source.payloadPath === '.'
      ? item
      : _.get(item, this.workflow._source.payloadPath);
  }
}