Dont persist row action name

This commit is contained in:
Adria Navarro 2024-10-23 12:49:42 +02:00
parent 7781e75491
commit 015854f1a2
4 changed files with 36 additions and 33 deletions

View File

@ -30,6 +30,16 @@ export async function find(ctx: Ctx<void, RowActionsResponse>) {
}
const { actions } = rowActions
const automations = await sdk.automations.find(
Object.values(actions).map(({ automationId }) => automationId)
)
const automationNames = automations.reduce<Record<string, string>>(
(names, a) => {
names[a._id] = a.name
return names
},
{}
)
const result: RowActionsResponse = {
actions: Object.entries(actions).reduce<Record<string, RowActionResponse>>(
(acc, [key, action]) => ({
@ -37,7 +47,7 @@ export async function find(ctx: Ctx<void, RowActionsResponse>) {
[key]: {
id: key,
tableId,
name: action.name,
name: automationNames[action.automationId],
automationId: action.automationId,
allowedSources: flattenAllowedSources(tableId, action.permissions),
},

View File

@ -2,7 +2,6 @@ import {
Automation,
AutomationActionStepId,
AutomationBuilderData,
TableRowActions,
} from "@budibase/types"
import { sdk as coreSdk } from "@budibase/shared-core"
import sdk from "../../../sdk"
@ -26,15 +25,6 @@ export async function getBuilderData(
return tableNameCache[tableId]
}
const rowActionNameCache: Record<string, TableRowActions | undefined> = {}
async function getRowActionName(tableId: string, rowActionId: string) {
if (!rowActionNameCache[tableId]) {
rowActionNameCache[tableId] = await sdk.rowActions.getAll(tableId)
}
return rowActionNameCache[tableId]?.actions[rowActionId]?.name
}
const result: Record<string, AutomationBuilderData> = {}
for (const automation of automations) {
const isRowAction = coreSdk.automations.isRowAction(automation)
@ -49,12 +39,7 @@ export async function getBuilderData(
}
const tableName = await getTableName(tableId)
const rowActionName = await getRowActionName(tableId, rowActionId)
if (!rowActionName) {
throw new Error(`Row action not found: ${rowActionId}`)
}
const rowActionName = automation.name
result[automation._id!] = {
displayName: rowActionName,
triggerInfo: {

View File

@ -13,16 +13,17 @@ import { definitions as TRIGGER_DEFINITIONS } from "../../automations/triggerInf
import * as triggers from "../../automations/triggers"
import sdk from ".."
function ensureUniqueAndThrow(
async function ensureUniqueAndThrow(
doc: TableRowActions,
name: string,
existingRowActionId?: string
) {
const names = await getNames(doc)
name = name.toLowerCase()
if (
Object.entries(doc.actions).find(
([id, a]) =>
a.name.toLowerCase() === name.toLowerCase() &&
id !== existingRowActionId
Object.entries(names).find(
([id, name]) => name === name && id !== existingRowActionId
)
) {
throw new HTTPError("A row action with the same name already exists.", 409)
@ -34,18 +35,12 @@ export async function create(tableId: string, rowAction: { name: string }) {
const db = context.getAppDB()
const rowActionsId = generateRowActionsID(tableId)
let doc: TableRowActions
try {
doc = await db.get<TableRowActions>(rowActionsId)
} catch (e: any) {
if (e.status !== 404) {
throw e
}
let doc = await db.tryGet<TableRowActions>(rowActionsId)
if (!doc) {
doc = { _id: rowActionsId, actions: {} }
}
ensureUniqueAndThrow(doc, action.name)
await ensureUniqueAndThrow(doc, action.name)
const appId = context.getAppId()
if (!appId) {
@ -74,7 +69,6 @@ export async function create(tableId: string, rowAction: { name: string }) {
})
doc.actions[newRowActionId] = {
name: action.name,
automationId: automation._id!,
permissions: {
table: { runAllowed: true },
@ -85,6 +79,7 @@ export async function create(tableId: string, rowAction: { name: string }) {
return {
id: newRowActionId,
name: automation.name,
...doc.actions[newRowActionId],
}
}
@ -253,3 +248,17 @@ export async function run(
{ getResponses: true }
)
}
export async function getNames(actions: TableRowActions) {
const automations = await sdk.automations.find(
Object.values(actions).map(({ automationId }) => automationId)
)
const automationNames = automations.reduce<Record<string, string>>(
(names, a) => {
names[a._id] = a.name
return names
},
{}
)
return automationNames
}

View File

@ -6,7 +6,6 @@ export interface TableRowActions extends Document {
}
export interface RowActionData {
name: string
automationId: string
permissions: RowActionPermissions
}