Merge pull request #14554 from Budibase/feat/automation-step-naming-updates
Update automations to default to using names for accessing context
This commit is contained in:
commit
61bce0163c
|
@ -595,9 +595,13 @@
|
||||||
let loopBlockCount = 0
|
let loopBlockCount = 0
|
||||||
const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => {
|
const addBinding = (name, value, icon, idx, isLoopBlock, bindingName) => {
|
||||||
if (!name) return
|
if (!name) return
|
||||||
const runtimeBinding = determineRuntimeBinding(name, idx, isLoopBlock)
|
const runtimeBinding = determineRuntimeBinding(
|
||||||
|
name,
|
||||||
|
idx,
|
||||||
|
isLoopBlock,
|
||||||
|
bindingName
|
||||||
|
)
|
||||||
const categoryName = determineCategoryName(idx, isLoopBlock, bindingName)
|
const categoryName = determineCategoryName(idx, isLoopBlock, bindingName)
|
||||||
|
|
||||||
bindings.push(
|
bindings.push(
|
||||||
createBindingObject(
|
createBindingObject(
|
||||||
name,
|
name,
|
||||||
|
@ -613,7 +617,7 @@
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const determineRuntimeBinding = (name, idx, isLoopBlock) => {
|
const determineRuntimeBinding = (name, idx, isLoopBlock, bindingName) => {
|
||||||
let runtimeName
|
let runtimeName
|
||||||
|
|
||||||
/* Begin special cases for generating custom schemas based on triggers */
|
/* Begin special cases for generating custom schemas based on triggers */
|
||||||
|
@ -634,12 +638,17 @@
|
||||||
}
|
}
|
||||||
/* End special cases for generating custom schemas based on triggers */
|
/* End special cases for generating custom schemas based on triggers */
|
||||||
|
|
||||||
|
let hasUserDefinedName = automation.stepNames?.[allSteps[idx]?.id]
|
||||||
if (isLoopBlock) {
|
if (isLoopBlock) {
|
||||||
runtimeName = `loop.${name}`
|
runtimeName = `loop.${name}`
|
||||||
} else if (block.name.startsWith("JS")) {
|
} else if (block.name.startsWith("JS")) {
|
||||||
runtimeName = `steps[${idx - loopBlockCount}].${name}`
|
runtimeName = hasUserDefinedName
|
||||||
|
? `stepsByName[${bindingName}].${name}`
|
||||||
|
: `steps[${idx - loopBlockCount}].${name}`
|
||||||
} else {
|
} else {
|
||||||
runtimeName = `steps.${idx - loopBlockCount}.${name}`
|
runtimeName = hasUserDefinedName
|
||||||
|
? `stepsByName.${bindingName}.${name}`
|
||||||
|
: `steps.${idx - loopBlockCount}.${name}`
|
||||||
}
|
}
|
||||||
return idx === 0 ? `trigger.${name}` : runtimeName
|
return idx === 0 ? `trigger.${name}` : runtimeName
|
||||||
}
|
}
|
||||||
|
@ -666,11 +675,11 @@
|
||||||
const field = Object.values(FIELDS).find(
|
const field = Object.values(FIELDS).find(
|
||||||
field => field.type === value.type && field.subtype === value.subtype
|
field => field.type === value.type && field.subtype === value.subtype
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
readableBinding: bindingName
|
readableBinding:
|
||||||
? `${bindingName}.${name}`
|
bindingName && !isLoopBlock
|
||||||
: runtimeBinding,
|
? `steps.${bindingName}.${name}`
|
||||||
|
: runtimeBinding,
|
||||||
runtimeBinding,
|
runtimeBinding,
|
||||||
type: value.type,
|
type: value.type,
|
||||||
description: value.description,
|
description: value.description,
|
||||||
|
@ -690,8 +699,12 @@
|
||||||
allSteps[idx]?.stepId === ActionStepID.LOOP &&
|
allSteps[idx]?.stepId === ActionStepID.LOOP &&
|
||||||
allSteps.some(x => x.blockToLoop === block.id)
|
allSteps.some(x => x.blockToLoop === block.id)
|
||||||
let schema = cloneDeep(allSteps[idx]?.schema?.outputs?.properties) ?? {}
|
let schema = cloneDeep(allSteps[idx]?.schema?.outputs?.properties) ?? {}
|
||||||
|
if (allSteps[idx]?.name.includes("Looping")) {
|
||||||
|
isLoopBlock = true
|
||||||
|
loopBlockCount++
|
||||||
|
}
|
||||||
let bindingName =
|
let bindingName =
|
||||||
automation.stepNames?.[allSteps[idx - loopBlockCount].id]
|
automation.stepNames?.[allSteps[idx].id] || allSteps[idx].name
|
||||||
|
|
||||||
if (isLoopBlock) {
|
if (isLoopBlock) {
|
||||||
schema = {
|
schema = {
|
||||||
|
@ -740,13 +753,12 @@
|
||||||
|
|
||||||
if (wasLoopBlock) {
|
if (wasLoopBlock) {
|
||||||
loopBlockCount++
|
loopBlockCount++
|
||||||
continue
|
schema = cloneDeep(allSteps[idx - 1]?.schema?.outputs?.properties)
|
||||||
}
|
}
|
||||||
Object.entries(schema).forEach(([name, value]) =>
|
Object.entries(schema).forEach(([name, value]) => {
|
||||||
addBinding(name, value, icon, idx, isLoopBlock, bindingName)
|
addBinding(name, value, icon, idx, isLoopBlock, bindingName)
|
||||||
)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return bindings
|
return bindings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
import { AutomationActionStepId } from "@budibase/types"
|
||||||
|
|
||||||
|
export const updateBindingsInInputs = (inputs, oldName, newName, stepIndex) => {
|
||||||
|
if (typeof inputs === "string") {
|
||||||
|
return inputs
|
||||||
|
.replace(
|
||||||
|
new RegExp(`stepsByName\\.${oldName}\\.`, "g"),
|
||||||
|
`stepsByName.${newName}.`
|
||||||
|
)
|
||||||
|
.replace(
|
||||||
|
new RegExp(`steps\\.${stepIndex}\\.`, "g"),
|
||||||
|
`stepsByName.${newName}.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(inputs)) {
|
||||||
|
return inputs.map(item =>
|
||||||
|
updateBindingsInInputs(item, oldName, newName, stepIndex)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof inputs === "object" && inputs !== null) {
|
||||||
|
const updatedInputs = {}
|
||||||
|
for (const [key, value] of Object.entries(inputs)) {
|
||||||
|
const updatedKey = updateBindingsInInputs(
|
||||||
|
key,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
stepIndex
|
||||||
|
)
|
||||||
|
updatedInputs[updatedKey] = updateBindingsInInputs(
|
||||||
|
value,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
stepIndex
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return updatedInputs
|
||||||
|
}
|
||||||
|
return inputs
|
||||||
|
}
|
||||||
|
|
||||||
|
export const updateBindingsInSteps = (
|
||||||
|
steps,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
changedStepIndex
|
||||||
|
) => {
|
||||||
|
return steps.map(step => {
|
||||||
|
const updatedStep = {
|
||||||
|
...step,
|
||||||
|
inputs: updateBindingsInInputs(
|
||||||
|
step.inputs,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
changedStepIndex
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("branches" in updatedStep.inputs) {
|
||||||
|
updatedStep.inputs.branches = updatedStep.inputs.branches.map(branch => ({
|
||||||
|
...branch,
|
||||||
|
condition: updateBindingsInInputs(
|
||||||
|
branch.condition,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
changedStepIndex
|
||||||
|
),
|
||||||
|
}))
|
||||||
|
|
||||||
|
if (updatedStep.inputs.children) {
|
||||||
|
for (const [key, childSteps] of Object.entries(
|
||||||
|
updatedStep.inputs.children
|
||||||
|
)) {
|
||||||
|
updatedStep.inputs.children[key] = updateBindingsInSteps(
|
||||||
|
childSteps,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
changedStepIndex
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedStep
|
||||||
|
})
|
||||||
|
}
|
||||||
|
export const getNewStepName = (automation, step) => {
|
||||||
|
const baseName = step.name
|
||||||
|
|
||||||
|
const countExistingSteps = steps => {
|
||||||
|
return steps.reduce((count, currentStep) => {
|
||||||
|
if (currentStep.name && currentStep.name.startsWith(baseName)) {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
currentStep.stepId === AutomationActionStepId.BRANCH &&
|
||||||
|
currentStep.inputs &&
|
||||||
|
currentStep.inputs.children
|
||||||
|
) {
|
||||||
|
Object.values(currentStep.inputs.children).forEach(branchSteps => {
|
||||||
|
count += countExistingSteps(branchSteps)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
let existingCount = 0
|
||||||
|
if (automation?.definition) {
|
||||||
|
existingCount = countExistingSteps(automation.definition.steps)
|
||||||
|
}
|
||||||
|
if (existingCount === 0) {
|
||||||
|
return baseName
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${baseName} ${existingCount + 1}`
|
||||||
|
}
|
|
@ -0,0 +1,177 @@
|
||||||
|
import { cloneDeep } from "lodash"
|
||||||
|
import {
|
||||||
|
updateBindingsInInputs,
|
||||||
|
updateBindingsInSteps,
|
||||||
|
} from "../automations/nameHelpers"
|
||||||
|
describe("Automation Binding Update Functions", () => {
|
||||||
|
const sampleAutomation = {
|
||||||
|
definition: {
|
||||||
|
steps: [
|
||||||
|
{
|
||||||
|
name: "First Step",
|
||||||
|
inputs: {
|
||||||
|
text: "Starting automation",
|
||||||
|
},
|
||||||
|
id: "step1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Second Step",
|
||||||
|
inputs: {
|
||||||
|
text: "{{ steps.0.success }} and {{ stepsByName.First Step.message }}",
|
||||||
|
},
|
||||||
|
id: "step2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Branch",
|
||||||
|
inputs: {
|
||||||
|
branches: [
|
||||||
|
{
|
||||||
|
name: "branch1",
|
||||||
|
condition: {
|
||||||
|
equal: {
|
||||||
|
"steps.1.success": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
children: {
|
||||||
|
branch1: [
|
||||||
|
{
|
||||||
|
name: "Nested Step",
|
||||||
|
inputs: {
|
||||||
|
text: "{{ stepsByName.Second Step.message }} and {{ steps.1.success }}",
|
||||||
|
},
|
||||||
|
id: "nestedStep",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
id: "branchStep",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
stepNames: {
|
||||||
|
step1: "First Step",
|
||||||
|
step2: "Second Step",
|
||||||
|
branchStep: "Branch",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
it("updateBindingsInInputs updates string bindings correctly", () => {
|
||||||
|
const input = "{{ stepsByName.oldName.success }} and {{ steps.1.message }}"
|
||||||
|
const result = updateBindingsInInputs(input, "oldName", "newName", 1)
|
||||||
|
expect(result).toBe(
|
||||||
|
"{{ stepsByName.newName.success }} and {{ stepsByName.newName.message }}"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updateBindingsInInputs handles nested objects", () => {
|
||||||
|
const input = {
|
||||||
|
text: "{{ stepsByName.oldName.success }}",
|
||||||
|
nested: {
|
||||||
|
value: "{{ steps.1.message }}",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const result = updateBindingsInInputs(input, "oldName", "newName", 1)
|
||||||
|
expect(result).toEqual({
|
||||||
|
text: "{{ stepsByName.newName.success }}",
|
||||||
|
nested: {
|
||||||
|
value: "{{ stepsByName.newName.message }}",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updateBindingsInSteps updates bindings in all steps", () => {
|
||||||
|
const steps = cloneDeep(sampleAutomation.definition.steps)
|
||||||
|
const result = updateBindingsInSteps(
|
||||||
|
steps,
|
||||||
|
"Second Step",
|
||||||
|
"Renamed Step",
|
||||||
|
1
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result[1].name).toBe("Second Step")
|
||||||
|
|
||||||
|
expect(result[2].inputs.branches[0].condition.equal).toEqual({
|
||||||
|
"stepsByName.Renamed Step.success": true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const nestedStepText = result[2].inputs.children.branch1[0].inputs.text
|
||||||
|
expect(nestedStepText).toBe(
|
||||||
|
"{{ stepsByName.Renamed Step.message }} and {{ stepsByName.Renamed Step.success }}"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updateBindingsInSteps handles steps with no bindings", () => {
|
||||||
|
const steps = [
|
||||||
|
{
|
||||||
|
name: "No Binding Step",
|
||||||
|
inputs: {
|
||||||
|
text: "Plain text",
|
||||||
|
},
|
||||||
|
id: "noBindingStep",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
const result = updateBindingsInSteps(steps, "Old Name", "New Name", 0)
|
||||||
|
expect(result).toEqual(steps)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updateBindingsInSteps updates bindings in deeply nested branches", () => {
|
||||||
|
const deeplyNestedStep = {
|
||||||
|
name: "Deep Branch",
|
||||||
|
inputs: {
|
||||||
|
branches: [
|
||||||
|
{
|
||||||
|
name: "deepBranch",
|
||||||
|
condition: {
|
||||||
|
equal: {
|
||||||
|
"stepsByName.Second Step.success": true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
children: {
|
||||||
|
deepBranch: [
|
||||||
|
{
|
||||||
|
name: "Deep Log",
|
||||||
|
inputs: {
|
||||||
|
text: "{{ steps.1.message }}",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const steps = [...sampleAutomation.definition.steps, deeplyNestedStep]
|
||||||
|
const result = updateBindingsInSteps(
|
||||||
|
steps,
|
||||||
|
"Second Step",
|
||||||
|
"Renamed Step",
|
||||||
|
1
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
result[3].inputs.branches[0].condition.equal[
|
||||||
|
"stepsByName.Renamed Step.success"
|
||||||
|
]
|
||||||
|
).toBe(true)
|
||||||
|
expect(result[3].inputs.children.deepBranch[0].inputs.text).toBe(
|
||||||
|
"{{ stepsByName.Renamed Step.message }}"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("updateBindingsInSteps does not affect unrelated bindings", () => {
|
||||||
|
const steps = cloneDeep(sampleAutomation.definition.steps)
|
||||||
|
const result = updateBindingsInSteps(
|
||||||
|
steps,
|
||||||
|
"Second Step",
|
||||||
|
"Renamed Step",
|
||||||
|
1
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result[1].inputs.text).toBe(
|
||||||
|
"{{ steps.0.success }} and {{ stepsByName.First Step.message }}"
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
|
@ -6,6 +6,10 @@ import { createHistoryStore } from "stores/builder/history"
|
||||||
import { notifications } from "@budibase/bbui"
|
import { notifications } from "@budibase/bbui"
|
||||||
import { updateReferencesInObject } from "dataBinding"
|
import { updateReferencesInObject } from "dataBinding"
|
||||||
import { AutomationTriggerStepId } from "@budibase/types"
|
import { AutomationTriggerStepId } from "@budibase/types"
|
||||||
|
import {
|
||||||
|
updateBindingsInSteps,
|
||||||
|
getNewStepName,
|
||||||
|
} from "helpers/automations/nameHelpers"
|
||||||
|
|
||||||
const initialAutomationState = {
|
const initialAutomationState = {
|
||||||
automations: [],
|
automations: [],
|
||||||
|
@ -275,13 +279,17 @@ const automationActions = store => ({
|
||||||
await store.actions.save(newAutomation)
|
await store.actions.save(newAutomation)
|
||||||
},
|
},
|
||||||
constructBlock(type, stepId, blockDefinition) {
|
constructBlock(type, stepId, blockDefinition) {
|
||||||
return {
|
let newName
|
||||||
|
const newStep = {
|
||||||
...blockDefinition,
|
...blockDefinition,
|
||||||
inputs: blockDefinition.inputs || {},
|
inputs: blockDefinition.inputs || {},
|
||||||
stepId,
|
stepId,
|
||||||
type,
|
type,
|
||||||
id: generate(),
|
id: generate(),
|
||||||
}
|
}
|
||||||
|
newName = getNewStepName(get(selectedAutomation), newStep)
|
||||||
|
newStep.name = newName
|
||||||
|
return newStep
|
||||||
},
|
},
|
||||||
addBlockToAutomation: async (block, blockIdx) => {
|
addBlockToAutomation: async (block, blockIdx) => {
|
||||||
const automation = get(selectedAutomation)
|
const automation = get(selectedAutomation)
|
||||||
|
@ -301,15 +309,34 @@ const automationActions = store => ({
|
||||||
saveAutomationName: async (blockId, name) => {
|
saveAutomationName: async (blockId, name) => {
|
||||||
const automation = get(selectedAutomation)
|
const automation = get(selectedAutomation)
|
||||||
let newAutomation = cloneDeep(automation)
|
let newAutomation = cloneDeep(automation)
|
||||||
if (!automation) {
|
if (!newAutomation) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newAutomation.definition.stepNames = {
|
|
||||||
...newAutomation.definition.stepNames,
|
|
||||||
[blockId]: name.trim(),
|
|
||||||
}
|
|
||||||
|
|
||||||
await store.actions.save(newAutomation)
|
const stepIndex = newAutomation.definition.steps.findIndex(
|
||||||
|
step => step.id === blockId
|
||||||
|
)
|
||||||
|
|
||||||
|
if (stepIndex !== -1) {
|
||||||
|
const oldName = newAutomation.definition.steps[stepIndex].name
|
||||||
|
const newName = name.trim()
|
||||||
|
|
||||||
|
newAutomation.definition.stepNames = {
|
||||||
|
...newAutomation.definition.stepNames,
|
||||||
|
[blockId]: newName,
|
||||||
|
}
|
||||||
|
|
||||||
|
newAutomation.definition.steps[stepIndex].name = newName
|
||||||
|
|
||||||
|
newAutomation.definition.steps = updateBindingsInSteps(
|
||||||
|
newAutomation.definition.steps,
|
||||||
|
oldName,
|
||||||
|
newName,
|
||||||
|
stepIndex
|
||||||
|
)
|
||||||
|
|
||||||
|
await store.actions.save(newAutomation)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
deleteAutomationName: async blockId => {
|
deleteAutomationName: async blockId => {
|
||||||
const automation = get(selectedAutomation)
|
const automation = get(selectedAutomation)
|
||||||
|
|
|
@ -242,4 +242,31 @@ describe("Loop automations", () => {
|
||||||
expect(results.steps[1].outputs.message).toContain("- 3")
|
expect(results.steps[1].outputs.message).toContain("- 3")
|
||||||
expect(results.steps[3].outputs.message).toContain("- 3")
|
expect(results.steps[3].outputs.message).toContain("- 3")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should use automation names to loop with", async () => {
|
||||||
|
const builder = createAutomationBuilder({
|
||||||
|
name: "Test Trigger with Loop and Create Row",
|
||||||
|
})
|
||||||
|
|
||||||
|
const results = await builder
|
||||||
|
.appAction({ fields: {} })
|
||||||
|
.loop(
|
||||||
|
{
|
||||||
|
option: LoopStepType.ARRAY,
|
||||||
|
binding: [1, 2, 3],
|
||||||
|
},
|
||||||
|
{ stepName: "FirstLoopStep" }
|
||||||
|
)
|
||||||
|
.serverLog(
|
||||||
|
{ text: "Message {{loop.currentItem}}" },
|
||||||
|
{ stepName: "FirstLoopLog" }
|
||||||
|
)
|
||||||
|
.serverLog(
|
||||||
|
{ text: "{{steps.FirstLoopLog.iterations}}" },
|
||||||
|
{ stepName: "FirstLoopIterationLog" }
|
||||||
|
)
|
||||||
|
.run()
|
||||||
|
|
||||||
|
expect(results.steps[1].outputs.message).toContain("- 3")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -49,151 +49,188 @@ describe("Automation Scenarios", () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
it("should trigger an automation which querys the database", async () => {
|
it("should trigger an automation which querys the database", async () => {
|
||||||
const table = await config.createTable()
|
const table = await config.createTable()
|
||||||
const row = {
|
const row = {
|
||||||
name: "Test Row",
|
name: "Test Row",
|
||||||
description: "original description",
|
description: "original description",
|
||||||
tableId: table._id,
|
tableId: table._id,
|
||||||
}
|
}
|
||||||
await config.createRow(row)
|
await config.createRow(row)
|
||||||
await config.createRow(row)
|
await config.createRow(row)
|
||||||
const builder = createAutomationBuilder({
|
const builder = createAutomationBuilder({
|
||||||
name: "Test Row Save and Create",
|
name: "Test Row Save and Create",
|
||||||
|
})
|
||||||
|
|
||||||
|
const results = await builder
|
||||||
|
.appAction({ fields: {} })
|
||||||
|
.queryRows({
|
||||||
|
tableId: table._id!,
|
||||||
|
})
|
||||||
|
.run()
|
||||||
|
|
||||||
|
expect(results.steps).toHaveLength(1)
|
||||||
|
expect(results.steps[0].outputs.rows).toHaveLength(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
const results = await builder
|
it("should trigger an automation which querys the database then deletes a row", async () => {
|
||||||
.appAction({ fields: {} })
|
const table = await config.createTable()
|
||||||
.queryRows({
|
const row = {
|
||||||
tableId: table._id!,
|
name: "DFN",
|
||||||
|
description: "original description",
|
||||||
|
tableId: table._id,
|
||||||
|
}
|
||||||
|
await config.createRow(row)
|
||||||
|
await config.createRow(row)
|
||||||
|
const builder = createAutomationBuilder({
|
||||||
|
name: "Test Row Save and Create",
|
||||||
})
|
})
|
||||||
.run()
|
|
||||||
|
|
||||||
expect(results.steps).toHaveLength(1)
|
const results = await builder
|
||||||
expect(results.steps[0].outputs.rows).toHaveLength(2)
|
.appAction({ fields: {} })
|
||||||
})
|
.queryRows({
|
||||||
|
tableId: table._id!,
|
||||||
|
})
|
||||||
|
.deleteRow({
|
||||||
|
tableId: table._id!,
|
||||||
|
id: "{{ steps.1.rows.0._id }}",
|
||||||
|
})
|
||||||
|
.queryRows({
|
||||||
|
tableId: table._id!,
|
||||||
|
})
|
||||||
|
.run()
|
||||||
|
|
||||||
it("should trigger an automation which querys the database then deletes a row", async () => {
|
expect(results.steps).toHaveLength(3)
|
||||||
const table = await config.createTable()
|
expect(results.steps[1].outputs.success).toBeTruthy()
|
||||||
const row = {
|
expect(results.steps[2].outputs.rows).toHaveLength(1)
|
||||||
name: "DFN",
|
|
||||||
description: "original description",
|
|
||||||
tableId: table._id,
|
|
||||||
}
|
|
||||||
await config.createRow(row)
|
|
||||||
await config.createRow(row)
|
|
||||||
const builder = createAutomationBuilder({
|
|
||||||
name: "Test Row Save and Create",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const results = await builder
|
it("should query an external database for some data then insert than into an internal table", async () => {
|
||||||
.appAction({ fields: {} })
|
const { datasource, client } = await setup.setupTestDatasource(
|
||||||
.queryRows({
|
config,
|
||||||
tableId: table._id!,
|
DatabaseName.MYSQL
|
||||||
})
|
|
||||||
.deleteRow({
|
|
||||||
tableId: table._id!,
|
|
||||||
id: "{{ steps.1.rows.0._id }}",
|
|
||||||
})
|
|
||||||
.queryRows({
|
|
||||||
tableId: table._id!,
|
|
||||||
})
|
|
||||||
.run()
|
|
||||||
|
|
||||||
expect(results.steps).toHaveLength(3)
|
|
||||||
expect(results.steps[1].outputs.success).toBeTruthy()
|
|
||||||
expect(results.steps[2].outputs.rows).toHaveLength(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should query an external database for some data then insert than into an internal table", async () => {
|
|
||||||
const { datasource, client } = await setup.setupTestDatasource(
|
|
||||||
config,
|
|
||||||
DatabaseName.MYSQL
|
|
||||||
)
|
|
||||||
|
|
||||||
const newTable = await config.createTable({
|
|
||||||
name: "table",
|
|
||||||
type: "table",
|
|
||||||
schema: {
|
|
||||||
name: {
|
|
||||||
name: "name",
|
|
||||||
type: FieldType.STRING,
|
|
||||||
constraints: {
|
|
||||||
presence: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
age: {
|
|
||||||
name: "age",
|
|
||||||
type: FieldType.NUMBER,
|
|
||||||
constraints: {
|
|
||||||
presence: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const tableName = await setup.createTestTable(client, {
|
|
||||||
name: { type: "string" },
|
|
||||||
age: { type: "number" },
|
|
||||||
})
|
|
||||||
|
|
||||||
const rows = [
|
|
||||||
{ name: "Joe", age: 20 },
|
|
||||||
{ name: "Bob", age: 25 },
|
|
||||||
{ name: "Paul", age: 30 },
|
|
||||||
]
|
|
||||||
|
|
||||||
await setup.insertTestData(client, tableName, rows)
|
|
||||||
|
|
||||||
const query = await setup.saveTestQuery(
|
|
||||||
config,
|
|
||||||
client,
|
|
||||||
tableName,
|
|
||||||
datasource
|
|
||||||
)
|
|
||||||
|
|
||||||
const builder = createAutomationBuilder({
|
|
||||||
name: "Test external query and save",
|
|
||||||
})
|
|
||||||
|
|
||||||
const results = await builder
|
|
||||||
.appAction({
|
|
||||||
fields: {},
|
|
||||||
})
|
|
||||||
.executeQuery({
|
|
||||||
query: {
|
|
||||||
queryId: query._id!,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.loop({
|
|
||||||
option: LoopStepType.ARRAY,
|
|
||||||
binding: "{{ steps.1.response }}",
|
|
||||||
})
|
|
||||||
.createRow({
|
|
||||||
row: {
|
|
||||||
name: "{{ loop.currentItem.name }}",
|
|
||||||
age: "{{ loop.currentItem.age }}",
|
|
||||||
tableId: newTable._id!,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.queryRows({
|
|
||||||
tableId: newTable._id!,
|
|
||||||
})
|
|
||||||
.run()
|
|
||||||
|
|
||||||
expect(results.steps).toHaveLength(3)
|
|
||||||
|
|
||||||
expect(results.steps[1].outputs.iterations).toBe(3)
|
|
||||||
expect(results.steps[1].outputs.items).toHaveLength(3)
|
|
||||||
|
|
||||||
expect(results.steps[2].outputs.rows).toHaveLength(3)
|
|
||||||
|
|
||||||
rows.forEach(expectedRow => {
|
|
||||||
expect(results.steps[2].outputs.rows).toEqual(
|
|
||||||
expect.arrayContaining([expect.objectContaining(expectedRow)])
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const newTable = await config.createTable({
|
||||||
|
name: "table",
|
||||||
|
type: "table",
|
||||||
|
schema: {
|
||||||
|
name: {
|
||||||
|
name: "name",
|
||||||
|
type: FieldType.STRING,
|
||||||
|
constraints: {
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
age: {
|
||||||
|
name: "age",
|
||||||
|
type: FieldType.NUMBER,
|
||||||
|
constraints: {
|
||||||
|
presence: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const tableName = await setup.createTestTable(client, {
|
||||||
|
name: { type: "string" },
|
||||||
|
age: { type: "number" },
|
||||||
|
})
|
||||||
|
|
||||||
|
const rows = [
|
||||||
|
{ name: "Joe", age: 20 },
|
||||||
|
{ name: "Bob", age: 25 },
|
||||||
|
{ name: "Paul", age: 30 },
|
||||||
|
]
|
||||||
|
|
||||||
|
await setup.insertTestData(client, tableName, rows)
|
||||||
|
|
||||||
|
const query = await setup.saveTestQuery(
|
||||||
|
config,
|
||||||
|
client,
|
||||||
|
tableName,
|
||||||
|
datasource
|
||||||
|
)
|
||||||
|
|
||||||
|
const builder = createAutomationBuilder({
|
||||||
|
name: "Test external query and save",
|
||||||
|
})
|
||||||
|
|
||||||
|
const results = await builder
|
||||||
|
.appAction({
|
||||||
|
fields: {},
|
||||||
|
})
|
||||||
|
.executeQuery({
|
||||||
|
query: {
|
||||||
|
queryId: query._id!,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.loop({
|
||||||
|
option: LoopStepType.ARRAY,
|
||||||
|
binding: "{{ steps.1.response }}",
|
||||||
|
})
|
||||||
|
.createRow({
|
||||||
|
row: {
|
||||||
|
name: "{{ loop.currentItem.name }}",
|
||||||
|
age: "{{ loop.currentItem.age }}",
|
||||||
|
tableId: newTable._id!,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.queryRows({
|
||||||
|
tableId: newTable._id!,
|
||||||
|
})
|
||||||
|
.run()
|
||||||
|
|
||||||
|
expect(results.steps).toHaveLength(3)
|
||||||
|
|
||||||
|
expect(results.steps[1].outputs.iterations).toBe(3)
|
||||||
|
expect(results.steps[1].outputs.items).toHaveLength(3)
|
||||||
|
|
||||||
|
expect(results.steps[2].outputs.rows).toHaveLength(3)
|
||||||
|
|
||||||
|
rows.forEach(expectedRow => {
|
||||||
|
expect(results.steps[2].outputs.rows).toEqual(
|
||||||
|
expect.arrayContaining([expect.objectContaining(expectedRow)])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("Name Based Automations", () => {
|
||||||
|
it("should fetch and delete a rpw using automation naming", async () => {
|
||||||
|
const table = await config.createTable()
|
||||||
|
const row = {
|
||||||
|
name: "DFN",
|
||||||
|
description: "original description",
|
||||||
|
tableId: table._id,
|
||||||
|
}
|
||||||
|
await config.createRow(row)
|
||||||
|
await config.createRow(row)
|
||||||
|
const builder = createAutomationBuilder({
|
||||||
|
name: "Test Query and Delete Row",
|
||||||
|
})
|
||||||
|
|
||||||
|
const results = await builder
|
||||||
|
.appAction({ fields: {} })
|
||||||
|
.queryRows(
|
||||||
|
{
|
||||||
|
tableId: table._id!,
|
||||||
|
},
|
||||||
|
{ stepName: "InitialQueryStep" }
|
||||||
|
)
|
||||||
|
.deleteRow({
|
||||||
|
tableId: table._id!,
|
||||||
|
id: "{{ steps.InitialQueryStep.rows.0._id }}",
|
||||||
|
})
|
||||||
|
.queryRows({
|
||||||
|
tableId: table._id!,
|
||||||
|
})
|
||||||
|
.run()
|
||||||
|
|
||||||
|
expect(results.steps).toHaveLength(3)
|
||||||
|
expect(results.steps[1].outputs.success).toBeTruthy()
|
||||||
|
expect(results.steps[2].outputs.rows).toHaveLength(1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -57,21 +57,27 @@ type BranchConfig = {
|
||||||
|
|
||||||
class BaseStepBuilder {
|
class BaseStepBuilder {
|
||||||
protected steps: AutomationStep[] = []
|
protected steps: AutomationStep[] = []
|
||||||
|
protected stepNames: { [key: string]: string } = {}
|
||||||
|
|
||||||
protected step<TStep extends AutomationActionStepId>(
|
protected step<TStep extends AutomationActionStepId>(
|
||||||
stepId: TStep,
|
stepId: TStep,
|
||||||
stepSchema: Omit<AutomationStep, "id" | "stepId" | "inputs">,
|
stepSchema: Omit<AutomationStep, "id" | "stepId" | "inputs">,
|
||||||
inputs: AutomationStepInputs<TStep>
|
inputs: AutomationStepInputs<TStep>,
|
||||||
|
stepName?: string
|
||||||
): this {
|
): this {
|
||||||
|
const id = uuidv4()
|
||||||
this.steps.push({
|
this.steps.push({
|
||||||
...stepSchema,
|
...stepSchema,
|
||||||
inputs: inputs as any,
|
inputs: inputs as any,
|
||||||
id: uuidv4(),
|
id,
|
||||||
stepId,
|
stepId,
|
||||||
|
name: stepName || stepSchema.name,
|
||||||
})
|
})
|
||||||
|
if (stepName) {
|
||||||
|
this.stepNames[id] = stepName
|
||||||
|
}
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
protected addBranchStep(branchConfig: BranchConfig): void {
|
protected addBranchStep(branchConfig: BranchConfig): void {
|
||||||
const branchStepInputs: BranchStepInputs = {
|
const branchStepInputs: BranchStepInputs = {
|
||||||
branches: [] as Branch[],
|
branches: [] as Branch[],
|
||||||
|
@ -99,66 +105,80 @@ class BaseStepBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
// STEPS
|
// STEPS
|
||||||
createRow(inputs: CreateRowStepInputs): this {
|
createRow(inputs: CreateRowStepInputs, opts?: { stepName?: string }): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.CREATE_ROW,
|
AutomationActionStepId.CREATE_ROW,
|
||||||
BUILTIN_ACTION_DEFINITIONS.CREATE_ROW,
|
BUILTIN_ACTION_DEFINITIONS.CREATE_ROW,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRow(inputs: UpdateRowStepInputs): this {
|
updateRow(inputs: UpdateRowStepInputs, opts?: { stepName?: string }): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.UPDATE_ROW,
|
AutomationActionStepId.UPDATE_ROW,
|
||||||
BUILTIN_ACTION_DEFINITIONS.UPDATE_ROW,
|
BUILTIN_ACTION_DEFINITIONS.UPDATE_ROW,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteRow(inputs: DeleteRowStepInputs): this {
|
deleteRow(inputs: DeleteRowStepInputs, opts?: { stepName?: string }): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.DELETE_ROW,
|
AutomationActionStepId.DELETE_ROW,
|
||||||
BUILTIN_ACTION_DEFINITIONS.DELETE_ROW,
|
BUILTIN_ACTION_DEFINITIONS.DELETE_ROW,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendSmtpEmail(inputs: SmtpEmailStepInputs): this {
|
sendSmtpEmail(
|
||||||
|
inputs: SmtpEmailStepInputs,
|
||||||
|
opts?: { stepName?: string }
|
||||||
|
): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.SEND_EMAIL_SMTP,
|
AutomationActionStepId.SEND_EMAIL_SMTP,
|
||||||
BUILTIN_ACTION_DEFINITIONS.SEND_EMAIL_SMTP,
|
BUILTIN_ACTION_DEFINITIONS.SEND_EMAIL_SMTP,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
executeQuery(inputs: ExecuteQueryStepInputs): this {
|
executeQuery(
|
||||||
|
inputs: ExecuteQueryStepInputs,
|
||||||
|
opts?: { stepName?: string }
|
||||||
|
): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.EXECUTE_QUERY,
|
AutomationActionStepId.EXECUTE_QUERY,
|
||||||
BUILTIN_ACTION_DEFINITIONS.EXECUTE_QUERY,
|
BUILTIN_ACTION_DEFINITIONS.EXECUTE_QUERY,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
queryRows(inputs: QueryRowsStepInputs): this {
|
queryRows(inputs: QueryRowsStepInputs, opts?: { stepName?: string }): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.QUERY_ROWS,
|
AutomationActionStepId.QUERY_ROWS,
|
||||||
BUILTIN_ACTION_DEFINITIONS.QUERY_ROWS,
|
BUILTIN_ACTION_DEFINITIONS.QUERY_ROWS,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
loop(inputs: LoopStepInputs): this {
|
loop(inputs: LoopStepInputs, opts?: { stepName?: string }): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.LOOP,
|
AutomationActionStepId.LOOP,
|
||||||
BUILTIN_ACTION_DEFINITIONS.LOOP,
|
BUILTIN_ACTION_DEFINITIONS.LOOP,
|
||||||
inputs
|
inputs,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
serverLog(input: ServerLogStepInputs): this {
|
serverLog(input: ServerLogStepInputs, opts?: { stepName?: string }): this {
|
||||||
return this.step(
|
return this.step(
|
||||||
AutomationActionStepId.SERVER_LOG,
|
AutomationActionStepId.SERVER_LOG,
|
||||||
BUILTIN_ACTION_DEFINITIONS.SERVER_LOG,
|
BUILTIN_ACTION_DEFINITIONS.SERVER_LOG,
|
||||||
input
|
input,
|
||||||
|
opts?.stepName
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,6 +206,7 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
definition: {
|
definition: {
|
||||||
steps: [],
|
steps: [],
|
||||||
trigger: {} as AutomationTrigger,
|
trigger: {} as AutomationTrigger,
|
||||||
|
stepNames: {},
|
||||||
},
|
},
|
||||||
type: "automation",
|
type: "automation",
|
||||||
appId: options.appId ?? setup.getConfig().getAppId(),
|
appId: options.appId ?? setup.getConfig().getAppId(),
|
||||||
|
@ -268,6 +289,7 @@ class AutomationBuilder extends BaseStepBuilder {
|
||||||
|
|
||||||
build(): Automation {
|
build(): Automation {
|
||||||
this.automationConfig.definition.steps = this.steps
|
this.automationConfig.definition.steps = this.steps
|
||||||
|
this.automationConfig.definition.stepNames = this.stepNames
|
||||||
return this.automationConfig
|
return this.automationConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ export interface TriggerOutput {
|
||||||
|
|
||||||
export interface AutomationContext extends AutomationResults {
|
export interface AutomationContext extends AutomationResults {
|
||||||
steps: any[]
|
steps: any[]
|
||||||
|
stepsByName?: Record<string, any>
|
||||||
env?: Record<string, string>
|
env?: Record<string, string>
|
||||||
trigger: any
|
trigger: any
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,7 +89,12 @@ class Orchestrator {
|
||||||
delete triggerOutput.appId
|
delete triggerOutput.appId
|
||||||
delete triggerOutput.metadata
|
delete triggerOutput.metadata
|
||||||
// step zero is never used as the template string is zero indexed for customer facing
|
// step zero is never used as the template string is zero indexed for customer facing
|
||||||
this.context = { steps: [{}], trigger: triggerOutput }
|
this.context = {
|
||||||
|
steps: [{}],
|
||||||
|
stepsByName: {},
|
||||||
|
trigger: triggerOutput,
|
||||||
|
}
|
||||||
|
|
||||||
this.automation = automation
|
this.automation = automation
|
||||||
// create an emitter which has the chain count for this automation run in it, so it can block
|
// create an emitter which has the chain count for this automation run in it, so it can block
|
||||||
// excessive chaining if required
|
// excessive chaining if required
|
||||||
|
@ -451,6 +456,9 @@ class Orchestrator {
|
||||||
outputs: tempOutput,
|
outputs: tempOutput,
|
||||||
inputs: steps[stepToLoopIndex].inputs,
|
inputs: steps[stepToLoopIndex].inputs,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const stepName = steps[stepToLoopIndex].name || steps[stepToLoopIndex].id
|
||||||
|
this.context.stepsByName![stepName] = tempOutput
|
||||||
this.context.steps[this.context.steps.length] = tempOutput
|
this.context.steps[this.context.steps.length] = tempOutput
|
||||||
this.context.steps = this.context.steps.filter(
|
this.context.steps = this.context.steps.filter(
|
||||||
item => !item.hasOwnProperty.call(item, "currentItem")
|
item => !item.hasOwnProperty.call(item, "currentItem")
|
||||||
|
@ -555,8 +563,13 @@ class Orchestrator {
|
||||||
loopIteration
|
loopIteration
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const stepFn = await this.getStepFunctionality(step.stepId)
|
const stepFn = await this.getStepFunctionality(step.stepId)
|
||||||
let inputs = await processObject(originalStepInput, this.context)
|
let inputs = await this.addContextAndProcess(
|
||||||
|
originalStepInput,
|
||||||
|
this.context
|
||||||
|
)
|
||||||
|
|
||||||
inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs)
|
inputs = automationUtils.cleanInputValues(inputs, step.schema.inputs)
|
||||||
|
|
||||||
const outputs = await stepFn({
|
const outputs = await stepFn({
|
||||||
|
@ -583,6 +596,18 @@ class Orchestrator {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async addContextAndProcess(inputs: any, context: any) {
|
||||||
|
const processContext = {
|
||||||
|
...context,
|
||||||
|
steps: {
|
||||||
|
...context.steps,
|
||||||
|
...context.stepsByName,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return processObject(inputs, processContext)
|
||||||
|
}
|
||||||
|
|
||||||
private handleStepOutput(
|
private handleStepOutput(
|
||||||
step: AutomationStep,
|
step: AutomationStep,
|
||||||
outputs: any,
|
outputs: any,
|
||||||
|
@ -600,6 +625,8 @@ class Orchestrator {
|
||||||
} else {
|
} else {
|
||||||
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
|
this.updateExecutionOutput(step.id, step.stepId, step.inputs, outputs)
|
||||||
this.context.steps[this.context.steps.length] = outputs
|
this.context.steps[this.context.steps.length] = outputs
|
||||||
|
const stepName = step.name || step.id
|
||||||
|
this.context.stepsByName![stepName] = outputs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,6 +124,8 @@ export interface Automation extends Document {
|
||||||
definition: {
|
definition: {
|
||||||
steps: AutomationStep[]
|
steps: AutomationStep[]
|
||||||
trigger: AutomationTrigger
|
trigger: AutomationTrigger
|
||||||
|
// stepNames is used to lookup step names from their correspnding step ID.
|
||||||
|
stepNames?: Record<string, string>
|
||||||
}
|
}
|
||||||
screenId?: string
|
screenId?: string
|
||||||
uiTree?: any
|
uiTree?: any
|
||||||
|
|
Loading…
Reference in New Issue