Files
alamin-br 588bae1b50 feat[5/6] Button field: start an automation workflow (#6035)
* feat: let a button field start an automation workflow

* fix: start only a workflow from the button's own workspace

* fix: drop an imported workflow the button's workspace does not hold

* test: cover what a click on a start workflow action does

* feat: offer the start workflow action in the button field editor

* test: cover the start workflow form inside the field editor

* docs: say what the start workflow action does and does not do

* docs: point to the start workflow action from phase 4's summary

* fix: keep a snapshot's start workflow action pointing at its workflow

* test: cover the create and type swap routes into the workspace guard

* test: pin the start workflow icon, label and immediate dispatch error

* docs: say what a click reaches through a started workflow

* fix: keep an imported workflow only when the reference is this instance's

Ids are one global sequence, so a file written elsewhere can name a
workflow number the destination workspace happens to own. Presence in the
workspace was read as identity, and the button started work nobody chose.

An imported action now keeps its workflow only when this import remapped
it, or when the data never left the instance, and in both cases when the
workspace still matches. Field duplication and a field type change say so
the way every other copy does, with an `is_duplicate` config.

The same rule now also refuses a workflow whose trigger cannot be
dispatched immediately, which a save with the same id already refused, and
a copy asked for by a person drops a workflow that person may not read.

* fix: say in the editor that the started workflow cannot be found

Trashing the automation leaves the id on the action. The click fails, and
the editor said nothing: the shared service type reports a workflow that is
missing from the list the same way it reports one still being fetched.

The database action type now says it, once the applications have actually
been fetched. Every automation of the workspace carries its workflows in
that same payload, so before it lands an empty store is what a load still
running looks like, and nothing is said.

The applications are filtered by what the reader may see, so absence
answers whether this editor can see the workflow, not whether it exists.
The copy says that, rather than sending someone to replace an action that
works for everybody else.

* fix: treat a template install as the file import it is

A template is imported with `is_duplicate=True`, so the rule that keeps an
imported workflow read it as a copy that never left the instance. It is the
one exception to that: the file was written on another installation, where
the same number meant a different workflow, which is the collision the rule
exists to stop.

`ImportExportConfig` now says which of the two an import is, and the start
workflow action drops a workflow a template names by a number this
workspace happens to own. Nothing else reads the new flag, so a template
install keeps behaving as it did.

Also drops the id mapping `_check_workflow` no longer receives: the import
path stopped calling it, and leaving the parameter suggested the save path
handles the snapshot case, which only the import path does.

* test: cover a duplicated button field keeping its workflow

Field duplication skips the serialization import path and builds its own
import config, so nothing covered the one call site that config was added
for: remove it and every duplicated button silently loses its workflow
while the suite stays green.

Also says in a comment that a field type change reaching the same code is a
restore of what the conversion backed up, not a copy, and that the read
check applies to it too.

* refactor: read the workspace from the caller, not the store

The action list already knows which workspace the button field is in and
passes it in the context every other check reads. Taking it from the
selected workspace instead said something the type had no business knowing.

* fix: resolve an imported workflow reference instead of following it

An export of a database without its automation names a workflow id this
installation does not have. The foreign key is deferred, so the service row
is written and following the reference is what fails, ending the whole import
job over a reference that simply has to go. Look the workflow up and blank it
when it is not here.

Read the id off the service defensively too: the service type builds whatever
type the file named, so a hand-edited or version skewed export can leave a
start workflow action holding a service that has no workflow at all.

* fix: ask the id mapping what it actually remapped

`MirrorDict` answers `in` and `get` for every key, and an import installs
one under this very key when a workflow is duplicated. Asked that way the
guard is told this import remapped an id it never touched, and the collision
check below it never runs, so the copy could keep a workflow nobody picked.
The key view answers only for what was written.

* test: cover a restored button keeping its action target

Converting away from a button deletes the row its actions cascade off, so
only the backup brings them back, and nothing covered what a restored action
ends up pointing at.

* refactor: trim comments on the start workflow action

* fix: decide a start workflow import before the row is written

The import callbacks run after the application's transaction has
committed, so a workflow id this installation lacks failed on insert.
Permission is now checked before the trigger, through the service.

* fix: restore the serializers import develop moved out of the module
2026-09-09 16:52:40 +02:00

226 lines
6.8 KiB
JavaScript

import { readFileSync } from 'fs'
import { resolve } from 'path'
import { TestApp } from '@baserow/test/helpers/testApp'
import DatabaseWorkflowActionWithService from '@baserow/modules/database/components/field/DatabaseWorkflowActionWithService'
// The i18n loader compiles imported locale files into message ASTs.
const en = JSON.parse(
readFileSync(
resolve(process.cwd(), 'modules/integrations/locales/en.json'),
'utf8'
)
)
const enDatabase = JSON.parse(
readFileSync(
resolve(process.cwd(), 'modules/database/locales/en.json'),
'utf8'
)
)
const WORKSPACE_ID = 1
const DATABASE_ID = 100
const AUTOMATION_ID = 200
const applications = () => [
{
id: DATABASE_ID,
name: 'Customers',
type: 'database',
workspace: { id: WORKSPACE_ID },
tables: [],
},
{
id: AUTOMATION_ID,
name: 'Onboarding',
type: 'automation',
order: 1,
workspace: { id: WORKSPACE_ID },
workflows: [
{
id: 11,
name: 'Send welcome',
order: 1,
immediate_dispatch: true,
published_on: '2026-09-01T00:00:00Z',
state: 'live',
},
{ id: 12, name: 'Nightly sync', order: 2, immediate_dispatch: false },
{
id: 13,
name: 'Still a draft',
order: 3,
immediate_dispatch: true,
published_on: null,
state: 'draft',
},
{
id: 14,
name: 'On hold',
order: 4,
immediate_dispatch: true,
published_on: '2026-09-01T00:00:00Z',
state: 'paused',
},
],
},
]
describe('start workflow action form', () => {
let testApp = null
beforeEach(async () => {
testApp = new TestApp()
const workspace = { id: WORKSPACE_ID, name: 'Acme', users: [] }
await testApp.store.dispatch('workspace/forceCreate', workspace)
// `workspace/select` would fetch permissions and roles.
testApp.store.commit(
'workspace/SET_SELECTED',
testApp.store.getters['workspace/get'](WORKSPACE_ID)
)
await testApp.store.dispatch('application/forceSetAll', {
applications: applications(),
})
})
afterEach(() => testApp.afterEach())
const database = () => testApp.store.getters['application/get'](DATABASE_ID)
test('only a workflow that can be started immediately is offered', async () => {
const wrapper = await testApp.mount(DatabaseWorkflowActionWithService, {
props: {
workflowAction: { id: 1, type: 'start_workflow', service: {} },
database: database(),
defaultValues: { service: {} },
},
})
await wrapper.findComponent({ name: 'Dropdown' }).vm.show()
await wrapper.vm.$nextTick()
const names = wrapper
.findAll('.select__item-link')
.map((item) => item.text())
expect(names).toContain('Send welcome')
expect(names).not.toContain('Nightly sync')
})
test('picking a workflow reaches the action', async () => {
const wrapper = await testApp.mount(DatabaseWorkflowActionWithService, {
props: {
workflowAction: { id: 1, type: 'start_workflow', service: {} },
database: database(),
defaultValues: { service: {} },
},
})
await wrapper.findComponent({ name: 'Dropdown' }).vm.show()
await wrapper.vm.$nextTick()
await wrapper.find('.select__item-link').trigger('click')
await wrapper.vm.$nextTick()
expect(wrapper.find('.dropdown__selected').text()).toBe('Send welcome')
const emitted = wrapper.emitted('values-changed')
expect(emitted.at(-1)[0].service.workflow_id).toBe(11)
})
test('an unpublished workflow says so', () => {
const message = startWorkflowType().getErrorMessage(
{ id: 1, type: 'start_workflow', service: { workflow_id: 13 } },
{ database: database(), workspace: { id: WORKSPACE_ID } }
)
expect(message).toBe('databaseWorkflowActionType.startWorkflowUnpublished')
expect(enDatabase.databaseWorkflowActionType.startWorkflowUnpublished).toBe(
"This workflow hasn't been published yet. Publish it before the " +
'button can start it.'
)
})
test('a paused workflow says so', () => {
const message = startWorkflowType().getErrorMessage(
{ id: 1, type: 'start_workflow', service: { workflow_id: 14 } },
{ database: database(), workspace: { id: WORKSPACE_ID } }
)
expect(message).toBe('databaseWorkflowActionType.startWorkflowNotLive')
})
test('a live workflow raises no error', () => {
const message = startWorkflowType().getErrorMessage(
{ id: 1, type: 'start_workflow', service: { workflow_id: 11 } },
{ database: database(), workspace: { id: WORKSPACE_ID } }
)
expect(message).toBeNull()
})
test('an action with no workflow yet says so', () => {
const type = testApp._app.$registry.get(
'databaseWorkflowActionType',
'start_workflow'
)
const message = type.getErrorMessage(
{ id: 1, type: 'start_workflow', service: { workflow_id: null } },
{ database: database() }
)
expect(message).toBeTruthy()
})
test('a workflow that cannot start immediately says so', () => {
const type = testApp._app.$registry.get(
'databaseWorkflowActionType',
'start_workflow'
)
const message = type.getErrorMessage(
{ id: 1, type: 'start_workflow', service: { workflow_id: 12 } },
{ database: database() }
)
// `$t` returns the key here.
expect(message).toBe('serviceType.errorWorkflowNotImmediateDispatch')
expect(en.serviceType.errorWorkflowNotImmediateDispatch).toBe(
'The selected workflow must use a trigger that can start immediately.'
)
})
const startWorkflowType = () =>
testApp._app.$registry.get('databaseWorkflowActionType', 'start_workflow')
const missingWorkflowAction = {
id: 1,
type: 'start_workflow',
service: { workflow_id: 999 },
}
test('a workflow the loaded applications do not hold is called out', () => {
const message = startWorkflowType().getErrorMessage(missingWorkflowAction, {
database: database(),
workspace: { id: WORKSPACE_ID },
})
// `$t` returns the key here. The copy must not claim the workflow was
// deleted: applications are filtered by what the reader may see.
expect(message).toBe('databaseWorkflowActionType.startWorkflowMissing')
expect(enDatabase.databaseWorkflowActionType.startWorkflowMissing).toBe(
"This action's workflow can't be found. It may have been deleted, or " +
'you may not have access to it. Pick another workflow, or ask ' +
'someone who can see this one.'
)
})
test('nothing is said while the applications are still being fetched', () => {
testApp.store.commit('application/SET_LOADED', false)
const message = startWorkflowType().getErrorMessage(missingWorkflowAction, {
database: database(),
workspace: { id: WORKSPACE_ID },
})
expect(message).toBeNull()
})
})