Re-fix types in the automation store.

This commit is contained in:
Sam Rose 2025-02-18 15:48:14 +00:00
parent 0d7946ac4a
commit a0f06bad78
No known key found for this signature in database
2 changed files with 64 additions and 28 deletions

View File

@ -15,7 +15,6 @@ import {
import { import {
AutomationTriggerStepId, AutomationTriggerStepId,
AutomationEventType, AutomationEventType,
AutomationStepType,
AutomationActionStepId, AutomationActionStepId,
Automation, Automation,
AutomationStep, AutomationStep,
@ -28,6 +27,11 @@ import {
AutomationIOType, AutomationIOType,
BranchPath, BranchPath,
BlockDefinitions, BlockDefinitions,
isBranchStep,
isTrigger,
isRowUpdateTrigger,
isRowSaveTrigger,
isAppTrigger,
BranchStep, BranchStep,
} from "@budibase/types" } from "@budibase/types"
import { ActionStepID } from "@/constants/backend/automations" import { ActionStepID } from "@/constants/backend/automations"
@ -295,8 +299,8 @@ const automationActions = (store: AutomationStore) => ({
result = steps.slice(0, stepIdx + 1) result = steps.slice(0, stepIdx + 1)
return return
} }
let last = result[result.length - 1] as BranchStep let last = result[result.length - 1]
if (last && "inputs" in last) { if (isBranchStep(last)) {
if (Number.isInteger(branchIdx)) { if (Number.isInteger(branchIdx)) {
const branchId = last.inputs.branches[branchIdx].id const branchId = last.inputs.branches[branchIdx].id
const children = last.inputs.children?.[branchId] || [] const children = last.inputs.children?.[branchId] || []
@ -473,25 +477,28 @@ const automationActions = (store: AutomationStore) => ({
}, },
] ]
if (block.stepId === AutomationActionStepId.BRANCH) { if (isBranchStep(block)) {
const branches = block.inputs.branches const branches = block.inputs?.branches || []
const children = block.inputs?.children || {}
branches.forEach((branch, bIdx) => { branches.forEach((branch, bIdx) => {
block.inputs?.children?.[branch.id].forEach((bBlock, sIdx, array) => { children[branch.id].forEach(
const isBranch = bBlock.stepId === AutomationActionStepId.BRANCH (bBlock: AutomationStep, sIdx: number, array: AutomationStep[]) => {
const hasBranches = isBranch && bBlock.inputs?.branches?.length > 0 const ended = array.length - 1 === sIdx && !branches.length
const ended = array.length - 1 === sIdx && !hasBranches treeTraverse(bBlock, pathToCurrentNode, sIdx, bIdx, ended)
treeTraverse(bBlock, pathToCurrentNode, sIdx, bIdx, ended) }
}) )
}) })
store.actions.registerBlock( terminating = terminating && !branches.length
blockRefs,
block,
pathToCurrentNode,
terminating && !branches.length
)
} }
store.actions.registerBlock(
blockRefs,
block,
pathToCurrentNode,
terminating
)
} }
// Traverse the entire tree. // Traverse the entire tree.
@ -576,7 +583,6 @@ const automationActions = (store: AutomationStore) => ({
pathBlock.stepId === ActionStepID.LOOP && pathBlock.stepId === ActionStepID.LOOP &&
pathBlock.blockToLoop in blocks pathBlock.blockToLoop in blocks
} }
const isTrigger = pathBlock.type === AutomationStepType.TRIGGER
if (isLoopBlock && loopBlockCount == 0) { if (isLoopBlock && loopBlockCount == 0) {
schema = { schema = {
@ -587,17 +593,14 @@ const automationActions = (store: AutomationStore) => ({
} }
} }
const icon = isTrigger const icon = isTrigger(pathBlock)
? pathBlock.icon ? pathBlock.icon
: isLoopBlock : isLoopBlock
? "Reuse" ? "Reuse"
: pathBlock.icon : pathBlock.icon
if (blockIdx === 0 && isTrigger) { if (blockIdx === 0 && isTrigger(pathBlock)) {
if ( if (isRowUpdateTrigger(pathBlock) || isRowSaveTrigger(pathBlock)) {
pathBlock.stepId === AutomationTriggerStepId.ROW_UPDATED ||
pathBlock.stepId === AutomationTriggerStepId.ROW_SAVED
) {
let table: any = get(tables).list.find( let table: any = get(tables).list.find(
(table: Table) => table._id === pathBlock.inputs.tableId (table: Table) => table._id === pathBlock.inputs.tableId
) )
@ -609,7 +612,7 @@ const automationActions = (store: AutomationStore) => ({
} }
} }
delete schema.row delete schema.row
} else if (pathBlock.stepId === AutomationTriggerStepId.APP) { } else if (isAppTrigger(pathBlock)) {
schema = Object.fromEntries( schema = Object.fromEntries(
Object.keys(pathBlock.inputs.fields || []).map(key => [ Object.keys(pathBlock.inputs.fields || []).map(key => [
key, key,
@ -915,7 +918,11 @@ const automationActions = (store: AutomationStore) => ({
...newAutomation.definition.steps, ...newAutomation.definition.steps,
] ]
let cache: AutomationStep | AutomationTrigger | AutomationStep[] let cache:
| AutomationStep
| AutomationTrigger
| AutomationStep[]
| undefined = undefined
pathWay.forEach((path, pathIdx, array) => { pathWay.forEach((path, pathIdx, array) => {
const { stepIdx, branchIdx } = path const { stepIdx, branchIdx } = path
@ -937,11 +944,10 @@ const automationActions = (store: AutomationStore) => ({
} }
return return
} }
if ( if (
Number.isInteger(branchIdx) && Number.isInteger(branchIdx) &&
!Array.isArray(cache) && !Array.isArray(cache) &&
cache.stepId === AutomationActionStepId.BRANCH isBranchStep(cache)
) { ) {
const branchId = cache.inputs.branches[branchIdx].id const branchId = cache.inputs.branches[branchIdx].id
const children = cache.inputs.children?.[branchId] || [] const children = cache.inputs.children?.[branchId] || []

View File

@ -342,6 +342,36 @@ export type AutomationStep =
| OpenAIStep | OpenAIStep
| BranchStep | BranchStep
export function isBranchStep(
step: AutomationStep | AutomationTrigger
): step is BranchStep {
return step.stepId === AutomationActionStepId.BRANCH
}
export function isTrigger(
step: AutomationStep | AutomationTrigger
): step is AutomationTrigger {
return step.type === AutomationStepType.TRIGGER
}
export function isRowUpdateTrigger(
step: AutomationStep | AutomationTrigger
): step is RowUpdatedTrigger {
return step.stepId === AutomationTriggerStepId.ROW_UPDATED
}
export function isRowSaveTrigger(
step: AutomationStep | AutomationTrigger
): step is RowSavedTrigger {
return step.stepId === AutomationTriggerStepId.ROW_SAVED
}
export function isAppTrigger(
step: AutomationStep | AutomationTrigger
): step is AppActionTrigger {
return step.stepId === AutomationTriggerStepId.APP
}
type EmptyInputs = {} type EmptyInputs = {}
export type AutomationStepDefinition = Omit<AutomationStep, "id" | "inputs"> & { export type AutomationStepDefinition = Omit<AutomationStep, "id" | "inputs"> & {
inputs: EmptyInputs inputs: EmptyInputs