From b4767cea7c7808901280958112dd4b1ee04b0b76 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 23 Jul 2024 12:07:05 +0200 Subject: [PATCH] Add checks to shared-core --- .../server/src/api/controllers/automation.ts | 6 ++---- .../server/src/sdk/app/automations/crud.ts | 19 ++++++------------- .../server/src/sdk/app/automations/utils.ts | 7 +++---- .../src/sdk/documents/automations.ts | 7 +++++++ .../shared-core/src/sdk/documents/index.ts | 1 + 5 files changed, 19 insertions(+), 21 deletions(-) create mode 100644 packages/shared-core/src/sdk/documents/automations.ts diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index 35799e5dee..ab5b6e3fd7 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -1,4 +1,5 @@ import * as triggers from "../../automations/triggers" +import { sdk as coreSdk } from "@budibase/shared-core" import { DocumentType } from "../../db/utils" import { updateTestHistory, removeDeprecated } from "../../automations/utils" import { setTestFlag, clearTestFlag } from "../../utilities/redis" @@ -12,7 +13,6 @@ import { UserCtx, DeleteAutomationResponse, FetchAutomationResponse, - AutomationTriggerStepId, } from "@budibase/types" import { getActionDefinitions as actionDefs } from "../../automations/actions" import sdk from "../../sdk" @@ -96,9 +96,7 @@ export async function destroy(ctx: UserCtx) { const automationId = ctx.params.id const automation = await sdk.automations.get(ctx.params.id) - if ( - automation.definition.trigger.stepId === AutomationTriggerStepId.ROW_ACTION - ) { + if (coreSdk.automations.isRowAction(automation)) { ctx.throw("Row actions cannot be deleted", 400) } diff --git a/packages/server/src/sdk/app/automations/crud.ts b/packages/server/src/sdk/app/automations/crud.ts index 875c7d4a9d..ee7e3a3506 100644 --- a/packages/server/src/sdk/app/automations/crud.ts +++ b/packages/server/src/sdk/app/automations/crud.ts @@ -1,9 +1,5 @@ -import { - Automation, - AutomationTriggerStepId, - Webhook, - WebhookActionType, -} from "@budibase/types" +import { Automation, Webhook, WebhookActionType } from "@budibase/types" +import { sdk } from "@budibase/shared-core" import { generateAutomationID, getAutomationParams } from "../../../db/utils" import { deleteEntityMetadata } from "../../../utilities" import { MetadataTypes } from "../../../constants" @@ -286,13 +282,10 @@ function guardInvalidUpdatesAndThrow( }) } - if (isRowAction(automation) && automation.name !== oldAutomation.name) { + if ( + sdk.automations.isRowAction(automation) && + automation.name !== oldAutomation.name + ) { throw new Error("Row actions cannot be renamed") } } - -function isRowAction(automation: Automation) { - const result = - automation.definition.trigger.stepId === AutomationTriggerStepId.ROW_ACTION - return result -} diff --git a/packages/server/src/sdk/app/automations/utils.ts b/packages/server/src/sdk/app/automations/utils.ts index 52ed935f3b..ad06e55418 100644 --- a/packages/server/src/sdk/app/automations/utils.ts +++ b/packages/server/src/sdk/app/automations/utils.ts @@ -2,9 +2,9 @@ import { Automation, AutomationActionStepId, AutomationBuilderData, - AutomationTriggerStepId, TableRowActions, } from "@budibase/types" +import { sdk as coreSdk } from "@budibase/shared-core" export function checkForCollectStep(automation: Automation) { return automation.definition.steps.some( @@ -39,14 +39,13 @@ export async function getBuilderData( const result: Record = {} for (const automation of automations) { - const { trigger } = automation.definition - const isRowAction = trigger.stepId === AutomationTriggerStepId.ROW_ACTION + const isRowAction = coreSdk.automations.isRowAction(automation) if (!isRowAction) { result[automation._id!] = { displayName: automation.name } continue } - const { tableId, rowActionId } = trigger.inputs + const { tableId, rowActionId } = automation.definition.trigger.inputs const tableName = await getTableName(tableId) diff --git a/packages/shared-core/src/sdk/documents/automations.ts b/packages/shared-core/src/sdk/documents/automations.ts new file mode 100644 index 0000000000..93e08c86e0 --- /dev/null +++ b/packages/shared-core/src/sdk/documents/automations.ts @@ -0,0 +1,7 @@ +import { Automation, AutomationTriggerStepId } from "@budibase/types" + +export function isRowAction(automation: Automation) { + const result = + automation.definition.trigger.stepId === AutomationTriggerStepId.ROW_ACTION + return result +} diff --git a/packages/shared-core/src/sdk/documents/index.ts b/packages/shared-core/src/sdk/documents/index.ts index d20631eef4..4b17c1ea08 100644 --- a/packages/shared-core/src/sdk/documents/index.ts +++ b/packages/shared-core/src/sdk/documents/index.ts @@ -1,2 +1,3 @@ export * as applications from "./applications" +export * as automations from "./automations" export * as users from "./users"