What are the effects?
Effects are the business logic written in TypeScript that is executed by Trace API after an action is submitted. The effects declared in the configuration project are stringified functions that are stored in the database, so they can be interpreted at runtime by Trace API.No external constants or functions can be used in the effects.
Workflow definitions
Thedsl.$definitions object is the read-only storage of the workflow. It can be used to store constants, environment variables, workflow ids and stringified functions that will be accessible in the effects.
The definitions can be of any shape, but by convention, it’s shape is the following:
- Documentation
- Example
- Usage
The repository of the workflow where workflow constants are stored.
An object where each key is the name of the function and the value is the stringified function.
A map between the workflow label and its id.
A map between the environment variable name and its value.
Notifications
Notifications (usually emails) are sent to a user (could be a single user, or a group) when performing an action. They are sent when thestate.notifications is updated. Thus, notifications are usually built and sent inside the effects of an action.
There is 3 steps to send a notification through configuration :
- Create a function inside
workflowDefinitionsto build the template of the email, and manipulate data derived from the state (because no external constants or functions can be used in the effects.)
create a custom function in the workflow /definitions/functions
- Inside the effects, import this function.
Usage in effects
- Updating
state.notificationsby pushing the template object with the correct data.
Update state
External functions
Some functions are declared directly in Trace API, and can be accessed direcly in the effects usingdsl.$modules.
createLink
createLink
An asynchronous function designed to create a new link,
enabling Trace to automatically perform an action.
This action can be executed within any workflow,
either by creating a new trace or targeting an existing one.
- Body
- Example
The id of the target workflow.
The id of the target trace.
If not provided, the link will be created in a new trace.
If not provided, the link will be created in a new trace.
The action that will be done.
The form data to be used in the action.
The label of the group that will do the automatic action.
Since
Since
createLink makes an automated action, the group label is usually the 'traceBot'.The date of creation of the link.
searchTraces
searchTraces
createScheduler
createScheduler
An asynchronous function used to create a new scheduler.
The scheduler can be for example used to create links at a specific interval.
The scheduler can be for example used to create links at a specific interval.
- Body
- Example
The unique name of the scheduler.
The data that will be used by the scheduler.
The cron expression that defines the schedule.
Any string to identify the creator of the scheduler.
stopScheduler
stopScheduler
An asynchronous function that stops a scheduler using its id.
- Body
- Example
The id of the scheduler to stop.
moment
moment
Exposes the
See the moment documentation for more information.
moment function from the moment library (v2.29.1).See the moment documentation for more information.
Example
zod
zod
Exposes
See the zod documentation for more information.
z from the zod library (v3.24.0).See the zod documentation for more information.
Example
Example - Order a meal
Let’s imagine a restaurant workflow where a client can order a meal. The workflow has two groups:cook and client.
In the first action, the client will select a meal to order and submit the form.
The goal of the effect is to:
- store the selected meal in the trace’s data,
- add the
'Order received'status to the trace with a progress of20%, - and update the next available actions for each group:
- the
cookgroup should be able to do theprepareOrderaction andcomment, - the
clientgroup should only be able to do thecommentaction.
orderMealEffect