# PXL Actions module

# Concept

An action is a single unit of work that can be executed by the backend. Every data mutation can only be executed through an action. As all actions are logged, this allows to audit any data changes. Furthermore, as the logged actions (or their operations) contain the initial state before the mutation, it allows to revert any changes. This is the foundation to enable offline work and data synchronization.

An action typically has one or more ActionOperations/Transformations. These are the individual steps that are executed to perform the action - and are also logged.

## Lifecycle

- Any business logic has access to the `DispatcherService` and can dispatch actions through it
- An action contains:
  - `scope` - for the dispatcher to know which business logic service to dispatch the action to
  - `type` - for the business logic's dispatcher to know which action to perform
  - `payload` - the data that is passed to the action
- Once an action is dispatched, the dispatcher initiates the action's `ActionExecution` (aka execution context). This logs the start of the action and its content
- The dispatcher dispatches the action and the execution context to the business logic
- The business logic can now perform any changes. Mutation is only possible in the data repositories and require the execution context.
- Each mutation in the repositories will be wrapped in an `ActionOperation` and logged.
- If an error occurs, the execution context will log the error (in both, the operation and the execution) and throw the error.
- The execution context will then finish the action and log the end of the action
