Event trigger - Examples
Introduction
This article aims to show you how to use event triggers to handle different scenarios in reality. It also covers important configurations of workflow definition to send and receive data from an event trigger. Before starting, it may be worth ensuring you know how to add a worker site and a workflow definition.
Examples
Example 1: Subscribe to the User unauthorized
event to log suspicious accesses
Introduction
When a user attempts to access a restricted page, Casewhere emits an event called User unauthorized
. In this example, you will configure an event trigger to listen to the event and starts a workflow, which extracts data from the event and add a log entry in database.
Configuration
Add the following resources in Casewhere Admin.
Workflow definition: Log Suspicious Accesses
The workflow to record unauthorized accesses.
The below script demonstrates how to access the event data.
var dataApi = ctx.Use<IDataApi>();
var username = ctx.Input.Username;
// Add a new log entry with data from the event
dataApi.Add("UnauthorizedAccessLog", new { Username = Username });
Event trigger: Log Suspicious Access
The event trigger that listens to the User unauthorized
event on the worker site "Sample Worker Site" and executes the workflow "Log Suspicious Access" when users access a page they are not allowed to.
The workflow should start immediately when the event is emitted if Is Synchronous is set. Otherwise, the workflow will be added to the queue and run afterwards in the background.
Example 2: Emit and subscribe to custom events
Introduction
Imagine you're developing a component to send emails in Casewhere. You want to provide your consumers with a convenient way to intercept and modify the email content, e.g., adding an email signature, before it is sent without modifying the component. In this example, you will learn how to emit an event from a workflow, and then handle the event using event trigger.
Configuration
Emit event
Assuming you have a workflow sending email with the following script:
var dataApi = ctx.Use<IDataApi>();
var pluginApi = ctx.Use<IPluginApi>();
var emailInfo = new
{
Recipient = "example@casewhere.com",
Body = "Welcome to custom event"
};
var emailId = dataApi.Add("CwUatEmailMessage", emailInfo);
// emit the event so others can subscribe or intercept
ctx.Emit("message sending", emailInfo);
// load the email again because someone might modify it by subscribing to the event
var email = dataApi.Load(emailId);
// send email using plugin
var mailParameters = new Dictionary<string, object>()
{
{ "body", email["Body"] },
{ "to", email["Recipient"] },
};
var pluginResult = pluginApi.Invoke("Mail plugin", "SendEmail", mailParameters);
Handle event
Add a workflow for adding the email signature. Casewhere only accepts automatic, create-case workflow definitions for using with event triggers. We also need to set Accept Input to receive the event data as workflow input.
Add a scripted activity to append the signature to the email content.
var dataApi = ctx.Use<IDataApi>();
var emailId = ctx.Input.Id;
var emailBody = ctx.Input.Body;
emailBody += "<br/>Cheers, your Casewhere team.";
dataApi.Update(emailId, new { Body = emailBody });
Add a event trigger to subscribe to the custom event message sending
. Make sure you enable Is Synchronous so that the workflow can be executed before the email is sent.
Example 3: Intercept the User authorizing
event to handle additional authorization logic
Introduction
The scenario is that your solution needs to verify the user contract when he or she logins into your system. If the contract is no longer valid, the user will not be allowed to access.
Configuration
Workflow definition: Validate User Contract
Casewhere provides the API IWebApi.AbandonAuthorization
for canceling the user login session.
In this example, a plugin is used to validate the user contract and determine whether to accept or reject the login request.
var webApi = ctx.Use<IWebApi>();
var pluginApi = ctx.Use<IPluginApi>();
var username = ctx.Input.Username;
var parameters = new Dictionary<string, object>();
parameters["Username"] = username;
var pluginResult = pluginApi.Invoke("ContractManagement", "Validate", parameters);
var isValid = pluginResult.Get<bool>("IsValid");
if(!isValid)
{
webApi.AbandonAuthorization();
}
Event trigger: Validate User Contract
Add an event trigger to subscribe to the User authorizing
event. Make sure you enable Is Synchronous so that the workflow can be executed in the context of the user authentication.