From 015854f1a2fd207f40b4552b4038aa4238d107c3 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 23 Oct 2024 12:49:42 +0200 Subject: [PATCH] Dont persist row action name --- .../src/api/controllers/rowAction/crud.ts | 12 +++++- .../server/src/sdk/app/automations/utils.ts | 17 +------- packages/server/src/sdk/app/rowActions.ts | 39 ++++++++++++------- packages/types/src/documents/app/rowAction.ts | 1 - 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index d6eadb65b1..014ebcf426 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -30,6 +30,16 @@ export async function find(ctx: Ctx) { } const { actions } = rowActions + const automations = await sdk.automations.find( + Object.values(actions).map(({ automationId }) => automationId) + ) + const automationNames = automations.reduce>( + (names, a) => { + names[a._id] = a.name + return names + }, + {} + ) const result: RowActionsResponse = { actions: Object.entries(actions).reduce>( (acc, [key, action]) => ({ @@ -37,7 +47,7 @@ export async function find(ctx: Ctx) { [key]: { id: key, tableId, - name: action.name, + name: automationNames[action.automationId], automationId: action.automationId, allowedSources: flattenAllowedSources(tableId, action.permissions), }, diff --git a/packages/server/src/sdk/app/automations/utils.ts b/packages/server/src/sdk/app/automations/utils.ts index 8b397df75f..f5ae8bf948 100644 --- a/packages/server/src/sdk/app/automations/utils.ts +++ b/packages/server/src/sdk/app/automations/utils.ts @@ -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 = {} - 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 = {} 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: { diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index f1064396fc..c6182ed38b 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -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(rowActionsId) - } catch (e: any) { - if (e.status !== 404) { - throw e - } - + let doc = await db.tryGet(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>( + (names, a) => { + names[a._id] = a.name + return names + }, + {} + ) + return automationNames +} diff --git a/packages/types/src/documents/app/rowAction.ts b/packages/types/src/documents/app/rowAction.ts index 071b480b1c..db3637b708 100644 --- a/packages/types/src/documents/app/rowAction.ts +++ b/packages/types/src/documents/app/rowAction.ts @@ -6,7 +6,6 @@ export interface TableRowActions extends Document { } export interface RowActionData { - name: string automationId: string permissions: RowActionPermissions }