Populate table name

This commit is contained in:
Adria Navarro 2024-07-22 10:51:18 +02:00
parent b7a969280f
commit 3c9c988d8c
2 changed files with 45 additions and 2 deletions

View File

@ -80,7 +80,9 @@ export async function fetch(ctx: UserCtx<void, FetchAutomationResponse>) {
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
)
}
}

View File

@ -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<Record<string, AutomationBuilderData>> {
const tableNameCache: Record<string, string> = {}
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<string, AutomationBuilderData> = {}
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
}