diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index 4495125e7f..bceebf39b7 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -80,7 +80,9 @@ export async function fetch(ctx: UserCtx) { const automations = await sdk.automations.fetch() ctx.body = { automations } if (enrich) { - ctx.body.builderData = await sdk.automations.getBuilderData(automations) + ctx.body.builderData = await sdk.automations.utils.getBuilderData( + automations + ) } } diff --git a/packages/server/src/sdk/app/automations/utils.ts b/packages/server/src/sdk/app/automations/utils.ts index 79e70923f1..63a7d37bc1 100644 --- a/packages/server/src/sdk/app/automations/utils.ts +++ b/packages/server/src/sdk/app/automations/utils.ts @@ -1,7 +1,48 @@ -import { Automation, AutomationActionStepId } from "@budibase/types" +import { + Automation, + AutomationActionStepId, + AutomationBuilderData, + AutomationTriggerStepId, +} from "@budibase/types" +import sdk from "src/sdk" export function checkForCollectStep(automation: Automation) { return automation.definition.steps.some( (step: any) => step.stepId === AutomationActionStepId.COLLECT ) } + +export async function getBuilderData( + automations: Automation[] +): Promise> { + const tableNameCache: Record = {} + async function getTableName(tableId: string) { + if (!tableNameCache[tableId]) { + const table = await sdk.tables.getTable(tableId) + tableNameCache[tableId] = table.name + } + + return tableNameCache[tableId] + } + + const result: Record = {} + for (const automation of automations) { + const { trigger } = automation.definition + const isRowAction = trigger.stepId === AutomationTriggerStepId.ROW_ACTION + if (!isRowAction) { + result[automation._id!] = { displayName: automation.name } + continue + } + + const tableName = await getTableName(trigger.inputs.tableId) + + result[automation._id!] = { + displayName: `${tableName}: ${automation.name}`, + triggerInfo: { + title: "Automation trigger", + description: `This trigger is tied to the row action TODO on your ${tableName} table`, + }, + } + } + return result +}