Open MercatoDocsSearch docsAPI reference

Framework

Entity Updates (UPDATE_ENTITY)

To let a workflow change an order, first choose the existing command that should make the change. The module must expose it for unattended use, the tenant must permit it, and the activity must run within the applicable execution permissions.

The UPDATE_ENTITY activity is how a workflow changes business data. It runs a registered command through the command bus, so every mutation keeps its audit log, undo entry, cache invalidation, events and index updates.

Which commands it may run is decided by three gates, all of which must pass.

GateOwnerAnswer to
1. CatalogueThe module that owns the command, in codeIs this command safe to run unattended at all?
2. Tenant enablementAn administrator, in Settings → Workflow CommandsHas this customer switched it on?
3. Actor featuresRBAC, at execution timeMay the user this run acts as do it?

Removing any one of them collapses the model, so none of them is optional. In particular, there is deliberately no way to type an arbitrary command id into the settings page: that would reach commands nobody designed for unattended execution, and a command that declares no permission would have no gate left. The tick-box list over a code-declared catalogue is the safety.

Gate 1 – declaring a candidate (module authors)

A module declares its candidates from a module-root workflows.ts, which the generator auto-discovers. See packages/core/src/modules/customers/workflows.ts for the reference.

import { createWorkflowsModuleConfig } from '@open-mercato/shared/modules/workflows'
import { registerWorkflowSafeCommands } from '@open-mercato/core/modules/workflows/lib/workflow-safe-commands'

registerWorkflowSafeCommands([
  {
    commandId: 'customers.deals.update',
    requiredFeatures: ['customers.deals.manage'],
    labelKey: 'customers.workflows.commands.deals.update',
  },
])

export const workflowsConfig = createWorkflowsModuleConfig({
  moduleId: 'customers',
  workflows: [],
})

export default workflowsConfig

Rules:

Deciding what is safe to run unattended

"Is a user allowed to do this?" and "is this safe fired by a trigger" are different questions. UPDATE_ENTITY carries no idempotency key, activities are retried on failure (retryPolicy), and a trigger can fire the same workflow repeatedly. So:

The shipped catalogue therefore declares updates only:

CommandRequires
sales.orders.updatesales.orders.manage
customers.deals.updatecustomers.deals.manage
customers.people.updatecustomers.people.manage
customers.companies.updatecustomers.companies.manage
catalog.products.updatecatalog.products.manage

Adding a create, a delete, or a sub-resource command (catalog variants, prices, offers) needs its own argument. A candidate in the catalogue is an invitation even when it ships switched off.

Gate 2 – the tenant setting (administrators)

Settings → Module Configs → Workflow Commands lists every candidate, grouped by the module that declared it, showing the command id and the permission it requires. Ticking one lets any workflow in that tenant execute it; the acting user's own permission is still checked at run time.

Storage is one tenant-scoped module_configs row: module workflows, key update_entity_enabled_commands, value a JSON array of command ids. It is read through ModuleConfigService, so it inherits that service's tenant-keyed cache (60s TTL, invalidated on write) – the enablement lib deliberately adds no second cache, which is what keeps the settings page and the runtime from ever disagreeing.

API: GET/PUT /api/workflows/command-settings, gated on workflows.definitions.view and workflows.manage respectively. A PUT carrying an id outside the catalogue is a 400 (WORKFLOW_COMMAND_NOT_IN_CATALOGUE), never a stored row.

What an absent setting means

Exactly the set that was reachable before this gate existed.

A declaration may carry defaultEnabled: true – the grandfather clause – and a tenant that has never saved the setting resolves to exactly those declarations. In the shipped product that is sales.orders.update alone, which is what the sales.order-approval workflow and its gallery template run on. Any candidate declared afterwards omits the flag and is off until a human ticks it.

Shipping is therefore byte-identical for every existing tenant, and fail-closed for every new capability. An unreadable or malformed stored value resolves the same way – it can never widen the reachable set, and can never break a workflow that already ran.

Warning · For module authors

defaultEnabled is reserved for commands that were already reachable before the gate existed. A module setting it on a new declaration is handing itself the tenant's decision. workflow-safe-command-catalogue.test.ts asserts that only sales.orders.update carries it.

Gate 3 – the actor's own permissions

Unchanged, and deliberately still last. executeUpdateEntity resolves the granted features of the user the run acts as and requires all of the command's declared requiredFeatures. Enabling a command for a tenant is not a grant of it to everybody in that tenant.

Authoring: what the picker shows

The UPDATE_ENTITY command field offers only the commands the tenant has enabled. Disabled candidates are still listed beneath it, with the remedy, because the field accepts free text – a command that simply vanished would turn a runtime failure into an unanswerable absence. An authored value that is disabled, or absent from the catalogue entirely, is called out with a warning that says the run will fail.

Typed context: what an update contributes downstream

What lands in the run context is the activity handler's return, not the command's:

{ "executed": true, "commandId": "customers.deals.update", "result": { … }, "logEntryId": "…" }

So a command's outputSchema describes what sits under result, and the context ledger nests it there – Update Deal.result.dealId, not Update Deal.dealId. Declaring a command workflow-safe is what makes its outputSchema reach an author at all: before customers.deals.update was declared, it was the only command in the repo carrying an outputSchema and no workflow could run it.

Troubleshooting

Run errorCauseRemedy
UPDATE_ENTITY command is not allowedGate 1 – the command is not in the catalogue.The owning module must declare it in its workflows.ts.
UPDATE_ENTITY command is not enabled for this tenantGate 2 – catalogued but not ticked.Settings → Workflow Commands.
UPDATE_ENTITY command is not authorizedGate 3 – the acting user lacks requiredFeatures.Grant the feature, or change the workflow's execution identity. Check the feature actually exists in the owning module's acl.ts.
UPDATE_ENTITY requires an authenticated workflow userThe run has no acting user.Start the instance with a user, or give the definition an execution identity.

Source map

ConcernFile
Catalogue registry (tier 1)packages/core/src/modules/workflows/lib/workflow-safe-commands.ts
Enablement decision (PURE)packages/core/src/modules/workflows/lib/workflow-command-enablement.ts
Per-request read/writepackages/core/src/modules/workflows/lib/workflow-command-settings.ts
Runtime gatepackages/core/src/modules/workflows/lib/activity-executor.ts → executeUpdateEntity
Settings APIpackages/core/src/modules/workflows/api/command-settings/route.ts
Picker feedpackages/core/src/modules/workflows/api/commands/route.ts
Settings pagepackages/core/src/modules/workflows/backend/config/workflows/

Test the command with the automation's permissions

Pick a disposable record in the same tenant as the workflow. First confirm the intended update with a role that may execute it, then run the workflow. If its activity is refused, check the command's eligibility, tenant policy and execution identity separately.

Previous: Workflow Architecture · Next: Extending Workflows